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

        private void MapObject(Skill oSkill, DataReader oReader)
        {
            base.SetObjectID(oSkill, oReader.GetInt32("SkillID").Value);
            oSkill.Code = oReader.GetString("Code");
            oSkill.Name = oReader.GetString("Name");
            oSkill.ParentID = oReader.GetInt32("ParentID", 0);

            oSkill.Tier = oReader.GetInt32("Tire").Value;
            oSkill.Sequence = oReader.GetInt32("SequenceNO").Value;
            oSkill.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            oSkill.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oSkill.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            oSkill.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oSkill.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oSkill, Ease.Core.ObjectState.Saved);
        }

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

        #region Service implementation

        public int Save(Skill oSkill)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oSkill.IsNew)
                {
                    int id = tc.GenerateID("Skill", "SkillID");
                    base.SetObjectID(oSkill, (id));
                    SkillDA.Insert(tc, oSkill);
                }
                else
                {
                    SkillDA.Update(tc, oSkill);
                }

                tc.End();

                return oSkill.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);
                SkillDA.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 Skill Get(int id)
        {
            Skill oSkill = null;

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

        public List<Skill> Get()
        {
            List<Skill> oSkill = new List<Skill>();

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

        public List<Skill> GetParents(EnumStatus status)
        {
            List<Skill> skills = new List<Skill>();

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

                DataReader dr = new DataReader(SkillDA.GetParents(tc, status));
                skills = this.CreateObjects<Skill>(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 skills;
        }

        public List<Skill> GetChilds(int parentID)
        {
            List<Skill> skills = new List<Skill>();

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

                DataReader dr = new DataReader(SkillDA.GetChilds(tc, parentID));
                skills = this.CreateObjects<Skill>(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 skills;
        }

        public bool IsExists(string Code)
        {
            TransactionContext tc = null;
            bool isExists = false;
            try
            {
                tc = TransactionContext.Begin(true);
                isExists = SkillDA.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
    }
}