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

namespace HRM.DA
{
    [Serializable]
    public class DAMasterService :ServiceTemplate, IDAMasterService 
    {

        #region Map Functions

        private void MapObject(DAMaster oDAMaster, DataReader oReader)
        {
            base.SetObjectID(oDAMaster, oReader.GetInt32("DAMasterID").Value);
            oDAMaster.DACNote = oReader.GetString("DACNote");
            oDAMaster.DACOMFileName = oReader.GetString("DACOMFileName");
            oDAMaster.DAMCode = oReader.GetString("DAMCode");
            oDAMaster.DAMDate = (oReader.GetDateTime("DAMDate") ?? DateTime.MinValue);
            oDAMaster.DAMFileName = oReader.GetString("DAMFileName");
            oDAMaster.DAMNote = oReader.GetString("DAMNote");
            oDAMaster.DAPRIFileName = oReader.GetString("DAPRIFileName");
            oDAMaster.DCDate = (oReader.GetDateTime("DCDate") ?? DateTime.MinValue);
            oDAMaster.DPRIDate = (oReader.GetDateTime("DPRIDate") ?? DateTime.MinValue);
            oDAMaster.DACSheetFileName = oReader.GetString("DACSheetFileName");
            oDAMaster.DACSheetNote = oReader.GetString("DACSheetNote");
            oDAMaster.DCSheetDate = (oReader.GetDateTime("DCSheetDate") ?? DateTime.MinValue);
            this.SetObjectState(oDAMaster, Ease.Core.ObjectState.Saved);
        }
        
        protected override T CreateObject<T>(DataReader oReader)
        {
            DAMaster oDAMaster = new DAMaster();
            MapObject(oDAMaster, oReader);
            return oDAMaster as T;

        }

        #region Child Map

        #region DAEmployee Map
        private List<DAEmployee> CreateDAEmployeeObjects(DataReader oReader)
        {
            List<DAEmployee> oDAEmployees = new List<DAEmployee>();
            while (oReader.Read())
            {
                DAEmployee oDAEmployee = new DAEmployee();
                MapDAEmployeeObject(oDAEmployee, oReader);
                oDAEmployees.Add(oDAEmployee);
            }
            return oDAEmployees;
        }

        private void MapDAEmployeeObject(DAEmployee oDaEmployee, DataReader oReader)
        {
            base.SetObjectID(oDaEmployee, oReader.GetInt32("DAEMPID").Value);
            oDaEmployee.NAME = oReader.GetString("NAME");
            oDaEmployee.EMPLOYEENO = oReader.GetString("EMPLOYEENO");
            oDaEmployee.DESIGNATION = oReader.GetString("DESIGNATION");
            this.SetObjectState(oDaEmployee, Ease.Core.ObjectState.Saved);
        }
        #endregion

        #region DAComplain Map

        private List<DAComplain> CreateDAComplainObjects(DataReader oReader)
        {
            List<DAComplain> oDAComplains = new List<DAComplain>();
            while (oReader.Read())
            {
                DAComplain oDAComplain = new DAComplain();
                MapDAComplainObject(oDAComplain, oReader);
                oDAComplains.Add(oDAComplain);
            }
            return oDAComplains;
        }

        private void MapDAComplainObject(DAComplain oDaComplain, DataReader oReader)
        {
            base.SetObjectID(oDaComplain, oReader.GetInt32("DACompID").Value);
            oDaComplain.DAID = oReader.GetInt32("DAID").Value;
            oDaComplain.ComplainID = oReader.GetInt32("ComplainID").Value;
            this.SetObjectState(oDaComplain, Ease.Core.ObjectState.Saved);
        }
        #endregion

        #region DAEmpExplanation Map

        private List<DAEmpExplanation> CreateDAEmpExplanationObjects(DataReader oReader)
        {
            List<DAEmpExplanation> oDAEmpExplanations = new List<DAEmpExplanation>();
            while (oReader.Read())
            {
                DAEmpExplanation oDAEmpExplanation = new DAEmpExplanation();
                MapDAEmpExplanationObject(oDAEmpExplanation, oReader);
                oDAEmpExplanations.Add(oDAEmpExplanation);
            }
            return oDAEmpExplanations;
        }

