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

namespace HRM.DA
{
    #region OPIPayment Service

    public class OPIPaymentService : ServiceTemplate, IOPIPaymentService
    {
        #region Private functions and declaration

        public OPIPaymentService()
        {
        }

        private void MapObject(OPIPayment oOPIPayment, DataReader oReader)
        {
            base.SetObjectID(oOPIPayment, oReader.GetInt32("OPIPaymentID").Value);
            oOPIPayment.OPIItemID = oReader.GetString("OPIItemID") == null ? 0 : oReader.GetInt32("OPIItemID").Value;
            oOPIPayment.EmployeeID = oReader.GetString("EmployeeID") == null ? 0 : oReader.GetInt32("EmployeeID").Value;
            oOPIPayment.PaymentDate = oReader.GetDateTime("PaymentDate").Value;
            oOPIPayment.Amount = oReader.GetDouble("Amount").Value;
            oOPIPayment.TaxAmount = oReader.GetDouble("TaxAmount").Value;
            oOPIPayment.Remarks = oReader.GetString("Remarks");
            oOPIPayment.CreatedBy = oReader.GetString("CreatedBy") == null ? 0 : oReader.GetInt32("CreatedBy").Value;
            oOPIPayment.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            oOPIPayment.ModifiedBy = oReader.GetString("ModifiedBy") == null ? 0 : oReader.GetInt32("ModifiedBy").Value;
            oOPIPayment.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oOPIPayment, Ease.Core.ObjectState.Saved);
        }

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

        protected OPIPayment CreateObject(DataReader oReader)
        {
            OPIPayment oOPIPayment = new OPIPayment();
            MapObject(oOPIPayment, oReader);
            return oOPIPayment;
        }

        #endregion

        #region Service implementation

        public OPIPayment Get(int id)
        {
            OPIPayment oOPIPayment = new OPIPayment();

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

        public List<OPIPayment> Get()
        {
            List<OPIPayment> oOPIPayments = new List<OPIPayment>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(OPIPaymentDA.Get(tc));
                oOPIPayments = this.CreateObjects<OPIPayment>(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 oOPIPayments;
        }

        public DataSet GetDataSetOfPaymentEmployees(int nOpiItemID, EnumOpiType nOpiType)
        {
            DataSet dsEmployees = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                dsEmployees = OPIPaymentDA.GetDataSetOfPaymentEmployees(tc, nOpiItemID, nOpiType);
                tc.End();
            }
            catch (Exception e)
            {
                throw new ServiceException("Failed to Get Employee ", e);
            }

            return dsEmployees;
        }

        public int Save(OPIPayment oOPIPayment)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oOPIPayment.IsNew)
                {
                    int id = tc.GenerateID("OPIPayment", "OPIPaymentID");
                    base.SetObjectID(oOPIPayment, id);
                    OPIPaymentDA.Insert(tc, oOPIPayment);
                }
                else
                {
                    OPIPaymentDA.Update(tc, oOPIPayment);
                }

                tc.End();
                return oOPIPayment.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<OPIPayment> opiPayments)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                foreach (OPIPayment oPayment in opiPayments)
                {
                    int id = tc.GenerateID("OPIPayment", "OPIPaymentID");
                    base.SetObjectID(oPayment, id);
                    oPayment.CreatedBy = oPayment.CreatedBy;
                    oPayment.CreatedDate = DateTime.Now;
                    OPIPaymentDA.Insert(tc, oPayment);
                }

                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);
                OPIPaymentDA.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
    }

    #endregion
}