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

namespace HRM.DA
{
    #region PFTransaction Service

    public class PFTransactionService : ServiceTemplate
    {
        #region Private functions and declaration

        #endregion

        public PFTransactionService()
        {
        }

        private void MapObject(PFTransaction oPFTransaction, DataReader oReader)
        {
            base.SetObjectID(oPFTransaction, oReader.GetInt32("PFTRANID").Value);
            oPFTransaction.EmployeeID =
                oReader.GetString("EmployeeID") == null ? 0 : oReader.GetInt32("EmployeeID").Value;
            oPFTransaction.MonthDate = oReader.GetDateTime("tranDate").Value;
            oPFTransaction.TranAmount = oReader.GetDouble("tranAmount").Value;
            oPFTransaction.TranType = (EnumPFTranType)oReader.GetInt32("tranType").Value;
            oPFTransaction.CreatedBy = oReader.GetString("CreatedBy") == null ? 0 : oReader.GetInt32("CreatedBy").Value;
            oPFTransaction.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            this.SetObjectState(oPFTransaction, Ease.Core.ObjectState.Saved);
        }

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

        protected PFTransaction CreateObject(DataReader oReader)
        {
            PFTransaction oPFTransaction = new PFTransaction();
            MapObject(oPFTransaction, oReader);
            return oPFTransaction;
        }

        #region Service implementation

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

                DataReader dr = new DataReader(PFTransactionDA.Get(tc));
                pFTransactions = this.CreateObjects<PFTransaction>(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 pFTransactions;
        }

        public double GetPFAmount(int nEmpID)
        {
            double nAmount = 0.0;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                nAmount = PFTransactionDA.GetPFAmount(tc, nEmpID);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return nAmount;
        }

        public DataSet GetPFBalance(int nEmpID, DateTime saalryMonth)
        {
            DataSet pfBalance = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                pfBalance = PFTransactionDA.GetPFBalance(tc, nEmpID, saalryMonth);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return pfBalance;
        }

        public DataSet GetPFBalance(DateTime saalryMonth)
        {
            DataSet pfBalance = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                pfBalance = PFTransactionDA.GetPFBalance(tc, saalryMonth);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return pfBalance;
        }

        public DataSet GetPFBalanceBAT(DateTime fromDate, DateTime toDate)
        {
            DataSet pfBalance = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                pfBalance = PFTransactionDA.GetPFBalanceBAT(tc, fromDate, toDate);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return pfBalance;
        }

        public int Save(PFTransaction oPFTransaction)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oPFTransaction.IsNew)
                {
                    int id = tc.GenerateID("PFTransaction", "PfTranID");
                    base.SetObjectID(oPFTransaction, id);
                    PFTransactionDA.Insert(tc, oPFTransaction);
                }
                else
                {
                    PFTransactionDA.Update(tc, oPFTransaction);
                }

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

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

                #endregion
            }
        }

        public void SaveAll(List<PFTransaction> oTrans)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                foreach (PFTransaction oItem in oTrans)
                {
                    PFTransactionDA.Delete(tc, oItem.EmployeeID, oItem.MonthDate, oItem.TranType);
                }

                foreach (PFTransaction oItem in oTrans)
                {
                    int id = tc.GenerateID("PFTransaction", "PfTranID");
                    base.SetObjectID(oItem, id);
                    PFTransactionDA.Insert(tc, oItem);
                }

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

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

                #endregion
            }
        }

        public static DataTable ToDataTable<T>(List<T> items)
        {
            DataTable dataTable = new DataTable(typeof(T).Name);

            //Get all the properties
            PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in Props)
            {
                //Setting column names as Property names
                dataTable.Columns.Add(prop.Name);
            }

            foreach (T item in items)
            {
                var values = new object[Props.Length];
                for (int i = 0; i < Props.Length; i++)
                {
                    //inserting property values to datatable rows
                    values[i] = Props[i].GetValue(item, null);
                }

                dataTable.Rows.Add(values);
            }

            //put a breakpoint here and check datatable
            return dataTable;
        }

        public int Save(TransactionContext tc, PFTransaction oPFTransaction)
        {
            try
            {
                if (oPFTransaction.IsNew)
                {
                    PFTransactionDA.Insert(tc, oPFTransaction);
                }
                else
                {
                    PFTransactionDA.Update(tc, oPFTransaction);
                }

                return oPFTransaction.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 employeeId, DateTime month, EnumPFTranType type)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                PFTransactionDA.Delete(tc, employeeId, month, type);
                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
}