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

        public static MenuItem GetMenuItemFromMenuName(string psMenuName)
        {
            MenuItem loMenuItem = new MenuItem();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                DataTable loMenu;

                string lsSQL = "SELECT * FROM tblMenu WHERE menu_name='" + psMenuName + "';";

                loDB.openConnection();
                loMenu = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();
                DataRow loDR = loMenu.Rows[0];

                loMenuItem.ID = (int)loDR["menu_id"];
                loMenuItem.BoxID = (int)loDR["menu_box_id"];
                loMenuItem.Name = (string)loDR["menu_name"];
                loMenuItem.ActionType = (string)loDR["menu_action_type"];
                loMenuItem.ActionData = (string)loDR["menu_action_data"];
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                IEnumerable loObjs = loDB.GetEntity("MenuItem");                
                foreach (object obj in loObjs)
                {
                    MenuItem loMenu = (MenuItem)obj;
                    if (loMenu.Name == psMenuName)
                    {
                        loMenuItem = loMenu;
                        break;
                    }
                }
                loDB.closeEntity();
            }
            return loMenuItem;
        }

        public static MenuItem GetMenuItemFromPageID(int piPageID)
        {
            MenuItem loMenuItem = new MenuItem();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                DataTable loMenu;

                string lsSQL = "EXEC spSelectMenuItem " + piPageID + ";";

                loDB.openConnection();
                loMenu = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();
                DataRow loDR = loMenu.Rows[0];

                loMenuItem.ID = (int)loDR["menu_id"];
                loMenuItem.BoxID = (int)loDR["menu_box_id"];
                loMenuItem.Name = (string)loDR["menu_name"];
                loMenuItem.ActionType = (string)loDR["menu_action_type"];
                loMenuItem.ActionData = (string)loDR["menu_action_data"];
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                loMenuItem = (MenuItem)loDB.GetEntity("MenuItem", piPageID);
                loDB.closeEntity();
            }
            return loMenuItem;
        }

        public static MenuItemList GetMenuItemList(int piBoxID)
        {
            MenuItemList loMenuItemList = new MenuItemList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                MenuItem loMenuItem;
                DataTable loMenus;

                string lsSQL;
                if (piBoxID >= 0)
                {
                    lsSQL = "SELECT * FROM tblMenu WHERE menu_box_id=" + piBoxID.ToString() + ";";
                }
                else
                {
                    lsSQL = "SELECT * FROM tblMenu;";
                }

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

                foreach (DataRow loDR in loMenus.Rows)
                {
                    loMenuItem = new MenuItem();
                    loMenuItem.ID = (int)loDR["menu_id"];
                    loMenuItem.BoxID = (int)loDR["menu_box_id"];
                    loMenuItem.Name = (string)loDR["menu_name"];
                    loMenuItem.ActionType = (string)loDR["menu_action_type"];
                    loMenuItem.ActionData = (string)loDR["menu_action_data"];

                    loMenuItemList.Add(loMenuItem);
                }
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                IEnumerable loObjs = loDB.GetEntity("MenuItem");
                foreach (object obj in loObjs)
                {
                    MenuItem loMenu = (MenuItem)obj;
                    if (loMenu.BoxID == piBoxID)
                    {
                        loMenuItemList.Add(loMenu);
                    }
                }
                loDB.closeEntity();
            }
            return loMenuItemList;
        }

        /// <summary>
        /// Overload that takes an action type as paramater
        /// </summary>
        /// <param name="piBoxID"></param>
        /// <returns></returns>
        public static MenuItemList GetMenuItemList(int piBoxID, string psActionType)
        {
            MenuItemList loMenuItemList = new MenuItemList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                MenuItem loMenuItem;
                DataTable loMenus;

                SqlParameter[] loParams;

                string lsSQL = "spGetMenuItemList";
                int liCount = 0;
                if (piBoxID >= 0)
                {
                    loParams = new SqlParameter[2];
                    loParams[0] = new SqlParameter("@BoxId", SqlDbType.Int);
                    loParams[1] = new SqlParameter("@ActionType", SqlDbType.NVarChar);
                    loParams[0].Value = piBoxID;
                    loParams[1].Value = psActionType;
                }
                else
                {
                    loParams = new SqlParameter[1];
                    loParams[0] = new SqlParameter("@ActionType", SqlDbType.NVarChar);
                    loParams[0].Value = psActionType;
                }

                try
                {
                    loDB.openConnection();
                    loMenus = loDB.QueryDBDatasetProc(lsSQL, out liCount, loParams);

                    foreach (DataRow loDR in loMenus.Rows)
                    {
                        loMenuItem = new MenuItem();
                        loMenuItem.ID = (int)loDR["menu_id"];
                        loMenuItem.BoxID = (int)loDR["menu_box_id"];
                        loMenuItem.Name = (string)loDR["menu_name"];
                        loMenuItem.ActionType = (string)loDR["menu_action_type"];
                        loMenuItem.ActionData = (string)loDR["menu_action_data"];

                        loMenuItemList.Add(loMenuItem);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                IEnumerable loObjs = loDB.GetEntity("MenuItem");
                foreach (object obj in loObjs)
                {
                    MenuItem loMenu = (MenuItem)obj;
                    if (loMenu.BoxID == piBoxID && loMenu.ActionType == psActionType)
                    {
                        loMenuItemList.Add(loMenu);
                    }
                }
                loDB.closeEntity();
            }
            return loMenuItemList;
        }


        public static bool SaveMenuItem(MenuItem poMenuItem, 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 spInsertMenu "
                            + "@mnu_name = '" + poMenuItem.Name + "',"
                            + "@mnu_action_type = '" + poMenuItem.ActionType + "',"
                            + "@mnu_action_data = '" + poMenuItem.ActionData + "',"
                            + "@mnu_box_id = " + poMenuItem.BoxID + ";";

                    loDB.openConnection();
                    loDB.ExecDB(lsSQL);
                    loDB.closeConnection();
                    lbResult = String.IsNullOrEmpty(loDB.sErr);  //lbResult = true;
                }
                else if (psMode == "Update")
                {
                    lsSQL = "EXEC spUpdateMenuItem "
                            + "@menu_id = " + poMenuItem.ID + ","
                            + "@menu_name = '" + poMenuItem.Name + "',"
                            + "@menu_action_type = '" + poMenuItem.ActionType + "',"
                            + "@menu_action_data = '" + poMenuItem.ActionData + "',"
                            + "@menu_box_id = " + poMenuItem.BoxID + ";";

                    loDB.openConnection();
                    loDB.ExecDB(lsSQL);
                    loDB.closeConnection();
                    lbResult = String.IsNullOrEmpty(loDB.sErr); //lbResult = true;
                }
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                loDB.SaveEntity(poMenuItem);
                loDB.closeEntity();
            }
            return lbResult;
        }

        public static bool DeleteMenuItem(MenuItem poMenuItem)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            bool lbResult = false;

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

                loDB.openConnection();
                loDB.ExecDB(lsSQL);
                loDB.closeConnection();
                lbResult = String.IsNullOrEmpty(loDB.sErr);  //lbResult = true;                                
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                loDB.DeleteEntity(poMenuItem);
                loDB.closeEntity();
            }
            return lbResult;
        }
    }
}


