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 HRM.DA;


namespace HRM.DA
{
    public class SurveyEmployeeService : ServiceTemplate, ISurveyEmployeeService
    {
        public SurveyEmployeeService()
        {
        }

        private void MapObject(SurveyEmployee oSurveyEmployee, DataReader oReader)
        {
            // base.SetObjectID(oSurveyEmployee, oReader.GetID("SurveyEmployeeID"));
            oSurveyEmployee.SurveyID = oReader.GetInt32("SurveyID", 0);
            oSurveyEmployee.EmployeeID = oReader.GetInt32("EmployeeID").GetValueOrDefault();
            oSurveyEmployee.CandidateID = oReader.GetInt32("CandidateID").GetValueOrDefault();
            oSurveyEmployee.StartTime = oReader.GetDateTime("STARTTIME").GetValueOrDefault();
            oSurveyEmployee.CurrentTime = oReader.GetDouble("CURRENTTIME").GetValueOrDefault();
            oSurveyEmployee.ErCircularID = oReader.GetInt32("ErCircularID").GetValueOrDefault();
            oSurveyEmployee.SurveyParticipantDate = oReader.GetDateTime("SurveyParticipantDate").GetValueOrDefault();
            oSurveyEmployee.SurveyMark = oReader.GetDouble("SurveyMark").GetValueOrDefault();
            oSurveyEmployee.SurveyTime = oReader.GetDouble("SurveyTime").GetValueOrDefault();
            oSurveyEmployee.IsCompleted = oReader.GetBoolean("IsCompleted").GetValueOrDefault();
            oSurveyEmployee.CurrentQuestionID = oReader.GetInt32("CURRENTQUESTIONID").GetValueOrDefault();
            oSurveyEmployee.CompleteTime = oReader.GetDateTime("CompleteTime").GetValueOrDefault();
            this.SetObjectState(oSurveyEmployee, Ease.Core.ObjectState.Saved);
        }

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

        #region Service implementation

        public List<SurveyEmployee> GetSurveyEmployee(int surveyId)
        {
            List<SurveyEmployee> oSurveyEmployees = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(SurveyEmployeeDA.GetSurveyEmployee(tc, surveyId));
                oSurveyEmployees = this.CreateObjects<SurveyEmployee>(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 oSurveyEmployees;
        }

        public List<SurveyEmployee> GetSurveyEmployee()
        {
            List<SurveyEmployee> oSurveyEmployees = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(SurveyEmployeeDA.GetSurveyEmployee(tc));
                oSurveyEmployees = this.CreateObjects<SurveyEmployee>(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 oSurveyEmployees;
        }

        public int Save(SurveyEmployee oSurveyEmployee)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);

                if (oSurveyEmployee.IsNew)
                {
                    //int id = tc.GenerateID("SurveyEmployee", "SurveyEmployeeID");
                    //base.SetObjectID(oSurveyEmployee, (id));


                    SurveyEmployeeDA.Insert(tc, oSurveyEmployee);
                }
                else
                {
                    SurveyEmployeeDA.Update(tc, oSurveyEmployee);
                }

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

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

                #endregion
            }
        }

        public List<SurveyEmployee> GetSurveyEmployeeByCandidateId(int surveyId,int candidateId, int erCircularId)
        {
            List<SurveyEmployee> oSurveyEmployees = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(SurveyEmployeeDA.GetSurveyEmployeebycandidateId(tc, surveyId,candidateId, erCircularId));
                oSurveyEmployees = this.CreateObjects<SurveyEmployee>(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 oSurveyEmployees;
        }

        public List<SurveyEmployee> GetSurveyEmployeeByCandidateId(int surveyId, int candidateId)
        {
            List<SurveyEmployee> oSurveyEmployees = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(SurveyEmployeeDA.GetSurveyEmployeebycandidateId(tc, surveyId, candidateId));
                oSurveyEmployees = this.CreateObjects<SurveyEmployee>(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 oSurveyEmployees;
        }

        public List<SurveyEmployee> GetSurveyEmployeebyJobId(int candidateId, int erCircularId)
        {
            List<SurveyEmployee> oSurveyEmployees = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(SurveyEmployeeDA.GetSurveyEmployeebyJobId(tc, candidateId, erCircularId));
                oSurveyEmployees = this.CreateObjects<SurveyEmployee>(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 oSurveyEmployees;
        }

        public List<SurveyEmployee> GetCompleteExamByCandidate(int candidateId, int erCircularId)
        {
            List<SurveyEmployee> oSurveyEmployees = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(SurveyEmployeeDA.GetCompleteExamByCandidate(tc, candidateId, erCircularId));
                oSurveyEmployees = this.CreateObjects<SurveyEmployee>(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 oSurveyEmployees;
        }



        public void Delete(int id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                SurveyEmployeeDA.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 DeleteByCandidateID(int surveyId, int candidateId,int erCircularId)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                SurveyEmployeeDA.DeleteSurveyEmployeeByCandidateID(tc, surveyId, candidateId, erCircularId);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
                #endregion
            }
        }

        #endregion
    }
}