Imports System
Imports System.Drawing
Imports System.Configuration
Imports System.Web.UI.WebControls

Public Class Menu
    Inherits System.Web.UI.UserControl

#Region " Código generado por el Diseñador de Web Forms "

    'El Diseñador de Web Forms requiere esta llamada.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents pholMenu As System.Web.UI.WebControls.PlaceHolder
    Protected WithEvents lblVersionValue As System.Web.UI.WebControls.Label
    Protected WithEvents pholdHeader As System.Web.UI.WebControls.PlaceHolder
    Protected WithEvents imgHome As System.Web.UI.WebControls.Image
    Protected WithEvents lnbLogout As System.Web.UI.WebControls.LinkButton
    Protected WithEvents hlnkHelp As System.Web.UI.WebControls.HyperLink

    'NOTA: el Diseñador de Web Forms necesita la siguiente declaración del marcador de posición.
    'No se debe eliminar o mover.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: el Diseñador de Web Forms requiere esta llamada de método
        'No la modifique con el editor de código.
        InitializeComponent()
    End Sub

#End Region

    Const STYLE_ERROR_MARK As String = " style=""color:red"" "
    Protected menuOptions As MenuItem()
    Protected _currentGroup As Integer = 0
    Protected _currentItem As Integer = 0
    Private _RoleIds As System.Collections.IList
    Private _errorMarkList As String
    Const strScriptWithConfirmation As String = "<script language=""javascript""> " & _
                "	function ShowWarning(page){ " & _
                "		if(confirm(""Navigating to another menu option will discard your current input. Do you want to continue?""))	" & _
                "						window.location = page;" & _
                "					} </script>"

    Const strScriptWithoutConfirmation As String = " <script language=""javascript""> " & _
                                                    "	function nothing(){} " & _
                                                        " function ShowWarning(page){ " & _
                                                        " window.location = page; " & _
                                                        "}" & _
                                                    "</script>"

    Private strScriptAbout As String

#Region "Propiedades"

    Public Property CurrentGroup() As Integer
        Get
            Return _currentGroup
        End Get
        Set(ByVal Value As Integer)
            _currentGroup = Value
        End Set
    End Property

    Public Property CurrentItem() As Integer
        Get
            Return _currentItem
        End Get
        Set(ByVal Value As Integer)
            _currentItem = Value
        End Set
    End Property

    Public Property RoleIds() As System.Collections.IList
        Get
            Return _RoleIds
        End Get
        Set(ByVal Value As System.Collections.IList)
            RoleIds = Value
        End Set
    End Property

#End Region
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strHome As String

        strHome = ConfigurationSettings.AppSettings("AppPath")

        If strHome.EndsWith("/") = False Then
            strHome &= "/"
        End If

        'version label
        lblVersionValue.Text = ConfigurationSettings.AppSettings("AppVersion")
        imgHome.ImageUrl = strHome & "images/icono_home.gif"
        strScriptAbout = "<script language=javascript>function fnAbout()" & _
                            "{ popabout=window.open(""" & _
                            strHome & "about/frmAbout.aspx"",""about"",""height=450px,width=500px,top=0,left=0"");}" & _
                            " </script>"

        hlnkHelp.Target = "_blank"
        hlnkHelp.Text = "Help"
        hlnkHelp.NavigateUrl = strHome & "About/CAMSUserManual.pdf"

        If _currentGroup = 0 Then
            Exit Sub
        End If

        menuOptions = ApplicationMenu.createMenuOptions

        Page.RegisterClientScriptBlock("FrmAbout", strScriptAbout)

        If Not menuOptions Is Nothing Then
            generateMenu(menuOptions, 0, True)
            If Not Page.IsClientScriptBlockRegistered("ConfirmationMessage") Then
                Page.RegisterClientScriptBlock("ConfirmationMessage", strScriptWithoutConfirmation)
            End If

        End If

    End Sub

    Private Sub generateMenu(ByVal options() As MenuItem, ByVal level As Integer, ByVal IsSelGroup As Boolean)
        For i As Integer = 0 To options.Length - 1
            Dim litOption As New Literal

            'validating home element
            If options(i).IsHome Then
                If options(i).Id = _currentItem Then
                    AddHomeHeader(MenuElementType.selected, options(i))
                Else
                    AddHomeHeader(MenuElementType.otherGroup, options(i))
                End If

                GoTo NextBucle
            End If

            If options(i).IsGroup Then
                If options(i).Id = _currentGroup Then
                    'Selected group
                    AddMenuElement(MenuElementType.selected, options(i), level)
                    'display the sub options
                    If (Not options(i).SubOptions Is Nothing) Then
                        generateMenu(options(i).SubOptions, level + 1, True)
                    End If

                Else
                    'sub element validation, perhaps it has elements
                    If options(i).Id = _currentItem Then
                        AddMenuElement(MenuElementType.selected, options(i), level)
                        If (Not options(i).SubOptions Is Nothing) Then
                            generateMenu(options(i).SubOptions, level + 1, True)
                        End If
                    Else
                        AddMenuElement(MenuElementType.otherGroup, options(i), level)
                    End If

                End If

            Else
                'Leaf 
                If options(i).Id = Me._currentItem Then
                    AddMenuElement(MenuElementType.selected, options(i), level)
                ElseIf IsSelGroup Then
                    'Leaf from the same group
                    AddMenuElement(MenuElementType.sameGroup, options(i), level)
                Else
                    'Current leaf
                    AddMenuElement(MenuElementType.otherGroup, options(i), level)
                End If

            End If

            If level = 0 Then
                litOption = New Literal
                litOption.Text = "<br>"
                pholMenu.Controls.Add(litOption)
            End If

