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 QuestionCategoryService : ServiceTemplate, IQuestionCategoryService
    {
        public QuestionCategoryService()
        {
        }

        private void MapObject(QuestionCategory oQuestionCategory, DataReader oReader)
        {
            base.SetObjectID(oQuestionCategory, (oReader.GetInt32("QuestionCategoryID").Value));
            oQuestionCategory.Code = oReader.GetString("Code");
            oQuestionCategory.Description = oReader.GetString("Description");
            //oQuestionCategory.Sequence = oReader.GetInt32("SequenceNo").Value;
            oQuestionCategory.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            oQuestionCategory.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oQuestionCategory.CreatedDate = oReader.GetDateTime("CreationDate").HasValue
                ? oReader.GetDateTime("CreationDate").Value
                : DateTime.MinValue;
            oQuestionCategory.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oQuestionCategory.ModifiedDate = oReader.GetDateTime("ModifiedDate").HasValue
                ? oReader.GetDateTime("ModifiedDate").Value
                : (DateTime?)null;
            this.SetObjectState(oQuestionCategory, Ease.Core.ObjectState.Saved);
        }

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

        protected QuestionCategory CreateObject(DataReader oReader)
        {
            QuestionCategory oQuestionCategory = new QuestionCategory();
            MapObject(oQuestionCategory, oReader);
            return oQuestionCategory;
        }

        #region Service implementation

        public string GetNextCode()
        {
            TransactionContext tc = null;
            string _code = "";
            try
            {
                tc = TransactionContext.Begin();
                //####  _code = GlobalFunctionService.GetMaxCode(tc, "QuestionCategory", "codeautogenerate", "QuestionCategory", "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 QuestionCategory Get(int id)
        {
            QuestionCategory oQuestionCategory = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(QuestionCategoryDA.Get(tc, id));
                if (oreader.Read())
                {
                    oQuestionCategory = this.CreateObject<QuestionCategory>(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 oQuestionCategory;
        }

        public QuestionCategory Get(string sCode)
        {
            QuestionCategory oQuestionCategory = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(QuestionCategoryDA.Get(tc, sCode));
                if (oreader.Read())
                {
                    oQuestionCategory = this.CreateObject<QuestionCategory>(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 oQuestionCategory;
        }

        public List<QuestionCategory> Get(EnumStatus status)
        {
            List<QuestionCategory> QuestionCategorys = null;

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

                DataReader dr = new DataReader(QuestionCategoryDA.Get(tc, status));
                QuestionCategorys = this.CreateObjects<QuestionCategory>(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 QuestionCategorys;
        }

        public int Save(QuestionCategory oQuestionCategory)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oQuestionCategory.IsNew)
                {
                    int id = tc.GenerateID("QuestionCategory", "QuestionCategoryID");
                    // int seqID = tc.GenerateID("QuestionCategory", "SequenceNO");
                    base.SetObjectID(oQuestionCategory, (id));
                    // oQuestionCategory.Sequence = seqID;
                    QuestionCategoryDA.Insert(tc, oQuestionCategory);
                }
                else
                {
                    QuestionCategoryDA.Update(tc, oQuestionCategory);
                }

                tc.End();
                return oQuestionCategory.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);
                QuestionCategoryDA.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
    }
}