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

namespace HRM.DA
{
    public class EmployeeCostCenterService : ServiceTemplate, IEmployeeCostCenterService
    {
        public EmployeeCostCenterService()
        {
        }

        private void MapObject(EmployeeCostCenter oEmployeeCostCenter, DataReader oReader)
        {
            base.SetObjectID(oEmployeeCostCenter, oReader.GetInt32("ID").Value);
            oEmployeeCostCenter.EmployeeID = oReader.GetInt32("EmployeeID", 0);
            oEmployeeCostCenter.MonthDate = oReader.GetDateTime("monthDate").Value;
            oEmployeeCostCenter.CostCenterID = oReader.GetInt32("costCenterID", 0);
            oEmployeeCostCenter.Percentage = oReader.GetDouble("percentage").Value;
            oEmployeeCostCenter.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oEmployeeCostCenter.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            oEmployeeCostCenter.IsCurrentCC = oReader.GetBoolean("CurrentCC").Value;
            oEmployeeCostCenter.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oEmployeeCostCenter.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oEmployeeCostCenter, Ease.Core.ObjectState.Saved);
        }

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

        #region Service implementation

        public EmployeeCostCenter Get(int id)
        {
            EmployeeCostCenter oEmployeeCostCenter = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(EmployeeCostCenterDA.Get(tc, id));
                if (oreader.Read())
                {
                    oEmployeeCostCenter = this.CreateObject<EmployeeCostCenter>(oreader);
                }

                oreader.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 oEmployeeCostCenter;
        }

