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

namespace HRM.DA
{
    public class EmployeeAppraisalService : ServiceTemplate, IEmployeeAppraisalService
    {
        #region Object Mapping

        private void MapObject(EmployeeAppraisal oEmployeeAppraisal, DataReader oReader)
        {
            SetObjectID(oEmployeeAppraisal, (oReader.GetInt32("EmployeeAppraisalID").Value));
            oEmployeeAppraisal.EmployeeID = oReader.GetInt32("EmployeeID", 0);
            oEmployeeAppraisal.AppraisalYear = (DateTime)oReader.GetDateTime("AppraisalYear");
            oEmployeeAppraisal.MKTSurveyID = oReader.GetInt32("MKTSurveyID", 0);
            oEmployeeAppraisal.CurrentBasic = (double)oReader.GetDouble("CurrentBasic");
            oEmployeeAppraisal.SurveyON = (EnumSurveyOn)oReader.GetInt32("SurveyON");
            oEmployeeAppraisal.BudgetAmount = (double)oReader.GetDouble("BudgetAmount");
            oEmployeeAppraisal.MKTAmount = (double)oReader.GetDouble("MKTAmount");
            oEmployeeAppraisal.LMID = oReader.GetInt32("LMID", 0);
            oEmployeeAppraisal.LMChangePercent = (double)oReader.GetDouble("LMChangePercent");
            oEmployeeAppraisal.LMChangeAmount = (double)oReader.GetDouble("LMChangeAmount");
            oEmployeeAppraisal.LMRemarks = (string)oReader.GetString("LMRemarks");
            oEmployeeAppraisal.HRApproveID = oReader.GetInt32("HRApproveID", 0);
            oEmployeeAppraisal.HRChangePercent = (double)oReader.GetDouble("HRChangePercent");
            oEmployeeAppraisal.HRChangeAmount = (double)oReader.GetDouble("HRChangeAmount");
            oEmployeeAppraisal.HRRemarks = (string)oReader.GetString("HRRemarks");
            oEmployeeAppraisal.ISLMDone = (bool)oReader.GetBoolean("ISLMDone");
            oEmployeeAppraisal.ISHRDone = (bool)oReader.GetBoolean("ISHRDone");
            oEmployeeAppraisal.AppraisalPointID = oReader.GetInt32("AppraisalPointID", 0);
            oEmployeeAppraisal.AppraisalPointRate = (double)oReader.GetDouble("AppraisalPointRate");
            oEmployeeAppraisal.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oEmployeeAppraisal.CreatedDate = oReader.GetDateTime("CreatedDate").HasValue
                ? oReader.GetDateTime("CreatedDate").Value
                : DateTime.MinValue;
            oEmployeeAppraisal.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oEmployeeAppraisal.ModifiedDate = oReader.GetDateTime("ModifiedDate").HasValue
                ? oReader.GetDateTime("ModifiedDate").Value
                : (DateTime?)null;
            this.SetObjectState(oEmployeeAppraisal, ObjectState.Saved);
        }

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

        #endregion

