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

namespace HRM.DA
{
    #region Loan Service

    public class LoanService : ServiceTemplate, ILoanService
    {
        public LoanService()
        {
        }

        private void MapObject(Loan oLoan, DataReader oReader)
        {
            base.SetObjectID(oLoan, oReader.GetInt32("LoanID").Value);
            oLoan.Name = oReader.GetString("description");
            oLoan.NameInBangla = oReader.GetString("descriptioninbangla", true, null);
            oLoan.IsConfirmationRequired = oReader.GetBoolean("isConfirmationRequired").HasValue
                ? oReader.GetBoolean("isConfirmationRequired").Value
                : false;
            oLoan.InterestRate = oReader.GetDouble("interestRate").Value;
            oLoan.Sequence = oReader.GetInt32("SequenceNo").Value;
            //oLoan.LoanGroup = (EnumLoanGroup)oReader.GetInt32("LoanGroup").Value;
            oLoan.LoanGroup = (EnumLoanGroup)oReader.GetInt32("LoanGroup", true, 0);
            //oLoan.calculationType = (EnumLoanFraction)oReader.GetInt32("calculationType").Value;
            oLoan.calculationType = (EnumLoanFraction)oReader.GetInt32("calculationType", true, 0);
            oLoan.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            oLoan.CreatedBy = oReader.GetString("CreatedBy") == null ? 0 : oReader.GetInt32("CreatedBy").Value;
            oLoan.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            oLoan.ModifiedBy = oReader.GetString("ModifiedBy") == null ? 0 : oReader.GetInt32("ModifiedBy").Value;
            oLoan.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oLoan, Ease.Core.ObjectState.Saved);
        }

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

        protected Loan CreateObject(DataReader oReader)
        {
            Loan oLoan = new Loan();
            MapObject(oLoan, oReader);
            return oLoan;
        }

        #region Service implementation

        public Loan Get(int id)
        {
            Loan oLoan = new Loan();

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

        public Loan Get(string loanNo)
        {
            Loan oLoan = new Loan();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(LoanDA.Get(tc, loanNo));
                if (oreader.Read())
                {
                    oLoan = this.CreateObject<Loan>(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 oLoan;
        }

        public List<Loan> Get(EnumStatus status)
        {
            List<Loan> loans = new List<Loan>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(LoanDA.Get(tc, status));
                loans = this.CreateObjects<Loan>(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 loans;
        }

        public int Save(Loan oLoan)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oLoan.IsNew)
                {
                    int id = tc.GenerateID("LOANBASIC", "LoanID");
                    base.SetObjectID(oLoan, id);
                    int seqNo = tc.GenerateID("LOANBASIC", "SequenceNO");
                    oLoan.Sequence = seqNo;
                    LoanDA.Insert(tc, oLoan);
                }
                else
                {
                    LoanDA.Update(tc, oLoan);
                }


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