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


public partial class controls_admin_PageManager : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMessage.Text = string.Empty;
        lblMessage.Visible = false;

        if (!IsPostBack)
        {
            // Get all menu items of type "DB"
            // The page manager only manages pages of type "DB"
            SetMenuList(-1, "DB");

            // Get all BoxItem of type "DB"
            SetBoxList();

            // Default to Add Page view/mode
            PageManagerMenu.Items[0].Selected = true;
            SetPageMode("New");
        }
    }

    /// <summary>
    /// Handle Page Manager menu
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void doOnPageMenuClick(object sender, MenuEventArgs e)
    {
        switch (e.Item.Value)
        {
            case "0":
                // Add page
                SetPageMode("New");
                break;
            case "1":
                // Edit page
                SetPageList();
                multiViewPageManager.ActiveViewIndex = 1;
                break;
            case "2":
                SetBoxMode("New");
                break;
            case "3":
                SetBoxContentList();
                multiViewPageManager.ActiveViewIndex = 3;
                break;
            case "4":
                SetPageMetadata();
                multiViewPageManager.ActiveViewIndex = 4;
                break;
        }

    }

    /// <summary>
    /// Save a new page to the database or Update an existing one
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSavePage_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            bool lbNewMode = (String.Compare(btnSavePage.CommandArgument, "New") == 0);
            lblMessage.Text = string.Empty;
            bool lbRetCode = false;
            PageDisplay loPage = new PageDisplay();
            if (lbNewMode)
            {
                loPage.ID = -1;
            }
            else
            {
                loPage.ID = Convert.ToInt64(lblID.Text);
            }
            loPage.MenuID = Convert.ToInt32(cbPageMenu.SelectedValue);
            loPage.Description = tbDescription.Text;
            loPage.Keyword = tbKeywords.Text;
            loPage.Title = tbTitle.Text;
            loPage.Content = ftPageContent.Text;
            lbRetCode = PageDisplayManagement.Save(loPage);

            if (lbRetCode == true)
            {
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Font.Bold = true;
                lblMessage.Text = string.Format("Page {0} successfully",
                                                (lbNewMode) ? "added" : "updated");
                //clear controls
                cbPageMenu.SelectedIndex = -1;
                tbTitle.Text = "";
                tbKeywords.Text = "";
                tbDescription.Text = "";
                ftPageContent.Text = "";

            }
            else
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Font.Bold = true;
                lblMessage.Text = string.Format("Failed to {0} page",
                                                (lbNewMode) ? "add" : "update");
            }
            lblMessage.Visible = true;
        }

    }

    /// <summary>
    /// Handle "Edit" or "Delete" commands from the PageDisplay List
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gridPageList_RowCommand(object sender,
                GridViewCommandEventArgs e)
    {
        if (e.CommandArgument == "")
        {
            gridPageList.SelectedIndex = 0;
        }
        else
        {
            gridPageList.SelectedIndex = Convert.ToInt32(e.CommandArgument);
        }
        if (String.Compare(e.CommandName, "Edit", true) == 0)
        {
            // Display UI for Adding/Editing a page
            SetPageMode("Update");
        }
        else if (String.Compare(e.CommandName, "Delete", true) == 0)
        {
            bool lbRetCode = false;
            lbRetCode = PageDisplayManagement.Delete(Convert.ToInt64(gridPageList.SelectedValue));

            // Reset page list after deleting
            SetPageList();
            lblMessage.Visible = true;
            if (lbRetCode == true)
            {
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Font.Bold = true;
                lblMessage.Text = string.Format("Page deleted successfully ");
            }
            else
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Font.Bold = true;
                lblMessage.Text = string.Format("Failed to delete page");
            }
        }
    }

    protected void gridPageList_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void gridPageList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gridPageList.PageIndex = e.NewPageIndex;
        this.SetPageList();

    }

    protected void gridPageList_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void gridPageList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }

    /// <summary>
    /// Link to "Add Page" view
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnNewBox_Click(object sender, EventArgs e)
    {
        SetBoxMode("New");
    }

    /// <summary>
    /// Handle command (button click) from BoxContent List GridView
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gridBoxContent_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandArgument == "")
        {
            gridBoxContent.SelectedIndex = 0;
        }
        else
        {
            gridBoxContent.SelectedIndex = Convert.ToInt32(e.CommandArgument);
        }
        if (String.Compare(e.CommandName, "Edit", true) == 0)
        {
            // Display UI for Adding/Editing a BoxContent
            SetBoxMode("Update");
        }
        else if (String.Compare(e.CommandName, "Delete", true) == 0)
        {
            bool lbRetCode = false;
            //gridBoxContent.SelectedIndex = Convert.ToInt32(e.CommandArgument);
            lbRetCode = BoxDisplayManagement.DeleteBoxDisplay(Convert.ToInt32(gridBoxContent.SelectedValue));

            // Reset BoxContent list after deleting
            SetBoxContentList();
            lblMessage.Visible = true;
            if (lbRetCode == true)
            {
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Font.Bold = true;
                lblMessage.Text = string.Format("Box deleted successfully ");
            }
            else
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Font.Bold = true;
                lblMessage.Text = string.Format("Failed to delete Box");
            }
        }

    }
    protected void gridBoxContent_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }
    protected void gridBoxContent_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void gridBoxContent_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void gridBoxContent_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gridBoxContent.PageIndex = e.NewPageIndex;
        this.SetBoxContentList();
    }

    /// <summary>
    /// Save new or updated Boxcontent to the database
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnsaveBox_Click(object sender, EventArgs e)
    {
        bool lbRetCode;
        bool lbNewMode = (String.Compare(btnsaveBox.CommandArgument, "New") == 0);
        if (Page.IsValid)
        {
            BoxDisplay loBox = new BoxDisplay();
            if (lbNewMode)
            {
                loBox.ID = -1;
            }
            else
            {
                loBox.ID = Convert.ToInt32(lblBoxID.Text);
            }
            loBox.Name = txtBoxName.Text;
            loBox.BoxData = ftBoxContent.Text;
            loBox.BoxID = Convert.ToInt32(cbBoxDisplayList.SelectedValue);

            lbRetCode = BoxDisplayManagement.SaveBoxDisplay(loBox, btnsaveBox.CommandArgument);
            lblMessage.Visible = true;

            if (lbRetCode == true)
            {
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Font.Bold = true;
                lblMessage.Text = string.Format("Box {0} successfully",
                                                (lbNewMode) ? "added" : "updated");
                //clear controls
                txtBoxName.Text = "";
                ftBoxContent.Text = "";
                cbBoxDisplayList.SelectedIndex = 0;
            }
            else
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Font.Bold = true;
                lblMessage.Text = string.Format("Failed to {0} page",
                                                (lbNewMode) ? "add" : "update");
            }
        }
    }

    /// <summary>
    /// Link to "Add BoxContent" view
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnNewPage_Click(object sender, EventArgs e)
    {
        SetPageMode("New");
    }

    private void SetPageMode(string psMode)
    {
        btnSavePage.CommandArgument = psMode;
        SetPageDetails(psMode);
        multiViewPageManager.ActiveViewIndex = 0;
    }

    private void SetBoxMode(string psMode)
    {
        SetBoxDetails(psMode);
        btnsaveBox.CommandArgument = psMode;
        multiViewPageManager.ActiveViewIndex = 2;
    }

    /// <summary>
    /// Display the menu items list of type "DB"
    /// </summary>
    /// <param name="piBoxID"></param>
    private void SetMenuList(int piBoxID, string psActionType)
    {
        this.cbPageMenu.DataSource =
            MenuItemManagement.GetMenuItemList(piBoxID, psActionType);
        this.cbPageMenu.DataBind();
    }

    /// <summary>
    /// Get all available PageDisplay records
    /// </summary>
    /// <param name="piMenuID"></param>
    private void SetPageList()
    {
        gridPageList.DataSource =
                PageDisplayManagement.GetPageDisplayList();
        gridPageList.DataBind();
    }

    /// <summary>
    /// Initialize controls for adding/editing a page
    /// </summary>
    private void SetPageDetails(string psMode)
    {
        if (string.Compare(psMode, "Update", true) == 0)
        {
            PageDisplay loPage =
                    PageDisplayManagement.getPageDetail(Convert.ToInt32(gridPageList.SelectedValue));
            if (loPage != null)
            {
                lblID.Text = loPage.ID.ToString();
                tbDescription.Text = loPage.Description;
                tbKeywords.Text = loPage.Keyword;
                tbTitle.Text = loPage.Title;
                ftPageContent.Text = loPage.Content;
                cbPageMenu.SelectedValue = loPage.MenuID.ToString();
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Font.Bold = true;
                lblMessage.Text = string.Format("Error loading page data");
            }
        }
        else
        {
            lblID.Text = "-1";
            tbDescription.Text = string.Empty;
            tbKeywords.Text = string.Empty;
            tbTitle.Text = string.Empty;
            ftPageContent.Text = string.Empty;
        }
    }

    /// <summary>
    /// Initialize controls for adding/edit a BoxContent
    /// </summary>
    /// <param name="psMode"></param>
    private void SetBoxDetails(string psMode)
    {
        if (String.Compare(psMode, "Update", true) == 0)
        {
            int liID = Convert.ToInt32(gridBoxContent.SelectedValue);
            BoxDisplay loBox = BoxDisplayManagement.GetBoxDisplay(liID);
            if (loBox != null)
            {
                lblBoxID.Text = loBox.ID.ToString();
                txtBoxName.Text = loBox.Name;
                ftBoxContent.Text = loBox.BoxData;
                cbBoxDisplayList.SelectedValue = loBox.BoxID.ToString();
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Font.Bold = true;
                lblMessage.Text = string.Format("Error loading box content");
            }
        }
        else
        {
            ftBoxContent.Text = string.Empty;
        }
    }

    /// <summary>
    /// Load BoxDisplay records for editing
    /// </summary>
    private void SetBoxContentList()
    {
        string lsError = string.Empty;
        gridBoxContent.DataSource = BoxDisplayManagement.GetBoxDisplayList(out lsError);
        gridBoxContent.DataBind();
        if (lsError != string.Empty)
        {
            lblMessage.Visible = true;
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Font.Bold = true;
            lblMessage.Text = string.Format("Error loading data");
        }
    }

    /// <summary>
    /// Load available BoxItem records.
    /// </summary>
    private void SetBoxList()
    {
        string lsError = string.Empty;
        cbBoxDisplayList.DataSource =
            BoxItemManagement.GetBoxItemListByType("DB", out lsError);
        cbBoxDisplayList.DataBind();
        if (lsError != string.Empty)
        {
            lblMessage.Visible = true;
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Font.Bold = true;
            lblMessage.Text = string.Format("Error loading data");
        }
    }

    private void SetPageMetadata()
    {
        PortalMetadata loPortalMetadata = PortalMetadataManagement.GetPortalMetadata();
        Session["PortalMetadataID"] = loPortalMetadata.ID;
        txtMetaKeywords.Text = loPortalMetadata.Keywords;
        txtMetaDescription.Text = loPortalMetadata.Description;
    }

    protected void btnSaveMetaData_Click(object sender, EventArgs e)
    {
        PortalMetadata loPortalMetadata = new PortalMetadata();
        loPortalMetadata.ID = Convert.ToInt16(Session["PortalMetadataID"]);
        loPortalMetadata.Keywords = Utility.ConvertHtmlToPlainText(txtMetaKeywords.Text);
        loPortalMetadata.Description = Utility.ConvertHtmlToPlainText(txtMetaDescription.Text);

        if (PortalMetadataManagement.SavePortalMetadata(loPortalMetadata))
        {
            lblMetaMessage.Text = "<font color='green'>Portal Metadata Saved!</font>";
        }
        else
        {
            lblMetaMessage.Text = "<font color='red'>Unable to Save Portal Metadata.</font>";
        }
    }
}
