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

namespace HRM.DA
{
    #region Shift Service

    [Serializable]
    public class ShiftService : ServiceTemplate, IShiftService
    {
        public ShiftService()
        {
        }

        private void MapObject(Shift oShift, DataReader oReader)
        {
            base.SetObjectID(oShift, oReader.GetInt32("ShiftID").Value);
            oShift.Code = oReader.GetString("Code");
            oShift.Name = oReader.GetString("Name");
            oShift.InTime = oReader.GetDateTime("InTime").Value;
            oShift.OutTime = oReader.GetDateTime("OutTime").Value;
            oShift.hasAbsentTime = oReader.GetBoolean("hasAbsentTime").Value;
            oShift.AbsentTime = oShift.hasAbsentTime ? oReader.GetDateTime("AbsentTime").Value : DateTime.MinValue;
            oShift.LateCalcualtion = oReader.GetDouble("LateCalcualtion").Value;
            oShift.DelayCalcualtion = oReader.GetDouble("DelayCalcualtion").HasValue
                ? oReader.GetDouble("DelayCalcualtion").Value
                : 0.0;
            oShift.EarlyExitBefore = oReader.GetDouble("EarlyExitBefore").Value;
            oShift.MinimumOTHour = oReader.GetDouble("MinimumOTHour", true, 0);
            oShift.MinimumInOTHour = oReader.GetDouble("MinimumInOTHour") == null
                ? 0
                : oReader.GetDouble("MinimumInOTHour").Value;
            oShift.MinimumOutOTHour = oReader.GetDouble("MinimumOTHour") == null
                ? 0
                : oReader.GetDouble("MinimumOTHour").Value;
            oShift.LunchHour = oReader.GetDouble("LunchHour") == null ? 0 : oReader.GetDouble("LunchHour").Value;
            oShift.IsOverlapingDay = oReader.GetBoolean("IsOverlapingDay").Value;
            oShift.ShiftType = (EnumWorkPlanGroup)oReader.GetInt16("ShiftType").Value;
            oShift.ShortName = oReader.GetString("ShortName");
            oShift.Sequence = oReader.GetInt32("SequenceNo").Value;
            oShift.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            oShift.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oShift.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
            oShift.ModifiedBy = oReader.GetInt32("ModifiedBy");
            oShift.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            oShift.IsCounterClock = oReader.GetBoolean("IsCounterClock").Value;
            oShift.WorkHour = oReader.GetDouble("WorkHour", 0);
            oShift.FlexibleHour = oReader.GetDouble("FlexibleHour", 0);
            oShift.MinimumInGraceTime = oReader.GetDouble("MinimumInGraceTime", 0);
            oShift.MinimumOutGraceTime = oReader.GetDouble("MinimumOutGraceTime", 0);
            oShift.Location = (EnumLocation)oReader.GetInt32("Location", 0);

            oShift.HasBenefits = oReader.GetBoolean("HasBenefits").HasValue ? oReader.GetBoolean("HasBenefits").Value: false;
            oShift.HasExtraHourAllowance = oReader.GetBoolean("HasExtraHourAllowance").HasValue ? oReader.GetBoolean("HasExtraHourAllowance").Value : false;
            oShift.ExtraHourAllowanceFromTime = oShift.HasExtraHourAllowance ? oReader.GetDateTime("ExtraHourAllowanceFromTime").Value : DateTime.MinValue;
            oShift.HasNightAllowance = oReader.GetBoolean("HasNightAllowance").HasValue ? oReader.GetBoolean("HasNightAllowance").Value : false;
            oShift.HasHolidayAllowance = oReader.GetBoolean("HasHolidayAllowance").HasValue ? oReader.GetBoolean("HasHolidayAllowance").Value : false;
            oShift.HasOtherAllowance = oReader.GetBoolean("HasOtherAllowance").HasValue ? oReader.GetBoolean("HasOtherAllowance").Value : false;
            oShift.HasRegularOverTime = oReader.GetBoolean("HasRegularOverTime").HasValue ? oReader.GetBoolean("HasRegularOverTime").Value: false;
            oShift.HasHolidayOverTime = oReader.GetBoolean("HasHolidayOverTime").HasValue ? oReader.GetBoolean("HasHolidayOverTime").Value : false;
            oShift.ExtraHourAllowanceID = oReader.GetInt32("ExtraHourAllowanceID");
            oShift.NightAllowanceID = oReader.GetInt32("NightAllowanceID");
            oShift.HolidayAllowanceID = oReader.GetInt32("HolidayAllowanceID");
            oShift.OtherAllowanceID = oReader.GetInt32("OtherAllowanceID");
            oShift.RegularOverTimeAllowanceID = oReader.GetInt32("RegularOverTimeAllowanceID");
            oShift.HolidayOverTimeAllowanceID = oReader.GetInt32("HolidayOverTimeAllowanceID");
            oShift.HasTiffinAllowance = oReader.GetBoolean("HasTiffinAllowance").HasValue ? oReader.GetBoolean("HasTiffinAllowance").Value : false;
            oShift.HasSpecialAllowance = oReader.GetBoolean("HasSpecialAllowance").HasValue ? oReader.GetBoolean("HasSpecialAllowance").Value : false;
            oShift.TiffinAllowanceID = oReader.GetInt32("TiffinAllowanceID");
            oShift.SpecialAllowanceID = oReader.GetInt32("SpecialAllowanceID");

            this.SetObjectState(oShift, Ease.Core.ObjectState.Saved);
        }

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

