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;

namespace HRM.DA
{
    public class ErAppliedApplicantService : ServiceTemplate, IErAppliedApplicantService
    {
        #region ErApplied Applicant Mapping

        private void MapAppliedApplicantObject(ErAppliedApplicant oErAppliedApplicant, DataReader oReader)
        {
            this.SetObjectID(oErAppliedApplicant, oReader.GetInt32("AppliedApplicantID").Value);
            oErAppliedApplicant.UserID = oReader.GetInt32("UserID", true, 0);
            oErAppliedApplicant.JobID = oReader.GetInt32("JobID", true, 0);
            oErAppliedApplicant.ApplyDate = oReader.GetDateTime("ApplyDate").Value;
            oErAppliedApplicant.ApplicationAcknowldged = oReader.GetBoolean("ApplicationAcknowldged").Value;
            oErAppliedApplicant.IsSelectedforInterview = oReader.GetBoolean("IsSelectedforInterview").GetValueOrDefault();
            oErAppliedApplicant.IsOfferLetteraccepted = oReader.GetBoolean("IsOfferLetteraccepted").GetValueOrDefault();
            oErAppliedApplicant.OfferletterRejectReason = oReader.GetString("OfferletterRejectReason") != null ? oReader.GetString("OfferletterRejectReason") : string.Empty;
            oErAppliedApplicant.OfferLetterIssueDate = oReader.GetDateTime("OfferLetterIssueDate").GetValueOrDefault();
            oErAppliedApplicant.OfferletterAcceptDate = oReader.GetDateTime("OfferletterAcceptDate").GetValueOrDefault();
            oErAppliedApplicant.OfferletterAcceptBy = oReader.GetInt32("OfferletterAcceptBy", true, 0);

            if (oReader.GetDateTime("CreationDate") == null)
            {
                oErAppliedApplicant.CreatedDate = DateTime.MinValue;
            }
            else
            {
                oErAppliedApplicant.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            }

            if (oReader.GetDateTime("MODIFIEDDATE") == null)
            {
                oErAppliedApplicant.ModifiedDate = DateTime.MinValue;
            }
            else
            {
                oErAppliedApplicant.ModifiedDate = oReader.GetDateTime("MODIFIEDDATE").Value;
            }

            oErAppliedApplicant.UserName = oReader.GetString("UserName",true,null);
            this.SetObjectState(oErAppliedApplicant, ObjectState.Saved);
        }

        protected override T CreateObject<T>(DataReader oReader)
        {
            ErAppliedApplicant oErAppliedApplicant = new ErAppliedApplicant();
            MapAppliedApplicantObject(oErAppliedApplicant, oReader);
            return oErAppliedApplicant as T;
        }

        #endregion

        #region ErJobupdate Object Mapping

        private void MapErJobupdate(ErJobupdate obErJobupdate, DataReader oReader)
        {
            obErJobupdate.UserID = oReader.GetInt32("UserID").Value;
            obErJobupdate.JobID = oReader.GetInt32("JobID").Value;
            obErJobupdate.AppliedApplicantID = oReader.GetInt32("AppliedApplicantID").Value; ;
            obErJobupdate.UpdatedBy = oReader.GetString("UpdatedBy") != null ? (EnumActorType)oReader.GetInt32("UpdatedBy").Value : 0;
            obErJobupdate.Description = oReader.GetString("Description") != null ? oReader.GetString("Description") : string.Empty;
            obErJobupdate.Remarks = oReader.GetString("Remarks") != null ? oReader.GetString("Remarks") : string.Empty;
            obErJobupdate.UpdateDate = oReader.GetString("UpdateDate") != null ? oReader.GetDateTime("UpdateDate").Value : DateTime.MinValue;
            obErJobupdate.IsAcknowledged = oReader.GetBoolean("IsAcknowledged").GetValueOrDefault();
            obErJobupdate.AcknowledgeDate = oReader.GetString("AcknowledgeDate") != null ? oReader.GetDateTime("AcknowledgeDate").Value : DateTime.MinValue; 
            obErJobupdate.ReferenceID = oReader.GetString("ReferenceID") != null ? oReader.GetInt32("ReferenceID").Value : 0;
            this.SetObjectState(obErJobupdate, ObjectState.Saved);
        }

        private List<ErJobupdate> CreateErJobUpdateObjects(DataReader oReader)
        {
            List<ErJobupdate> allJobUpdates = new List<ErJobupdate>();
            while (oReader.Read())
            {
                ErJobupdate obErJobupdate = new ErJobupdate();
                MapErJobupdate(obErJobupdate, oReader);
                allJobUpdates.Add(obErJobupdate);
            }

            return allJobUpdates;
        }

