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 BoxItemManagement
    {
        private static PortalSettings coPortalSettings;

        public static BoxItemList GetBoxItemList()
        {
            BoxItemList loBoxItemList = new BoxItemList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                BoxItem loBoxItem;
                DataTable loBoxes;

                string lsSQL = "SELECT * FROM tblBoxes order by box_location, box_order_default;";

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

                foreach (DataRow loDR in loBoxes.Rows)
                {
                    loBoxItem = new BoxItem();
                    loBoxItem.ID = (int)loDR["box_id"];
                    loBoxItem.Order = (int)loDR["box_order_default"];
                    loBoxItem.Location = (string)loDR["box_location"];
                    loBoxItem.Name = (string)loDR["box_name"];
                    loBoxItem.ActionType = (string)loDR["box_action_type"];
                    loBoxItem.ActionData = (string)loDR["box_action_data"];
                    loBoxItem.Permission = (int)loDR["permission"];
                    loBoxItemList.Add(loBoxItem);
                }
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                IEnumerable loObjs = loDB.GetEntity("BoxItem");                
                foreach (object obj in loObjs)
                {
                    loBoxItemList.Add((BoxItem)obj);
                }
                loDB.closeEntity();
            }
            return loBoxItemList;
        }

        public static BoxItemList GetBoxItemListByLocation(string psLocation)
        {
            digioz.BoxItemList loBoxItemList = digioz.BoxItemManagement.GetBoxItemList();
            digioz.BoxItemList loBoxItemListReturned = new BoxItemList();
            foreach (digioz.BoxItem loBoxItem in loBoxItemList)
            {
                if (loBoxItem.Location == psLocation)
                {
                    loBoxItemListReturned.Add(loBoxItem);
                }
            }
            return loBoxItemListReturned;
        }


        public static BoxItemList GetBoxItemListByType(string boxActionType, 
                                                       out string psError)
        {
            psError = string.Empty;
            BoxItemList loBoxItemList = new BoxItemList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                BoxItem loBoxItem;
                DataTable loBoxes;
                string lsSQL = "spGetBoxItemListByType";
                int liCount = 0;
                SqlParameter[] loParamaters = new SqlParameter[1];
                loParamaters[0] = new SqlParameter();
                loParamaters[0].ParameterName = "@BoxActionType";
                loParamaters[0].DbType = DbType.String;
                loParamaters[0].Value = boxActionType;

                try
                {
                    loDB.openConnection();
                    loBoxes = loDB.QueryDBDatasetProc(lsSQL, out liCount, loParamaters);
                    foreach (DataRow loDR in loBoxes.Rows)
                    {
                        loBoxItem = new BoxItem();
                        loBoxItem.ID = (int)loDR["box_id"];
                        loBoxItem.Order = (int)loDR["box_order_default"];
                        loBoxItem.Location = (string)loDR["box_location"];
                        loBoxItem.Name = (string)loDR["box_name"];
                        loBoxItem.ActionType = (string)loDR["box_action_type"];
                        loBoxItem.ActionData = (string)loDR["box_action_data"];

                        loBoxItemList.Add(loBoxItem);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
                psError = loDB.sErr;
            }
            else
            {
                digioz.BoxItemList loAllBoxItems = digioz.BoxItemManagement.GetBoxItemList();                
                foreach (digioz.BoxItem loBoxItem in loAllBoxItems)
                {
                    if (loBoxItem.ActionType == boxActionType)
                    {
                        loBoxItemList.Add(loBoxItem);
                    }
                }
            }
            return loBoxItemList;
        }

        public static bool SaveBoxItem(BoxItem poBoxItem, string psMode)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            bool lbResult = false;

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

                if (psMode == "New")
                {
                    lsSQL = "EXEC spInsertBoxes "
                            + "@bx_name = '" + poBoxItem.Name + "',"
                            + "@bx_loc = '" + poBoxItem.Location + "',"
                            + "@bx_ord = '" + poBoxItem.Order + "',"
                            + "@bx_act_typ = '" + poBoxItem.ActionType + "',"
                            + "@bx_act_dta = '" + poBoxItem.ActionData + "',"
                            + "@permission = '" + poBoxItem.Permission + "';";

                    loDB.openConnection();
                    loDB.ExecDB(lsSQL);
                    loDB.closeConnection();
                    lbResult = String.IsNullOrEmpty(loDB.sErr);   //lbResult = true;
                }
                else if (psMode == "Update")
                {
                    lsSQL = "EXEC spUpdateBoxItem "
                            + "@box_id = " + poBoxItem.ID + ","
                            + "@box_name = '" + poBoxItem.Name + "',"
                            + "@box_location = '" + poBoxItem.Location + "',"
                            + "@box_order_default = '" + poBoxItem.Order + "',"
                            + "@box_action_type = '" + poBoxItem.ActionType + "',"
                            + "@box_action_data = '" + poBoxItem.ActionData + "',"
                            + "@permission = '" + poBoxItem.Permission + "';";

                    loDB.openConnection();
                    loDB.ExecDB(lsSQL);
                    loDB.closeConnection();
                    lbResult = String.IsNullOrEmpty(loDB.sErr);   //lbResult = true;
                }
            }
            else
            {
                digioz.XML xml = new XML(coPortalSettings);
                xml.SaveEntity(poBoxItem);
            }
            return lbResult;
        }

        public static bool DeleteBoxItem(BoxItem poBoxItem)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            bool lbResult = false;

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

                lsSQL = "EXEC spDeleteBoxItem @box_id = " + poBoxItem.ID + ";";

                loDB.openConnection();
                loDB.ExecDB(lsSQL);
                loDB.closeConnection();
                lbResult = String.IsNullOrEmpty(loDB.sErr);  //lbResult = true;
            }
            else
            {
                digioz.XML xml = new XML(coPortalSettings);
                xml.DeleteEntity(poBoxItem);
            }
            return lbResult;
        }
    }
}


