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

namespace HRM.DA
{
    public class ITEmpHeadService : ServiceTemplate, IITEmpHeadService
    {
        private EmployeeService employeeService = new EmployeeService();

        public ITEmpHeadService()
        {
        }

        private void MapObject(ITEmpHead oITEmpHead, DataReader oReader)
        {
            base.SetObjectID(oITEmpHead, oReader.GetInt32("ITEMPHEADID").Value);
            oITEmpHead.EmployeeID = oReader.GetInt32("EmployeeID", 0);
            oITEmpHead.EmployeeNoView = oReader.GetString("EmployeeNo", string.Empty);
            oITEmpHead.EmployeeNameView = oReader.GetString("Name", string.Empty);
            oITEmpHead.HeadID = (EnumIncomeTaxHead)oReader.GetInt32("HEADID");
            oITEmpHead.StartDate = oReader.GetDateTime("STARTDATE").Value;
            oITEmpHead.EndDate = oReader.GetDateTime("ENDDATE").Value;
            oITEmpHead.HouseRent = oReader.GetDouble("HOUSERENT").Value;
            oITEmpHead.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oITEmpHead.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            oITEmpHead.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oITEmpHead.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            oITEmpHead.TaxparamID = oReader.GetInt32("TaxparamID", 0);
            this.SetObjectState(oITEmpHead, Ease.Core.ObjectState.Saved);
        }

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

        #region Service implementation

        public ITEmpHead Get(int id)
        {
            ITEmpHead oITEmpHead = null;

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

        public List<ITEmpHead> GetWithTaxParam(int taxparamID)
        {
            List<ITEmpHead> ITEmpHeads = new List<ITEmpHead>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                ITEmpHeads= this.GetWithTaxParam(tc, taxparamID);
              
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return ITEmpHeads;
        }


        public List<ITEmpHead> GetWithTaxParam(TransactionContext tc, int taxparamID)
        {
            List<ITEmpHead> ITEmpHeads = new List<ITEmpHead>();

           try
            {

                DataReader dr = new DataReader(ITEmpHeadDA.GetWithTaxParam(tc, taxparamID));
                ITEmpHeads = this.CreateObjects<ITEmpHead>(dr);
                dr.Close();
            }
            catch (Exception e)
            {
            }

            return ITEmpHeads;
        }

        public int Save(ITEmpHead oITEmpHead)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oITEmpHead.IsNew)
                {
                    int id = tc.GenerateID("ITEmpHead", "ITEMPHEADID");
                    base.SetObjectID(oITEmpHead, (id));
                    ITEmpHeadDA.Insert(tc, oITEmpHead);
                }
                else
                {
                    ITEmpHeadDA.Update(tc, oITEmpHead);
                }

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

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

                #endregion
            }
        }

        public int Save2(ITEmpHead oITEmpHead)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);

                int id = tc.GenerateID("ITEmpHead", "ITEMPHEADID");
                base.SetObjectID(oITEmpHead, (id));
                ITEmpHeadDA.Insert(tc, oITEmpHead);
                tc.End();
                return oITEmpHead.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(ITEmpHead Item)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                ITEmpHeadDA.Delete(tc, Item);
                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 Private Finctions
        public List<ITEmpHead> Get(List<ITEmpHead> ITEmpHeads, int employeeId)
        {
            var ReturnItems = from item in ITEmpHeads
                              where item.EmployeeID == employeeId
                              select item;

            List<ITEmpHead> Items = new List<ITEmpHead>();
            foreach (ITEmpHead oi in ReturnItems)
            {
                Items.Add(oi);
            }
            return Items;
        }
        #endregion Private Finctions
    }
}