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

public partial class controls_Comments : System.Web.UI.UserControl
{
    private string csDecodedUrl;
    private PortalSettings coPortalSettings;
    private int ciPageID;
    private string csPageName;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.Visible)
        {
            return;
        }

        WhatPageIsIt();
        csDecodedUrl = HttpUtility.UrlDecode(Request.Url.PathAndQuery);
        coPortalSettings = PortalSettingsManagement.GetPortalSettings();
        commentsGridView.DataSource = CommentEntryManagement.GetCommentPageCommentEntries(csDecodedUrl);
        commentsGridView.DataBind();
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (imgVerifyCodeTextBox.Text != imageVerifier.Text)
        {
            imageVerifierErrorLabel.Text = "You have entered a wrong code. Please enter the new one correctly.";
            imgVerifyCodeTextBox.Text = String.Empty;
            return;
        }

        string lsUserName = Convert.ToString(Session["username"]);
        DateTime ldCommentEntry = DateTime.Now;
        CommentEntryManagement.SaveCommentEntry(0, csDecodedUrl, newCommentTextBox.Text, lsUserName, ldCommentEntry, false, ciPageID);
        CommentPageManagement.SaveCommentPage(new CommentPage(csDecodedUrl, true, ciPageID));

        commentsGridView.DataSource = CommentEntryManagement.GetCommentPageCommentEntries(csDecodedUrl);
        commentsGridView.DataBind();

        imageVerifierErrorLabel.Text = String.Empty;
        imgVerifyCodeTextBox.Text = String.Empty;
        newCommentTextBox.Text = String.Empty;
    }
    protected void CommentsGridView_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink loProfileLink = e.Row.FindControl("profileHyperLink") as HyperLink;
            CommentEntry loCommentEntry = e.Row.DataItem as CommentEntry;

            if (loProfileLink != null && loCommentEntry != null)
            {
                loProfileLink.NavigateUrl = String.Format("{0}?page={1}&username={2}",
                    "../Default.aspx",
                    HttpUtility.UrlEncode(digioz.Encryption64.Encrypt("Profile", coPortalSettings.URLEncryptionKey)),
                    HttpUtility.UrlEncode(digioz.Encryption64.Encrypt(loCommentEntry.UserName, coPortalSettings.URLEncryptionKey)));
            }
        }
    }

    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;
            }
        }
    }

    protected void commentsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        commentsGridView.PageIndex = e.NewPageIndex;
        commentsGridView.DataBind();
    }
}
