using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using digioz;

public partial class _Default : System.Web.UI.Page
{
    public digioz.Hashtable coConfig = new digioz.Hashtable();
    private PortalSettings coPortalSettings;
    public ASP.controls_box_ascx ctlBox;
    public ASP.controls_pagedisplaybox_ascx ctlPageDisplayBox;
    public int ciPageID;
    public string csPageName;

    protected void Page_Init(object sender, EventArgs e)
    {
        // Load Database Settings -----------------------
        coPortalSettings = PortalSettingsManagement.GetPortalSettings();

        // Update hit count -----------------------------
        UpdateHitCount();

        // Check if a page has been set or not ----------
        WhatPageIsIt();

        // Load Configuration Hashtable -----------------
        LoadConfiguration();

        // myStyleSheet.Href = (string)(coConfig.GetValue("Style Sheet"));
        Page.Title = (string)(coConfig.GetValue("Site Title")) + "- Powered by DigiOz .NET Portal";

        Footer1.Text = "&#169; 2010 <a href=\"http://" + coConfig.GetValue("Site URL") + "\">" + coConfig.GetValue("Site Title") + "</a>";

        // Set Banner Text ------------------------------
        Banner1.Text = "&nbsp;&nbsp;<br><br>" + coConfig.GetValue("Site Title");

        // Populate the Boxes ----------------------------
        LoadBoxControl();

        // Populate the main Display ---------------------
        LoadDisplay();

        // Populate the Comments -------------------------
        LoadCommentBox();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Utility.LoginPrompt(this);
    }

    private void UpdateHitCount()
    {
        string lsPageName = Request.Url.Query;
        PageHitsManagement.IncreaseHitCount(lsPageName);
    }

    private void WhatPageIsIt()
    {
        try
        {
            ciPageID = Int16.Parse(Request.QueryString["id"]);
        }
        catch
        {
            try
            {
                csPageName = Request.QueryString["page"];

                if (csPageName != null && csPageName != String.Empty)
                {
                    digioz.MenuItem loMenuItem;
                    csPageName = digioz.Encryption64.Decrypt(csPageName, coPortalSettings.URLEncryptionKey);
                    loMenuItem = MenuItemManagement.GetMenuItemFromMenuName(csPageName);
                    ciPageID = loMenuItem.ID;
                }
                else
                {
                    ciPageID = 1;
                }
            }
            catch
            {
                ciPageID = 1;
            }

            if (csPageName == null || csPageName == String.Empty)
            {
                ciPageID = 1;
            }
        }
    }

    private void LoadConfiguration()
    {
        ConfigurationList loConfigurationList = ConfigurationManagement.GetConfigurationList();

        foreach (digioz.Configuration loConfig in loConfigurationList)
        {
            coConfig.AddToTable(loConfig.KeyName, loConfig.KeyValue);
        }
    }

    private void LoadDisplay()
    {
        digioz.MenuItem loMenuItem = MenuItemManagement.GetMenuItemFromPageID(ciPageID);
  

        if (loMenuItem.ActionType == "X")
        {
            // Load control
            pnlDisplay.Controls.Clear();
            pnlDisplay.Controls.Add(LoadControl(loMenuItem.ActionData));
        }
        else if (loMenuItem.ActionType == "F")
        {
            // Read local file
            Label ctlLabel = new Label();
            ctlLabel.Text = Utility.GetFileContents(loMenuItem.ActionData);
            pnlDisplay.Controls.Add(ctlLabel);
        }
        else if (loMenuItem.ActionType == "W")
        {
            // Read web page
            Label ctlLabel = new Label();
            ctlLabel.Text = Utility.GetWebPageContent(loMenuItem.ActionData);
            pnlDisplay.Controls.Add(ctlLabel);
        }
        else if (loMenuItem.ActionType == "DB")
        {
            // Query Database for page content
            PageDisplayList loPageDisplayList = PageDisplayManagement.GetPageDisplayList(loMenuItem.ID);
            pnlDisplay.Controls.Clear();

            foreach (PageDisplay loPageDisplay in loPageDisplayList)
            {
                Control ctlControl = LoadControl("controls/PageDisplayBox.ascx");
                ctlPageDisplayBox = (ASP.controls_pagedisplaybox_ascx)ctlControl;
                ctlPageDisplayBox.ID = "Page" + loPageDisplay.ID;
                ctlPageDisplayBox.PageDisplay = loPageDisplay;
                pnlDisplay.Controls.Add(ctlPageDisplayBox);
            }
        }
        else if (loMenuItem.ActionType == "LI")
        {
            Response.Redirect(loMenuItem.ActionData);
        }
        else if (loMenuItem.ActionType == "LE")
        {
            Response.Redirect(loMenuItem.ActionData);
        }
        else if (loMenuItem.ActionType == "XE")
        {
            String lsPageURL = loMenuItem.ActionData;
            string[] laWords = lsPageURL.Split('&');
            csPageName = Server.UrlEncode(digioz.Encryption64.Encrypt(laWords[0].ToString(), coPortalSettings.URLEncryptionKey));
            Response.Redirect("Default.aspx?page=" + csPageName + "&" + laWords[1].ToString());
        }
    }

