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

public partial class controls_admin_CommentManager : System.Web.UI.UserControl
{
    private const string csCommentID = "commentID";
    List<CommentEntry> coCommentList = new List<CommentEntry>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            pagesDropDownList.DataSource = CommentPageManagement.GetCommentPageList();
            addPageDropDownList.DataSource = MenuItemManagement.GetMenuItemList(-1);
            DataBind();
            pagesDropDownList.Items.Insert(0, new ListItem("Select a page"));
            addPageDropDownList.Items.Insert(0, new ListItem("Select a page"));
            addPageDropDownList.Items.Add(new ListItem("Other...", addPageDropDownList.Items.Count.ToString()));
        }

        UpdateCommentsGrid();
        updateResultLabel.Text = String.Empty;
    }

    protected void UpdateCommentButton_Click(object sender, EventArgs e)
    {
        int liID;

        if (!Int32.TryParse((string)ViewState[csCommentID], out liID))
        {
            return;
        }

        CommentEntryManagement.SaveCommentEntry(liID, String.Empty,
            editCommentTextBox.Text, String.Empty, DateTime.Now, true, 0);

        commentsGridView.DataSource = CommentEntryManagement.
            GetCommentPageCommentEntries(pagesDropDownList.SelectedValue);
        commentsGridView.DataBind();

        commentManagerMultiView.ActiveViewIndex = 0;
    }

    protected void CancelButton_Click(object sender, EventArgs e)
    {
        commentManagerMultiView.ActiveViewIndex = 0;
    }

    protected void CommentsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        CommentEntryManagement.DeleteCommentEntry((int)commentsGridView.DataKeys[e.RowIndex].Value);
        UpdateCommentsGrid();
    }

    protected void CommentsGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string lsCommentID = Convert.ToString(commentsGridView.DataKeys[e.NewEditIndex].Value);
        CommentEntry loCommentEntry = CommentEntryManagement.GetCommentEntryByID(lsCommentID);
        digioz.MenuItem loPageDesc = MenuItemManagement.GetMenuItemFromPageID(loCommentEntry.PageID);

        editCommentTextBox.Text = ((Label)commentsGridView.Rows[e.NewEditIndex].FindControl("commentLabel")).Text;
        ViewState[csCommentID] = lsCommentID;
        lblHeader.Text= "By:  ";
        lblHeader.Text += loCommentEntry.UserName + " " + loCommentEntry.ComDateTime;
        lblHeader.Text += " on the " + loPageDesc.Name + " page.";
        e.Cancel = true;
        commentManagerMultiView.ActiveViewIndex = 2;
    }

    protected void PagesDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        UpdateCommentsGrid();
    }

    protected void AddPageDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (addPageDropDownList.SelectedIndex == addPageDropDownList.Items.Count - 1)
        {
            customPageTextBox.Visible = true;
        }
        else
        {
            customPageTextBox.Visible = false;
        }
    }

    protected void EnableCommentsButton_Click(object sender, EventArgs e)
    {
        ManagePageEntry(true);
    }

    protected void DisableButton_Click(object sender, EventArgs e)
    {
        ManagePageEntry(false);
    }

    protected void DisableCommentsButton_Click(object sender, EventArgs e)
    {
        CommentPageManagement.SaveCommentPage(new CommentPage(pagesDropDownList.SelectedValue, false, 0));
        pagesDropDownList.Items.Remove(pagesDropDownList.SelectedItem);
        UpdateCommentsGrid();
    }

    protected void MainMenu_MenuItemClick(object sender, MenuEventArgs e)
    {
        commentManagerMultiView.ActiveViewIndex = Convert.ToInt32(e.Item.Value);
    }

    private void ManagePageEntry(bool pbIsEnabling)
    {
        if (addPageDropDownList.SelectedIndex == 0)
        {
            return;
        }

        bool lbIsSuccessful;

        if (addPageDropDownList.SelectedIndex == addPageDropDownList.Items.Count - 1)
        {
            string lsDecodedUrl = ControlsHelper.GetDecodedUrl(customPageTextBox.Text);
            int liPageID = Convert.ToInt16(addPageDropDownList.SelectedValue);

            if (pbIsEnabling)
            {
                lbIsSuccessful = CommentPageManagement.SaveCommentPage(new CommentPage(lsDecodedUrl, true, liPageID));
                ShowUpdateResult(lsDecodedUrl, true, false, lbIsSuccessful);
            }
            else
            {
                lbIsSuccessful = CommentPageManagement.SaveCommentPage(new CommentPage(lsDecodedUrl, false, liPageID));
                ShowUpdateResult(lsDecodedUrl, false, false, lbIsSuccessful);
            }
        }
        else
        {
            bool lbIsSuccessful2;
            string lsPageName = String.Format("/Default.aspx?id={0}", addPageDropDownList.SelectedValue);
            int liPageID = Convert.ToInt16(addPageDropDownList.SelectedValue);

            if (pbIsEnabling)
            {
                lbIsSuccessful = CommentPageManagement.SaveCommentPage(new CommentPage(lsPageName, true, liPageID));
                lbIsSuccessful2 = CommentPageManagement.SaveCommentPage(new CommentPage(addPageDropDownList.SelectedItem.Text, true, liPageID));
                ShowUpdateResult(addPageDropDownList.SelectedItem.Text, true, true, lbIsSuccessful && lbIsSuccessful2);
            }
            else
            {
                lbIsSuccessful = CommentPageManagement.DeleteCommentPage(lsPageName);
                lbIsSuccessful2 = CommentPageManagement.DeleteCommentPage(addPageDropDownList.SelectedItem.Text);
                ShowUpdateResult(addPageDropDownList.SelectedItem.Text, false, true, lbIsSuccessful && lbIsSuccessful2);
            }
        }
    }

    private void ShowUpdateResult(string psUpdatedPage, bool pbIsCommentationEnabled,
        bool pbIsParentPage, bool pbIsSuccessful)
    {
        if (!pbIsSuccessful)
        {
            updateResultLabel.Text = "Error occured when processing request. Please check entered Url or contact the support team.";
            return;
        }
        string lsState = pbIsCommentationEnabled ? "enabled" : "disabled";

        if (pbIsParentPage)
        {
            updateResultLabel.Text = String.Format(
                "Comments have been {0} on subpages of the page {1}.", lsState, psUpdatedPage);
        }
        else
        {
            updateResultLabel.Text = String.Format("Comments have been {0} on the page {1}.",
                lsState, psUpdatedPage);
        }
    }

    private void UpdateCommentsGrid()
    {
        coCommentList = CommentEntryManagement.GetCommentPageCommentEntries(pagesDropDownList.SelectedValue);
        List<CommentEntry> loGridSource = coCommentList;

        if (pagesDropDownList.SelectedIndex == 0)
        {
            noCommentsLabel.Visible = false;
            commentsGridView.DataSource = null;
            commentsGridView.DataBind();
        }
        else if (loGridSource.Count == 0)
        {
            noCommentsLabel.Visible = true;
            commentsGridView.DataSource = null;
            commentsGridView.DataBind();
        }
        else
        {
            noCommentsLabel.Visible = false;
            commentsGridView.DataSource = loGridSource;
            commentsGridView.DataBind();
        }
    }

}