        protected Shift CreateObject(DataReader oReader)
        {
            Shift oShift = new Shift();
            MapObject(oShift, oReader);
            return oShift;
        }

        #region Service implementation

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

        public DataTable GetShiftForMobile(int id)
        {
            DataTable oShift = new DataTable();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                oShift = ShiftDA.GetShiftForMobile(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
            }

            return oShift;
        }

        public Shift Get(string shiftName)
        {
            Shift oShift = new Shift();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(ShiftDA.Get(tc, shiftName));
                if (oreader.Read())
                {
                    oShift = this.CreateObject<Shift>(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 oShift;
        }

        public List<Shift> Get(string code, string name, EnumStatus status, int payrollTypeID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(ShiftDA.Get(tc,code, name, status, payrollTypeID));
                var shifts = this.CreateObjects<Shift>(dr);
                dr.Close();
                tc.End();
                return shifts;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<Shift> Get(EnumStatus status, int payrollTypeID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(ShiftDA.Get(tc, string.Empty, string.Empty, status, payrollTypeID));
                List<Shift> shifts = this.CreateObjects<Shift>(dr);
                dr.Close();
                tc.End();
                return shifts;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<Shift> Get(bool isCounterClock)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(ShiftDA.Get(tc, isCounterClock));
                var shifts = this.CreateObjects<Shift>(dr);
                dr.Close();
                tc.End();
                return shifts;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }


        public List<Shift> GetAllShift()
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(ShiftDA.GetAllShift(tc));
                var shifts = this.CreateObjects<Shift>(dr);
                dr.Close();
                tc.End();
                return shifts;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public DataTable GetAllShiftWithNoLock()
        {
            DataTable oShifts = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                oShifts = ShiftDA.GetAllShiftWithNoLock(tc);
                tc.End();
                return oShifts;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<Shift> GetAllShiftByPayrollType(int payrollTypeid)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(ShiftDA.GetAllShiftByPayrollType(tc, payrollTypeid));
                var shifts = this.CreateObjects<Shift>(dr);
                dr.Close();
                tc.End();
                return shifts;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<Shift> GetShiftByIds(string shiftIds)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(ShiftDA.GetShiftByIds(tc, shiftIds));
                var shifts = this.CreateObjects<Shift>(dr);
                dr.Close();
                tc.End();
                return shifts;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public int Save(Shift oShift)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oShift.IsNew)
                {
                    int id = tc.GenerateID("Shift", "ShiftID");
                    base.SetObjectID(oShift, (id));
                    ShiftDA.Insert(tc, oShift);
                }
                else
                {
                    ShiftDA.Update(tc, oShift);
                }

                tc.End();
                return oShift.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);
                ShiftDA.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 bool IsExist(string shiftCode, string shortName, int shiftid)
        {
            bool isExist = false;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                isExist = ShiftDA.IsExist(tc, shiftCode, shortName, shiftid);
                //accessCards = this.CreateObjects<AccessCard>(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 isExist;
        }

        #endregion
    }

    #endregion
}