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

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

        public static ForumPostList GetForumPostTop5List()
        {
            ForumPostList loForumPostList = new ForumPostList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

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

                string lsSQL = "select Top 5* from tblForumPosts Order BY post_id DESC;";

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

                foreach (DataRow loDR in loMenus.Rows)
                {
                    loForumPost = new ForumPost();
                    loForumPost.PostID = Convert.ToInt16(loDR["post_id"]);
                    loForumPost.TopicID = Convert.ToInt16(loDR["topic_id"]);
                    loForumPost.ForumID = Convert.ToInt16(loDR["forum_id"]);
                    loForumPost.PosterID = Convert.ToInt16(loDR["poster_id"]);
                    loForumPost.PosterName = Convert.ToString(loDR["poster_name"]);
                    loForumPost.PosterIP = Convert.ToString(loDR["poster_ip"]);
                    loForumPost.PostTime = Convert.ToDateTime(loDR["post_time"]);
                    loForumPost.PostSubject = Convert.ToString(loDR["post_subject"]);
                    loForumPost.PostText = Convert.ToString(loDR["post_text"]);
                    loForumPost.PostAttachment = Convert.ToInt16(loDR["post_attachment"]);

                    loForumPostList.Add(loForumPost);
                }
            }
            return loForumPostList;
        }

        public static ForumPostList GetForumTopicView(int piTopicID)
        {
            ForumPostList loForumPostList = new ForumPostList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

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

                string lsSQL = "SELECT tblForumPosts.*,tblUserProfile.profile_avatar,tblUserProfile.profile_signature FROM tblForumPosts LEFT JOIN tblUserProfile ON tblForumPosts.poster_name=tblUserProfile.profile_username WHERE tblForumPosts.topic_id=" + piTopicID + " ORDER BY tblForumPosts.post_id ASC;";

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

                foreach (DataRow loDR in loMenus.Rows)
                {
                    loForumPost = new ForumPost();
                    loForumPost.PostID = Convert.ToInt16(loDR["post_id"]);
                    loForumPost.TopicID = Convert.ToInt16(loDR["topic_id"]);
                    loForumPost.ForumID = Convert.ToInt16(loDR["forum_id"]);
                    loForumPost.PosterID = Convert.ToInt16(loDR["poster_id"]);
                    loForumPost.PosterName = Convert.ToString(loDR["poster_name"]);
                    loForumPost.PosterIP = Convert.ToString(loDR["poster_ip"]);
                    loForumPost.PostTime = Convert.ToDateTime(loDR["post_time"]);
                    loForumPost.PostSubject = Convert.ToString(loDR["post_subject"]);
                    loForumPost.PostText = Convert.ToString(loDR["post_text"]);
                    loForumPost.PostAttachment = Convert.ToInt16(loDR["post_attachment"]);
                    loForumPost.ProfileAvatar = Convert.ToString(loDR["profile_avatar"]);
                    loForumPost.ProfileSignature = Convert.ToString(loDR["profile_signature"]);

                    loForumPostList.Add(loForumPost);
                }
            }
            return loForumPostList;
        }

        public static bool SaveForumPost(ForumPost poForumPost, string psMode)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            bool lbResult = false;

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

                loDB.openConnection();

                if (psMode == "post")
                {
                    string query = "spInsertForumTopic";
                    DbParameter[] _par = new DbParameter[11];

                    _par[0] = new SqlParameter();
                    _par[0].ParameterName = "@in_forum_id";
                    _par[0].DbType = DbType.Int32;
                    _par[0].Value = poForumPost.ForumID; // ciForumID;

                    _par[1] = new SqlParameter();
                    _par[1].ParameterName = "@in_topic_title";
                    _par[1].DbType = DbType.String;
                    _par[1].Value = poForumPost.PostSubject; // lsSubject;

                    _par[2] = new SqlParameter();
                    _par[2].ParameterName = "@in_post_message";
                    _par[2].DbType = DbType.String;
                    _par[2].Value = poForumPost.PostText; // lsMessage;

                    _par[3] = new SqlParameter();
                    _par[3].ParameterName = "@in_topic_time";
                    _par[3].DbType = DbType.DateTime;
                    _par[3].Value = poForumPost.PostTime; // lsDateTime;

                    _par[4] = new SqlParameter();
                    _par[4].ParameterName = "@in_topic_views";
                    _par[4].DbType = DbType.Int32;
                    _par[4].Value = 0;

                    _par[5] = new SqlParameter();
                    _par[5].ParameterName = "@in_topic_replies";
                    _par[5].DbType = DbType.Int32;
                    _par[5].Value = 0;

                    _par[6] = new SqlParameter();
                    _par[6].ParameterName = "@in_topic_status";
                    _par[6].DbType = DbType.Int32;
                    _par[6].Value = 1;

                    _par[7] = new SqlParameter();
                    _par[7].ParameterName = "@in_topic_first_poster_id";
                    _par[7].DbType = DbType.Int32;
                    _par[7].Value = poForumPost.PosterID; // Convert.ToInt32(Session["userid"]);

                    _par[8] = new SqlParameter();
                    _par[8].ParameterName = "@in_topic_first_poster_name";
                    _par[8].DbType = DbType.String;
                    _par[8].Value = poForumPost.PosterName; // Session["username"].ToString();

                    _par[9] = new SqlParameter();
                    _par[9].ParameterName = "@in_poster_ip";
                    _par[9].DbType = DbType.String;
                    _par[9].Value = poForumPost.PosterIP; // ""; // Session["vistorIP"].ToString();

                    _par[10] = new SqlParameter();
                    _par[10].ParameterName = "@in_post_attachment";
                    _par[10].DbType = DbType.Int32;
                    _par[10].Value = poForumPost.PostAttachment; // liAttachment;

                    loDB.ExecDBProc(query, out liCount, _par);
                    lbResult = true; 
                }
                else if (psMode == "reply")
                {
                    string query = "spInsertForumPost";
                    DbParameter[] _par = new DbParameter[9];

                    _par[0] = new SqlParameter();
                    _par[0].ParameterName = "@in_topic_id";
                    _par[0].DbType = DbType.Int64;
                    _par[0].Value = poForumPost.TopicID; // ciTopicID;

                    _par[1] = new SqlParameter();
                    _par[1].ParameterName = "@in_forum_id";
                    _par[1].DbType = DbType.Int32;
                    _par[1].Value = poForumPost.ForumID; // ciForumID;

                    _par[2] = new SqlParameter();
                    _par[2].ParameterName = "@in_poster_id";
                    _par[2].DbType = DbType.Int32;
                    _par[2].Value = poForumPost.PosterID; // Convert.ToInt32(Session["userid"]);

                    _par[3] = new SqlParameter();
                    _par[3].ParameterName = "@in_poster_name";
                    _par[3].DbType = DbType.String;
                    _par[3].Value = poForumPost.PosterName; // Session["username"].ToString();

                    _par[4] = new SqlParameter();
                    _par[4].ParameterName = "@in_poster_ip";
                    _par[4].DbType = DbType.String;
                    _par[4].Value = poForumPost.PosterIP; // ""; // Session["vistorIP"].ToString();

                    _par[5] = new SqlParameter();
                    _par[5].ParameterName = "@in_post_time";
                    _par[5].DbType = DbType.DateTime;
                    _par[5].Value = poForumPost.PostTime; // lsDateTime;

                    _par[6] = new SqlParameter();
                    _par[6].ParameterName = "@in_post_subject";
                    _par[6].DbType = DbType.String;
                    _par[6].Value = poForumPost.PostSubject; // lsSubject;

                    _par[7] = new SqlParameter();
                    _par[7].ParameterName = "@in_post_text";
                    _par[7].DbType = DbType.String;
                    _par[7].Value = poForumPost.PostText; // lsMessage;

                    _par[8] = new SqlParameter();
                    _par[8].ParameterName = "@in_post_attachment";
                    _par[8].DbType = DbType.Int32;
                    _par[8].Value = poForumPost.PostAttachment; // liAttachment;

                    loDB.ExecDBProc(query, out liCount, _par);
                    lbResult = true;
                }
                else
                {
                    // Do Nothing for now
                    lbResult = true; 
                }
                loDB.closeConnection();
            }
            return lbResult;
        }
    }
}

