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

namespace HRM.DA
{
    public class HRDocService : ServiceTemplate, IHRDocService
    {
        public HRDocService()
        {
        }

        #region MapObject For HRDoc

        private void MapObject(HRDoc ohrDoc, DataReader oReader)
        {
            base.SetObjectID(ohrDoc, oReader.GetInt32("HRDocID").Value);
            ohrDoc.Code = oReader.GetString("Code");
            ohrDoc.Name = oReader.GetString("Name");
            ohrDoc.Description = oReader.GetString("Description");
            ohrDoc.ParentID = oReader.GetInt32("ParentID", 0);
            ohrDoc.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            ohrDoc.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            ohrDoc.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            ohrDoc.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            ohrDoc.ModifiedDate = oReader.GetDateTime("ModifiedDate");

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

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

        #endregion

        #region Service Implementation of HRDocs

        public HRDoc Get(int id)
        {
            HRDoc oHRDoc = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(HRDocDA.Get(tc, id));
                if (oreader.Read())
                {
                    oHRDoc = this.CreateObject<HRDoc>(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 oHRDoc;
        }

        public List<HRDoc> Get()
        {
            List<HRDoc> HRDocs = new List<HRDoc>();

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

                DataReader dr = new DataReader(HRDocDA.Get(tc));
                HRDocs = this.CreateObjects<HRDoc>(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 HRDocs;
        }

        public List<HRDoc> GetParent(EnumStatus status)
        {
            List<HRDoc> HRDocs = new List<HRDoc>();

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

                DataReader dr = new DataReader(HRDocDA.GetParent(tc, status));
                HRDocs = this.CreateObjects<HRDoc>(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 HRDocs;
        }

        public List<HRDoc> GetChilds(int parentID)
        {
            List<HRDoc> HRDocs = new List<HRDoc>();

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

                DataReader dr = new DataReader(HRDocDA.GetChilds(tc, parentID));
                HRDocs = this.CreateObjects<HRDoc>(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 HRDocs;
        }

        public int Save(HRDoc oHRDoc)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oHRDoc.IsNew)
                {
                    int id = tc.GenerateID("HRDoc", "HRDocID");
                    base.SetObjectID(oHRDoc, id);
                    HRDocDA.Insert(tc, oHRDoc);
                }
                else
                {
                    HRDocDA.Update(tc, oHRDoc);
                }

                return oHRDoc.ID;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
            finally
            {
                tc.End();
            }
        }

        public void Delete(int id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                HRDocDA.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 IsExists(string Code)
        {
            TransactionContext tc = null;
            bool isExists = false;
            try
            {
                tc = TransactionContext.Begin(true);
                isExists = HRDocDA.IsExists(tc, Code);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException("Failed to get Skill due to " + e.Message, e);

                #endregion
            }

            return isExists;
        }

        #endregion
    }
}