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

namespace HRM.DA
{
    #region EmployeeWorkPlanSetup Service

    [Serializable]
    public class EmployeeWorkPlanSetupService : ServiceTemplate, IEmployeeWorkPlanSetupService
    {
        #region Object Mapping

        private void MapObject(EmployeeWorkPlanSetup oEmployeeWorkPlanSetup, DataReader oReader)
        {
            base.SetObjectID(oEmployeeWorkPlanSetup, oReader.GetInt32("EmployeeWorkPlanSetupID").Value);
            oEmployeeWorkPlanSetup.EmployeeID = oReader.GetInt32("EmployeeID", 0);
            oEmployeeWorkPlanSetup.StartDate = oReader.GetDateTime("StartDate").Value;
            oEmployeeWorkPlanSetup.WorkPlanGroupID = oReader.GetInt32("WorkPlanGroupID", 0);
            oEmployeeWorkPlanSetup.EmployeeNameView = oReader.GetString("Name", true, "");
            oEmployeeWorkPlanSetup.EmployeeNoView = oReader.GetString("employeeNO", true, "");
            oEmployeeWorkPlanSetup.WorkPlanGroupNameView = oReader.GetString("workPlanGroupName", true, "");

 

            //object ovalue = oReader.GetInt32("WeekEndOn");
            //if (ovalue != null) oEmployeeWorkPlanSetup.WeekEndOn = (DayOfWeek)ovalue;

            if (oReader.GetInt32("WeekEndOn").HasValue)
                oEmployeeWorkPlanSetup.WeekEndOn = (DayOfWeek)oReader.GetInt32("WeekEndOn").Value;

            if (oReader.GetInt32("WeekEndOn2").HasValue)
                oEmployeeWorkPlanSetup.WeekEndOn2 = (DayOfWeek)oReader.GetInt32("WeekEndOn2").Value;

            oEmployeeWorkPlanSetup.SaturdayShiftID = oReader.GetInt32("SaturdayShift", true, 0) == 0 ? null : oReader.GetInt32("SaturdayShift").Value;
            oEmployeeWorkPlanSetup.SundayShiftID = oReader.GetInt32("SundayShift", true, 0) == 0 ? null : oReader.GetInt32("SundayShift").Value;
            oEmployeeWorkPlanSetup.MondayShiftID = oReader.GetInt32("MondayShift", true, 0) == 0 ? null : oReader.GetInt32("MondayShift").Value;
            oEmployeeWorkPlanSetup.TuesdayShiftID = oReader.GetInt32("TuesdayShift", true, 0) == 0 ? null : oReader.GetInt32("TuesdayShift").Value;
            oEmployeeWorkPlanSetup.WednesdayShiftID = oReader.GetInt32("WednesdayShift", true, 0) == 0 ? null : oReader.GetInt32("WednesdayShift").Value;
            oEmployeeWorkPlanSetup.ThursdayShiftID = oReader.GetInt32("ThursdayShift", true, 0) == 0 ? null : oReader.GetInt32("ThursdayShift").Value;
            oEmployeeWorkPlanSetup.FridayShiftID = oReader.GetInt32("FridayShift", true, 0) == 0 ? null : oReader.GetInt32("FridayShift").Value;


            //if (oReader.GetInt32("SaturdayShift").HasValue)
            //    oEmployeeWorkPlanSetup.SaturdayShiftID = oReader.GetInt32("SaturdayShift").Value;

            //if (oReader.GetInt32("SundayShift").HasValue)
            //    oEmployeeWorkPlanSetup.SundayShiftID = oReader.GetInt32("SundayShift").Value;

            //if (oReader.GetInt32("MondayShift").HasValue)
            //    oEmployeeWorkPlanSetup.MondayShiftID = oReader.GetInt32("MondayShift").Value;

            //if (oReader.GetInt32("TuesdayShift").HasValue)
            //    oEmployeeWorkPlanSetup.TuesdayShiftID = oReader.GetInt32("TuesdayShift").Value;

            //if (oReader.GetInt32("WednesdayShift").HasValue)
            //    oEmployeeWorkPlanSetup.WednesdayShiftID = oReader.GetInt32("WednesdayShift").Value;

            //if (oReader.GetInt32("ThursdayShift").HasValue)
            //    oEmployeeWorkPlanSetup.ThursdayShiftID = oReader.GetInt32("ThursdayShift").Value;

            //if (oReader.GetInt32("FridayShift").HasValue)
            //    oEmployeeWorkPlanSetup.FridayShiftID = oReader.GetInt32("FridayShift").Value;

            oEmployeeWorkPlanSetup.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oEmployeeWorkPlanSetup.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
            oEmployeeWorkPlanSetup.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oEmployeeWorkPlanSetup.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oEmployeeWorkPlanSetup, Ease.Core.ObjectState.Saved);
        }

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

