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 ReligionService : ServiceTemplate, IReligionService
    {
        public ReligionService()
        {
        }

        private void MapObject(Religion oReligion, DataReader oReader)
        {
            base.SetObjectID(oReligion, oReader.GetInt32("ReligionID").Value);
            oReligion.Code = oReader.GetString("code");
            oReligion.Name = oReader.GetString("name");
            oReligion.NameInBangla = oReader.GetString("nameinbangla", true, null);
            oReligion.Sequence = oReader.GetInt32("SequenceNO").Value;
            oReligion.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            oReligion.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oReligion.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            oReligion.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oReligion.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oReligion, Ease.Core.ObjectState.Saved);
        }

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

        #region Service implementation

        public string GetNextCode()
        {
            TransactionContext tc = null;
            string _code = "";
            try
            {
                tc = TransactionContext.Begin();
                //####      _code = GlobalFunctionService.GetMaxCode(tc, "religion", "codeautogenerate", "Religion", "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 Religion Get(int id)
        {
            Religion oReligion = null;

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

        public List<Religion> Get(EnumStatus status)
        {
            List<Religion> grades = new List<Religion>();

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

                DataReader dr = new DataReader(ReligionDA.Get(tc, status));
                grades = this.CreateObjects<Religion>(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 List<Religion> Get(EnumStatus status, int payrolltypeid)
        {
            List<Religion> grades = new List<Religion>();

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

                DataReader dr = new DataReader(ReligionDA.Get(tc, status, payrolltypeid));
                grades = this.CreateObjects<Religion>(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 Religion Get(TransactionContext tc, int id)
        {
            Religion oReligion = null;
            try
            {
                DataReader oreader = new DataReader(ReligionDA.Get(tc, id));
                if (oreader.Read())
                {
                    oReligion = this.CreateObject<Religion>(oreader);
                }

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

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

                #endregion
            }

            return oReligion;
        }


        public Religion Get(string sCode)
        {
            Religion oReligion = null;

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

        public List<Religion> Get()
        {
            List<Religion> religions = new List<Religion>();

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

                DataReader dr = new DataReader(ReligionDA.Get(tc));
                religions = this.CreateObjects<Religion>(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 religions;
        }


        //public List<Religion> Get(EnumStatus status)
        //{
        //    List<Religion> religions = new List<Religion>();

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

        //        DataReader dr = new DataReader(ReligionDA.Get(tc, status));
        //        religions = this.CreateObjects<Religion>(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 religions;
        //}

        public int Save(Religion oReligion)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oReligion.IsNew)
                {
                    int id = tc.GenerateID("Religion", "ReligionID");
                    base.SetObjectID(oReligion, id);
                    if (oReligion.Code == "")
                        oReligion.Code = oReligion.ID.ToString();
                    ReligionDA.Insert(tc, oReligion);
                }
                else
                {
                    ReligionDA.Update(tc, oReligion);
                }

                tc.End();
                return oReligion.ID;
            }
            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<Religion> religions, List<Religion>  saveItems)
        {
            try
            {
                foreach (Religion oReligion in religions)
                {
                    if (saveItems.FindIndex(x => x.ID == oReligion.ID) < 0)
                    {
                        int seqNo = tc.GenerateID("Religion", "SequenceNo");
                        oReligion.Sequence = seqNo;

                        //bool isAutoGenerated = ConfigurationManager.GetBoolValue("religion", "codeautogenerate",
                        //    EnumConfigurationType.Logic);
                        if (oReligion.Code ==string.Empty)
                            oReligion.Code = GlobalFunctionService.GetMaxCode(tc, "Religion", "Code");

                        oReligion.Status = EnumStatus.Active;
                        oReligion.CreatedBy = oReligion.CreatedBy;
                        oReligion.CreatedDate = DateTime.Now;
                        ReligionDA.Insert(tc, oReligion);
                    }
                    else
                    {
                        oReligion.ModifiedBy = oReligion.ModifiedBy;
                        oReligion.ModifiedDate = DateTime.Now;
                        ReligionDA.Update(tc, oReligion);
                    }
                }
            }
            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);
                ReligionDA.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
            }

//            throw new NotImplementedException();
        }
    }
}