using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using HRM.BO;

namespace HRM.DA
{
    #region SetupDetail Service

    public class SetupDetailService : ServiceTemplate
    {
        #region Private functions and declaration

        #endregion

        public SetupDetailService()
        {
        }

        private void MapObject(SetupDetail oSetupDetail, DataReader oReader)
        {
            base.SetObjectID(oSetupDetail, oReader.GetInt32("DetailID").Value);
            oSetupDetail.SetupID = oReader.GetString("SetupID") == null ? 0 : oReader.GetInt32("SetupID").Value;
            oSetupDetail.TranID = oReader.GetString("tranID") == null ? 0 : oReader.GetInt32("tranID").Value;
            oSetupDetail.TranType = (EnmSetupManagerTranType)oReader.GetInt32("tranType").Value;
            this.SetObjectState(oSetupDetail, Ease.Core.ObjectState.Saved);
        }

        protected override T CreateObject<T>(DataReader oReader)
        {
            SetupDetail oSetupDetail = new SetupDetail();
            MapObject(oSetupDetail, oReader);
            return oSetupDetail as T;
        }

        protected SetupDetail CreateObject(DataReader oReader)
        {
            SetupDetail oSetupDetail = new SetupDetail();
            MapObject(oSetupDetail, oReader);
            return oSetupDetail;
        }

        #region Service implementation

        public List<SetupDetail> GetParametersById(EnumParameterSetup setup, int termParameterId)
        {
            List<SetupDetail> setupDetails = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(SetupDetailDA.GetParametersById(tc, setup, termParameterId));
                setupDetails = this.CreateObjects<SetupDetail>(dr);
                dr.Close();

                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return setupDetails;
        }

        public List<SetupDetail> GetParameters(EnumParameterSetup setup)
        {
            List<SetupDetail> setupDetails = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(SetupDetailDA.GetParameters(tc, setup));
                setupDetails = this.CreateObjects<SetupDetail>(dr);
                dr.Close();

                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return setupDetails;
        }
        public DataSet GetParameters(EnumParameterSetup setup,
            List<string> sqlRelation, string InEmpSQL)
        {
            TransactionContext tc = null;
            DataSet odataset;
            try
            {
                tc = TransactionContext.Begin();
                EmployeeSetupParameter empsetup = new EmployeeSetupParameter(setup);

                odataset = SetupDetailDA.GetParameters(tc, setup, sqlRelation, InEmpSQL);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return odataset;
        }

        public List<SetupDetail> GetParameters(
            EnumParameterSetup setup, int setupID)
        {
            List<SetupDetail> setupDetails = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(SetupDetailDA.GetParameters(tc, setupID, setup));
                setupDetails = this.CreateObjects<SetupDetail>(dr);
                dr.Close();

                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }


            return setupDetails;
        }

        public List<SetupDetail> GetUsedParameters(
            EnumParameterSetup setup, int setupID, int parameterID)
        {
            List<SetupDetail> setupDetails = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(SetupDetailDA.GetUsedParameters(tc, setup, setupID, parameterID));
                setupDetails = this.CreateObjects<SetupDetail>(dr);
                dr.Close();

                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }


            return setupDetails;
        }

        public List<SetupDetail> GetUsedParameters(
            EnumParameterSetup setup, string setupIDs, int parameterID)
        {
            List<SetupDetail> setupDetails = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(SetupDetailDA.GetUsedParameters(tc, setup, setupIDs, parameterID));
                setupDetails = this.CreateObjects<SetupDetail>(dr);
                dr.Close();

                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }


            return setupDetails;
        }

        public List<EnmSetupManagerTranType> GetTypes(
            EnumParameterSetup setup)
        {
            List<EnmSetupManagerTranType> types = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataSet dataset = SetupDetailDA.GetTypes(tc, setup);
                types = new List<EnmSetupManagerTranType>();
                foreach (DataRow orow in dataset.Tables[0].Rows)
                {
                    types.Add((EnmSetupManagerTranType)Convert.ToInt32(orow[0]));
                }

                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }


            return types;
        }


        public List<int> GetParameterID(
            EnumParameterSetup setup, EnmSetupManagerTranType type, int TranID)
        {
            List<int> parameters = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataSet dataset = SetupDetailDA.GetParameters(tc, setup, type, TranID);
                parameters = new List<int>();
                foreach (DataRow orow in dataset.Tables[0].Rows)
                {
                    parameters.Add(Convert.ToInt32(orow[0]));
                }

                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }


            return parameters;
        }

        public List<SetupDetail> GetUsedParameters(
            EnumParameterSetup setup, string parameterIDs)
        {
            List<SetupDetail> setupDetails = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(SetupDetailDA.GetUsedParameters(tc, setup, parameterIDs));
                setupDetails = this.CreateObjects<SetupDetail>(dr);
                dr.Close();

                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }


            return setupDetails;
        }

        public List<SetupDetail> GetUsedParameters(
            EnumParameterSetup setup, int parameterID)
        {
            List<SetupDetail> setupDetails = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(SetupDetailDA.GetUsedParameters(tc, setup, parameterID));
                setupDetails = this.CreateObjects<SetupDetail>(dr);
                dr.Close();

                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }


            return setupDetails;
        }

        public void Save(TransactionContext tc, EnumParameterSetup setup,
            List<SetupDetail> Details, int SetupID)
        {
            try
            {
                this.Delete(tc, setup, SetupID);
                foreach (SetupDetail item in Details)
                {
                    item.SetupID = SetupID;
                    this.Save(tc, setup, item);
                }
            }
            catch (Exception Ex)
            {
                throw new ServiceException(Ex.Message);
            }
        }

        public int Save(TransactionContext tc, EnumParameterSetup setup, SetupDetail oSetupDetail)
        {
            try
            {
                //tc = TransactionContext.Begin(true);
                if (oSetupDetail.IsNew)
                {
                    int id = SetupDetailDA.GenerateID(tc, setup);
                    base.SetObjectID(oSetupDetail, id);

                    SetupDetailDA.Insert(tc, oSetupDetail, setup);
                }
                else
                {
                    SetupDetailDA.Update(tc, oSetupDetail, setup);
                }

                //   tc.End();
                return oSetupDetail.ID;
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);

                #endregion
            }
        }


        public void Delete(TransactionContext tc, EnumParameterSetup setup, int setupID)
        {
            try
            {
                SetupDetailDA.Delete(tc, setup, setupID);
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);

                #endregion
            }
        }

        public static int GetTranID(Employee employee, EnmSetupManagerTranType type)
        {
            int? nTranID = null;
            switch (type)
            {
                case EnmSetupManagerTranType.Category:
                    nTranID = employee.CategoryID;
                    break;
                case EnmSetupManagerTranType.Designation:
                    nTranID = employee.DesignationID;
                    break;
                case EnmSetupManagerTranType.Grade:
                    nTranID = employee.GradeID;
                    break;
                case EnmSetupManagerTranType.Location:
                    nTranID = employee.LocationID;
                    break;
            }
            return nTranID == null ? 0 : (int)nTranID;
        }

        #endregion
    }

    #endregion
}