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

        private void MapObject(SuccessionRole oSuccessionRole, DataReader oReader)
        {
            base.SetObjectID(oSuccessionRole, oReader.GetInt32("SuccessionRoleID").Value);
            oSuccessionRole.Code = oReader.GetString("Code");
            oSuccessionRole.Name = oReader.GetString("Name");
            oSuccessionRole.PayrollTypeID = oReader.GetInt32("PayrollTypeID", 0);
            oSuccessionRole.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            oSuccessionRole.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oSuccessionRole.CreatedDate = oReader.GetDateTime("CreatedDate") == null
                ? DateTime.Today
                : oReader.GetDateTime("CreatedDate").Value;
            oSuccessionRole.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oSuccessionRole.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oSuccessionRole, Ease.Core.ObjectState.Saved);
        }

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

        #region Service implementation

        public string GetNextCode()
        {
            TransactionContext tc = null;
            string _code = "";
            try
            {
                tc = TransactionContext.Begin();
                _code = GlobalFunctionService.GetMaxCode(tc, "successionRole", "codeautogenerate", "SuccessionRole", "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 SuccessionRole Get(TransactionContext tc, int id)
        {
            SuccessionRole oSuccessionRole = null;
            try
            {
                DataReader oreader = new DataReader(SuccessionRoleDA.Get(tc, id));
                if (oreader.Read())
                {
                    oSuccessionRole = this.CreateObject<SuccessionRole>(oreader);
                }

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

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

                #endregion
            }

            return oSuccessionRole;
        }

        public SuccessionRole Get(int id)
        {
            SuccessionRole oSuccessionRole = null;

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

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

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

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



        public int GetMaxID()
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                return tc.GenerateID("SuccessionRole", "SuccessionRoleID") - 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<SuccessionRole> GetAll()
        {
            List<SuccessionRole> roles = new List<SuccessionRole>();

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

                DataReader dr = new DataReader(SuccessionRoleDA.GetAll(tc));
                roles = this.CreateObjects<SuccessionRole>(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 roles;
        }

     

        public int Save(SuccessionRole oSuccessionRole)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oSuccessionRole.IsNew)
                {
                    int id = tc.GenerateID("SuccessionRole", "SuccessionRoleID");
                    base.SetObjectID(oSuccessionRole, id);
                    SuccessionRoleDA.Insert(tc, oSuccessionRole);
                }
                else
                {
                    SuccessionRoleDA.Update(tc, oSuccessionRole);
                }

                tc.End();
                return oSuccessionRole.ID;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

     
        #endregion

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