using System;
using System.Collections;
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 controls_admin_WebLinksManager : System.Web.UI.UserControl
{
    #region Page/Controls Events
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.LoadComplete += new EventHandler(Page_LoadComplete);
        btnDeleteEdit.Attributes.Add("onClick", "if(!confirm('Are you sure you want to Delete this link ?')) return false;");
    }
    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        ShowMessage(csMsgCode, csMsg);
    }       
    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        if (e.Item.Value == "0")
        {
            MultiView1.ActiveViewIndex = 0;            
        }
        else if (e.Item.Value == "1")
        {
            MultiView1.ActiveViewIndex = 1;            
        }
        else if (e.Item.Value == "2")
        {
            MultiView1.ActiveViewIndex = 2;
        }
        else if (e.Item.Value == "3")
        {
            MultiView1.ActiveViewIndex = 3;
        }
    }
    protected void btnSubmitAdd_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            WebLinkManagement loService = new WebLinkManagement();
            if (WebLinkManagement.AddWebLink(textboxLinkNameAdd.Text, textboxLinkURLAdd.Text, textboxDetailAdd.Text, textboxCategoryAdd.Text))
            {
                ResetAddForm();
            }
        }
    }    
    protected void GridViewWebLinks_RowEditing(object sender, GridViewEditEventArgs e)
    {
        MultiView1.ActiveViewIndex = 4;        
        WebLink loWebLink = WebLinkManagement.GetWebLink(Convert.ToInt32(GridViewWebLinks.Rows[e.NewEditIndex].Cells[1].Text));
        PopulateEditForm(loWebLink);
        e.Cancel = true;
    }
    protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
    {
        if (((MultiView)sender).ActiveViewIndex == 1)
        {
            LoadViewActiveWebLinks();
        }
        if (((MultiView)sender).ActiveViewIndex == 1)
        {
            LoadViewPendingWebLinks();
        }
    }
    protected void btnSubmitEdit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (WebLinkManagement.UpdateWebLink(Convert.ToInt32(lblIdEdit.Text), textboxLinkNameEdit.Text, textboxLinkURLEdit.Text, textboxDetailEdit.Text, textboxCategoryEdit.Text))
            {
                ResetEditForm();
                ShowSuccessMessage("Changes saved successfully !");
                MultiView1.ActiveViewIndex = 1;
            }
            else 
            { 
                ShowErrorMessage("Error saving changes. Please try again"); 
            }
            //now need to call the update method
            /*if (loService.AddWebLink(textboxLinkNameAdd.Text, textboxLinkURLAdd.Text, textboxDetailAdd.Text, textboxCategoryAdd.Text))
            {
                textboxLinkNameAdd.Text = "";
                textboxLinkURLAdd.Text = "http://";
                textboxDetailAdd.Text = "";
                textboxCategoryAdd.Text = "";
                MultiView1.ActiveViewIndex = 1;
            }*/
        }
    }
    protected void btnCancelEdit_Click(object sender, EventArgs e)
    {
        ResetEditForm();
        MultiView1.ActiveViewIndex = 1; //switching back to active links list
    }
    protected void btnDeleteEdit_Click(object sender, EventArgs e)
    {
        if (WebLinkManagement.DeleteWebLink(Convert.ToInt32(lblIdEdit.Text)))
        {
            ShowSuccessMessage("Deletion Successful !");
            ResetEditForm();
            MultiView1.ActiveViewIndex = 1;
        }
    }
    protected void GridViewWebLinksPending_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //Approves the Link.
        if (WebLinkManagement.ApproveWebLink(Convert.ToInt32(GridViewWebLinksPending.Rows[e.NewEditIndex].Cells[1].Text)))
        {
            ShowSuccessMessage("Weblink Approved !");
            LoadViewPendingWebLinks();//Refresh the pending weblinks list            
        }
        else
        {
            ShowErrorMessage("Error Approving the Weblink. Please try again.");
        }
        e.Cancel = true;
    }
    protected void GridViewWebLinksDeleted_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //Restores a deleted link
        if (WebLinkManagement.RestoreWebLink(Convert.ToInt32(GridViewWebLinksDeleted.Rows[e.NewEditIndex].Cells[1].Text)))
        {
            ShowSuccessMessage("Weblink Restored !");
            LoadViewDeletedWebLinks();//Refresh the pending weblinks list            
        }
        else
        {
            ShowErrorMessage("Error Restoring the Weblink. Please try again.");
        }
        e.Cancel = true;
    }
    #endregion

    /// <summary>
    /// Loads/Refreshes the Active WebLinks List
    /// </summary>
    private void LoadViewActiveWebLinks()
    {
        GridViewWebLinks.DataBind();
    }

    /// <summary>
    /// Loads/Refreshes the Pending Weblinks List
    /// </summary>
    private void LoadViewPendingWebLinks()
    {
        GridViewWebLinksPending.DataBind();
    }

    /// <summary>
    /// Loads/Refreshes Deleted Weblinks List
    /// </summary>
    private void LoadViewDeletedWebLinks()
    {
        GridViewWebLinksDeleted.DataBind();
    }

    /// <summary>
    /// Resets the Edit Form by clearing all the textboxes and labels in it. Makes it ready for a new entry.
    /// </summary>
    private void ResetEditForm()
    {
        lblIdEdit.Text = "";
        lblAddedEdit.Text = "";
        lblUpdatedEdit.Text = "";
        textboxLinkNameEdit.Text = "";
        textboxLinkURLEdit.Text = "";
        textboxCategoryEdit.Text = "";
        textboxDetailEdit.Text = "";
    }

    /// <summary>
    /// Resets the Add Form by clearing all the textboxes and labels in it. Makes it ready for a new entry.
    /// </summary>
    private void ResetAddForm()
    {
        textboxLinkNameAdd.Text = "";
        textboxLinkURLAdd.Text = "http://";
        textboxDetailAdd.Text = "";
        textboxCategoryAdd.Text = "";
        MultiView1.ActiveViewIndex = 1;
    }

    /// <summary>
    /// Populates the WebLink Edit form for further editing
    /// </summary>
    /// <param name="poWebLink"></param>
    private void PopulateEditForm(WebLink poWebLink)
    {
        lblIdEdit.Text = poWebLink.ID;
        lblAddedEdit.Text = poWebLink.AddedOn;
        lblUpdatedEdit.Text = poWebLink.UpdatedOn;
        if (poWebLink.UpdatedOn.Length < 1)
            lblUpdatedEdit.Text = "Never";
        textboxLinkNameEdit.Text = poWebLink.Name;
        textboxLinkURLEdit.Text = poWebLink.Url;
        textboxCategoryEdit.Text = poWebLink.Category;
        textboxDetailEdit.Text = poWebLink.Detail;
    }    

    #region Error/Success Message
    private short csMsgCode = 2;
    private string csMsg = "";
    /// <summary>
    /// Show the successful message
    /// </summary>
    /// <param name="psMessage">The actual success message</param>
    private void ShowSuccessMessage(string psMessage)
    {
        csMsgCode = 0;
        csMsg = psMessage;
    }

    /// <summary>
    /// Show the error message
    /// </summary>
    /// <param name="psMessage">The actual error message</param>
    private void ShowErrorMessage(string psMessage)
    {
        csMsgCode = 1;
        csMsg = psMessage;
    }

    /// <summary>
    /// Shows a success or error message. To be called only from inside of Page_LoadComplete Event handler
    /// </summary>
    /// <param name="psVal">pass 0 , 1 or 2 for success, error or no message respectively</param>
    /// <param name="psMessage">Actual Message to display. Pass an empty string if no message to display</param>
    private void ShowMessage(short psVal, string psMessage)
    {
        lblMsg.Text = psMessage;
        if (psVal == 0)
        {
            lblMsg.CssClass = "successMsg";
        }
        else if (psVal == 1)
        {
            lblMsg.CssClass = "errorMsg";
        }
    }
    #endregion        


}