        private void MapDAEmpExplanationObject(DAEmpExplanation oDAEmpExplanation, DataReader oReader)
        {
            base.SetObjectID(oDAEmpExplanation, oReader.GetInt32("DAExpID").Value);
            oDAEmpExplanation.DAID = oReader.GetInt32("DAID").Value;
            oDAEmpExplanation.DAEXNote = oReader.GetString("DAEXNote");
            oDAEmpExplanation.DAEXDate = oReader.GetDateTime("DAEXDate").Value;
            oDAEmpExplanation.EmployeeID = oReader.GetInt32("EmployeeID").Value;
            oDAEmpExplanation.FileName = oReader.GetString("FileName");
            this.SetObjectState(oDAEmpExplanation, Ease.Core.ObjectState.Saved);
        }
        #endregion

        #region DAPunishment Map

        private List<DAPunishment> CreateDAPunishmentObjects(DataReader oReader)
        {
            List<DAPunishment> oDAPunishments = new List<DAPunishment>();
            while (oReader.Read())
            {
                DAPunishment oDAPunishment = new DAPunishment();
                MapDAPunishmentObject(oDAPunishment, oReader);
                oDAPunishments.Add(oDAPunishment);
            }
            return oDAPunishments;
        }

        private void MapDAPunishmentObject(DAPunishment oDAPunishment, DataReader oReader)
        {
            base.SetObjectID(oDAPunishment, oReader.GetInt32("DAPID").Value);
            oDAPunishment.DAID = oReader.GetInt32("DAID").Value;
            oDAPunishment.DAPDate = oReader.GetDateTime("DAPDate").Value;
            oDAPunishment.DAPFromDate = oReader.GetDateTime("DAPFromDate").Value;
            oDAPunishment.DAPNote = oReader.GetString("DAPNote");
            oDAPunishment.DAPToDate = oReader.GetDateTime("DAPToDate").Value;
            oDAPunishment.EmployeeID = oReader.GetInt32("EmployeeID").Value;
            oDAPunishment.DAPFileName = oReader.GetString("DAPFileName");
            this.SetObjectState(oDAPunishment, Ease.Core.ObjectState.Saved);
        }
        #endregion

        #region DAComEmp Map

        private List<DAComEmp> CreateDAComEmpObjects(DataReader oReader)
        {
            List<DAComEmp> oDAComEmps = new List<DAComEmp>();
            while (oReader.Read())
            {
                DAComEmp oDAComEmp = new DAComEmp();
                MapDAComEmpObject(oDAComEmp, oReader);
                oDAComEmps.Add(oDAComEmp);
            }
            return oDAComEmps;
        }

        private void MapDAComEmpObject(DAComEmp oDAComEmp, DataReader oReader)
        {
            base.SetObjectID(oDAComEmp, oReader.GetInt32("DACEID").Value);
            oDAComEmp.DAID = oReader.GetInt32("DAID").Value;
            oDAComEmp.EmployeeID = oReader.GetInt32("EMPLOYEEID").Value;
            this.SetObjectState(oDAComEmp, Ease.Core.ObjectState.Saved);
        }
        #endregion

        #region DAPEmployee Map

        private List<DAPEmployee> CreateDAPEmployeeObjects(DataReader oReader)
        {
            List<DAPEmployee> oDAPEmployees = new List<DAPEmployee>();
            while (oReader.Read())
            {
                DAPEmployee oDAPEmployee = new DAPEmployee();
                MapDAPEmployeeObject(oDAPEmployee, oReader);
                oDAPEmployees.Add(oDAPEmployee);
            }
            return oDAPEmployees;
        }

        private void MapDAPEmployeeObject(DAPEmployee oDAPEmployee, DataReader oReader)
        {
            base.SetObjectID(oDAPEmployee, oReader.GetInt32("DAPEID").Value);
            oDAPEmployee.DAID = oReader.GetInt32("DAID").Value;
            oDAPEmployee.EmpID = oReader.GetInt32("EmpID").Value;
            oDAPEmployee.PID = oReader.GetInt32("PID").Value;
            oDAPEmployee.PunishmentID = oReader.GetInt32("PunishmentID").Value;
            this.SetObjectState(oDAPEmployee, Ease.Core.ObjectState.Saved);
        }
        #endregion

        #region DAPRIEmp Map

        private List<DAPRIEmp> CreateDAPRIEmpObjects(DataReader oReader)
        {
            List<DAPRIEmp> oDAPRIEmps = new List<DAPRIEmp>();
            while (oReader.Read())
            {
                DAPRIEmp oDAPRIEmp = new DAPRIEmp();
                MapDAPRIEmpObject(oDAPRIEmp, oReader);
                oDAPRIEmps.Add(oDAPRIEmp);
            }
            return oDAPRIEmps;
        }

