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 Payroll.BO;

namespace HRM.DA
{
    public class QuestionAnswerService : ServiceTemplate, IQuestionAnswerService
    {
        public QuestionAnswerService()
        {
        }

        private void MapObject(QuestionAnswer oQuestionAnswer, DataReader oReader)
        {
            base.SetObjectID(oQuestionAnswer, (oReader.GetInt32("AnswerID").Value));
            oQuestionAnswer.AnswerID = oReader.GetInt32("AnswerID", 0);
            oQuestionAnswer.QuestionID = oReader.GetInt32("QuestionID").GetValueOrDefault();
            oQuestionAnswer.AnswerBody = oReader.GetString("ANSWERBODY");
            oQuestionAnswer.Image = oReader.GetString("Image");
            oQuestionAnswer.IsCorrect = oReader.GetBoolean("IsCorrect").GetValueOrDefault();
            oQuestionAnswer.DisplayOrder = oReader.GetInt32("DisplayOrder").GetValueOrDefault();
            oQuestionAnswer.Weight = oReader.GetDouble("Weight").GetValueOrDefault();
            this.SetObjectState(oQuestionAnswer, Ease.Core.ObjectState.Saved);
        }

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

        protected QuestionAnswer CreateObject(DataReader oReader)
        {
            QuestionAnswer oQuestionAnswer = new QuestionAnswer();
            MapObject(oQuestionAnswer, oReader);
            return oQuestionAnswer;
        }

        #region Service implementation

        public QuestionAnswer Get(int id)
        {
            QuestionAnswer oQuestionAnswer = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(QuestionAnswerDA.Get(tc, id));
                if (oreader.Read())
                {
                    oQuestionAnswer = this.CreateObject<QuestionAnswer>(oreader);
                }

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

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException("Failed to Get QuestionAnswer", e);

                #endregion
            }

            return oQuestionAnswer;
        }

        public List<QuestionAnswer> Get()
        {
            List<QuestionAnswer> oQuestionAnswers = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(QuestionAnswerDA.Get(tc));
                oQuestionAnswers = this.CreateObjects<QuestionAnswer>(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 oQuestionAnswers;
        }

        public List<QuestionAnswer> GetByQuestionID(int questionID)
        {
            List<QuestionAnswer> oQuestionAnswers = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(QuestionAnswerDA.GetByQuestionID(tc, questionID));
                oQuestionAnswers = this.CreateObjects<QuestionAnswer>(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 oQuestionAnswers;
        }

        public List<QuestionAnswerAttachment> GetQuestionFileAttachments(int questionID)
        {
            List<QuestionAnswerAttachment> oFileAttachments = new List<QuestionAnswerAttachment>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(QuestionAnswerDA.GetAttachmentByQuestionID(tc, questionID));
                while (oreader.Read())
                {
                    QuestionAnswerAttachment oIRFileAtachment = new QuestionAnswerAttachment();
                    oIRFileAtachment.ID = oreader.GetInt32("QUESTIONANSWERATTACHMENTID").Value;
                    oIRFileAtachment.QuestionID = oreader.GetInt32("QuestionID", 0);
                    oIRFileAtachment.AnswerID = oreader.GetInt32("AnswerID", 0);
                    oIRFileAtachment.FileAsByteArray = oreader.GetLob("FileData");
                    oIRFileAtachment.OriginalFileName = oreader.GetString("OriginalFileName");
                    oIRFileAtachment.AttachmentType = (EnumQuestionAnswerFileType)oreader.GetInt32("AttachmentType").GetValueOrDefault();
                    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;
        }

        public int Save(QuestionAnswer oQuestionAnswer)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);

                if (oQuestionAnswer.IsNew)
                {
                    //int id = tc.GenerateID("QuestionAnswer", "QuestionAnswerID");
                    //base.SetObjectID(oQuestionAnswer, (id));


                    QuestionAnswerDA.Insert(tc, oQuestionAnswer);
                }
                else
                {
                    QuestionAnswerDA.Update(tc, oQuestionAnswer);
                }

                tc.End();
                return oQuestionAnswer.AnswerID;
            }
            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);
                QuestionAnswerDA.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
    }
}