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

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

        public static List<TopMenu> GetTopMenuItems(int piPermissionID)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            List<TopMenu> coResultList = new List<TopMenu>();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                DataTable loMenuItems;
                string lsSQL = "SELECT * FROM tblTopMenu where topmenu_permission_id<=@id";
                DbParameter[] loParameters = new DbParameter[1];
                loParameters[0] = new SqlParameter("@id", SqlDbType.Int);
                loParameters[0].Value = piPermissionID;

                try
                {
                    loDB.openConnection();
                    loMenuItems = loDB.QueryDBDataset(lsSQL, loParameters);

                    foreach (DataRow loDR in loMenuItems.Rows)
                    {
                        coResultList.Add(new TopMenu(
                            Convert.ToInt32(loDR[0]),
                            Convert.ToInt32(loDR[1]),
                            Convert.ToString(loDR[2]),
                            Convert.ToString(loDR[3]), 
                            Convert.ToString(loDR[4]),
                            Convert.ToInt32(loDR[5])));
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }

            return coResultList;
        }

        public static bool SaveTopMenuItem(int piID, int piParentID, string psName, string psActionType,
            string psActionData, int piPermissionID, bool pbIsUpdating)
        {
            if (String.IsNullOrEmpty(psName) || String.IsNullOrEmpty(psActionData))
            {
                return false;
            }

            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            bool lbResult = false;

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

                if (pbIsUpdating)
                {
                    loParameters = new DbParameter[6];
                    loParameters[0] = new SqlParameter("@topmenu_id", SqlDbType.Int);
                    loParameters[0].Value = piID;
                    loParameters[1] = new SqlParameter("@topmenu_parent_id", SqlDbType.Int);
                    loParameters[1].Value = piParentID;
                    loParameters[2] = new SqlParameter("@topmenu_name", SqlDbType.VarChar);
                    loParameters[2].Value = psName;
                    loParameters[3] = new SqlParameter("@topmenu_action_type", SqlDbType.VarChar);
                    loParameters[3].Value = psActionType;
                    loParameters[4] = new SqlParameter("@topmenu_action_data", SqlDbType.VarChar);
                    loParameters[4].Value =psActionData;
                    loParameters[5] = new SqlParameter("@topmenu_permission_id", SqlDbType.Int);
                    loParameters[5].Value = piPermissionID;
                    lsSQL = "spUpdateTopMenuItem";
                }
                else
                {
                    loParameters = new DbParameter[5];
                    loParameters[0] = new SqlParameter("@topmenu_parent_id", SqlDbType.Int);
                    loParameters[0].Value = piParentID;
                    loParameters[1] = new SqlParameter("@topmenu_name", SqlDbType.VarChar);
                    loParameters[1].Value = psName;
                    loParameters[2] = new SqlParameter("@topmenu_action_type", SqlDbType.VarChar);
                    loParameters[2].Value = psActionType;
                    loParameters[3] = new SqlParameter("@topmenu_action_data", SqlDbType.VarChar);
                    loParameters[3].Value = psActionData;
                    loParameters[4] = new SqlParameter("@topmenu_permission_id", SqlDbType.Int);
                    loParameters[4].Value = piPermissionID;
                    lsSQL = "spInsertTopMenuItem";
                }

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

            return lbResult;
        }

        public static bool DeleteTopMenuItem(int piTopMenuItemID)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            string lsSql = "spDeleteTopMenuItem";
            int liCount = 0;
            bool lbResult = false;
            DbParameter[] loParameters = new DbParameter[1];

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                loParameters[0] = new SqlParameter("@id", SqlDbType.Int);
                loParameters[0].Value = piTopMenuItemID;

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

            return lbResult;
        }
    }
}