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_ForumTopicView : System.Web.UI.UserControl
{
    PortalSettings coPortalSettings;
    public string csForumName;
    public int ciForumID, ciTopicID;
    public string csPage, csPageProfile;

    protected void Page_Load(object sender, EventArgs e)
    {
        // Load Settings -------------------------------------------------------------

        coPortalSettings = PortalSettingsManagement.GetPortalSettings();

        // Set some query string parameters ------------------------------------------
        try
        {
            ciForumID = Convert.ToInt16(Request.QueryString["forum"]);
        }
        catch
        {
            ciForumID = 1;
        }

        if (ciForumID == 0)
        {
            ciForumID = 1;
        }

        try
        {
            csPage = Server.UrlEncode(Convert.ToString(Request.QueryString["page"]));
        }
        catch
        {
            csPage = String.Empty;
        }

        try
        {
            ciTopicID = Convert.ToInt16(Request.QueryString["topic"]);
        }
        catch
        {
            ciTopicID = 1;
        }

        // Encode URL String for Grid display ---------------------------------------------

        csPageProfile = Server.UrlEncode(digioz.Encryption64.Encrypt("Profile", coPortalSettings.URLEncryptionKey));

        // Load Data into Grid ------------------------------------------------------------

        ForumPostList loForumPostList = ForumPostManagement.GetForumTopicView(ciTopicID);
        ForumTopicManagement.UpdateForumTopicViewCount(ciTopicID);

        if (loForumPostList.Count > 0)
        {
            PagedDataSource ps = new PagedDataSource();
            ps.DataSource = loForumPostList;
            lstPosts.DataSource = ps;
            lstPosts.DataBind();
        }
        else
        {
            // lblForumNotice.Text = "No Posts Available!<br>";
        }

        // Get Forum Name ----------------------------------------------------------------

        csForumName = digioz.ForumManagement.GetForumName(ciForumID); 

        // Load Forum Navigation ---------------------------------------------------------

        lblNavigation.Text = LoadForumNavigation();
    }

    protected void btnPostReply_Click(object sender, EventArgs e)
    {
        PostReply();
    }
    protected void btnPostReply2_Click(object sender, EventArgs e)
    {
        PostReply();
    }

    private string LoadForumNavigation()
    {
        string lsNavigationString = String.Empty;

        lsNavigationString = "<a href=\"Default.aspx?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Forum", coPortalSettings.URLEncryptionKey)) + "\"> Index </a>";
        lsNavigationString += " &gt; ";
        lsNavigationString += "<a href=\"Default.aspx?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Forum View", coPortalSettings.URLEncryptionKey)) + "&forum=" + ciForumID + "\">" + csForumName + "</a>";

        return lsNavigationString;
    }

    public void PostReply()
    {
        Response.Redirect("Default.aspx?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Forum Posting", coPortalSettings.URLEncryptionKey)) + "&mode=reply&forum=" + ciForumID + "&topic=" + ciTopicID);
    }

    public string GetEncryptedUsername(string psUserID)
    {
        string lsUsername;
        int liUserID = Convert.ToInt16(psUserID);

        lsUsername = (UserManagement.GetUserByID(liUserID)).Username;
        lsUsername = Server.UrlEncode(digioz.Encryption64.Encrypt(lsUsername, coPortalSettings.URLEncryptionKey));

        return lsUsername;
    }

    /// <summary>
    /// Format data before it is rendered
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lstPosts_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        /* Enocde the new line sequence before displaying it */
        if ((e.Item.ItemType == ListItemType.Item) ||
            (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            Label  postText = (Label)e.Item.FindControl("PostText");
            if (null !=  postText)
            {
                postText.Text = Server.HtmlEncode(postText.Text);
                postText.Text = Utility.EncodeNewLine(postText.Text);
            }
        }
    }
}