        protected EmployeeWorkPlanSetup CreateObject(DataReader oReader)
        {
            EmployeeWorkPlanSetup oEmployeeWorkPlanSetup = new EmployeeWorkPlanSetup();
            MapObject(oEmployeeWorkPlanSetup, oReader);
            return oEmployeeWorkPlanSetup;
        }

        #endregion

        #region Service implementation

        public EmployeeWorkPlanSetup Get(int id)
        {
            EmployeeWorkPlanSetup oEmployeeWorkPlanSetup = new EmployeeWorkPlanSetup();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(EmployeeWorkPlanSetupDA.Get(tc, id));
                if (oreader.Read())
                {
                    oEmployeeWorkPlanSetup = this.CreateObject<EmployeeWorkPlanSetup>(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 oEmployeeWorkPlanSetup;
        }

        public List<EmployeeWorkPlanSetup> GetAll()
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(EmployeeWorkPlanSetupDA.GetAll(tc));
                var employeeWorkPlanSetups = this.CreateObjects<EmployeeWorkPlanSetup>(dr);
                dr.Close();
                tc.End();
                return employeeWorkPlanSetups;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<EmployeeWorkPlanSetup> GetByPayrollTypeID(int payrollTypeId)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(EmployeeWorkPlanSetupDA.GetUsingPayrollType(tc, payrollTypeId));
                var employeeWorkPlanSetups = this.CreateObjects<EmployeeWorkPlanSetup>(dr);
                dr.Close();
                tc.End();
                return employeeWorkPlanSetups;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public EmployeeWorkPlanSetup GetByEmpID(int nEmpID)
        {
            EmployeeWorkPlanSetup oEmployeeWorkPlanSetup = new EmployeeWorkPlanSetup();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(EmployeeWorkPlanSetupDA.GetByEmpID(tc, nEmpID));
                if (oreader.Read())
                {
                    oEmployeeWorkPlanSetup = this.CreateObject<EmployeeWorkPlanSetup>(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 oEmployeeWorkPlanSetup;
        }

        public List<EmployeeWorkPlanSetup> Get()
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(EmployeeWorkPlanSetupDA.Get(tc));
                var employeeWorkPlanSetups = this.CreateObjects<EmployeeWorkPlanSetup>(dr);
                dr.Close();
                tc.End();
                return employeeWorkPlanSetups;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<EmployeeWorkPlanSetup> GetByWPGroupID(int wpGroupID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(EmployeeWorkPlanSetupDA.GetByWPGroupID(tc, wpGroupID));
                var employeeWorkPlanSetups = this.CreateObjects<EmployeeWorkPlanSetup>(dr);
                dr.Close();
                tc.End();

                List<WorkPlanGroup> workPlanGroups = new WorkPlanGroupService().GetAll();
                foreach (EmployeeWorkPlanSetup item in employeeWorkPlanSetups)
                {
                    WorkPlanGroup workPlanGroup = workPlanGroups.Where(x => x.ID == item.WorkPlanGroupID).FirstOrDefault();
                    item.WorkPlanGroupType = workPlanGroup.Type;
                }
                return employeeWorkPlanSetups;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<EmployeeWorkPlanSetup> GetForFixedWP(int groupID, DayOfWeek holiday)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(EmployeeWorkPlanSetupDA.GetForFixedWP(tc, groupID, holiday));
                var employeeWorkPlanSetups = this.CreateObjects<EmployeeWorkPlanSetup>(dr);
                dr.Close();

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

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

                throw new ServiceException(e.Message, e);

                #endregion
            }
        }

        public List<EmployeeWorkPlanSetup> Get(EnumWorkPlanGroup type)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(EmployeeWorkPlanSetupDA.Get(tc, type));
                var employeeWorkPlanSetups = this.CreateObjects<EmployeeWorkPlanSetup>(dr);
                dr.Close();

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

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

                throw new ServiceException(e.Message, e);

                #endregion
            }
        }

        public bool IsEmployeeExistInWorkplan(int empID)
        {
            bool isExist = false;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                isExist = EmployeeWorkPlanSetupDA.IsEmployeeExistInWorkplan(tc, empID);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return isExist;
        }

        public List<EmployeeWorkPlanSetup> IsEmpExistInWorkplan(List<SearchEmployee> emps, int payrollTypeID)
        {
            string str = "";
            emps.ForEach(x => {
                str = str + x.EmployeeID + ",";
            });

            if(str.Length>0)
            {
                str = str.Substring(0, str.Length - 1);
            }
            List<EmployeeWorkPlanSetup> items = new List<EmployeeWorkPlanSetup>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(EmployeeWorkPlanSetupDA.IsEmployeeExistIntheWrokPlan(tc, str, payrollTypeID));
                items = this.CreateObjects<EmployeeWorkPlanSetup>(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 items;
        }

        public List<EmployeeWorkPlanSetup> NotYetAssigned(int payrollTypeID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(EmployeeWorkPlanSetupDA.NotYetAssigned(tc, payrollTypeID));
                var employeeWorkPlanSetups = this.CreateObjects<EmployeeWorkPlanSetup>(dr);
                dr.Close();

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

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

                throw new ServiceException(e.Message, e);

                #endregion
            }
        }
                public List<EmployeeWorkPlanSetup> GetMissingEmps(int wpGroupID, DateTime assignDate)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(EmployeeWorkPlanSetupDA.GetMissingEmps(tc, wpGroupID, assignDate));
                var employeeWorkPlanSetups = this.CreateObjects<EmployeeWorkPlanSetup>(dr);
                dr.Close();

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

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

                throw new ServiceException(e.Message, e);

                #endregion
            }
        }
        public List<EmployeeWorkPlanSetup> getEmployeeWorkPlan(string empid)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(EmployeeWorkPlanSetupDA.getEmployeeWorkPlan(tc, empid));
                var employeeWorkPlanSetups = this.CreateObjects<EmployeeWorkPlanSetup>(dr);
                dr.Close();

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

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

                throw new ServiceException(e.Message, e);

                #endregion
            }
        }
        public DataSet GetEmpNotInWorkPlan(int PayrollTypeID)
        {
            DataSet empInfo = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                empInfo = EmployeeWorkPlanSetupDA.GetEmpNotInWorkPlan(tc, PayrollTypeID);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return empInfo;
        }

        public int Save(EmployeeWorkPlanSetup oEmployeeWorkPlanSetup, int payrollTypeID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oEmployeeWorkPlanSetup.IsNew)
                {
                    EmployeeWorkPlanSetupDA.DeleteExistingData(tc, oEmployeeWorkPlanSetup.EmployeeID);
                    int id = tc.GenerateID("EmployeeWorkPlanSetup", "EmployeeWorkPlanSetupID");
                    base.SetObjectID(oEmployeeWorkPlanSetup, (id));
                    EmployeeWorkPlanSetupDA.Insert(tc, oEmployeeWorkPlanSetup, payrollTypeID);
                }
                else
                {
                    EmployeeWorkPlanSetupDA.Update(tc, oEmployeeWorkPlanSetup);
                }

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

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

                #endregion
            }
        }

        public void Save(List<EmployeeWorkPlanSetup> oEmpWPSetups, int PayrollTypeID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);

                EmployeeWorkPlanSetupDA.Delete(oEmpWPSetups[0].WorkPlanGroupID,tc);
                foreach (EmployeeWorkPlanSetup oEMpSetup in oEmpWPSetups)
                {
                    EmployeeWorkPlanSetupDA.DeleteExistingData(tc, oEMpSetup.EmployeeID);
                    int id = tc.GenerateID("EmployeeWorkPlanSetup", "EmployeeWorkPlanSetupID");
                    base.SetObjectID(oEMpSetup, (id));
                    EmployeeWorkPlanSetupDA.Insert(tc, oEMpSetup, PayrollTypeID);
                }

                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(List<EmployeeWorkPlanSetup> oEmpWPSetups)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);

                string empIds = string.Join(",", oEmpWPSetups.Select(item => item.EmployeeID));

                EmployeeWorkPlanSetupDA.DeleteExistingData(tc, empIds);

                foreach (EmployeeWorkPlanSetup oEMpSetup in oEmpWPSetups)
                {
                    int id = tc.GenerateID("EmployeeWorkPlanSetup", "EmployeeWorkPlanSetupID");
                    base.SetObjectID(oEMpSetup, id);
                    EmployeeWorkPlanSetupDA.Insert(tc, oEMpSetup, oEMpSetup.PayrolltypeID);
                }

                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 SaveFixedWPlans(List<EmployeeWorkPlanSetup> oEmpWPSetups, int PayrollTypeID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);

                EmployeeWorkPlanSetupDA.Delete(oEmpWPSetups[0].WorkPlanGroupID, tc);
                foreach (EmployeeWorkPlanSetup oEMpSetup in oEmpWPSetups)
                {
                    EmployeeWorkPlanSetupDA.DeleteExistingData(tc, oEMpSetup.EmployeeID);
                    int id = tc.GenerateID("EmployeeWorkPlanSetup", "EmployeeWorkPlanSetupID");
                    base.SetObjectID(oEMpSetup, (id));
                    EmployeeWorkPlanSetupDA.Insert(tc, oEMpSetup, PayrollTypeID);
                }

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

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

                #endregion
            }
        }

        internal void DeleteByEmpID(TransactionContext tc, int employeeID)
        {
            try
            {
                EmployeeWorkPlanSetupDA.DeleteByEmpID(tc, employeeID);
            }
            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);
                EmployeeWorkPlanSetupDA.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(int groupID, DayOfWeek weekEndOn)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                EmployeeWorkPlanSetupDA.Delete(tc, groupID, weekEndOn);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public bool IsExist(int groupID, DayOfWeek weekEndOn)
        {
            bool isExist = false;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                isExist = EmployeeWorkPlanSetupDA.IsExist(tc, groupID, weekEndOn);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return isExist;
        }

        //for excel Upload
        public void Save(TransactionContext tc, List<EmployeeWorkPlanSetup> oEmpWPSetups, int PayrollTypeID)
        {
            try
            {
                foreach (EmployeeWorkPlanSetup oEMpSetup in oEmpWPSetups)
                {
                    int id = tc.GenerateID("EmployeeWorkPlanSetup", "EmployeeWorkPlanSetupID");
                    base.SetObjectID(oEMpSetup, (id));
                    oEMpSetup.CreatedBy = 1;
                    oEMpSetup.CreatedDate = DateTime.Now;
                    EmployeeWorkPlanSetupDA.Insert(tc, oEMpSetup, PayrollTypeID);
                }
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        //

        #endregion
    }

    #endregion
}