NextBucle:
        Next

    End Sub

    Enum MenuElementType
        selected
        sameGroup
        otherGroup
    End Enum

    Private Sub AddHomeHeader(ByVal eType As MenuElementType, ByVal menuElem As MenuItem)
        'pholdHeader
        Dim litOption As New Literal
        Dim leftSpace As String = ""

        If eType = MenuElementType.selected Then
            litOption.Text = leftSpace + "<a href='#' class=MENU_HOME>" + menuElem.Description + "</a><br>"
        Else
            litOption.Text = leftSpace + "<a href=javascript:ShowWarning('" + _
                                       menuElem.Href + "');" + " class=MENU_HOME>" + menuElem.Description + "</a><br>"
        End If

        pholdHeader.Controls.Add(litOption)
    End Sub

    Private Sub AddMenuElement(ByVal eType As MenuElementType, ByVal menuElem As MenuItem, ByVal level As Integer)
        Dim leftSpace As String = ""
        Dim errorMarkStyle As String = ""

        Dim hasErrorMark = IsInErrorMarkList(menuElem.Id)

        If hasErrorMark Then
            errorMarkStyle = STYLE_ERROR_MARK
            For i As Integer = 0 To level - 2
                leftSpace &= "&nbsp;&nbsp;"
            Next

            leftSpace &= "&nbsp<span class=errorMark>*</span>"
        Else
            For i As Integer = 0 To level - 1
                leftSpace += "&nbsp;&nbsp;"
            Next

        End If

        Dim litOption As New Literal

        Select Case eType
            Case MenuElementType.selected
                litOption.Text = leftSpace & "<a " & errorMarkStyle & " href=# class=MENU_ITEM>" & menuElem.Description & "</a><br>"
            Case MenuElementType.sameGroup
                'litOption.Text = leftSpace
                'pholMenu.Controls.Add(litOption)
                'Dim lbtnJump As New LinkButton
                'lbtnJump.CssClass = "Con_TextoEnlace"
                'lbtnJump.Text = menuElem.Description
                'lbtnJump.CausesValidation = False
                'If (hasErrorMark) Then lbtnJump.ForeColor = Color.Red

                'lbtnJump.CommandArgument = menuElem.Href
                'AddHandler lbtnJump.Command, AddressOf LinkButton_Click
                'pholMenu.Controls.Add(lbtnJump)
                'litOption = New Literal
                'litOption.Text = "<br>"
                litOption.Text = leftSpace + "<a  " + errorMarkStyle + " href=javascript:ShowWarning('" + _
                   menuElem.Href + "');" + " class=MENU_ITEM>" + menuElem.Description + "</a><br>"
            Case MenuElementType.otherGroup
                litOption.Text = leftSpace + "<a  " + errorMarkStyle + " href=javascript:ShowWarning('" + _
                   menuElem.Href + "');" + " class=MENU_ITEM>" + menuElem.Description + "</a><br>"
        End Select

        'If (menuElem.Id = Constants.MENU_REPORTS_TARGETSMET_ID) Or (menuElem.Id = Constants.MENU_REPORTS_QSUMM_ID) Then
        '    If (_RoleIds.Contains(NBOConstants.NBO_ROL_PRODUCTMANAGER) Or _RoleIds.Contains(NBOConstants.NBO_ROL_ENGINEER)) Then

        '    End If
        'else
        '
        'End If

        pholMenu.Controls.Add(litOption)

    End Sub

    Public Event ItemClick As CommandEventHandler

    Protected Sub doItemClick(ByVal e As CommandEventArgs)
        RaiseEvent ItemClick(Me, e)
    End Sub

    Private Sub LinkButton_Click(ByVal sender As Object, ByVal e As CommandEventArgs)
        doItemClick(e)
    End Sub

    Private Function IsInErrorMarkList(ByVal ItemId As Integer) As Boolean
        If _errorMarkList Is Nothing Then Return False
        Return IIf(_errorMarkList.IndexOf("," + ItemId.ToString().Trim() + ",") >= 0, True, False)
    End Function

    Private Sub lnbLogout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnbLogout.Click
        Dim strHome As String

        KillSession()

        strHome = ConfigurationSettings.AppSettings("AppPath")

        If strHome.EndsWith("/") = False Then
            strHome &= "/"
        End If

        Dim sbScript As New System.Text.StringBuilder
        sbScript.Append("<script language=""javascript"">")
        sbScript.Append("     window.parent.location=""" & strHome & "frmLogin.aspx""")
        sbScript.Append("</script>")
        Response.Write(sbScript.ToString())

    End Sub

    Private Sub KillSession()
        Try
            Session.Clear()
            Session.Abandon()
        Catch 
        End Try
    End Sub
End Class
