using System;
using System.Data;
using System.Configuration;
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;

namespace digioz
{
    public class ForumTopicManagement
    {
        private static PortalSettings coPortalSettings;

        public static string GetForumTopicTitle(int piTopicID)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            string lsTopicTitle = String.Empty;

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);

                string lsSQL = "SELECT topic_title FROM tblForumTopics WHERE topic_id=" + piTopicID + ";";

                loDB.openConnection();
                lsTopicTitle = Convert.ToString(loDB.ExecDBScalar(lsSQL));
                loDB.closeConnection();
            }
            return lsTopicTitle;
        }

        public static ForumTopicList GetForumTopicList(int piForumID)
        {
            ForumTopicList loForumTopicList = new ForumTopicList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                ForumTopic loForumTopic;
                DataTable loMenus;

                string lsSQL = "select * from tblForumTopics WHERE forum_id=" + piForumID + " ORDER BY topic_last_post_time DESC;";

                loDB.openConnection();
                loMenus = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();

                foreach (DataRow loDR in loMenus.Rows)
                {
                    loForumTopic = new ForumTopic();
                    loForumTopic.TopicID = Convert.ToInt16(loDR["topic_id"]);
                    loForumTopic.ForumID = Convert.ToInt16(loDR["forum_id"]);
                    loForumTopic.TopicTitle = Convert.ToString(loDR["topic_title"]);
                    loForumTopic.TopicTime = Convert.ToDateTime(loDR["topic_time"]);
                    loForumTopic.TopicViews = Convert.ToInt16(loDR["topic_views"]);
                    loForumTopic.TopicReplies = Convert.ToInt16(loDR["topic_replies"]);
                    loForumTopic.TopicStatus = Convert.ToInt16(loDR["topic_status"]);
                    loForumTopic.TopicFirstPostID = Convert.ToInt16(loDR["topic_first_post_id"]);
                    loForumTopic.TopicFirstPosterID = Convert.ToInt16(loDR["topic_first_poster_id"]);
                    loForumTopic.TopicFirstPosterName = Convert.ToString(loDR["topic_first_poster_name"]);
                    loForumTopic.TopicLastPostID = Convert.ToInt16(loDR["topic_last_post_id"]);
                    if (!loDR["topic_last_post_time"].Equals(DBNull.Value))
                    {
                        loForumTopic.TopicLastPostTime = Convert.ToDateTime(loDR["topic_last_post_time"]);
                    }
                    loForumTopic.TopicLastPosterID = Convert.ToInt16(loDR["topic_last_poster_id"]);
                    loForumTopic.TopicLastPosterName = Convert.ToString(loDR["topic_last_poster_name"]);

                    loForumTopicList.Add(loForumTopic);
                }
            }
            return loForumTopicList;
        }

        public static void UpdateForumTopicViewCount(int piTopicID)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);

                string lsSQL = "UPDATE tblForumTopics SET topic_views = (topic_views + 1) WHERE topic_id=" + piTopicID + ";";

                loDB.openConnection();
                loDB.ExecDB(lsSQL);
                loDB.closeConnection();
            }
        }
    }
}

