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

namespace HRM.DA
{
    public class LoanCategoryService : ServiceTemplate //, ILoanCategoryService
    {
        public LoanCategoryService()
        {
        }

        #region Function

        #region Child Function

        //private void MapFundRelation(LoanCategory.LoanCategoryFundRelation fundRelation, DataReader dr)
        //{
        //    fundRelation.LoanCategoryID = dr.GetID("LoanCategoryID");
        //    fundRelation.FundTypeID = dr.GetID("FundTypeID");
        //    fundRelation.ProjectID = dr.GetID("ProjectID");
        //    base.SetObjectID(fundRelation, dr.GetID("LoanCategoryID", "FundTypeID"));
        //    base.SetObjectState(fundRelation, ObjectState.Saved);
        //}
        //private LoanCategory.LoanCategoryFundRelation CreateFundRelations(DataReader dr)
        //{
        //    LoanCategory.LoanCategoryFundRelation fundRelation = new LoanCategory.LoanCategoryFundRelation();
        //    MapFundRelation(fundRelation, dr);
        //    return fundRelation;
        //}

        //private void FillFundRelations(LoanCategory parent, DataReader dr)
        //{
        //    while (dr.Read())
        //    {
        //        LoanCategory.LoanCategoryFundRelation item = this.CreateFundRelations(dr);
        //        parent.LoanFundRelations.Add(item);
        //    }
        //}
        //internal void loadChildren(TransactionContext tc, LoanCategory loanCategory)
        //{
        //    try
        //    {
        //        if (loanCategory != null && loanCategory.ID != null && loanCategory.ID != 0)
        //        {
        //            //Get FundRelations
        //            DataReader dr = new DataReader(LoanCategoryDA.GetFundRelations(tc, loanCategory.ID));
        //            FillFundRelations(loanCategory, dr);
        //            dr.Close();
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        throw e;
        //    }
        //}

        #endregion

        private void MapObject(LoanCategory loanCategory, DataReader dr)
        {
            base.SetObjectID(loanCategory, dr.GetInt32("LoanCategoryID").Value);
            loanCategory.DefaultInterestRate = dr.GetDouble("DefaultInterestRate").Value;
            loanCategory.LoanCategoryCode = dr.GetString("LoanCategoryCode");
            loanCategory.Description = dr.GetString("Description");
            loanCategory.UserObjectName = dr.GetString("UserObjectName");
            loanCategory.IsActive = (EnumLoanCategoryStatus)dr.GetInt16("IsActive").Value;
            loanCategory.MaxAlowableFlatAmount = dr.GetDouble("MaxAlowableFlatAmount");
            loanCategory.MaxInstallment = dr.GetInt32("MaxInstallment").Value;
            loanCategory.MinInstallment = dr.GetInt32("MinInstallment").Value;
            loanCategory.PercentageofFund = dr.GetDouble("PercentageofFund");
            loanCategory.Version = dr.GetInt16("Version").Value;
            loanCategory.CreatedBy = dr.GetInt32("CreatedBy").Value;
            loanCategory.CreatedDate = dr.GetDateTime("CreatedDate").Value;
            loanCategory.ModifiedBy = dr.GetInt32("ModifiedBy");
            loanCategory.ModifiedDate = dr.GetDateTime("ModifiedDate").Value;
            loanCategory.ProjectID = dr.GetInt32("ProjectID").Value;
            base.SetObjectState(loanCategory, Ease.Core.ObjectState.Saved);
        }

        protected override T CreateObject<T>(DataReader dr)
        {
            LoanCategory loanCategory = new LoanCategory();

            MapObject(loanCategory, dr);

            return loanCategory as T;
        }

        protected LoanCategory CreateObject(DataReader oReader)
        {
            LoanCategory loanCategory = new LoanCategory();
            MapObject(loanCategory, oReader);
            return loanCategory;
        }
        //internal void SaveFundRelation(TransactionContext tc, LoanCategory loanCategory)
        //{
        //    try
        //    {
        //        LoanCategoryDA.Delete(tc, loanCategory.ID);

        //        //Insert fundRelations

        //        foreach (var item in loanCategory.LoanFundRelations)
        //        {
        //            item.LoanCategoryID = loanCategory.ID;
        //            base.SetObjectID(item, ID.FromComplex(item.LoanCategoryID.Integer, item.FundTypeID.Integer));
        //            LoanCategoryDA.Insert(tc, item);
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        throw e;
        //    }
        //}

        #endregion

        #region Service Implementation

        #region save

        //public void Insert(LoanCategory loanCategory)
        //{
        //    TransactionContext tc = null;
        //    try
        //    {
        //        tc = TransactionContext.Begin(true);
        //        if (!loanCategory.IsNew)
        //        {