    private void LoadCommentBox()
    {
        if (Session["UserID"] == null)
        {
            CommentsControl.Visible = false;
            return;
        }

        // Check commentation is enabled on the page.
        string lsDecodedUrl = ControlsHelper.GetDecodedUrl(Request.Url.PathAndQuery);
        CommentPage loPage = CommentPageManagement.GetCommentPage(lsDecodedUrl);
        string lsQueryParameter = Request.QueryString["page"];
        digioz.MenuItem loMenuItem = new digioz.MenuItem();

        if (csPageName == null || csPageName == String.Empty)
        {
            loMenuItem = MenuItemManagement.GetMenuItemFromPageID(ciPageID);
            csPageName = loMenuItem.Name;
        }

        // Check by Url.
        if (loPage != null)
        {
            if (!(loPage.IsCommentationEnabled))
            {
                CommentsControl.Visible = false;
            }
        }
        // Check by parent page.
        else
        {
            loPage = CommentPageManagement.GetCommentPage(csPageName);

            if (loPage == null)
            {
                CommentsControl.Visible = false;
            }
        }
    }

    private void LoadBoxControl()
    {
        BoxItemList loBoxItemList = BoxItemManagement.GetBoxItemList();
        int ciUserPermission = -2;
        int ciloginboxpermission = 0;
        if (Session["UserID"] != null)
        {
            User loUserInfo = UserManagement.GetUserByID(Int32.Parse(Session["UserID"].ToString()));
            ciUserPermission = Int32.Parse(loUserInfo.Permission);
            ciloginboxpermission = 1;
        }

        pnlMenuLeft.Controls.Clear();
        pnlMenuRight.Controls.Clear();

        foreach (BoxItem loBoxItem in loBoxItemList)
        {
			if((loBoxItem.Name == "Login")&&(ciloginboxpermission >= 1))
			{
				loBoxItem.Permission = -2;
			}
	   
            if(loBoxItem.Permission != -2)
            {
        
        
            if ((loBoxItem.Permission == -1) || (ciUserPermission >= loBoxItem.Permission))
            {
                Control ctlControl = LoadControl("controls/Box.ascx");
                ctlBox = (ASP.controls_box_ascx)ctlControl;
                ctlBox.ID = "Box" + loBoxItem.ID;
                ctlBox.BoxTitle = loBoxItem.Name;

                // Determine action type ----------------------

                if (loBoxItem.ActionType == "X")
                {
                    // Execute custom control
                    ctlBox.BoxDynamicControl = loBoxItem.ActionData;
                }
                else if (loBoxItem.ActionType == "M")
                {
                    // Get list of links
                    ctlBox.BoxDisplay = GetMenuItems(loBoxItem.ID);
                }
                else if (loBoxItem.ActionType == "F")
                {
                    // Read local file
                    digioz.Utility loUtility = new digioz.Utility();
                    ctlBox.BoxDisplay = Utility.GetFileContents(loBoxItem.ActionData);
                }
                else if (loBoxItem.ActionType == "W")
                {
                    // Read web page
                    digioz.Utility loUtility = new digioz.Utility();
                    ctlBox.BoxDisplay = Utility.GetWebPageContent(loBoxItem.ActionData);
                }
                else if (loBoxItem.ActionType == "DB")
                {
                    // Query Database for box content
                    BoxDisplayList loBoxDisplayList = BoxDisplayManagement.GetBoxDisplayList(loBoxItem.ID);
                    ctlBox.BoxDisplay = String.Empty;
                    string lsBoxDisplay = String.Empty;

                    foreach (BoxDisplay loBoxDisplay in loBoxDisplayList)
                    {
                        lsBoxDisplay += loBoxDisplay.BoxData;
                    }

                    ctlBox.BoxDisplay = lsBoxDisplay;
                }

                // Add Box to left or right menu --------------

                if (loBoxItem.Location == "L")
                {
                    pnlMenuLeft.Controls.Add(ctlBox);
                }
                else
                {
                    pnlMenuRight.Controls.Add(ctlBox);
                }                
              }
            }
        }
    }

    private string GetMenuItems(int piBoxID)
    {
        string lsOutput = String.Empty;
        MenuItemList loMenuItemList = MenuItemManagement.GetMenuItemList(piBoxID);

        lsOutput = "<ul>";

        foreach (digioz.MenuItem loMenuItem in loMenuItemList)
        {
            lsOutput += "<li><a class=\"box\" href=\"?id=" + loMenuItem.ID.ToString() + "\">" + loMenuItem.Name.ToString() + "</a></li>";
        }

        lsOutput += "</ul>";

        return lsOutput;
    }
}
