using System;
using System.Collections.Generic;
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 AdhocClaimService Service

    [Serializable]
    public class AdhocClaimService : ServiceTemplate
    {
        public AdhocClaimService()
        {
        }

        private void MapObject(AdhocClaim oAdhocClaim, DataReader oReader)
        {
            base.SetObjectID(oAdhocClaim, oReader.GetInt32("AdhocClaimID").Value);
            oAdhocClaim.EmployeeID = oReader.GetInt32("EmployeeID", 0);
            oAdhocClaim.BatchID = oReader.GetInt32("BatchID", 0);
            oAdhocClaim.ItemType = (enumPayrollComponentType)oReader.GetInt32("ItemType").Value;
            oAdhocClaim.ItemID = oReader.GetInt32("ItemID", 0);
            oAdhocClaim.LineManagerID = oReader.GetInt32("LineManagerID", 0);
            oAdhocClaim.ClaimDate = oReader.GetDateTime("ClaimDateTime").Value;
            oAdhocClaim.SalaryMonth = oReader.GetDateTime("SalaryMonth").Value;
            oAdhocClaim.AdjustmentSalaryMonth = oReader.GetDateTime("AdjustmentSalaryMonth") == null
                ? DateTime.MinValue
                : oReader.GetDateTime("AdjustmentSalaryMonth").Value;
            oAdhocClaim.LMApproveDate = oReader.GetDateTime("LMApproveDate") == null
                ? DateTime.MinValue
                : oReader.GetDateTime("LMApproveDate").Value;
            oAdhocClaim.SMApproveDate = oReader.GetDateTime("SMApproveDate") == null
                ? DateTime.MinValue
                : oReader.GetDateTime("SMApproveDate").Value;
            oAdhocClaim.FromTime = oReader.GetDateTime("FromTime") == null
                ? DateTime.MinValue
                : oReader.GetDateTime("FromTime").Value;
            oAdhocClaim.ToTime = oReader.GetDateTime("ToTime") == null
                ? DateTime.MinValue
                : oReader.GetDateTime("ToTime").Value;
            oAdhocClaim.EmployeeValue = oReader.GetDouble("EmployeeValue").Value;
            oAdhocClaim.LMValue = oReader.GetDouble("LMValue").Value;
            oAdhocClaim.EmployeeRemarks = oReader.GetString("EmployeeRemarks");
            oAdhocClaim.LMRemarks = oReader.GetString("LMRemarks");
            oAdhocClaim.IsLMSubmitted = oReader.GetBoolean("IsLMSubmitted") == null
                ? false
                : oReader.GetBoolean("IsLMSubmitted").Value;
            oAdhocClaim.IsLMEdited =
                oReader.GetBoolean("IsLMEdited") == null ? false : oReader.GetBoolean("IsLMEdited").Value;
            oAdhocClaim.IsSTSubmitted = oReader.GetBoolean("IsSTSubmitted") == null
                ? false
                : oReader.GetBoolean("IsSTSubmitted").Value;
            oAdhocClaim.IsBatchEntry = oReader.GetBoolean("IsBatchEntry") == null
                ? false
                : oReader.GetBoolean("IsBatchEntry").Value;
            oAdhocClaim.ClaimValueType = (EnumClaimValueType)oReader.GetInt32("ClaimValueType").Value;
            oAdhocClaim.SystemMode = (EnumSystemMode)oReader.GetInt32("SystemMode").Value;
            oAdhocClaim.ClaimWFStatus = (EnumClaimWFStatus)oReader.GetInt32("ClaimWFStatus").Value;
            oAdhocClaim.ShiftManagerID = oReader.GetInt32("ShiftManagerID", 0);
            oAdhocClaim.SeniorTechnicianID = oReader.GetInt32("SeniorTechnicianID", 0);
            oAdhocClaim.ShiftID = oReader.GetInt32("ShiftID", 0);
            oAdhocClaim.WorkPlanGroupID = oReader.GetInt32("WorkPlanGroupID", 0);
            oAdhocClaim.ApproverID = oReader.GetInt32("ApproverID", 0);
            oAdhocClaim.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oAdhocClaim.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
            oAdhocClaim.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oAdhocClaim.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oAdhocClaim, Ease.Core.ObjectState.Saved);
        }

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

        #region Service implementation

        public AdhocClaim Get(int id)
        {
            TransactionContext tc = null;
            try
            {
                var oAdhocClaim = new AdhocClaim();
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(AdhocClaimDA.Get(tc, id));
                if (oreader.Read())
                {
                    oAdhocClaim = this.CreateObject<AdhocClaim>(oreader);
                }

                oreader.Close();
                tc.End();
                return oAdhocClaim;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public long GetMaxBatchID()
        {
            long maxBatchID = 0;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                maxBatchID = AdhocClaimDA.GetMaxBatchID(tc);
                maxBatchID = maxBatchID + 1;
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return maxBatchID;
        }

        public List<AdhocClaim> Get()
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(AdhocClaimDA.Get(tc));
                var AdhocClaims = this.CreateObjects<AdhocClaim>(dr);

                dr.Close();
                tc.End();
                return AdhocClaims;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }


        public List<AdhocClaim> GetByEmployeeID(int employeeID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(AdhocClaimDA.GetByEmployeeID(tc, employeeID));
                var AdhocClaims = this.CreateObjects<AdhocClaim>(dr);

                dr.Close();
                tc.End();
                return AdhocClaims;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<AdhocClaim> GetByEmployeeID(string employeeIDs, EnumClaimWFStatus wfStatus)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(AdhocClaimDA.GetByEmployeeID(tc, employeeIDs, wfStatus));
                var AdhocClaims = this.CreateObjects<AdhocClaim>(dr);

                dr.Close();
                tc.End();
                return AdhocClaims;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<AdhocClaim> GetByShiftManagerID(int employeeID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(AdhocClaimDA.GetByShiftManagerID(tc, employeeID));
                var AdhocClaims = this.CreateObjects<AdhocClaim>(dr);

                dr.Close();
                tc.End();
                return AdhocClaims;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public int GetAdhocClaimDataByWFStatusCount(DateTime fromDate, DateTime toDate, EnumWFAttnStatus status)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                return AdhocClaimDA.GetAdhocClaimDataByWFStatusCount(tc, fromDate, toDate, status);
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
            finally
            {
                if (tc != null)
                {
                    tc.End();
                }
            }
        }

        public List<AdhocClaim> GetByApproverID(int approverID, DateTime fromDate, DateTime toDate)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(AdhocClaimDA.GetByApproverID(tc, approverID, fromDate, toDate));
                var AdhocClaims = this.CreateObjects<AdhocClaim>(dr);

                dr.Close();
                tc.End();
                return AdhocClaims;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }


        public List<AdhocClaim> GetByShiftManagerID(int shiftManagerID, DateTime fromDate, DateTime toDate)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(AdhocClaimDA.GetByShiftManagerID(tc, shiftManagerID, fromDate, toDate));
                var AdhocClaims = this.CreateObjects<AdhocClaim>(dr);

                dr.Close();
                tc.End();
                return AdhocClaims;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<AdhocClaim> GetBySeniorTechnicianID(int seniorTechnicianID, DateTime fromDate, DateTime toDate)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr =
                    new DataReader(AdhocClaimDA.GetBySeniorTechnicianID(tc, seniorTechnicianID, fromDate, toDate));
                var AdhocClaims = this.CreateObjects<AdhocClaim>(dr);

                dr.Close();
                tc.End();
                return AdhocClaims;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<AdhocClaim> GetByShiftManagerID(int employeeID, EnumClaimWFStatus wfStatus)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(AdhocClaimDA.GetByShiftManagerID(tc, employeeID, wfStatus));
                var AdhocClaims = this.CreateObjects<AdhocClaim>(dr);

                dr.Close();
                tc.End();
                return AdhocClaims;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<AdhocClaim> GetByApprover(int shiftID, int deparetmentID, EnumClaimWFStatus wfStatus)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(AdhocClaimDA.GetByApprover(tc, shiftID, deparetmentID, wfStatus));
                var AdhocClaims = this.CreateObjects<AdhocClaim>(dr);

                dr.Close();
                tc.End();
                return AdhocClaims;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }


        public List<AdhocClaim> GetByEmployeeID(int employeeID, DateTime fromDate, DateTime toDate)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(AdhocClaimDA.GetByEmployeeID(tc, employeeID, fromDate, toDate));
                var AdhocClaims = this.CreateObjects<AdhocClaim>(dr);

                dr.Close();
                tc.End();
                return AdhocClaims;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<AdhocClaim> Get(DateTime deploymentDate)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(AdhocClaimDA.Get(tc, deploymentDate));
                var AdhocClaims = this.CreateObjects<AdhocClaim>(dr);

                dr.Close();
                tc.End();
                return AdhocClaims;
            }
            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<AdhocClaim> oAdhocClaims)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                int id = tc.GenerateID("AdhocClaim", "AdhocClaimID");
                foreach (AdhocClaim item in oAdhocClaims)
                {
                    if (item.IsNew)
                    {
                        base.SetObjectID(item, id);
                        AdhocClaimDA.Insert(tc, item);
                    }
                    else
                    {
                        AdhocClaimDA.Update(tc, item);
                    }

                    id++;
                }
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
            finally
            {
                if (tc != null)
                {
                    tc.End();
                }
            }
        }

        public void Save(List<AdhocClaim> oAdhocClaims, List<AttnMonthlyBenefit> attnMonthlyBenefits,
            List<AttnBenefitAuditTrail> auditTrails)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                int id = tc.GenerateID("AdhocClaim", "AdhocClaimID");
                foreach (AdhocClaim item in oAdhocClaims)
                {
                    if (item.IsNew)
                    {
                        base.SetObjectID(item, id);
                        //item.CreatedBy = User.CurrentUser.ID; //###$
                        //item.CreatedDate = DateTime.Today;
                        AdhocClaimDA.Insert(tc, item);
                    }
                    else
                    {
                        //item.ModifiedBy = User.CurrentUser.ID; //###$
                        //item.ModifiedDate = DateTime.Today;
                        AdhocClaimDA.Update(tc, item);
                    }

                    id++;
                }

                id = tc.GenerateID("AttnMonthlyBenefit", "AttnMonthlyBenefitID");
                foreach (AttnMonthlyBenefit mbenifit in attnMonthlyBenefits)
                {
                    if (mbenifit.IsNew)
                    {
                        if (AttnMonthlyBenefitDA.IsExist(tc, mbenifit.EmployeeID, mbenifit.SalaryMonth,
                            mbenifit.ItemType, mbenifit.ItemID))
                        {
                            AttnMonthlyBenefitDA.Delete(tc, mbenifit);
                            base.SetObjectID(mbenifit, id);
                            AttnMonthlyBenefitDA.Insert(tc, mbenifit);
                        }
                        else
                        {
                            base.SetObjectID(mbenifit, id);
                            AttnMonthlyBenefitDA.Insert(tc, mbenifit);
                        }
                    }
                    else
                    {
                        AttnMonthlyBenefitDA.Update(tc, mbenifit);
                    }

                    id++;
                }

                id = tc.GenerateID("ATTNBENEFITAUDITTRAIL", "ATTNBENEFITAUDITTRAILID");
                foreach (AttnBenefitAuditTrail item in auditTrails)
                {
                    base.SetObjectID(item, id);
                    AttnBenefitAuditTrailDA.Insert(tc, item);
                    id++;
                }
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
            finally
            {
                if (tc != null)
                {
                    tc.End();
                }
            }
        }

        public int Save(AdhocClaim oAdhocClaim)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oAdhocClaim.IsNew)
                {
                    int id = tc.GenerateID("AdhocClaim", "AdhocClaimID");
                    base.SetObjectID(oAdhocClaim, id);
                    //oAdhocClaim.CreatedBy = User.CurrentUser.ID;
                    //oAdhocClaim.CreatedDate = DateTime.Today;
                    AdhocClaimDA.Insert(tc, oAdhocClaim);
                }
                else
                {
                    //oAdhocClaim.ModifiedBy = User.CurrentUser.ID;
                    //oAdhocClaim.ModifiedDate = DateTime.Today;
                    AdhocClaimDA.Update(tc, oAdhocClaim);
                }

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