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

namespace HRM.DA
{
    public class InterviewAssessmentKPIService : ServiceTemplate, IInterviewAssessmentKPIService
    {
        #region MapAssesmentKpiObject
        private void MapAssesmentKpiObject(InterviewAssessmentKPI obKpi, DataReader oReader)
        {
            this.SetObjectID(obKpi, oReader.GetInt32("InterviewAssessmentKPIID").Value);
            obKpi.Description = oReader.GetString("Description") != null ? oReader.GetString("Description") : string.Empty;
            obKpi.MinMark = oReader.GetInt32("MinMark", true, 0);
            obKpi.MaxMark = oReader.GetInt32("MaxMark", true, 0);
            obKpi.CreatedBy = oReader.GetInt32("CreatedBy", true, 0);
            obKpi.CreatedDate = oReader.GetDateTime("CreationDate") == null
                ? DateTime.MinValue
                : oReader.GetDateTime("CreationDate").Value;
            obKpi.ModifiedBy = oReader.GetInt32("ModifiedBy");
            obKpi.ModifiedDate = oReader.GetDateTime("ModificationDate");


            this.SetObjectState(obKpi, ObjectState.Saved);
        }

        protected override T CreateObject<T>(DataReader oReader)
        {
            InterviewAssessmentKPI obKpi = new InterviewAssessmentKPI();
            MapAssesmentKpiObject(obKpi, oReader);
            return obKpi as T;
        }

        protected InterviewAssessmentKPI CreateObject(DataReader oReader)
        {
            InterviewAssessmentKPI obKpi = new InterviewAssessmentKPI();
            MapAssesmentKpiObject(obKpi, oReader);
            return obKpi;
        }
        #endregion

        #region InterviewBoardAssessmentKPI Object Mapping

        private void MapReference(InterviewBoardAssessmentKPI onBoardKpi, DataReader oReader)
        {
            SetObjectID(onBoardKpi, (oReader.GetInt32("InterviewBoardAssessmentKPIID").Value));
            onBoardKpi.InterviewAssessmentKPIID = oReader.GetString("InterviewAssessmentKPIID") != null ? oReader.GetInt32("InterviewAssessmentKPIID").Value : 0;
            onBoardKpi.Marks = oReader.GetString("Marks") != null ? oReader.GetInt32("Marks").Value : 0;
            this.SetObjectState(onBoardKpi, ObjectState.Saved);
        }

        private List<InterviewBoardAssessmentKPI> CreateBoardKpiObjects(DataReader oReader)
        {
            List<InterviewBoardAssessmentKPI> allboardKpis = new List<InterviewBoardAssessmentKPI>();
            while (oReader.Read())
            {
                InterviewBoardAssessmentKPI boardKpi = new InterviewBoardAssessmentKPI();
                MapReference(boardKpi, oReader);
                allboardKpis.Add(boardKpi);
            }

            return allboardKpis;
        }

        #endregion

        #region InterviewAssessmentKPI Service Implementation

        public void Save(InterviewAssessmentKPI obKpi)
        {
            TransactionContext tc = null;
            int oID = 0;
            try
            {
                tc = TransactionContext.Begin(true);
                if (obKpi.IsNew)
                {
                    int id = tc.GenerateID("InterviewAssessmentKPI", "InterviewAssessmentKPIID");
                    oID = (id);
                    base.SetObjectID(obKpi, (id));
                    InterviewAssessmentKPIDA.Insert(obKpi, tc);

                }
                else
                {
                    oID = obKpi.ID;
                    InterviewAssessmentKPIDA.Update(obKpi, tc);
                }
                tc.End();
            }
            catch (Exception ex)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<InterviewAssessmentKPI> Get()
        {
            List<InterviewAssessmentKPI> allRefs = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(InterviewAssessmentKPIDA.Get(tc));
                allRefs = this.CreateObjects<InterviewAssessmentKPI>(oreader);
                oreader.Close();
                tc.End();
            }

            catch (Exception ex)
            {
                #region Handle Exception

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

                #endregion
            }

            return allRefs;
        }

        public InterviewAssessmentKPI GetById(int id)
        {
            InterviewAssessmentKPI obKpi = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(InterviewAssessmentKPIDA.GeById(tc, id));
                if (oreader.Read())
                {
                    obKpi = this.CreateObject<InterviewAssessmentKPI>(oreader);
                }
                oreader.Close();
                tc.End();
            }

            catch (Exception ex)
            {
                #region Handle Exception

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

                #endregion
            }
            return obKpi;
        }

        public void Delete(InterviewAssessmentKPI interviewAssessmentKpi)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                InterviewAssessmentKPIDA.Delete(interviewAssessmentKpi, tc);
                tc.End();
            }
            catch (Exception ex)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        #endregion

        #region InterviewBoardAssessmentKPI Service Implementation

        public void SaveBoardKpi(InterviewBoardAssessmentKPI obKpi)
        {
            TransactionContext tc = null;
            int oID = 0;
            try
            {
                tc = TransactionContext.Begin(true);
                if (obKpi.IsNew)
                {
                    int id = tc.GenerateID("InterviewBoardAssessmentKPI", "InterviewBoardAssessmentKPIID");
                    oID = (id);
                    base.SetObjectID(obKpi, (id));
                    InterviewAssessmentKPIDA.InsertBoardKpi(obKpi, tc);

                }
                else
                {
                    oID = obKpi.ID;
                    InterviewAssessmentKPIDA.UpdateBoardKpi(obKpi, tc);
                }
                tc.End();
            }
            catch (Exception ex)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public void SaveBoardKpis(List<InterviewBoardAssessmentKPI> Items)
        {
            TransactionContext tc = null;
            try
            {
                int RequisitionID = 0;
                tc = TransactionContext.Begin(true);
                foreach (var obKpi in Items)
                {
                    int oID = 0;
                    if (obKpi.IsNew)
                    {
                        int id = tc.GenerateID("InterviewBoardAssessmentKPI", "InterviewBoardAssessmentKPIID");
                        oID = (id);
                        base.SetObjectID(obKpi, (id));
                        InterviewAssessmentKPIDA.InsertBoardKpi(obKpi, tc);

                    }
                    else
                    {
                        oID = obKpi.ID;
                        InterviewAssessmentKPIDA.UpdateBoardKpi(obKpi, tc);
                    }
                }
                tc.End();
            }
            catch (Exception ex)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<InterviewBoardAssessmentKPI> GetBoardKpi()
        {
            List<InterviewBoardAssessmentKPI> allRefs = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(InterviewAssessmentKPIDA.GetBoardKpi(tc));
                allRefs = this.CreateObjects<InterviewBoardAssessmentKPI>(oreader);
                oreader.Close();
                tc.End();
            }

            catch (Exception ex)
            {
                #region Handle Exception

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

                #endregion
            }

            return allRefs;
        }

        public InterviewBoardAssessmentKPI GetBoardKpiById(int id)
        {
            InterviewBoardAssessmentKPI obKpi = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(InterviewAssessmentKPIDA.GetBoardbyId(tc, id));
                if (oreader.Read())
                {
                    obKpi = this.CreateObject<InterviewBoardAssessmentKPI>(oreader);
                }
                oreader.Close();
                tc.End();
            }

            catch (Exception ex)
            {
                #region Handle Exception

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

                #endregion
            }
            return obKpi;
        }

        public void DeleteBoardKpi(InterviewBoardAssessmentKPI interviewAssessmentBoardKpi)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                InterviewAssessmentKPIDA.DeleteBoardKpi(interviewAssessmentBoardKpi, tc);
                tc.End();
            }
            catch (Exception ex)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        #endregion

    }
}