        //            if (!loanCategory.IsModified)
        //            {
        //                SaveFundRelation(tc, loanCategory);
        //                tc.End();
        //                return;
        //            }
        //            else
        //            {
        //                //tc = TransactionContext.Begin(true);
        //                loanCategory.Version = 1;
        //                //loanCategory.IsActive = EnumLoanCategoryStatus.Inactive;
        //                LoanCategoryDA.Update(tc, loanCategory);
        //                //loanCategory.IsActive = EnumLoanCategoryStatus.Active;
        //                loanCategory.Version = 0;
        //            }
        //        }
        //        int id = base.GetNewID(tc, "LoanCategory", "LoanCategoryID");
        //        base.SetObjectID(loanCategory, ID.FromInteger(id));
        //        LoanCategoryDA.Insert(tc, loanCategory);
        //        SaveFundRelation(tc, loanCategory);
        //        tc.End();
        //        CacheInfo.ClearCache(typeof(LoanCategoryService).FullName);
        //    }
        //    catch (Exception e)
        //    {
        //        #region Handle Exception

        //        if (tc != null)
        //            tc.HandleError();
        //        ExceptionLog.Write(e);

        //        throw new ServiceException(e.Message, e);

        //        #endregion
        //    }
        //}

        #endregion

        #region Get()

        public LoanCategory Get(int loanCategoryID, bool isGetChild)
        {
            LoanCategory loanCategory = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(LoanCategoryDA.Get(tc, loanCategoryID));
                if (dr.Read())
                {
                    loanCategory = this.CreateObject<LoanCategory>(dr);
                }

                dr.Close();
                //if (isGetChild)
                //{
                //    loadChildren(tc, loanCategory);
                //}
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return loanCategory;
        }
        //public LoanCategory Get(string loanCategoryCode, bool isGetChild)
        //{
        //    #region Cache Header
        //    LoanCategory loanCategory = _cache["Get", loanCategoryCode, isGetChild] as LoanCategory;
        //    if (loanCategory != null)
        //        return loanCategory;
        //    #endregion

        //    TransactionContext tc = null;
        //    try
        //    {
        //        tc = TransactionContext.Begin();
        //        DataReader dr = new DataReader(LoanCategoryDA.Get(tc, loanCategoryCode));
        //        if (dr.Read())
        //        {
        //            loanCategory = this.CreateObject<LoanCategory>(dr);
        //        }
        //        dr.Close();
        //        if (isGetChild)
        //        {
        //            loadChildren(tc, loanCategory);
        //        }
        //        tc.End();
        //    }
        //    catch (Exception e)
        //    {
        //        #region Handle Exception
        //        if (tc != null)
        //            tc.HandleError();
        //        ExceptionLog.Write(e);

        //        throw new ServiceException(e.Message, e);
        //        #endregion
        //    }

        //    #region Cache Footer
        //    _cache.Add(loanCategory, "Get", loanCategoryCode, isGetChild);
        //    #endregion

        //    return loanCategory;
        // }
        public List<LoanCategory> Get()
        {
            List<LoanCategory> loanCategorys = new List<LoanCategory>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(LoanCategoryDA.Get(tc));
                loanCategorys = base.CreateObjects<LoanCategory>(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 loanCategorys;
        }

        public List<LoanCategory> Get(EnumLoanCategoryStatus status)
        {
            List<LoanCategory> loanCategorys = new List<LoanCategory>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(LoanCategoryDA.Get(tc, status));
                loanCategorys = base.CreateObjects<LoanCategory>(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 loanCategorys;
        }

        #endregion

        #region GetTable

        public DataTable GetTable()
        {
            DataTable dTbl = new DataTable("LoanCategorys");
            //TransactionContext tc = null;
            //try
            //{
            //    tc = TransactionContext.Begin();

            //    IDataReader ir = LoanCategoryDA.Get(tc);
            //    dTbl.Load(ir);
            //    ir.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 dTbl;
        }

        #endregion

        #region Change Status

        public void ChangeStatus(LoanCategory loanCategory, EnumLoanCategoryStatus status)
        {
            TransactionContext tc = null;
            try
            {
                //int activeStatus = Convert.ToInt16(status);
                tc = TransactionContext.Begin(true);
                //loanCategory.Version = 1;
                LoanCategoryDA.ChangeStatus(tc, loanCategory, status);

                tc.End();
                // CacheInfo.ClearCache(typeof(LoanCategoryService).FullName);
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }
        }

        #endregion

        #region IsExists

        public bool IsExist(string code, int version)
        {
            bool exists = false;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                exists = LoanCategoryDA.IsExist(tc, code, version);
                tc.End();
            }
            catch (Exception e)
            {
                throw new ServiceException("Failed to Check by code" + e.Message, e);
            }

            return exists;
        }

        #endregion

        #region IsIssued

        public bool IsIssued(int loancatID)
        {
            bool isIssued = false;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                isIssued = LoanCategoryDA.IsIssued(tc, loancatID);
                tc.End();
            }
            catch (Exception e)
            {
                throw new ServiceException("Failed to Check by ID" + e.Message, e);
            }

            return isIssued;
        }

        #endregion


        #region Update Activity

        internal void UpdateActivityInfo(TransactionContext tc, int ActivityID, int RecordID)
        {
            try
            {
                LoanCategoryDA.UpdateActivity(tc, ActivityID, RecordID);
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }
        }

        #endregion

        #endregion
    }
}