using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using Ease.CoreV35.DataAccess;
using HRM.BO;

namespace HRM.DA
{
    #region AccessCard Service

    [Serializable]
    public class AccessCardService : ServiceTemplate, IAccessCardService
    {
        private void MapObject(AccessCard oAccessCard, DataReader oReader)
        {
            base.SetObjectID(oAccessCard, oReader.GetInt32("AccessCardID").Value);
            oAccessCard.CardNumber = oReader.GetString("CardNumber");
            oAccessCard.TypeID = oReader.GetInt32("TypeID", 0);
            oAccessCard.Status = (EnumCardStatus)oReader.GetUInt16("Status").Value;
            this.SetObjectState(oAccessCard, Ease.Core.ObjectState.Saved);
        }

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

        #region Service implementation

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

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

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

                #endregion
            }
        }

        public AccessCard Get(string CardNumber)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(AccessCardDA.Get(tc, CardNumber));
                AccessCard oAccessCard = null;
                if (oreader.Read())
                {
                    oAccessCard = this.CreateObject<AccessCard>(oreader);
                }

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

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

                #endregion
            }
        }

        public AccessCard Get(string CardNumber, TransactionContext tc)
        {
            try
            {
                DataReader oreader = new DataReader(AccessCardDA.Get(tc, CardNumber));
                AccessCard oAccessCard = null;
                if (oreader.Read())
                {
                    oAccessCard = this.CreateObject<AccessCard>(oreader);
                }

                oreader.Close();
                return oAccessCard;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<AccessCard> Get()
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(AccessCardDA.Get(tc));
                var accessCards = this.CreateObjects<AccessCard>(dr);
                dr.Close();
                tc.End();

                return accessCards;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<AccessCard> Get(EnumCardStatus status)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(AccessCardDA.Get(tc, status));
                var accessCards = this.CreateObjects<AccessCard>(dr);
                dr.Close();
                tc.End();

                return accessCards;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<AccessCard> Get(params EnumCardStatus[] status)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(AccessCardDA.Get(tc, status));
                var accessCards = this.CreateObjects<AccessCard>(dr);
                dr.Close();
                tc.End();

                return accessCards;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public int Save(AccessCard oAccessCard, TransactionContext tc)
        {
            try
            {
                if (oAccessCard.IsNew)
                {
                    int id = tc.GenerateID("AccessCard", "AccessCardID");
                    base.SetObjectID(oAccessCard, id);
                    AccessCardDA.Insert(tc, oAccessCard);
                }
                else
                {
                    AccessCardDA.Update(tc, oAccessCard);
                }

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

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

                #endregion
            }
        }

        public int Save(AccessCard oAccessCard)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                int id = Save(oAccessCard, tc);
                tc.End();
                return id;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public void Save(List<AccessCard> oAccessCards)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                foreach (AccessCard ac in oAccessCards)
                {
                    Save(ac, tc);
                }

                tc.End();
            }
            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);
                AccessCardDA.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 IsExist(string CardNumber)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                var isExist = AccessCardDA.IsExist(tc, CardNumber);
                tc.End();

                return isExist;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public void UpdateStatus(TransactionContext tc, int cardID, EnumCardStatus status, int payrollTypeId)
        {
            try
            {
                AccessCardDA.UpdateStatus(tc, cardID, status, payrollTypeId);
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public void UpdateStatus(int cardID, EnumCardStatus status, int payrollTypeId)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                AccessCardDA.UpdateStatus(tc, cardID, status, payrollTypeId);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }


        //for excel Upload
        public static void SaveForExcelUpload(TransactionContext tc, List<AccessCard> accessCards)
        {
            List<CardOperation> cardOperations = new List<CardOperation>();
            List<EmployeeWorkPlanSetup> empWorkPlans = new List<EmployeeWorkPlanSetup>();

            try
            {
                int id = tc.GenerateID("AccessCard", "AccessCardID");

                foreach (AccessCard ac in accessCards)
                {
                    // This is checked because we wil only save new access cards
                    if (ac.ID < id)
                    {
                        continue;
                    }

                    EmployeeWorkPlanSetup empWorkPaln = new EmployeeWorkPlanSetup();

                    AccessCardDA.Insert(tc, ac);

                    ac.CardOperation.CardNumber = ac.CardNumber;
                    ac.CardOperation.Status = EnumCardStatus.Attached;

                    cardOperations.Add(ac.CardOperation);
                }

                if (cardOperations.Count > 0)
                {
                    CardOperationService cardOPService = new CardOperationService();
                    cardOPService.Save(tc, cardOperations);
                }
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public  DataTable getCardInformation(int enumCardStatus)
        {
            DataTable dt = new DataTable();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                dt = AccessCardDA.GetCardInformation(tc, enumCardStatus);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
            return dt;
        }
        #endregion
    }

    #endregion
}