using System;
using Ease.Core.Model;
using Ease.Core.DataAccess;
using HRM.BO;
using System.Collections.Generic;
using Ease.Core.Utility;

namespace HRM.DA
{
    public class DisciplineService : ServiceTemplate, IDisciplineService
    {
        #region constructor

        public DisciplineService()
        {
        }

        #endregion

        #region Object

        private void MapObject(Discipline oDiscipline, DataReader oReader)
        {
            base.SetObjectID(oDiscipline, oReader.GetInt32("DISCIPLINEID").Value);
            oDiscipline.Code = oReader.GetString("CODE");
            oDiscipline.Description = oReader.GetString("DESCRIPTION");
            //oDiscipline.EducationLevelID = ID.FromInteger(oReader.GetInt32("EDUCATIONLEVELID").Value).IsUnassigned ? ID.FromInteger(1) : ID.FromInteger(oReader.GetInt32("EDUCATIONLEVELID").Value);
            oDiscipline.EducationLevelID = oReader.GetInt32("EDUCATIONLEVELID",true, 0);
            oDiscipline.Sequence = oReader.GetInt32("SequenceNo").Value;
            oDiscipline.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            oDiscipline.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oDiscipline.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            oDiscipline.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oDiscipline.ModifiedDate = oReader.GetDateTime("ModifiedDate");

            this.SetObjectState(oDiscipline, Ease.Core.ObjectState.Saved);
        }

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

        private void MapDisLevelObject(Discipline.DisciplineLevel oDisLevel, DataReader oReader)
        {
            base.SetObjectID(oDisLevel, oReader.GetInt32("DISCIPLINELEVELID").Value);
            oDisLevel.DiscilineID = oReader.GetInt32("DISCIPLINEID", 0);
            oDisLevel.LevelID = oReader.GetInt32("LEVELID", 0);

            this.SetObjectState(oDisLevel, Ease.Core.ObjectState.Saved);
        }

        protected List<Discipline.DisciplineLevel> CreateDisLevelObjects(DataReader oReader)
        {
            List<Discipline.DisciplineLevel> oDisciplineLevels = new List<Discipline.DisciplineLevel>();
            while (oReader.Read())
            {
                Discipline.DisciplineLevel oDisLevel = new Discipline.DisciplineLevel();
                MapDisLevelObject(oDisLevel, oReader);
                oDisciplineLevels.Add(oDisLevel);
            }

            return oDisciplineLevels;
        }

        public Discipline GetRelatedData(TransactionContext tc, Discipline oDiscipline)
        {
            oDiscipline.DisciplineLevels = GetDisplineByDID(tc, oDiscipline.ID);
            return oDiscipline;
        }

        #endregion

        #region Service implementation

        public Discipline Get(int id)
        {
            Discipline oDiscipline = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(DisciplineDA.Get(tc, id));
                if (oreader.Read())
                {
                    oDiscipline = this.CreateObject<Discipline>(oreader);
                }
                oreader.Close();
                if (oDiscipline != null)
                    oDiscipline = GetRelatedData(tc, oDiscipline);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return oDiscipline;
        }

        public Discipline Get(string sCode)
        {
            Discipline oDiscipline = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(DisciplineDA.Get(tc, sCode));
                if (oreader.Read())
                {
                    oDiscipline = this.CreateObject<Discipline>(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 oDiscipline;
        }

        public List<Discipline> Get()
        {
            List<Discipline> disciplines = new List<Discipline>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DisciplineDA.Get(tc));
                disciplines = this.CreateObjects<Discipline>(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 disciplines;
        }

        public List<Discipline> Get(EnumStatus status, int payrolltypeid)
        {
            List<Discipline> disciplines = new List<Discipline>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DisciplineDA.Get(tc, status));
                disciplines = this.CreateObjects<Discipline>(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 disciplines;
        }
        public List<Discipline> Get(EnumStatus status)
        {
            List<Discipline> disciplines = new List<Discipline>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DisciplineDA.Get(tc, status));
                disciplines = this.CreateObjects<Discipline>(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 disciplines;
        }

        public List<Discipline> GetByEducationLevel(int levelID)
        {
            List<Discipline> disciplines = new List<Discipline>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DisciplineDA.GetByEducationLevel(tc, levelID));
                disciplines = this.CreateObjects<Discipline>(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 disciplines;
        }

        public List<Discipline.DisciplineLevel> GetDisplineByDID(int DisID)
        {
            List<Discipline.DisciplineLevel> disciplines = new List<Discipline.DisciplineLevel>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DisciplineDA.GetDisplineByDID(tc, DisID));
                disciplines = this.CreateDisLevelObjects(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 disciplines;
        }

        public List<Discipline.DisciplineLevel> GetDisplineByDID(TransactionContext tc, int DisID)
        {
            List<Discipline.DisciplineLevel> disciplines = new List<Discipline.DisciplineLevel>();
            try
            {
                DataReader dr = new DataReader(DisciplineDA.GetDisplineByDID(tc, DisID));
                disciplines = this.CreateDisLevelObjects(dr);
                dr.Close();
            }
            catch (Exception e)
            {
                #region Handle Exception

                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);

                #endregion
            }

            return disciplines;
        }

        public List<Discipline.DisciplineLevel> GetDisByEducationLevel(int LevelID)
        {
            List<Discipline.DisciplineLevel> disciplines = new List<Discipline.DisciplineLevel>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DisciplineDA.GetDisByEducationLevel(tc, LevelID));

                disciplines = this.CreateDisLevelObjects(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 disciplines;
        }

        public string GetNextCode()
        {
            TransactionContext tc = null;
            string _code = "";
            try
            {
                tc = TransactionContext.Begin();
                _code = GlobalFunctionService.GetMaxCode(tc, "DISCIPLINE", "Code");

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

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

                #endregion
            }

            return _code;
        }
        public int Save(Discipline oDiscipline)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oDiscipline.IsNew)
                {
                    int id = tc.GenerateID("DISCIPLINE", "DISCIPLINEID");
                    base.SetObjectID(oDiscipline, id);
                    oDiscipline.Code = GlobalFunctionService.GetMaxCode(tc, "DISCIPLINE", "Code");
                    DisciplineDA.Insert(tc, oDiscipline);
                }
                else
                 {
                    DisciplineDA.Update(tc, oDiscipline);
                    DisciplineDA.DeleteByDisID(tc, oDiscipline.ID);
                }

                //foreach (Discipline.DisciplineLevel disciplineLevel in oDiscipline.DisciplineLevels)
                //{
                //    int id = tc.GenerateID("DisciplineLevel", "DisciplineLevelID");
                //    disciplineLevel.DiscilineID = oDiscipline.ID;
                //    base.SetObjectID(disciplineLevel, ID.FromInteger(id));
                //    DisciplineDA.Insert(tc, disciplineLevel);
                //}
                tc.End();
                return oDiscipline.ID;
            }
            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);
                DisciplineDA.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
            }
        }

        internal List<Discipline> GetByID(TransactionContext tc, int id)
        {
            List<Discipline> oEducationLevels = new List<Discipline>();
            try
            {
                DataReader dr = new DataReader(DisciplineDA.GetByID(tc, id));
                oEducationLevels = this.CreateObjects<Discipline>(dr);
                dr.Close();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return oEducationLevels;
        }

        public List<Discipline> GetByIDs(string ids)
        {
            List<Discipline> oDisciplineList = new List<Discipline>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(DisciplineDA.GetByIDs(tc, ids));
                oDisciplineList = this.CreateObjects<Discipline>(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 oDisciplineList;
        }

        #endregion
    }
}