        private void MapDAPRIEmpObject(DAPRIEmp oDAPRIEmp, DataReader oReader)
        {
            base.SetObjectID(oDAPRIEmp, oReader.GetInt32("DAPRIID").Value);
            oDAPRIEmp.DAID = oReader.GetInt32("DAID").Value;
            oDAPRIEmp.EmployeeID = oReader.GetInt32("EmployeeID").Value;
            this.SetObjectState(oDAPRIEmp, Ease.Core.ObjectState.Saved);
        }
        #endregion

        #endregion

        #endregion

        #region Implementation of IDAMasterService

        public DAMaster Get(int id)
        {
            DAMaster oDAMaster = new DAMaster();
            
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(DAMasterDA.Get(tc, id));
                if (oreader.Read())
                {
                    oDAMaster = this.CreateObject<DAMaster>(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 oDAMaster;
        }

        public DAMaster Get(string code)
        {
            DAMaster oDAMaster = new DAMaster();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(DAMasterDA.Get(tc, code));
                if (oreader.Read())
                {
                    oDAMaster = this.CreateObject<DAMaster>(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 oDAMaster;
        }

        public List<DAMaster> GetDAMasters(DateTime? fromDate, DateTime? toDate, string daCode)
        {
            List<DAMaster> DAMasters = new List<DAMaster>(); 
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DAMasterDA.GetDAMasterList(tc, fromDate, toDate, daCode));
                DAMasters = this.CreateObjects<DAMaster>(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 DAMasters;
        }

        public List<DAMaster> GetDAs(string sql)
        {

            #region

            List<DAMaster> daMasters = new List<DAMaster>();
            if (daMasters != null)
                return daMasters;

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

                DataReader dr = new DataReader(DAMasterDA.GetDAs(tc,sql));
                daMasters = this.CreateObjects<DAMaster>(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 daMasters;
        } 

        public int Save(DAMaster oDAMaster)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                
                if (oDAMaster.IsNew)
                {
                    if(DAMasterDA.CheckDAMasterDuplicateCode(tc,oDAMaster.DAMCode))
                    {
                        Exception exception=new Exception("Duplicate DAMaster Code found.Try another Code");
                        throw exception;
                    }
                    int id = tc.GenerateID("DAMaster", "DAMasterID");
                    base.SetObjectID(oDAMaster, id);
                    DAMasterDA.Insert(tc, oDAMaster);
                }
                else
                {
                    DAMasterDA.Update(tc, oDAMaster);

                    #region Delete Childs
                    DAMasterDA.DeleteDAEmployee(tc,oDAMaster.ID);
                    DAMasterDA.DeleteDAComplains(tc, oDAMaster.ID);
                    DAMasterDA.DeleteDAEmpExplanations(tc, oDAMaster.ID);
                    DAMasterDA.DeleteDAPEmployees(tc, oDAMaster.ID);
                    DAMasterDA.DeleteDAPunishments(tc, oDAMaster.ID);
                    DAMasterDA.DeleteDAComEmps(tc, oDAMaster.ID);
                    DAMasterDA.DeleteDAPRIEmps(tc, oDAMaster.ID);
                    #endregion
                }

                #region Insert Childs

                foreach (DAEmployee dAEmployee in oDAMaster.DAEmployees)
                {
                    dAEmployee.DAID = oDAMaster.ID;
                    base.SetObjectID(dAEmployee, tc.GenerateID("DAEmployee", "DAEMPID"));
                    DAMasterDA.InsertDAEmployee(tc,dAEmployee);
                }
                foreach (DAComplain dAComplain in oDAMaster.DAComplains)
                {
                    dAComplain.DAID = oDAMaster.ID;
                    base.SetObjectID(dAComplain,tc.GenerateID("DAComplain", "DACompID"));
                    DAMasterDA.InsertDAComplain(tc,dAComplain);
                }
              
                foreach (DAEmpExplanation dAEmpExplanation in oDAMaster.DAEmpExplanations)
                {
                    dAEmpExplanation.DAID = oDAMaster.ID;
                    base.SetObjectID(dAEmpExplanation, tc.GenerateID("DAEmpExplanation", "DAExpID"));
                    DAMasterDA.InsertDAEmpExplanation(tc, dAEmpExplanation);
                }
                foreach (DAPunishment dAPunishment in oDAMaster.DAPunishments)
                {
                    dAPunishment.DAID = oDAMaster.ID;
                    base.SetObjectID(dAPunishment, tc.GenerateID("DAPunishment", "DAPID"));
                    DAMasterDA.InsertDAPunishment(tc,dAPunishment);
                    foreach (DAPEmployee dAPEmployee in dAPunishment.DAPEmployees)
                    {
                        dAPEmployee.DAID = oDAMaster.ID;
                        dAPEmployee.PID = dAPunishment.ID;
                        base.SetObjectID(dAPEmployee, tc.GenerateID("DAPEmployee", "DAPEID"));
                        DAMasterDA.InsertDAPEmployee(tc, dAPEmployee);
                    }
                }
                foreach (DAComEmp dAComEmp in oDAMaster.DAComEmps)
                {
                    dAComEmp.DAID = oDAMaster.ID;
                    base.SetObjectID(dAComEmp, tc.GenerateID("DAComEmp", "DACEID"));
                    DAMasterDA.InsertDAComEmp(tc,dAComEmp);
                }
                foreach (DAPRIEmp dAPRIEmp in oDAMaster.DAPRIEmps)
                {
                    dAPRIEmp.DAID = oDAMaster.ID;
                    base.SetObjectID(dAPRIEmp, tc.GenerateID("DAPRIEmp", "DAPRIID"));
                    DAMasterDA.InsertDAPRIEmp(tc,dAPRIEmp);
                }
                #endregion

                tc.End();
                return oDAMaster.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);

                #region Delete Childs
                DAMasterDA.DeleteDAEmployee(tc, id);
                DAMasterDA.DeleteDAComplains(tc, id);
                DAMasterDA.DeleteDAEmpExplanations(tc, id);
                DAMasterDA.DeleteDAPEmployees(tc, id);
                DAMasterDA.DeleteDAPunishments(tc, id);
                DAMasterDA.DeleteDAComEmps(tc, id);
                DAMasterDA.DeleteDAPRIEmps(tc, id);
                #endregion

                DAMasterDA.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
            }
        }

        public void DeleteDAEmployee(int id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);

                #region Delete Childs
                DAMasterDA.DeleteDAEmployee(tc, id);
                #endregion

                DAMasterDA.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
            }
        }


