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

namespace HRM.DA
{
    public class DesignationService : ServiceTemplate, IDesignationService
    {
        public DesignationService()
        {
        }

        private void MapObject(Designation oDesignation, DataReader oReader)
        {
            base.SetObjectID(oDesignation, oReader.GetInt32("DesignationID").Value);
            oDesignation.Code = oReader.GetString("Code");
            oDesignation.Name = oReader.GetString("Name");
            oDesignation.GradeID = oReader.GetInt32("GradeID", 0);
            oDesignation.PayrollTypeID = oReader.GetInt32("PayrollTypeID", 0);
            oDesignation.Sequence = oReader.GetInt32("SequenceNo").Value;
            oDesignation.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            oDesignation.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oDesignation.CreatedDate = oReader.GetDateTime("CreationDate") == null
                ? DateTime.Today
                : oReader.GetDateTime("CreationDate").Value;
            oDesignation.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oDesignation.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            oDesignation.NameInBangla = oReader.GetString("NameInBangla", true, null);
            this.SetObjectState(oDesignation, Ease.Core.ObjectState.Saved);
        }

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

        #region Service implementation

        public string GetNextCode()
        {
            TransactionContext tc = null;
            string _code = "";
            try
            {
                tc = TransactionContext.Begin();
                _code = GlobalFunctionService.GetMaxCode(tc, "designation", "codeautogenerate", "Designation", "Code");
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return _code;
        }

        public Designation Get(TransactionContext tc, int id)
        {
            Designation oDesignation = null;
            try
            {
                DataReader oreader = new DataReader(DesignationDA.Get(tc, id));
                if (oreader.Read())
                {
                    oDesignation = this.CreateObject<Designation>(oreader);
                }

                oreader.Close();
            }
            catch (Exception e)
            {
                #region Handle Exception

                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);

                #endregion
            }

            return oDesignation;
        }

        public Designation Get(int id)
        {
            Designation oDesignation = null;

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

        public Designation Get(string sCode, int payrollTypeID)
        {
            Designation oDesignation = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(DesignationDA.Get(tc, sCode, payrollTypeID));
                if (oreader.Read())
                {
                    oDesignation = this.CreateObject<Designation>(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 oDesignation;
        }

        public int GetMaxID()
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                return tc.GenerateID("Designation", "DesignationID") - 1;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
            finally
            {
                tc.End();
            }
        }

        public List<Designation> Get(EnumStatus status, int payrollTypeID)
        {
            List<Designation> designations = new List<Designation>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DesignationDA.Get(tc, status, payrollTypeID));
                designations = this.CreateObjects<Designation>(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 designations;
        }

        public List<Designation> GetRelateWithEmpolyee(EnumStatus status, int payrollTypeID)
        {
            List<Designation> designations = new List<Designation>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DesignationDA.GetRelateWithEmpolyee(tc, status, payrollTypeID));
                designations = this.CreateObjects<Designation>(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 designations;
        }

        public List<Designation> GetAll()
        {
            List<Designation> designations = new List<Designation>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DesignationDA.GetAll(tc));
                designations = this.CreateObjects<Designation>(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 designations;
        }


        public List<Designation> GetWithSapCodes(EnumStatus status)
        {
            List<Designation> designations = new List<Designation>();

            return designations;
        }

        public List<Designation> Get(EnumStatus status, string sIDs, string sTargetPropertyName, int payrollTypeID)
        {
            List<Designation> grades = new List<Designation>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DesignationDA.Get(tc, status, sIDs, sTargetPropertyName, payrollTypeID));
                grades = this.CreateObjects<Designation>(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 grades;
        }

        public int Save(Designation oDesignation)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oDesignation.IsNew)
                {
                    int id = tc.GenerateID("Designation", "DesignationID");
                    int seqID = tc.GenerateID("Designation", "SequenceNO");
                    base.SetObjectID(oDesignation, id);
                    oDesignation.Sequence = seqID;
                    DesignationDA.Insert(tc, oDesignation, oDesignation.PayrollTypeID);
                }
                else
                {
                    DesignationDA.Update(tc, oDesignation, oDesignation.PayrollTypeID);
                }

                tc.End();
                return oDesignation.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, int UserID)
        //{
        //    TransactionContext tc = null;
        //    try
        //    {
        //        tc = TransactionContext.Begin(true);
        //        DesignationDA.Delete(tc, id , UserID);
        //        tc.End();
        //    }
        //    catch (Exception e)
        //    {
        //        #region Handle Exception
        //        if (tc != null)
        //            tc.HandleError();
        //        ExceptionLog.Write(e);
        //        throw new ServiceException(e.Message, e);
        //        #endregion
        //    }
        //}

        #endregion

        //For excel Upload
        public static void SaveForUpload(TransactionContext tc, List<Designation> designations, int payrollTypeID, List<Designation> saveItems)
        {
            try
            {
                foreach (Designation oDesignation in designations)
                {
                    if (saveItems.FindIndex(x => x.ID == oDesignation.ID) < 0)
                    {
                        int seqNo = tc.GenerateID("Designation", "SequenceNO");
                        oDesignation.Sequence = seqNo;


                        if (oDesignation.Code == string.Empty)
                        oDesignation.Code = GlobalFunctionService.GetMaxCode(tc,  "Designation", "Code");

                        oDesignation.CreatedBy = oDesignation.CreatedBy;
                        oDesignation.CreatedDate = DateTime.Now;
                        DesignationDA.Insert(tc, oDesignation, payrollTypeID);
                    }
                    else
                    {
                        //oDesignation.ModifiedBy = oDesignation.ModifiedBy;
                        //oDesignation.ModifiedDate = DateTime.Now;
                        //DesignationDA.Update(tc, oDesignation, payrollTypeID);
                    }
                }
            }
            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);
                DesignationDA.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 List<Designation> GetByDesignationIDs(string designationIds)
        {
            List<Designation> designations = new List<Designation>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DesignationDA.GetByDesignationIDs(tc, designationIds));
                designations = this.CreateObjects<Designation>(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 designations;
        }
        public List<Designation> GetAllDesignation(int payrollTypeID, EnumStatus status, string code, string name)
        {
            List<Designation> designations = new List<Designation>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DesignationDA.GetAllDesignation(tc, payrollTypeID, status, code, name));
                designations = this.CreateObjects<Designation>(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 designations;
        }
        public List<Designation> GetAll(int payrollTypeID)
        {
            List<Designation> departments = null;
            TransactionContext tc = null;

            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DesignationDA.GetAll(tc, payrollTypeID));
                departments = this.CreateObjects<Designation>(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 departments;
        }
    }
}