        public List<EmployeeCostCenter> GetByEmpID(int employeeID)
        {
            List<EmployeeCostCenter> employeeCostCenters = new List<EmployeeCostCenter>();

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

                DataReader dr = new DataReader(EmployeeCostCenterDA.Get(tc, employeeID));
                employeeCostCenters = this.CreateObjects<EmployeeCostCenter>(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 employeeCostCenters;
        }

        public List<EmployeeCostCenter> GetByMonthStartEnd(DateTime strt, DateTime end, int payrollTypeID)
        {
            List<EmployeeCostCenter> employeeCostCenters = new List<EmployeeCostCenter>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(EmployeeCostCenterDA.GetByMonthStartEnd(tc, strt, end, payrollTypeID));
                employeeCostCenters = this.CreateObjects<EmployeeCostCenter>(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 employeeCostCenters;
        }

        public List<EmployeeCostCenter> GetByCCID(int CCID)
        {
            List<EmployeeCostCenter> employeeCostCenters = new List<EmployeeCostCenter>();

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

                DataReader dr = new DataReader(EmployeeCostCenterDA.GetByCCID(tc, CCID));
                employeeCostCenters = this.CreateObjects<EmployeeCostCenter>(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 employeeCostCenters;
        }

        public List<EmployeeCostCenter> GetByEmpIDCCID(int nCCID, int nEmpID)
        {
            List<EmployeeCostCenter> employeeCostCenters = new List<EmployeeCostCenter>();

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

                DataReader dr = new DataReader(EmployeeCostCenterDA.GetByEmpIDCCID(tc, nCCID, nEmpID));
                employeeCostCenters = this.CreateObjects<EmployeeCostCenter>(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 employeeCostCenters;
        }

        public List<EmployeeCostCenter> Get()
        {
            List<EmployeeCostCenter> employeeCostCenters = new List<EmployeeCostCenter>();

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

                DataReader dr = new DataReader(EmployeeCostCenterDA.Get(tc));
                employeeCostCenters = this.CreateObjects<EmployeeCostCenter>(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 employeeCostCenters;
        }

        //public List<EmployeeCostCenter> Get(EnumStatus status, int payrolltypeid)
        //{
        //    List<EmployeeCostCenter> employeeCostCenters = new List<EmployeeCostCenter>();

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

        //        DataReader dr = new DataReader(EmployeeCostCenterDA.Get(tc));
        //        employeeCostCenters = this.CreateObjects<EmployeeCostCenter>(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 employeeCostCenters;
        //}

        public DataSet GetEmpCC(DateTime dMonthDate, string sEmpID)
        {
            DataSet oCC = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                oCC = EmployeeCostCenterDA.GetEmpCC(tc, dMonthDate, sEmpID);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return oCC;
        }

        public DataSet GetEmpCCDetails(DateTime dMonthDate, string nEmpID)
        {
            DataSet oCC = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                oCC = EmployeeCostCenterDA.GetEmpCCDetails(tc, dMonthDate, nEmpID);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return oCC;
        }

        public DateTime GetMaxDate(string EmpID, DateTime dMonthDate)
        {
            DateTime oCC = new DateTime();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                oCC = EmployeeCostCenterDA.GetMaxDate(tc, EmpID, dMonthDate);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return oCC;
        }

        public void Save(List<EmployeeCostCenter> _empCostCenters)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (_empCostCenters.Count == 0) throw new ServiceException("Empty collection is not allowed");
                foreach (EmployeeCostCenter empCostCenter in _empCostCenters)
                {
                    EmployeeCostCenterDA.DeleteByMonth(tc, empCostCenter.MonthDate, empCostCenter.EmployeeID);
                    EmployeeCostCenterDA.UpdateCurrentCCbeforeMonth(tc, empCostCenter.EmployeeID, false,
                        empCostCenter.MonthDate);
                }

                foreach (EmployeeCostCenter empCostCenter in _empCostCenters)
                {
                    this.SetObjectState(empCostCenter, ObjectState.New);
                    if (empCostCenter.IsNew)
                    {
                        int id = tc.GenerateID("EMPCOSTCENTER", "ID");
                        base.SetObjectID(empCostCenter, id);
                        EmployeeCostCenterDA.Insert(tc, empCostCenter);
                    }
                    else
                    {
                        EmployeeCostCenterDA.Update(tc, empCostCenter);
                    }
                }

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

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

                #endregion
            }
        }

        public void Save(TransactionContext tc, List<EmployeeCostCenter> _empCostCenters)
        {
            try
            {
                if (_empCostCenters.Count == 0) throw new ServiceException("Empty collection is not allowed");
                foreach (EmployeeCostCenter empCostCenter in _empCostCenters)
                {
                    EmployeeCostCenterDA.DeleteByMonth(tc, empCostCenter.MonthDate, empCostCenter.EmployeeID);
                    EmployeeCostCenterDA.UpdateCurrentCCbeforeMonth(tc, empCostCenter.EmployeeID, false,
                        empCostCenter.MonthDate);
                }

                foreach (EmployeeCostCenter empCostCenter in _empCostCenters)
                {
                    this.SetObjectState(empCostCenter, ObjectState.New);
                    if (empCostCenter.IsNew)
                    {
                        int id = tc.GenerateID("EMPCOSTCENTER", "ID");
                        base.SetObjectID(empCostCenter, id);
                        EmployeeCostCenterDA.Insert(tc, empCostCenter);
                    }
                    else
                    {
                        EmployeeCostCenterDA.Update(tc, empCostCenter);
                    }
                }
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public int Save(EmployeeCostCenter oEmployeeCostCenter)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oEmployeeCostCenter.IsNew)
                {
                    int id = tc.GenerateID("EMPCOSTCENTER", "ID");
                    base.SetObjectID(oEmployeeCostCenter, id);
                    EmployeeCostCenterDA.Insert(tc, oEmployeeCostCenter);
                }
                else
                {
                    EmployeeCostCenterDA.Update(tc, oEmployeeCostCenter);
                }

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

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

                #endregion
            }
        }

        public int Save(TransactionContext tc, EmployeeCostCenter oEmployeeCostCenter)
        {
            try
            {
                if (oEmployeeCostCenter.IsNew)
                {
                    int id = tc.GenerateID("EMPCOSTCENTER", "ID");
                    base.SetObjectID(oEmployeeCostCenter, id);
                    EmployeeCostCenterDA.Insert(tc, oEmployeeCostCenter);
                }
                else
                {
                    EmployeeCostCenterDA.Update(tc, oEmployeeCostCenter);
                }

                return oEmployeeCostCenter.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(int id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                EmployeeCostCenterDA.Delete(tc, id);
                tc.End();
            }
            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, int id)
        {
            try
            {
                EmployeeCostCenterDA.Delete(tc, id);
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<EmployeeCostCenter> Get(List<EmployeeCostCenter> empCRGInvolvements, int employeeID)
        {
            List<EmployeeCostCenter> empCRGs = new List<EmployeeCostCenter>();
            foreach (EmployeeCostCenter crg in empCRGInvolvements)
            {
                if (crg.EmployeeID == employeeID)
                {
                    empCRGs.Add(crg);
                }
            }

            return empCRGs;
        }

        #endregion
    }
}