        #region Child Class Functions

        public List<DAEmployee> GetDAEmployees(int id)
        {
            List<DAEmployee> oDAEmployees = new List<DAEmployee>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(DAMasterDA.GetDAEmployees(tc, id));
                oDAEmployees = this.CreateDAEmployeeObjects(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 oDAEmployees;
        }

        public List<DAComplain> GetDAComplains(int id)
        {

            List<DAComplain> oDAComplains = new List<DAComplain>();


            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(DAMasterDA.GetDAComplains(tc, id));
                //if (oreader.Read())
                //{
                    oDAComplains = this.CreateDAComplainObjects(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 oDAComplains;
        }

        public List<DAEmpExplanation> GetDAEmpExplanations(int id)
        {

            List<DAEmpExplanation> oDAEmpExplanations = new List<DAEmpExplanation>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(DAMasterDA.GetDAEmpExplanations(tc, id));
                //if (oreader.Read())
                //{
                    oDAEmpExplanations = this.CreateDAEmpExplanationObjects(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 oDAEmpExplanations;

        }

        public List<DAPunishment> GetDAPunishments(int id)
        {

            List<DAPunishment> oDAPunishments = new List<DAPunishment>();


            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(DAMasterDA.GetDAPunishments(tc, id));
                //if (oreader.Read())
                //{
                    oDAPunishments = this.CreateDAPunishmentObjects(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 oDAPunishments;
        }

        public List<DAComEmp> GetDAComEmps(int id)
        {

            List<DAComEmp> oDAComEmps = new List<DAComEmp>();


            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(DAMasterDA.GetDAComEmps(tc, id));
                //if (oreader.Read())
                //{
                    oDAComEmps = this.CreateDAComEmpObjects(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 oDAComEmps;
        }

        public List<DAPEmployee> GetDAPEmployees(int id)
        {
            List<DAPEmployee> oDAPEmployees = new List<DAPEmployee>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(DAMasterDA.GetDAPEmployees(tc, id));
                if (oreader.Read())
                {
                    oDAPEmployees = this.CreateDAPEmployeeObjects(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 oDAPEmployees;
        }

        public List<DAPRIEmp> GetDAPRIEmps(int id)
        {
            List<DAPRIEmp> oDAPRIEmps = new List<DAPRIEmp>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(DAMasterDA.GetDAPRIEmps(tc, id));
                if (oreader.Read())
                {
                    oDAPRIEmps = this.CreateDAPRIEmpObjects(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 oDAPRIEmps;
        }

       

        #endregion

        #endregion

    }
}