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;

namespace HRM.DA
{
    public class ObjectiveSetRemarksService : ServiceTemplate, IObjectiveSetRemarksService
    {
        #region Object Mapping

        private void MapObject(ObjectiveSetRemarks oObjectiveReview, DataReader oReader)
        {
            SetObjectID(oObjectiveReview, oReader.GetInt32("ObjectiveSetRemarksID", 0));
            oObjectiveReview.ObjectiveSetID = oReader.GetInt32("ObjectiveSetID", 0);
            oObjectiveReview.MidYearEmpReviewOne = oReader.GetString("MidYearEmpRemarksOne");
            oObjectiveReview.MidYearEmpReviewTwo = oReader.GetString("MidYearEmpRemarksTwo");
            oObjectiveReview.MidYearLMReviewOne = oReader.GetString("MidYearLMRemarksOne");
            oObjectiveReview.MidYearLMReviewTwo = oReader.GetString("MidYearLMRemarksTwo");
            oObjectiveReview.MidYearEmpReviewDate = oReader.GetDateTime("MidYearEmpRemarksDate").HasValue
                ? oReader.GetDateTime("MidYearEmpRemarksDate").Value
                : DateTime.MinValue;
            oObjectiveReview.MidYearLMReviewDate = oReader.GetDateTime("MidYearLMRemarksDate").HasValue
                ? oReader.GetDateTime("MidYearLMRemarksDate").Value
                : DateTime.MinValue;
            oObjectiveReview.YearEndEmpReviewDate = oReader.GetDateTime("YearEndEmpRemarksDate").HasValue
                ? oReader.GetDateTime("YearEndEmpRemarksDate").Value
                : DateTime.MinValue;
            oObjectiveReview.YearEndLMReviewDate = oReader.GetDateTime("YearEndLMRemarksDate").HasValue
                ? oReader.GetDateTime("YearEndLMRemarksDate").Value
                : DateTime.MinValue;
            oObjectiveReview.YearEndEmpReviewOne = oReader.GetString("YearEndEmpRemarksOne");
            oObjectiveReview.YearEndEmpReviewTwo = oReader.GetString("YearEndEmpRemarksTwo");
            oObjectiveReview.YearEndLMReviewOne = oReader.GetString("YearEndLMRemarksOne");
            oObjectiveReview.YearEndLMReviewTwo = oReader.GetString("YearEndLMRemarksTwo");
            this.SetObjectState(oObjectiveReview, ObjectState.Saved);
        }


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

        #endregion

        #region IObjectiveReviewService Members

        #region Get All

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

        public List<ObjectiveSetRemarks> GetByPMPYear(int nPMPYearID, string sEmpIDs)
        {
            List<ObjectiveSetRemarks> oObjectives = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(ObjectiveSetRemarksDA.GetByPMPYear(tc, nPMPYearID, sEmpIDs));
                oObjectives = this.CreateObjects<ObjectiveSetRemarks>(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 oObjectives;
        }

        #endregion

        #region Get By ID

        public ObjectiveSetRemarks Get(int id)
        {
            ObjectiveSetRemarks oObjectiveSetRemarks = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(ObjectiveSetRemarksDA.Get(tc, id));
                oObjectiveSetRemarks = this.CreateObject<ObjectiveSetRemarks>(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 oObjectiveSetRemarks;
        }

        #endregion

        #region Get By Objective Set ID

        public ObjectiveSetRemarks GetByObjectiveSetID(int sID)
        {
            ObjectiveSetRemarks oObjectiveSetRemarks = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(ObjectiveSetRemarksDA.GetByObjectiveSetID(tc, sID));
                if (oreader.Read())
                {
                    oObjectiveSetRemarks = this.CreateObject<ObjectiveSetRemarks>(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 oObjectiveSetRemarks;
        }

        #endregion

        #region Insert

        public int Save(ObjectiveSetRemarks item)
        {
            TransactionContext tc = null;
            try
            {
                //ObjectiveSet objSet = (new ObjectiveSetService()).Get(item.ObjectiveSetID);
                tc = TransactionContext.Begin(true);
                if (item.IsNew)
                {
                    int id = tc.GenerateID("ObjectiveSetRemarks", "ObjectiveSetRemarksID");
                    base.SetObjectID(item, (id));
                    ObjectiveSetRemarksDA.Save(tc, item);
                    //objSet.MYEmpComplete = true;
                    //ObjectiveSetDA.Update(tc, objSet);
                }
                else
                {
                    ObjectiveSetRemarksDA.Update(tc, item);
                    //objSet.MYEmpComplete = true;
                    //ObjectiveSetDA.Update(tc, objSet);
                }

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

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

                #endregion
            }
        }

        //public int Insert(ObjectiveSetRemarks item, List<Objective> oObjectives, ObjectiveSet oObjectiveSet)
        //{
        //    TransactionContext tc = null;
        //    try
        //    {
        //        //ObjectiveSet objSet = (new ObjectiveSetService()).Get(item.ObjectiveSetID);
        //        tc = TransactionContext.Begin(true);
        //        if (item.IsNew)
        //        {
        //            int id = tc.GenerateID("ObjectiveSetRemarks", "ObjectiveSetRemarksID");
        //            base.SetObjectID(item, (id));
        //            ObjectiveSetRemarksDA.Insert(tc, item);
        //            //objSet.MYEmpComplete = true;
        //            //ObjectiveSetDA.Update(tc, objSet);
        //        }
        //        else
        //        {
        //            ObjectiveSetRemarksDA.Update(tc, item);
        //            //objSet.MYEmpComplete = true;
        //            //ObjectiveSetDA.Update(tc, objSet);
        //        }
        //        foreach (Objective objitem in oObjectives)
        //        {
        //            if (objitem.IsNew)
        //            {
        //                int id = tc.GenerateID("Objective", "ObjectiveID");
        //                base.SetObjectID(item, (id));
        //                ObjectiveDA.Insert(tc, objitem);
        //            }
        //            else
        //            {
        //                ObjectiveDA.Update(tc, objitem);
        //            }
        //        }
        //        if (oObjectiveSet.IsNew)
        //        {
        //            int id = tc.GenerateID("ObjectiveSet", "ObjectiveSetID");
        //            base.SetObjectID(oObjectiveSet, (id));
        //            ObjectiveSetDA.Insert(tc, oObjectiveSet);
        //        }
        //        else
        //        {
        //            ObjectiveSetDA.Update(tc, oObjectiveSet);
        //        }
        //        tc.End();
        //        return item.ID;
        //    }
        //    catch (Exception e)
        //    {
        //        #region Handle Exception
        //        if (tc != null)
        //            tc.HandleError();
        //        ExceptionLog.Write(e);
        //        throw new ServiceException(e.Message, e);
        //        #endregion
        //    }
        //}

        #endregion

        #region Delete

        public void Delete(int id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                ObjectiveSetRemarksDA.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
            }
        }

        #endregion

        #endregion
    }
}