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

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

        coPortalSettings = PortalSettingsManagement.GetPortalSettings();
        digioz.Hashtable coConfig = new digioz.Hashtable();
        ConfigurationList loConfigurationList = ConfigurationManagement.GetConfigurationList();

        foreach (digioz.Configuration loConfig in loConfigurationList)
        {
            coConfig.AddToTable(loConfig.KeyName, loConfig.KeyValue);
        }

        try { GridForumView.PageSize = Convert.ToInt16(coConfig.GetValue("Forum Threads Per Page")); } catch {}

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

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

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

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

        csForumName = digioz.ForumManagement.GetForumName(ciForumID); 

        lblForumName.Text = csForumName;

        ForumTopicList loForumTopicList = ForumTopicManagement.GetForumTopicList(ciForumID);

        if (loForumTopicList.Count > 0)
        {
            GridForumView.DataSource = loForumTopicList;
            if (!IsPostBack)
            {
                GridForumView.DataBind();
            }
        }
        else
        {
            lblForumNotice.Text = "No Topics Available!<br>";
        }

        // Set Forum Navigation Menu ------------------------------------------------------

        lblNavigation.Text = LoadForumNavigation();
    }

    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=" + csPage + "&forum=" + ciForumID + "\">" + csForumName + "</a>";

        return lsNavigationString;
    }

    protected void btnNewTopic_Click(object sender, EventArgs e)
    {
        PostNewTopic();
    }

    protected void btnNewTopic2_Click(object sender, EventArgs e)
    {
        PostNewTopic();
    }

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

    protected void GridForumView_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        this.GridForumView.PageIndex = e.NewPageIndex;
        this.GridForumView.DataBind();
    }

    protected void GridForumView_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

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