using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

namespace digioz
{
    /// <summary>
    /// Summary description for CommentPageManagement
    /// </summary>
    public class CommentPageManagement
    {
        private static PortalSettings coPortalSettings;

        public CommentPageManagement()
        { }

        /// <summary>
        /// Gets the list of comment pages which have comments.
        /// </summary>
        /// <returns>List of comment pages which have comments.</returns>
        public static List<string> GetCommentPageList()
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            List<string> loCommentPageList = new List<string>();


            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                DataTable loCommentPages;
                string lsSQL = "EXEC spGetCommentPageList";

                try
                {
                    loDB.openConnection();
                    loCommentPages = loDB.QueryDBDataset(lsSQL);

                    if (loCommentPages.Rows.Count > 0)
                    {
                        loCommentPageList.Add("All");
                        foreach (DataRow loDR in loCommentPages.Rows)
                        {
                            loCommentPageList.Add(Convert.ToString(loDR["commentpage_parent_page"]));
                        }
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }

            return loCommentPageList;
        }

        public static CommentPage GetCommentPage(string psCommentPageParentPage)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            CommentPage loCommentPage = null;

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                DataTable loCommentPages;
                string lsSQL = "EXEC spGetCommentPage [" + psCommentPageParentPage + "];";

                try
                {
                    loDB.openConnection();
                    loCommentPages = loDB.QueryDBDataset(lsSQL);

                    if (loCommentPages.Rows.Count > 0)
                    {
                        loCommentPage = new CommentPage(
                            Convert.ToString(loCommentPages.Rows[0][0]),
                            Convert.ToBoolean(loCommentPages.Rows[0][1]),
                            Convert.ToInt16(loCommentPages.Rows[0][2]));
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }

            return loCommentPage;
        }

        /// <summary>
        /// Tries to update the specified comment page.
        /// If the specified page does not exists in the db,
        /// inserts new record.
        /// </summary>
        /// <param name="poCommentPage">The comment page.</param>
        /// <returns>True if operation completed successful.</returns>
        public static bool SaveCommentPage(CommentPage poCommentPage)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

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

                // Try update comment page.
                string lsSQL = "spUpdateCommentPage";
                DbParameter[] loParameters = new DbParameter[2];

                loParameters[0] = new SqlParameter("@commentpage_parent_page", SqlDbType.VarChar);
                loParameters[0].Value = poCommentPage.ParentPage;
                loParameters[1] = new SqlParameter("@commentpage_comments_enabled", SqlDbType.Bit);
                loParameters[1].Value = poCommentPage.IsCommentationEnabled;

                try
                {
                    loDB.openConnection();
                    loDB.ExecDBProc(lsSQL, out liCount, loParameters);
                }
                catch (SqlException)
                {
                    return false;
                }
                finally
                {
                    loDB.closeConnection();
                }

                // If there is no record for this page in the db yet,
                // insert new record.
                if (liCount == 0)
                {
                    loParameters = new DbParameter[3];
                    lsSQL = "spInsertCommentPage";

                    loParameters[0] = new SqlParameter("@commentpage_parent_page", SqlDbType.VarChar);
                    loParameters[0].Value = poCommentPage.ParentPage;
                    loParameters[1] = new SqlParameter("@commentpage_comments_enabled", SqlDbType.Bit);
                    loParameters[1].Value = poCommentPage.IsCommentationEnabled;
                    loParameters[2] = new SqlParameter("@commentpage_pageID", SqlDbType.Int);
                    loParameters[2].Value = poCommentPage.CommentPagePageID;

                    try
                    {
                        loDB.openConnection();
                        loDB.ExecDBProc(lsSQL, out liCount, loParameters);
                    }
                    catch (SqlException)
                    {
                        return false;
                    }
                    finally
                    {
                        loDB.closeConnection();
                    }
                }
            }

            return true;
        }

        public static bool DeleteCommentPage(string psParentPage)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            bool lbResult = false;

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

                DbParameter[] loParameters = new DbParameter[1];
                loParameters[0] = new SqlParameter("@commentpage_parent_page", SqlDbType.VarChar);
                loParameters[0].Value = psParentPage;

                try
                {
                    loDB.openConnection();
                    loDB.ExecDBProc(lsSQL, out liCount, loParameters);
                    lbResult = true;
                }
                finally
                {
                    loDB.closeConnection();
                }
            }

            return lbResult;
        }
    }
}