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

namespace HRM.DA
{
    #region WorkPlanGroup Service

    [Serializable]
    public class WorkPlanGroupService : ServiceTemplate, IWorkPlanGroupService
    {
        public WorkPlanGroupService()
        {
        }

        private void MapObject(WorkPlanGroup oWorkPlanGroup, DataReader oReader)
        {
            try
            {
                base.SetObjectID(oWorkPlanGroup, oReader.GetInt32("WorkPlanGroupID").Value);
                oWorkPlanGroup.Name = oReader.GetString("Name");
                //oWorkPlanGroup.FromTime = oReader.GetDateTime("FromTime",DateTime.MinValue);
                //oWorkPlanGroup.ToTime = oReader.GetDateTime("ToTime", DateTime.MinValue);
                oWorkPlanGroup.Type = (EnumWorkPlanGroup)oReader.GetInt32("Type").Value;
                //oWorkPlanGroup.TypeName = EnumDescription.GetEnumDescription(oWorkPlanGroup.Type);
                oWorkPlanGroup.Sequence = oReader.GetInt32("SequenceNo").Value;
                oWorkPlanGroup.Status = (EnumStatus)oReader.GetInt32("Status").Value;
                oWorkPlanGroup.CreatedBy = oReader.GetInt32("CreatedBy", 0);
                oWorkPlanGroup.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
                oWorkPlanGroup.ModifiedBy = oReader.GetInt32("ModifiedBy");
                oWorkPlanGroup.ModifiedDate = oReader.GetDateTime("ModifiedDate");
                oWorkPlanGroup.PayrollTypeID = oReader.GetInt32("PayrollTypeID", 0);

                if (oReader.GetInt32("HolidayOne").HasValue)
                    oWorkPlanGroup.HolidayOne = (HolidayDayOfWeek)oReader.GetInt32("HolidayOne").Value;

                if (oReader.GetInt32("HolidayTwo").HasValue)
                    oWorkPlanGroup.HolidayTwo = (HolidayDayOfWeek)oReader.GetInt32("HolidayTwo").Value;

                oWorkPlanGroup.SaturdayShiftID = oReader.GetInt32("SaturdayShiftID") == null
                    ? 0
                    : oReader.GetInt32("SaturdayShiftID").Value;
                oWorkPlanGroup.SundayShiftID =
                    oReader.GetInt32("SundayShiftID") == null ? 0 : oReader.GetInt32("SundayShiftID").Value;
                oWorkPlanGroup.MondayShiftID =
                    oReader.GetInt32("MondayShiftID") == null ? 0 : oReader.GetInt32("MondayShiftID").Value;
                oWorkPlanGroup.TuesdayShiftID = oReader.GetInt32("TuesdayShiftID") == null
                    ? 0
                    : oReader.GetInt32("TuesdayShiftID").Value;
                oWorkPlanGroup.WednesdayShiftID = oReader.GetInt32("WednesdayShiftID") == null
                    ? 0
                    : oReader.GetInt32("WednesdayShiftID").Value;
                oWorkPlanGroup.ThursdayShiftID = oReader.GetInt32("ThursdayShiftID") == null
                    ? 0
                    : oReader.GetInt32("ThursdayShiftID").Value;
                oWorkPlanGroup.FridayShiftID =
                    oReader.GetInt32("FridayShiftID") == null ? 0 : oReader.GetInt32("FridayShiftID").Value;
                oWorkPlanGroup.InitialShiftID = oReader.GetInt32("ShiftID") == null ? 0 : oReader.GetInt32("ShiftID").Value;


                this.SetObjectState(oWorkPlanGroup, ObjectState.Saved);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

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

        protected WorkPlanGroup CreateObject(DataReader oReader)
        {
            WorkPlanGroup oWorkPlanGroup = new WorkPlanGroup();
            MapObject(oWorkPlanGroup, oReader);
            return oWorkPlanGroup;
        }

        #region Service implementation

        public WorkPlanGroup Get(int id)
        {
            WorkPlanGroup oWorkPlanGroup = new WorkPlanGroup();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(WorkPlanGroupDA.Get(tc, id));
                if (oreader.Read())
                {
                    oWorkPlanGroup = this.CreateObject<WorkPlanGroup>(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 oWorkPlanGroup;
        }

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

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

                #endregion
            }
        }
        
        public List<WorkPlanGroup> GetByPayrollTypeId(int payrollTypeId)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(WorkPlanGroupDA.GetByPayrollTypeId(tc, payrollTypeId));
                var workPlanGroups = this.CreateObjects<WorkPlanGroup>(dr);
                dr.Close();
                tc.End();
                return workPlanGroups;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<WorkPlanGroup> Get(EnumStatus status, int payrollTypeID, string name, EnumWorkPlanGroup? wPlanGroup)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(WorkPlanGroupDA.Get(tc, status, payrollTypeID, name, wPlanGroup));
                var workPlanGroups = this.CreateObjects<WorkPlanGroup>(dr);
                dr.Close();

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

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

                throw new ServiceException(e.Message, e);

                #endregion
            }
        }

        public WorkPlanGroup Get(Employee oEmployee)
        {
            WorkPlanGroup oWorkPlanGroup = new WorkPlanGroup();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(WorkPlanGroupDA.Get(tc, oEmployee));
                if (oreader.Read())
                {
                    oWorkPlanGroup = this.CreateObject<WorkPlanGroup>(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 oWorkPlanGroup;
        }
        public WorkPlanGroup GetByEmpId(int oEmployeeId)
        {
            WorkPlanGroup oWorkPlanGroup = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(WorkPlanGroupDA.GetByEmpId(tc, oEmployeeId));
                if (oreader.Read())
                {
                    oWorkPlanGroup = this.CreateObject<WorkPlanGroup>(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 oWorkPlanGroup;
        }
        public int Save(WorkPlanGroup oWorkPlanGroup, int payrollTypeID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oWorkPlanGroup.IsNew)
                {
                    int id = tc.GenerateID("WorkPlanGroup", "WorkPlanGroupID");
                    base.SetObjectID(oWorkPlanGroup, (id));
                    int seqNo = tc.GenerateID("WorkPlanGroup", "SequenceNO");
                    oWorkPlanGroup.Sequence = seqNo;
                    WorkPlanGroupDA.Insert(tc, oWorkPlanGroup, payrollTypeID);
                }
                else
                {
                    WorkPlanGroupDA.Update(tc, oWorkPlanGroup, payrollTypeID);
                }

                tc.End();
                return oWorkPlanGroup.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);
                WorkPlanGroupDA.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
            }
        }

        #endregion
    }

    #endregion
}