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

namespace digioz
{
    /// <summary>
    /// Class for Managing Forums
    /// </summary>
    public class ForumManagement
    {
        private static PortalSettings coPortalSettings;

        public ForumManagement() 
        {}

        public static bool Insert(Forum poForum)
        {
            bool lbReturnValue = false;
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                try
                {
                    string lsSQL = "EXEC spInsertForum "
                                        + "'" + FormatForumText(poForum.ForumName) + "',"
                                        + "'" + FormatForumText(poForum.ForumDesc) + "',"
                                        + "'" + poForum.ForumLink + "',"
                                        + "'" + poForum.ForumImage + "',"
                                        + poForum.ForumType + ","
                                        + poForum.ForumStatus + ","
                                        + poForum.ForumPosts + ","
                                        + poForum.ForumTopics + ","
                                        + "null,null,null,null;";

                    loDB.openConnection();
                    loDB.ExecDB(lsSQL);
                    loDB.closeConnection();
                    lbReturnValue = true;
                }
                catch
                {
                    lbReturnValue = false;
                }
            }
            return lbReturnValue;
        }

        public static bool Save(Forum poForum)
        {
            if (poForum.ID < 0)
            {
                return Insert(poForum);
            }
            else
            {
                return Update(poForum);
            }
        }

        public static bool Update(Forum poForum)
        {
            bool lbReturnValue = false;
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                try
                {
                    string lsSQL = "spUpdateForumBasic";
                    loDB.openConnection();

                    DbParameter[] loParameters = new DbParameter[8];

                    loParameters[0] = new SqlParameter();
                    loParameters[0].ParameterName = "@frm_id";
                    loParameters[0].DbType = DbType.Int32;
                    loParameters[0].Value = poForum.ID;

                    loParameters[1] = new SqlParameter();
                    loParameters[1].ParameterName = "@frm_name";
                    loParameters[1].DbType = DbType.String;
                    loParameters[1].Value = FormatForumText(poForum.ForumName);

                    loParameters[2] = new SqlParameter();
                    loParameters[2].ParameterName = "@frm_desc";
                    loParameters[2].DbType = DbType.String;
                    loParameters[2].Value = FormatForumText(poForum.ForumDesc);

                    loParameters[3] = new SqlParameter();
                    loParameters[3].ParameterName = "@frm_link";
                    loParameters[3].DbType = DbType.String;
                    loParameters[3].Value = poForum.ForumLink;

                    loParameters[4] = new SqlParameter();
                    loParameters[4].ParameterName = "@frm_image";
                    loParameters[4].DbType = DbType.String;
                    loParameters[4].Value = poForum.ForumImage;

                    loParameters[5] = new SqlParameter();
                    loParameters[5].ParameterName = "@frm_type";
                    loParameters[5].DbType = DbType.Int32;
                    loParameters[5].Value = poForum.ForumType;

                    loParameters[6] = new SqlParameter();
                    loParameters[6].ParameterName = "@frm_status";
                    loParameters[6].DbType = DbType.Int32;
                    loParameters[6].Value = poForum.ForumStatus;

                    loParameters[7] = new SqlParameter();
                    loParameters[7].ParameterName = "@frm_order";
                    loParameters[7].DbType = DbType.Int32;
                    if (poForum.ForumOrder > 0)
                    {
                        loParameters[7].Value = poForum.ForumOrder;
                    }
                    else
                    {
                        loParameters[7].Value = -1;
                    }

                    int liCount = 0;
                    loDB.ExecDBProc(lsSQL, out liCount, loParameters);
                    loDB.closeConnection();

                    lbReturnValue = liCount == 1;
                }
                catch
                {
                    lbReturnValue = false;
                }
            }
            return lbReturnValue;
        }

        public static string GetForumName(int piForumID)
        {
            string lsForumName = String.Empty;
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = "SELECT forum_name FROM tblForum WHERE forum_id=" + piForumID + ";";

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

        public static ForumList GetForumList()
        {
            Forum loForum;
            ForumList loForumList = new ForumList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = "SELECT * FROM tblForum ORDER BY forum_order";
                DataTable loForums;

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

                foreach (DataRow loDR in loForums.Rows)
                {
                    loForum = new Forum();
                    loForum.ID = Convert.ToInt16(loDR["forum_id"]);
                    loForum.ForumName = ReformatForumText(Convert.ToString(loDR["forum_name"]));
                    loForum.ForumDesc =  ReformatForumText(Convert.ToString(loDR["forum_desc"]));
                    loForum.ForumLink = Convert.ToString(loDR["forum_link"]);
                    loForum.ForumImage = Convert.ToString(loDR["forum_image"]);
                    loForum.ForumType = Convert.ToInt16(loDR["forum_type"]);
                    loForum.ForumStatus = Convert.ToInt16(loDR["forum_status"]);
                    loForum.ForumOrder = Convert.ToInt16(loDR["forum_order"]);
                    loForum.ForumPosts = Convert.ToInt32(loDR["forum_posts"]);
                    loForum.ForumTopics = Convert.ToInt32(loDR["forum_topics"]);
                    if (!loDR["forum_last_post_id"].Equals(DBNull.Value))
                    {
                        loForum.ForumLastPostID = Convert.ToInt32(loDR["forum_last_post_id"]);
                    }
                    if (!loDR["forum_last_post_subject"].Equals(DBNull.Value))
                    {
                        loForum.ForumLastPostSubject = Convert.ToString(loDR["forum_last_post_subject"]);
                    }
                    if (!loDR["forum_last_post_time"].Equals(DBNull.Value))
                    {
                        loForum.ForumLastPostTime = Convert.ToDateTime(loDR["forum_last_post_time"]);
                    }
                    if (!loDR["forum_last_poster_name"].Equals(DBNull.Value))
                    {
                        loForum.ForumLastPosterName = Convert.ToString(loDR["forum_last_poster_name"]);
                    }

                    loForumList.Add(loForum);
                }
            }
            return loForumList;
        }

        public static string PrintSignature(string psValue)
        {
            string lsSignature = String.Empty;

            if (psValue.Length > 0)
            {
                lsSignature = "<hr>" + psValue;
            }
            return lsSignature;
        }

        public static bool DeleteForum(Forum poForum)
        {
            bool lbReturnValue = false;
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                try
                {
                    if (poForum == null || poForum.ID <= 0) return false;

                    loDB.openConnection();

                    //string lsSQL = "SELECT forum_order FROM tblForum WHERE forum_id=" + poForum.ID + ";";
                    //int ciForumOrder = Convert.ToInt16(loDB.ExecDBScalar(lsSQL));

                    //lsSQL = "DELETE FROM tblForum WHERE forum_id=" + poForum.ID + ";";
                    //loDB.ExecDB(lsSQL);

                    //lsSQL = "UPDATE tblForum SET forum_order = forum_order -1 WHERE forum_order > " + ciForumOrder + ";";

                    string lsSQL = "EXEC spDeleteForum " + poForum.ID + ";";
                    loDB.ExecDB(lsSQL);

                    loDB.closeConnection();
                    lbReturnValue = true;
                }
                catch
                {
                    lbReturnValue = false;
                }
            }
            return lbReturnValue;
        }

        public static bool MoveForum(Forum poForum, Forum poSwapForum, int piStep)
        {
            poSwapForum.ForumOrder = poForum.ForumOrder;
            Update(poSwapForum);
            poForum.ForumOrder = poForum.ForumOrder + piStep;
            Update(poForum);

            return true;
        }

        public static string FormatForumText(string psForumText)
        {
            string lsOutput = psForumText;
            lsOutput = System.Text.RegularExpressions.Regex.Replace(psForumText, "<[^>]*>", string.Empty);
            lsOutput = lsOutput.Replace("'", "''");
            return lsOutput;
        }   //end FormatForumText

        public static string ReformatForumText(string psForumText)
        {
            string lsOutput = psForumText;
            lsOutput = System.Text.RegularExpressions.Regex.Replace(psForumText, "<[^>]*>", string.Empty);
            //lsOutput = lsOutput.Replace("\n", "<br />");
            lsOutput = lsOutput.Replace("''", "'");
            return lsOutput;
        }   //end ReformatFormText


    }
}

