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

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

        public static BoxDisplayList GetBoxDisplayList()
        {
            string lsError = string.Empty;
            return GetBoxDisplayList(out lsError);
        }

        public static BoxDisplayList GetBoxDisplayList(int piBoxID)
        {
            string lsError = string.Empty;
            return GetBoxDisplayList(piBoxID, out lsError);
        }

        public static BoxDisplayList GetBoxDisplayList(out string psError)
        {
            psError = string.Empty;
            BoxDisplayList loBoxDisplayList = new BoxDisplayList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                BoxDisplay loBoxDisplay;
                DataTable loBoxes;
                string lsSQL = "EXEC spGetBoxDisplayList";

                try
                {
                    loDB.openConnection();
                    loBoxes = loDB.QueryDBDataset(lsSQL);
                    foreach (DataRow loDR in loBoxes.Rows)
                    {
                        loBoxDisplay = new BoxDisplay();
                        loBoxDisplay.ID = Convert.ToInt32(loDR["box_data_id"]);
                        if (loDR["box_id"] == DBNull.Value)
                        {
                            loBoxDisplay.BoxID = -1;
                        }
                        else
                        {
                            loBoxDisplay.BoxID = Convert.ToInt32(loDR["box_id"]);
                        }
                        loBoxDisplay.BoxData = Convert.ToString(loDR["box_data_content"]);
                        loBoxDisplay.Name = Convert.ToString(loDR["box_name"]);
                        loBoxDisplayList.Add(loBoxDisplay);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
                psError = loDB.sErr;
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                IEnumerable loObjs = loDB.GetEntity("BoxDisplay");                
                foreach (object obj in loObjs)
                {
                    loBoxDisplayList.Add((BoxDisplay)obj);
                }
                loDB.closeEntity();
            }
            return loBoxDisplayList;
        }

        /// <summary>
        /// Retrieve only the BoxDisplay that are associated with a specific box.
        /// </summary>
        /// <param name="psError"></param>
        /// <returns></returns>
        public static BoxDisplayList GetBoxDisplayList(int piBoxID, out string psError)
        {
            psError = string.Empty;
            BoxDisplayList loBoxDisplayList = new BoxDisplayList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                BoxDisplay loBoxDisplay;
                DataTable loBoxes;
                string lsSQL = "EXEC spGetBoxDisplayList @box_id = " + piBoxID.ToString() + ";";

                try
                {
                    loDB.openConnection();
                    loBoxes = loDB.QueryDBDataset(lsSQL);
                    foreach (DataRow loDR in loBoxes.Rows)
                    {
                        loBoxDisplay = new BoxDisplay();
                        loBoxDisplay.ID = Convert.ToInt32(loDR["box_data_id"]);
                        if (loDR["box_id"] == DBNull.Value)
                        {
                            loBoxDisplay.BoxID = -1;
                        }
                        else
                        {
                            loBoxDisplay.BoxID = Convert.ToInt32(loDR["box_id"]);
                        }
                        loBoxDisplay.BoxData = Convert.ToString(loDR["box_data_content"]);
                        loBoxDisplay.Name = Convert.ToString(loDR["box_name"]);
                        loBoxDisplayList.Add(loBoxDisplay);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
                psError = loDB.sErr;
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                IEnumerable loObjs = loDB.GetEntity("BoxDisplay");
                foreach (object obj in loObjs)
                {
                    loBoxDisplayList.Add((BoxDisplay)obj);
                }                
            }
            return loBoxDisplayList;
        }


        public static BoxDisplay GetBoxDisplay(int piBoxDisplayID)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            BoxDisplay loBoxDisplay = null;

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = String.Empty;
                lsSQL = "EXEC spGetBoxDisplay @ID = " + piBoxDisplayID + ";";

                try
                {
                    loDB.openConnection();
                    DataTable loTable = loDB.QueryDBDataset(lsSQL);
                    if (loTable.Rows.Count > 0)
                    {
                        loBoxDisplay = new BoxDisplay();
                        DataRow loDR = loTable.Rows[0];
                        loBoxDisplay.ID = Convert.ToInt32(loDR["box_data_id"]);
                        if (loDR["box_id"] == DBNull.Value)
                        {
                            loBoxDisplay.BoxID = -1;
                        }
                        else
                        {
                            loBoxDisplay.BoxID = (int)loDR["box_id"];
                        }
                        loBoxDisplay.BoxData = (Convert.ToString(loDR["box_data_content"]));
                        loBoxDisplay.Name = Convert.ToString(loDR["box_name"]);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            else
            {
                digioz.XML xml = new XML(coPortalSettings);
                loBoxDisplay = (BoxDisplay)xml.GetEntity(piBoxDisplayID);
            }
            return loBoxDisplay;
        }


        public static bool SaveBoxDisplay(BoxDisplay poBoxDisplay, string psMode)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            bool lbResult = false;

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = String.Empty;
                int liCount = 0;

                SqlParameter[] loParamaters = null;
                
                if (psMode == "New")
                {
                    loParamaters = new SqlParameter[3];
                    loParamaters[0] = new SqlParameter("@box_id", poBoxDisplay.BoxID);
                    loParamaters[1] = new SqlParameter("@box_data", poBoxDisplay.BoxData);
                    loParamaters[2] = new SqlParameter("@box_name", poBoxDisplay.Name);
                    lsSQL = "spInsertBoxDisplay";
                }
                else if (psMode == "Update")
                {
                    loParamaters = new SqlParameter[4];
                    loParamaters[0] = new SqlParameter("@box_id", poBoxDisplay.BoxID);
                    loParamaters[1] = new SqlParameter("@box_data", poBoxDisplay.BoxData);
                    loParamaters[2] = new SqlParameter("@box_name", poBoxDisplay.Name);
                    loParamaters[3] = new SqlParameter("@id", poBoxDisplay.ID);
                    lsSQL = "spUpdateBoxDisplay";
                }

                try
                {
                    loDB.openConnection();
                    loDB.ExecDBProc(lsSQL, out liCount, loParamaters);
                    lbResult = true;
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            else
            {
                digioz.XML xml = new XML(coPortalSettings);
                xml.SaveEntity(poBoxDisplay);
            }
            return lbResult;
        }

        /// <summary>
        /// Delete a BoxDisplay record from the database
        /// </summary>
        /// <param name="piBoxDisplayID"></param>
        /// <returns></returns>
        public static bool DeleteBoxDisplay(int piBoxDisplayID)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            bool lbResult = false;

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = String.Empty;

                lsSQL = "EXEC spDeleteBoxDisplay @id = " + piBoxDisplayID + ";";

                try
                {
                    loDB.openConnection();
                    loDB.ExecDB(lsSQL);
                    lbResult = true;
                }
                finally
                {
                    loDB.closeConnection();
                }

            }
            else
            {
                digioz.XML xml = new XML(coPortalSettings);
                BoxDisplay loBoxDisplay = new BoxDisplay();
                loBoxDisplay.ID = piBoxDisplayID;
                xml.DeleteEntity(loBoxDisplay);
            }
            return lbResult;
        }
    }
}


