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;


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

        /// <summary>
        /// get the options of a poll subject from its id
        /// </summary>
        /// <param name="SubjectID"></param>
        /// <returns></returns>
        public static PollOptionInfoList GetPollOptionInfoList(int SubjectID)
        {
            PollOptionInfoList loPollOptions = new PollOptionInfoList();
            PollOptionInfo loOption;

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

                String sql = "SELECT option_id, subject_id, option_name, option_votes FROM tblPollOption " +
                             "WHERE subject_id = " + SubjectID;

                try
                {
                    loDB.openConnection();
                    DataTable ta = loDB.QueryDBDataset(sql);
                    foreach (DataRow dr in ta.Rows)
                    {
                        loOption = new PollOptionInfo();
                        loOption.ID = (int)dr["option_id"];
                        loOption.SubjectID = (int)dr["subject_id"];
                        loOption.OptionName = (string)dr["option_name"];
                        loOption.OptionVotes = (int)dr["option_votes"];
                        loPollOptions.Add(loOption);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                IEnumerable loObjs = loDB.GetEntity("PollOptionInfo");
                foreach (object obj in loObjs)
                {
                    if(((PollOptionInfo)obj).SubjectID == SubjectID)
                        loPollOptions.Add((PollOptionInfo)obj);
                }
                loDB.closeEntity();
            }

            return loPollOptions;
        }

        /// <summary>
        /// Add a new PollOption record to the database
        /// </summary>
        /// <param name="option"></param>
        public static bool InsertPollOption(PollOptionInfo option, 
                                            out string sError)
        {
            bool lbRetValue=false;
            sError = "";
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                int RetValue = 0;
                string sql = "exec spSubmitPollOption " +
                             option.SubjectID + ", '" +
                             DoubleUpSingleQuotes(option.OptionName) + "'";
                try
                {
                    loDB.openConnection();
                    loDB.ExecDB(sql, out RetValue, null);
                    lbRetValue = (String.IsNullOrEmpty(loDB.sErr));
                    sError = loDB.sErr;
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                loDB.CreateEntity(option);
                lbRetValue = true;
                sError = "";
                loDB.closeEntity();
            }
            return lbRetValue;
        }

        /// <summary>
        /// Add a new PollOption record to the database
        /// </summary>
        /// <param name="option"></param>
        public static bool InsertPollOption(int SubjectID,
                                            string OptionName,
                                            out string sError)
        {
            bool lbRetValue=false;
            sError = "";
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                int RetValue = 0;
                string sql = "exec spSubmitPollOption " +
                             SubjectID + ", '" +
                             DoubleUpSingleQuotes(OptionName) + "'";
                System.Diagnostics.Trace.WriteLine(sql);

                try
                {
                    loDB.openConnection();
                    loDB.ExecDB(sql, out RetValue, null);
                    lbRetValue = (String.IsNullOrEmpty(loDB.sErr));
                    sError = loDB.sErr;
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                PollOptionInfo option = new PollOptionInfo();
                loDB.CreateEntity(option);
                lbRetValue = true;
                sError = "";
                loDB.closeEntity();
            }
            return lbRetValue;
        }

        /// <summary>
        /// Update a PollOption record
        /// </summary>
        /// <param name="option"></param>
        public static bool UpdatePollOption(PollOptionInfo option, 
                                           out string sError)
        {
            bool lbRetValue=false;
            sError = "";
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string sql = "exec spUpdatePollOption " +
                             option.SubjectID +
                             ", " + option.ID +
                             ", N'" + DoubleUpSingleQuotes(option.OptionName) + "'";
                System.Diagnostics.Trace.WriteLine(sql);

                try
                {
                    loDB.openConnection();
                    loDB.ExecDB(sql);
                    lbRetValue = (String.IsNullOrEmpty(loDB.sErr));
                    sError = loDB.sErr;
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            return lbRetValue;
        }

        /// <summary>
        /// Update a PollOption record
        /// </summary>
        /// <param name="option"></param>
        public static bool UpdatePollOption(int SubjectID,
                                            int ID,            // OptionID
                                            string OptionName,
                                            out string sError)
        {
            bool lbRetValue=false;
            sError = "";
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string sql = "exec spUpdatePollOption " +
                              SubjectID +
                             ", " + ID +
                             ", N'" + DoubleUpSingleQuotes(OptionName) + "'";
                System.Diagnostics.Trace.WriteLine(sql);

                try
                {
                    loDB.openConnection();
                    loDB.ExecDB(sql);
                    lbRetValue = (String.IsNullOrEmpty(loDB.sErr));
                    sError = loDB.sErr;
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            return lbRetValue;
        }



        /// <summary>
        /// Delete a PollOption record
        /// </summary>
        /// <param name="option"></param>
        public static bool DeletePollOption(int SubjectID,
                                            int ID,            //OptionID
                                            out string sError)
        {
            sError = "";
            bool lbRetValue=false;
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string sql = "exec spDeletePollOption " +
                             SubjectID +
                             ", " + ID;
                System.Diagnostics.Trace.WriteLine(sql);

                try
                {
                    loDB.openConnection();
                    loDB.ExecDB(sql);
                    lbRetValue = (String.IsNullOrEmpty(loDB.sErr));
                    sError = loDB.sErr;
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            return lbRetValue;
        }

        /// <summary>
        /// Replace the single quotes in a string with double quotes
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        static string DoubleUpSingleQuotes(string strInput)
        {
            if (!String.IsNullOrEmpty(strInput))
            {
                return strInput.Replace("'", "''");
            }
            else
            {
                return strInput;
            }
        }

    }
}