        #endregion

        #region ErApplicantDoc Object Mapping

        private void MapApplicantDoc(ErApplicantDoc oErApplicantDoc, DataReader oReader)
        {
            //SetObjectID(obExperience, (oReader.GetInt32("CVReferenceID").Value));
            oErApplicantDoc.AppliedApplicantID = oReader.GetString("AppliedApplicantID") != null ? oReader.GetInt32("AppliedApplicantID").Value : 0;
            oErApplicantDoc.UserID = oReader.GetString("UserID") != null ? oReader.GetInt32("UserID").Value : 0;
            oErApplicantDoc.DocType = oReader.GetString("DocType") != null ?(EnumFileType)oReader.GetInt32("DocType").Value : 0;
            oErApplicantDoc.UpdatedBy = oReader.GetString("UpdatedBy") != null ? (EnumActorType)oReader.GetInt32("UpdatedBy").Value : 0; ;
            oErApplicantDoc.CreatedDate = oReader.GetString("CreatedDate") != null ? oReader.GetDateTime("CreatedDate").Value : DateTime.MinValue;
            oErApplicantDoc.Remarks = oReader.GetString("Remarks") != null ? oReader.GetString("Remarks") : string.Empty;
            this.SetObjectState(oErApplicantDoc, ObjectState.Saved);
        }

        private List<ErApplicantDoc> CreateApplicantDocbjects(DataReader oReader)
        {
            List<ErApplicantDoc> allApplicantDocs = new List<ErApplicantDoc>();
            while (oReader.Read())
            {
                ErApplicantDoc oApplicantDoc = new ErApplicantDoc();
                MapApplicantDoc(oApplicantDoc, oReader);
                allApplicantDocs.Add(oApplicantDoc);
            }

            return allApplicantDocs;
        }

        #endregion

     

        #region Service Implementation


        #region Insert(ErAppliedApplicant ErAppliedApplicant)