        #region Service Implementation
        public EmployeeAppraisal Get(int employeeId)
        {
            EmployeeAppraisal oEmployeeAppraisal = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(EmployeeAppraisalDA.Get(tc, employeeId));
                oEmployeeAppraisal = this.CreateObject<EmployeeAppraisal>(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 oEmployeeAppraisal;
        }
        public List<EmployeeAppraisal> Get(EnumStatus status)
        {
            List<EmployeeAppraisal> oEmployeeAppraisal = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(EmployeeAppraisalDA.Get(tc, status));
                oEmployeeAppraisal = this.CreateObjects<EmployeeAppraisal>(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 oEmployeeAppraisal;
        }
        public List<EmployeeAppraisal> Get(DateTime apprisalYear)
        {
            List<EmployeeAppraisal> oEmployeeAppraisal = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(EmployeeAppraisalDA.Get(tc, apprisalYear));
                oEmployeeAppraisal = this.CreateObjects<EmployeeAppraisal>(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 oEmployeeAppraisal;
        }

        public List<EmployeeAppraisal> GetAppraisalDataFromObjSet(int LMId, DateTime apprisalYear)
        {
            List<EmployeeAppraisal> oEmployeeAppraisal = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(EmployeeAppraisalDA.GetAppraisalDataFromObjSet(tc, LMId, apprisalYear));
                oEmployeeAppraisal = this.CreateObjects<EmployeeAppraisal>(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 oEmployeeAppraisal;
        }

        public List<EmployeeAppraisal> Get(int LMId, DateTime apprisalYear)
        {
            List<EmployeeAppraisal> oEmployeeAppraisal = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(EmployeeAppraisalDA.Get(tc, LMId, apprisalYear));
                oEmployeeAppraisal = this.CreateObjects<EmployeeAppraisal>(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 oEmployeeAppraisal;
        }

        public List<EmployeeAppraisal> GetByLM(int LMId, DateTime apprisalYear)
        {
            List<EmployeeAppraisal> oEmployeeAppraisal = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(EmployeeAppraisalDA.GetByLM(tc, LMId, apprisalYear));
                oEmployeeAppraisal = this.CreateObjects<EmployeeAppraisal>(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 oEmployeeAppraisal;
        }

        public List<EmployeeAppraisal> GetPreviouslySavedData(int LMId, DateTime apprisalYear)
        {
            List<EmployeeAppraisal> oEmployeeAppraisal = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(EmployeeAppraisalDA.GetPreviouslySavedData(tc, LMId, apprisalYear));
                oEmployeeAppraisal = this.CreateObjects<EmployeeAppraisal>(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 oEmployeeAppraisal;
        }
        public int Save(EmployeeAppraisal item)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (item.IsNew)
                {
                    int id = tc.GenerateID("EmployeeAppraisal", "EmployeeAppraisalID");
                    SetObjectID(item, (id));
                    EmployeeAppraisalDA.Insert(tc, item);
                }
                else
                {
                    EmployeeAppraisalDA.Update(tc, item);
                }

                tc.End();

                return item.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<EmployeeAppraisal> items)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                foreach (EmployeeAppraisal item in items)
                {
                    if (item.IsNew)
                    {
                        int id = tc.GenerateID("EmployeeAppraisal", "EmployeeAppraisalID");
                        SetObjectID(item, (id));
                        EmployeeAppraisalDA.Insert(tc, item);
                    }
                    else
                    {
                        EmployeeAppraisalDA.Update(tc, item);
                    }
                }

                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 id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                EmployeeAppraisalDA.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 DeleteByEmployeeId(int empID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                EmployeeAppraisalDA.Delete(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
            }
        }

        public void Save(List<EmployeeAppraisal> newItems, List<EmployeeAppraisal> oldItems, CurrentUser user)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                foreach (EmployeeAppraisal item in newItems)
                {
                    if (!oldItems.Any(x => x.EmployeeID == item.EmployeeID
                        && x.AppraisalYear.Year == item.AppraisalYear.Year))
                    {
                        int id = tc.GenerateID("EmployeeAppraisal", "EmployeeAppraisalID");
                        SetObjectID(item, id);
                        EmployeeAppraisalDA.Insert(tc, item);
                    }
                    else
                    {
                        EmployeeAppraisal oldData = oldItems.Where(x => x.EmployeeID == item.EmployeeID
                            && x.AppraisalYear.Year == item.AppraisalYear.Year).FirstOrDefault();
                        oldData.AppraisalPointRate = item.AppraisalPointRate;
                        oldData.ModifiedBy = user.UserID;
                        oldData.ModifiedDate = DateTime.Now;
                        EmployeeAppraisalDA.Update(tc, oldData);
                    }
                }
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
                #endregion
            }
        }

        #endregion
    }
}