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

namespace HRM.DA
{
    public class FundService : ServiceTemplate //, IFundService
    {
        public FundService()
        {
        }

        private void MapObject(FmFund fund, DataReader dr)
        {
            base.SetObjectID(fund, dr.GetInt32("FundTypeID").Value);
            fund.UserObjectName = dr.GetString("UserObjectName");
            fund.Code = dr.GetString("Code");
            fund.Description = dr.GetString("Description");
            fund.CreatedBy = dr.GetInt32("CreatedBy").Value;
            fund.CreatedDate = dr.GetDateTime("CreatedDate").Value;
            fund.ModifiedBy = dr.GetInt32("ModifiedBy").Value;
            fund.ModifiedDate = dr.GetDateTime("ModifiedDate");
            fund.ProjectID = dr.GetInt32("ProjectID").Value;
            base.SetObjectState(fund, Ease.Core.ObjectState.Saved);
        }

        protected override T CreateObject<T>(DataReader dr)
        {
            FmFund fund = new FmFund();

            MapObject(fund, dr);

            return fund as T;
        }

        #region Service Implementation

        #region Insert

        public void Save(FmFund fund)
        {
            FmFund oFund = new FmFund();
            oFund = fund;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);

                if (oFund.IsNew)
                {
                    int id = tc.GenerateID("Fund", "FundTypeID");
                    base.SetObjectID(oFund, id);
                    FmFundDA.Insert(tc, oFund);
                }
                else
                {
                    FmFundDA.Update(tc, oFund);
                }

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

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

                throw new ServiceException(e.Message, e);

                #endregion
            }
        }

        #endregion

        #region Delete

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

        #region Get(By fundID)

        public FmFund Get(int fundID)
        {
            FmFund fund = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(FmFundDA.Get(tc, fundID));
                if (dr.Read())
                {
                    fund = this.CreateObject<FmFund>(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 fund;
        }

        #endregion

        #region Get()

        public List<FmFund> Get()
        {
            List<FmFund> funds = new List<FmFund>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(FmFundDA.Get(tc));
                funds = this.CreateObjects<FmFund>(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 funds;
        }

        #endregion

        #region Internal Get()

        internal List<FmFund> Get(TransactionContext tc)
        {
            List<FmFund> funds = new List<FmFund>();
            try
            {
                DataReader dr = new DataReader(FmFundDA.Get(tc));
                funds = this.CreateObjects<FmFund>(dr);
                dr.Close();
            }
            catch (Exception e)
            {
                throw e;
            }

            return funds;
        }

        #endregion

        #region GetByProjectID

        public List<FmFund> GetByProjectID(int projectID)
        {
            List<FmFund> funds = new List<FmFund>();

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

                DataReader dr = new DataReader(FmFundDA.GetByProjectID(tc, projectID));
                funds = this.CreateObjects<FmFund>(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 funds;
        }

        #endregion

        #region GetTable

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

            //    IDataReader ir = FundDA.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

        #endregion

        #region Internal function

        internal FmFund Get(TransactionContext tc, string desc)
        {
            FmFund fund = null;

            try
            {
                DataReader dr = new DataReader(FmFundDA.Get(tc, desc));
                if (dr.Read())
                {
                    fund = this.CreateObject<FmFund>(dr);
                }

                dr.Close();
            }
            catch (Exception e)
            {
                throw e;
            }

            return fund;
        }

        #endregion
    }
}