        public void Save(ErAppliedApplicant oErAppliedApplicant)
        {
            TransactionContext tc = null;
            int oID = 0;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oErAppliedApplicant.IsNew)
                {
                    int id = tc.GenerateID("ErAppliedApplicant", "AppliedApplicantID");
                    oID = (id);
                    base.SetObjectID(oErAppliedApplicant, (id));
                    ErAppliedApplicantDA.InsertErAppliedApplicant(oErAppliedApplicant, tc);
                }
                else
                {
                    oID = oErAppliedApplicant.ID;
                    ErAppliedApplicantDA.UpdateErAppliedApplicant(oErAppliedApplicant, 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 Insert(ErJobupdate oErJobupdate)

        public void Save(ErJobupdate oErJobupdate)
        {
            TransactionContext tc = null;
            int oID = 0;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oErJobupdate.IsNew)
                {
                    //int id = tc.GenerateID("UserCvSort", "USERCVSORTID");
                    //oID = (id);
                    //base.SetObjectID(obCv, (id));
                    ErAppliedApplicantDA.InsertErJobupdate(oErJobupdate, 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 Save(ErApplicantDoc oErJobupdate,string connectionstring)
        {
            TransactionContext tc = null;
            int oID = 0;
            try
            {
                if (oErJobupdate.IsNew)
                {
                    tc = TransactionContext.Begin(true);
                    ErAppliedApplicantDA.DeleteApplicantDoc(tc,oErJobupdate.AppliedApplicantID);
                    tc.End();
                    ErAppliedApplicantDA.Insert(oErJobupdate, connectionstring);
                    tc = TransactionContext.Begin(true);
                    ErAppliedApplicantDA.UpdateAppliedApplicant(oErJobupdate, 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 Save(ErApplicantDoc oErApplicantDoc)
        {
            TransactionContext tc = null;
            int oID = 0;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oErApplicantDoc.IsNew)
                {
                    //int id = tc.GenerateID("UserCvSort", "USERCVSORTID");
                    //oID = (id);
                    //base.SetObjectID(obCv, (id));
                    ErAppliedApplicantDA.InsertErApplicantDoc(oErApplicantDoc, tc);
                }
                tc.End();

            }
            catch (Exception ex)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        #region GetReferredBy(int cVID)

        public List<ErAppliedApplicant> Get()
        {
            List<ErAppliedApplicant> allAppliedApplicantList = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(ErAppliedApplicantDA.Get(tc));
                allAppliedApplicantList = this.CreateObjects<ErAppliedApplicant>(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 allAppliedApplicantList;
        }

        #endregion

        #region GetByID(int applicantId)

        public List<ErJobupdate> GetByID(int applicantId)
        {
            List<ErJobupdate> list = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(ErAppliedApplicantDA.GetJobupdate(applicantId, tc));
                list = this.CreateErJobUpdateObjects(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 list;
        }

        public List<ErJobupdate> GetJobupdateByUserId(int userId, int jobId)
        {
            List<ErJobupdate> list = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(ErAppliedApplicantDA.GetJobupdateByUserId(userId, jobId, tc));
                list = this.CreateErJobUpdateObjects(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 list;
        }

        public List<ErApplicantDoc> GetApplicantDocByUserID(int userId, int jobId)
        {
            List<ErApplicantDoc> oFileAttachments = new List<ErApplicantDoc>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(ErAppliedApplicantDA.GetApplicantDocByUserID(jobId, userId,tc));
                while (oreader.Read())
                {
                    ErApplicantDoc oIRFileAtachment = new ErApplicantDoc();
                    oIRFileAtachment.AppliedApplicantID = oreader.GetInt32("AppliedApplicantID").Value;
                    oIRFileAtachment.UserID = oreader.GetInt32("UserID", 0);
                    oIRFileAtachment.FileData = oreader.GetLob("FileData");
                    oIRFileAtachment.OriginalFileName = oreader.GetString("OriginalFileName");
                    oIRFileAtachment.DocType = (EnumFileType)oreader.GetInt32("DocType").GetValueOrDefault();
                    oIRFileAtachment.UpdatedBy = (EnumActorType)oreader.GetInt32("UpdatedBy").GetValueOrDefault();
                    oIRFileAtachment.JobID = oreader.GetInt32("JobID", 0);
                    oFileAttachments.Add(oIRFileAtachment);
                }
                oreader.Close();
            }
            catch (Exception ex)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(ex);
                throw new ServiceException("Failed to Get IRNotifications: " + ex.Message, ex);

                #endregion
            }

            return oFileAttachments;
        }

        #endregion

        #region GetByApplicantID(int applicantId)

        public List<ErApplicantDoc> GetByApplicantID(int applicantId)
        {
            List<ErApplicantDoc> list = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(ErAppliedApplicantDA.GetApplicantDoc(applicantId, tc));
                list = this.CreateApplicantDocbjects(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 list;
        }

        public List<ErAppliedApplicant> GetApplicantInfoByRecruitmentID(int recruitmentId)
        {
            List<ErAppliedApplicant> allAppliedApplicantList = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(ErAppliedApplicantDA.GetApplicantInfoByRecruitmentID(recruitmentId,tc));
                allAppliedApplicantList = this.CreateObjects<ErAppliedApplicant>(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 allAppliedApplicantList;
        }


        public List<ErAppliedApplicant> GetJobApplicantByUserId(int userId,int jobid)
        {
            List<ErAppliedApplicant> allAppliedApplicantList = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(ErAppliedApplicantDA.GetJobApplicantByUserId(userId, jobid, tc));
                allAppliedApplicantList = this.CreateObjects<ErAppliedApplicant>(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 allAppliedApplicantList;
        }

        public List<ErAppliedApplicant> GetAllJobApplicantByUserId(int userId)
        {
            List<ErAppliedApplicant> allAppliedApplicantList = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(ErAppliedApplicantDA.GetAllJobApplicantByUserId(userId, tc));
                allAppliedApplicantList = this.CreateObjects<ErAppliedApplicant>(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 allAppliedApplicantList;
        }


        public ErAppliedApplicant GetApplicantInfoByUserID(int recruitmentId,int userId)
        {
            ErAppliedApplicant allAppliedApplicantList = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(ErAppliedApplicantDA.GetApplicantInfoByUserID(recruitmentId, userId, tc));
                if (oreader.Read())
                {
                    allAppliedApplicantList = this.CreateObject<ErAppliedApplicant>(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 allAppliedApplicantList;
        }

        #endregion

        #endregion


        #region List< CV > GetByQuery(string query)


        public void Delete(ErAppliedApplicant oErAppliedApplicant)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                #region Delete UserCvSort
                ErAppliedApplicantDA.Delete(oErAppliedApplicant.ID, tc);
                #endregion
                tc.End();
            }
            catch (Exception ex)
            {
                #region Handle Exception

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

                #endregion
            }
        }




        #endregion

        #endregion
       

    }
}