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

namespace HRM.DA
{
    public class StoreService : ServiceTemplate, IStoreService
    {
        public StoreService()
        {
        }

        private void MapObject(Store oStore, DataReader oReader)
        {
            base.SetObjectID(oStore, oReader.GetInt32("StoreID").Value);
            oStore.Code = oReader.GetString("Code");
            oStore.Name = oReader.GetString("Name");
            oStore.Address = oReader.GetString("Address");
            oStore.Phone = oReader.GetString("Phone");
            //oStore.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            oStore.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oStore.CreatedDate = oReader.GetDateTime("CreatedDate") == null
                ? DateTime.Today
                : oReader.GetDateTime("CreatedDate").Value;
            oStore.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oStore.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oStore, Ease.Core.ObjectState.Saved);
        }

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

        #region Service implementation

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

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

                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);

                #endregion
            }

            return oStore;
        }

        public Store Get(int id)
        {
            Store oStore = null;

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

        public Store Get(string sCode)
        {
            Store oStore = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(StoreDA.Get(tc, sCode));
                if (oreader.Read())
                {
                    oStore = this.CreateObject<Store>(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 oStore;
        }

        public int GetMaxID()
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                return tc.GenerateID("Store", "StoreID") - 1;
            }
            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 List<Store> Get(EnumStatus status)
        {
            List<Store> stores = new List<Store>();

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

                DataReader dr = new DataReader(StoreDA.Get(tc, status));
                stores = this.CreateObjects<Store>(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 stores;
        }

        //public List<Designation> GetRelateWithEmpolyee(EnumStatus status, int payrollTypeID)
        //{
        //    List<Designation> designations = new List<Designation>();

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

        //        DataReader dr = new DataReader(DesignationDA.GetRelateWithEmpolyee(tc, status, payrollTypeID));
        //        designations = this.CreateObjects<Designation>(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 designations;
        //}

        public List<Store> GetWithSapCodes(EnumStatus status)
        {
            List<Store> stores = new List<Store>();

            return stores;
        }

        //public List<Designation> Get(EnumStatus status, string sIDs, string sTargetPropertyName, int payrollTypeID)
        //{
        //    List<Designation> grades = new List<Designation>();

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

        //        DataReader dr = new DataReader(DesignationDA.Get(tc, status, sIDs, sTargetPropertyName, payrollTypeID));
        //        grades = this.CreateObjects<Designation>(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 grades;
        //}

        public int Save(Store oStore)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oStore.IsNew)
                {
                    int id = tc.GenerateID("Store", "StoreID");
                    //int seqID = tc.GenerateID("Store", "SequenceNO");
                    base.SetObjectID(oStore, id);
                    //oStore.Sequence = seqID;
                    StoreDA.Insert(tc, oStore);
                }
                else
                {
                    StoreDA.Update(tc, oStore);
                }

                tc.End();
                return oStore.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, int UserID)
        //{
        //    TransactionContext tc = null;
        //    try
        //    {
        //        tc = TransactionContext.Begin(true);
        //        DesignationDA.Delete(tc, id , UserID);
        //        tc.End();
        //    }
        //    catch (Exception e)
        //    {
        //        #region Handle Exception
        //        if (tc != null)
        //            tc.HandleError();
        //        ExceptionLog.Write(e);
        //        throw new ServiceException(e.Message, e);
        //        #endregion
        //    }
        //}

        #endregion

        //For excel Upload
        //public static void SaveForUpload(TransactionContext tc, List<Designation> designations, int payrollTypeID, List<Designation> saveItems)
        //{
        //    try
        //    {
        //        foreach (Designation oDesignation in designations)
        //        {
        //            if (saveItems.FindIndex(x => x.ID == oDesignation.ID) < 0)
        //            {
        //                int seqNo = tc.GenerateID("Designation", "SequenceNO");
        //                oDesignation.Sequence = seqNo;


        //                if (oDesignation.Code == string.Empty)
        //                oDesignation.Code = GlobalFunctionService.GetMaxCode(tc,  "Designation", "Code");

        //                oDesignation.CreatedBy = oDesignation.CreatedBy;
        //                oDesignation.CreatedDate = DateTime.Now;
        //                DesignationDA.Insert(tc, oDesignation, payrollTypeID);
        //            }
        //            else
        //            {
        //                oDesignation.ModifiedBy = oDesignation.ModifiedBy;
        //                oDesignation.ModifiedDate = DateTime.Now;
        //                DesignationDA.Update(tc, oDesignation, payrollTypeID);
        //            }
        //        }
        //    }
        //    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);
                StoreDA.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 List<Store> GetAllStore(EnumStatus status, string code, string name)
        {
            List<Store> stores = new List<Store>();

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

                DataReader dr = new DataReader(StoreDA.GetAllStore(tc,status, code, name));
                stores = this.CreateObjects<Store>(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 stores;
        }
    }
}