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 AppraisalPointRatingService : ServiceTemplate, IAppraisalPointRatingService
    {
        #region Object Mapping

        private void MapObject(AppraisalPointRating oAppraisalPointRating, DataReader oReader)
        {
            SetObjectID(oAppraisalPointRating, (oReader.GetInt32("AppraisalPointRatingID").Value));
            oAppraisalPointRating.AppraisalPointID = oReader.GetInt32("AppraisalPointID", 0);
            oAppraisalPointRating.MaxParcent = oReader.GetDouble("MaxParcent").HasValue ? oReader.GetDouble("MaxParcent").Value : null;
            oAppraisalPointRating.MinParcent = oReader.GetDouble("MinParcent").HasValue ? oReader.GetDouble("MinParcent").Value : null;
            //oAppraisalPointRating.Parcent = (double)oReader.GetDouble("Parcent");
            //oAppraisalPointRating.AppraisalYear = (DateTime)oReader.GetDateTime("AppraisalYear");
            oAppraisalPointRating.AppraisalYear = oReader.GetDateTime("AppraisalYear").HasValue
                ? oReader.GetDateTime("AppraisalYear").Value
                : (DateTime?)null;
            oAppraisalPointRating.Type = (EnumAppraisalPointType)oReader.GetInt32("Type", 0);
            oAppraisalPointRating.Status = (EnumStatus)oReader.GetInt32("Status");

            oAppraisalPointRating.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oAppraisalPointRating.CreatedDate = oReader.GetDateTime("CreatedDate").HasValue
                ? oReader.GetDateTime("CreatedDate").Value
                : DateTime.MinValue;
            oAppraisalPointRating.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oAppraisalPointRating.ModifiedDate = oReader.GetDateTime("ModifiedDate").HasValue
                ? oReader.GetDateTime("ModifiedDate").Value
                : (DateTime?)null;

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

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

        #endregion


        #region Service Implementation

        public List<AppraisalPointRating> Get()
        {
            List<AppraisalPoint> oAppraisalPoints = new AppraisalPointService().Get(EnumStatus.Regardless);
            List<AppraisalPointRating> oAppraisalPointRatings = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(AppraisalPointRatingDA.Get(tc));
                oAppraisalPointRatings = this.CreateObjects<AppraisalPointRating>(oreader);
                oreader.Close();
                tc.End();

                if (oAppraisalPointRatings.Count > 0)
                {
                    foreach (AppraisalPointRating item in oAppraisalPointRatings)
                    {
                        item.AppraisalPoint = oAppraisalPoints.Find(x => x.ID == item.AppraisalPointID);
                    }
                }
            }

            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return oAppraisalPointRatings;
        }


        public List<AppraisalPointRating> Get(DateTime Appyear)
        {
            List<AppraisalPoint> oAppraisalPoints = new AppraisalPointService().Get(EnumStatus.Regardless);
            List<AppraisalPointRating> oAppraisalPointRatings = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(AppraisalPointRatingDA.Get(tc, Appyear));
                oAppraisalPointRatings = this.CreateObjects<AppraisalPointRating>(oreader);
                oreader.Close();
                tc.End();

                if(oAppraisalPointRatings.Count > 0)
                {
                    foreach(AppraisalPointRating item in oAppraisalPointRatings)
                    {
                        item.AppraisalPoint = oAppraisalPoints.Find(x => x.ID == item.AppraisalPointID);
                    }

                    //oAppraisalPointRatings = new AppraisalPointService().Get(EnumStatus.Regardless).FindAll(delegate (AppraisalPoint item) { return item.ID == gradeId });
                }
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return oAppraisalPointRatings;
        }
        public List<AppraisalPointRating> Get(EnumAppraisalPointType type, DateTime? Appyear)
        {
            List<AppraisalPoint> oAppraisalPoints = new AppraisalPointService().Get(EnumStatus.Regardless);
            List<AppraisalPointRating> oAppraisalPointRatings = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(AppraisalPointRatingDA.Get(tc, type, Appyear));
                oAppraisalPointRatings = this.CreateObjects<AppraisalPointRating>(oreader);
                oreader.Close();
                tc.End();

                if(oAppraisalPointRatings.Count > 0)
                {
                    foreach(AppraisalPointRating item in oAppraisalPointRatings)
                    {
                        item.AppraisalPoint = oAppraisalPoints.Find(x => x.ID == item.AppraisalPointID);
                    }

                    //oAppraisalPointRatings = new AppraisalPointService().Get(EnumStatus.Regardless).FindAll(delegate (AppraisalPoint item) { return item.ID == gradeId });
                }
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return oAppraisalPointRatings;
        }


        public int Save(AppraisalPointRating item)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (item.IsNew)
                {
                    int id = tc.GenerateID("AppraisalPointRating", "AppraisalPointRatingID");
                    SetObjectID(item, (id));
                    AppraisalPointRatingDA.Insert(tc, item);
                }

                else
                {
                    AppraisalPointRatingDA.Update(tc, item);
                }

                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 void Update(AppraisalPointRating item)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                AppraisalPointRatingDA.Update(tc, item);
                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 Delete(int id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                AppraisalPointRatingDA.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 bool CheckAppraisalPointRating(int id, DateTime year)
        {
            bool flag = true;
            DataSet ds = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                ds = AppraisalPointRatingDA.CheckAppraisalPointRating(tc, id, year);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            if (ds.Tables[0].Rows.Count >= 1)
            {
                flag = false;
            }

            return flag;
        }

        #endregion
    }
}