using System;
using System.Collections.Generic;
using digioz;
using webControls = System.Web.UI.WebControls;

public partial class controls_TopMenu : System.Web.UI.UserControl
{
    private PortalSettings coPortalSettings;

    protected void Page_Load(object sender, EventArgs e)
    {
        coPortalSettings = PortalSettingsManagement.GetPortalSettings();

        //if (!IsPostBack)
        //{
            LoadTopMenu();
        //}
    }

    private void LoadTopMenu()
    {
        topMenu.Items.Clear();
        string lsMenuItemUrl = String.Empty;

        int liPermissionID = -1;
        
        if (Session["permission"] != null)
        {
            liPermissionID = Convert.ToInt32(Session["permission"]);
        }            

        List<TopMenu> loItemsList = TopMenuManagement.GetTopMenuItems(liPermissionID);
        List<TopMenu> rootItems;

        if (Session["UserID"] == null)
        {
            rootItems = loItemsList.FindAll(delegate(TopMenu poMenuItem) { return poMenuItem.ParentID == 0; });
        }
        else
        {
            rootItems = loItemsList.FindAll(delegate(TopMenu poMenuItem) { return poMenuItem.ParentID == 0 && poMenuItem.PermissionID != -1; });
        }

        foreach (TopMenu rootItem in rootItems)
        {
            string lsActionData;

            if (rootItem.ActionType == "LM")
            {
                lsActionData = String.Format("~/?page={0}", Server.UrlEncode(digioz.Encryption64.Encrypt(rootItem.ActionData, coPortalSettings.URLEncryptionKey)));
            }
            else
            {
                lsActionData = rootItem.ActionData;
            }

            webControls.MenuItem mi = new webControls.MenuItem(rootItem.Name, rootItem.Name, null, lsActionData);
            AddSubItems(rootItem, loItemsList, mi);
            topMenu.Items.Add(mi);
        }

        string lsUserName = Session["username"] as string;

        if (String.IsNullOrEmpty(lsUserName))
        {
            userNameLabel.Text = String.Empty;
        }
        else
        {
            userNameLabel.Text = String.Format("(logged in as {0})", lsUserName);
        }
    }

    private void AddSubItems(TopMenu rootItem, List<TopMenu> allItems, webControls.MenuItem rootMenuItem)
    {
        List<TopMenu> childItems = allItems.FindAll(delegate(TopMenu poMenuItem) { return poMenuItem.ParentID == rootItem.ID; });

        foreach (TopMenu childItem in childItems)
        {
            string lsActionData;

            if (childItem.ActionType == "LM")
            {
                lsActionData = String.Format("~/?page={0}", Server.UrlEncode(digioz.Encryption64.Encrypt(childItem.ActionData, coPortalSettings.URLEncryptionKey)));
            }
            else
            {
                lsActionData = childItem.ActionData;
            }

            webControls.MenuItem mi = new webControls.MenuItem(childItem.Name, null, null, lsActionData);
            AddSubItems(childItem, allItems, mi);
            rootMenuItem.ChildItems.Add(mi);
        }
    }
}
