using Ease.Core.Model;
using Ease.Core.DataAccess;
using HRM.BO;
using Ease.Core.Utility;
using System.Collections.Generic;
using System.Data;
using Ease.Core;
using Payroll.Service;
using System;

namespace HRM.DA
{
    public class TaxAdjustmentService : ServiceTemplate, ITaxAdjustmentService
    {
        public TaxAdjustmentService()
        {
        }

        private void MapObject(TaxAdjustment oTaxAdjustment, DataReader oReader)
        {
            base.SetObjectID(oTaxAdjustment, oReader.GetInt32("TaxAdjustmentID").Value);
            oTaxAdjustment.Name = oReader.GetString("name");
            oTaxAdjustment.TaxType = (EnumIncomeTaxSide)oReader.GetInt32("TaxType").Value;
            oTaxAdjustment.Sequence = oReader.GetInt32("SequenceNO").Value;
            oTaxAdjustment.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            oTaxAdjustment.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oTaxAdjustment.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            //oTaxAdjustment.ModifiedDate = DataReader.GetNullValue(oReader.GetDateTime("ModifiedDate").Value);
            this.SetObjectState(oTaxAdjustment, Ease.Core.ObjectState.Saved);
        }

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

        #region Service implementation

        public TaxAdjustment Get(int id)
        {
            TaxAdjustment oTaxAdjustment = null;

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

        public List<TaxAdjustment> Get()
        {
            List<TaxAdjustment> taxAdjustments = new List<TaxAdjustment>();

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

                DataReader dr = new DataReader(TaxAdjustmentDA.Get(tc));
                taxAdjustments = this.CreateObjects<TaxAdjustment>(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 taxAdjustments;
        }

        public List<TaxAdjustment> Get(EnumStatus status)
        {
            List<TaxAdjustment> TaxAdjustments = null;

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

                DataReader dr = new DataReader(TaxAdjustmentDA.Get(tc, status));
                TaxAdjustments = this.CreateObjects<TaxAdjustment>(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 TaxAdjustments;
        }

        public int Save(TaxAdjustment oTaxAdjustment)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oTaxAdjustment.IsNew)
                {
                    int id = tc.GenerateID("TaxAdjustment", "TaxAdjustmentID");
                    base.SetObjectID(oTaxAdjustment, (id));
                    int seqNo = tc.GenerateID("TaxAdjustment", "SequenceNo");
                    oTaxAdjustment.Sequence = seqNo;
                    TaxAdjustmentDA.Insert(tc, oTaxAdjustment);
                }
                else
                {
                    TaxAdjustmentDA.Update(tc, oTaxAdjustment);
                }

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