using HRM.BO;
using HRM.DA;
using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.AspNetCore.Http;

namespace HRM.DA
{
    [Serializable]
    public class DepartmentService : ServiceTemplate, IDepartmentService
    {
        #region Private functions and declaration

        #endregion

        public DepartmentService()
        {
        }

        private void MapObject(Department oDepartment, DataReader oReader)
        {
            base.SetObjectID(oDepartment, oReader.GetInt32("DepartmentID").Value);
            oDepartment.Code = oReader.GetString("code");
            oDepartment.Name = oReader.GetString("description");
            oDepartment.CostCenter = oReader.GetString("CostCenter");
            oDepartment.ParentID = oReader.GetInt32("parentID");
            oDepartment.ParentsID = oReader.GetString("parentsID");
            oDepartment.Sequence = oReader.GetInt32("SequenceNo").Value;
            oDepartment.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            oDepartment.Tier = oReader.GetInt32("TIRE").Value;
            oDepartment.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oDepartment.CreatedDate = oReader.GetDateTime("CreationDate") == null
                ? DateTime.Today
                : oReader.GetDateTime("CreationDate").Value;
            oDepartment.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oDepartment.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oDepartment, Ease.Core.ObjectState.Saved);
        }

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

        protected Department CreateObject(DataReader oReader)
        {
            Department oDepartment = new Department();
            MapObject(oDepartment, oReader);
            return oDepartment;
        }

        public List<Department> Get(EnumStatus status, int payrollTypeID)
        {
            List<Department> departments = new List<Department>();

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

                DataReader dr = new DataReader(DepartmentDA.Get(tc, status, payrollTypeID));
                departments = this.CreateObjects<Department>(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 departments;
        }

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

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

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

                #endregion
            }

            return oDepartment;
        }

        public List<Department> GetAll(int payrollTypeID)
        {
            List<Department> departments = null;
            TransactionContext tc = null;

            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DepartmentDA.GetAll(tc));
                departments = this.CreateObjects<Department>(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 departments;
        }

        public List<Department> GetAll()
        {
            List<Department> departments = null;
            TransactionContext tc = null;

            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DepartmentDA.GetAll(tc));
                departments = this.CreateObjects<Department>(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 departments;
        }

        public static void SaveForUpload(TransactionContext tc, List<Department> deptss, List<Department> saveItems)
        {
            try
            {
                string parentcode = "";
                foreach (Department item in deptss)
                {
                    if (saveItems.FindIndex(x => x.ID == item.ID) < 0)
                    {
                        int seqNo = tc.GenerateID("department", "SequenceNo");
                        item.Sequence = seqNo;
                        parentcode = string.Empty;
                        if (item.ParentID != null && item.ParentID != 0)
                            parentcode = deptss.Find(o => o.ID == item.ParentID).Code;
                        if (item.Code == string.Empty)
                        {
                            item.Code = GlobalFunctionService.GetMaxCodeofTree(tc, "department", "codeautogenerate", "Department",
                                     "Code", "DepartmentID", "PARENTID", parentcode, item.Tier);
                        }
                        item.CreatedDate = DateTime.Now;
                        DepartmentDA.Insert(tc, item);
                    }
                    else
                    {
                        //DepartmentDA.Update(tc, item);
                    }
                }
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<Department> GetByDeptIDs(string deptIds)
        {
            List<Department> departments = null;
            TransactionContext tc = null;

            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(DepartmentDA.GetByDeptIds(tc, deptIds));
                departments = this.CreateObjects<Department>(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 departments;
        }

        public List<Department> GetChield(int parentID)
        {
            throw new NotImplementedException();
        }

        public List<Department> GetParents(EnumStatus status,int payrolltypeid)
        {
            List<Department> depts = new List<Department>();

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

                DataReader dr = new DataReader(DepartmentDA.GetParent(tc, status, payrolltypeid));
                depts = this.CreateObjects<Department>(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 depts;
        }

        public List<Department> GetByTier(int tier, int id)
        {
            throw new NotImplementedException();
        }

        public List<Department> GetByTier(int tier)
        {
            throw new NotImplementedException();
        }

        public int Save(Department item)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (item.IsNew)
                {
                    int id = tc.GenerateID("Department", "DepartmentID");
                    base.SetObjectID(item, id);
                    int sequenceId = tc.GenerateID("Department", "SequenceNO");
                    item.Sequence = sequenceId;

                    DepartmentDA.Insert(tc, item);
                }
                else
                {
                    DepartmentDA.Update(tc, item);
                }

                tc.End();
                return item.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<Department> _oDepartments)
        {
            throw new NotImplementedException();
        }

        public void Delete(int id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                DepartmentDA.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 Department Get(string sCode)
        {
            throw new NotImplementedException();
        }

        public List<Department> GetParentLessChilds()
        {
            throw new NotImplementedException();
        }

        public string GetNextCode(int tier, int parentid)
        {
            TransactionContext tc = null;
            string code = "";
            string parentCode = "";
            try
            {
                if (parentid != -1)
                {
                    Department parent = this.Get((int)parentid);
                    if (parent != null)
                        parentCode = parent.Code;
                }

                tc = TransactionContext.Begin();
                code = GlobalFunctionService.GetMaxCodeofTree(tc, "department", "codeautogenerate", "Department",
                    "Code", "DepartmentID", "PARENTID", parentCode, tier);
                //code = GlobalFunctionService.GetMaxCodeofTree(tc, "costcenter", "codeautogenerate", "costcenter",
                //    "Code", "costcenterid", "PARENTID", parentCode, tier);
                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 string GetNextCode(int nteir)
        //{
        //    throw new NotImplementedException();
        //}

        public DataSet GetForTree()
        {
            throw new NotImplementedException();
        }

        public DataSet GetDepartmentWiseManpower(string sParam)
        {
            throw new NotImplementedException();
        }

        public List<Department> GetAllDepartment(int payrollTypeID, EnumStatus status, string code, string name)
        {
            List<Department> departments = new List<Department>();

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

                DataReader dr = new DataReader(DepartmentDA.GetAllDepartment(tc, payrollTypeID, status, code, name));
                departments = this.CreateObjects<Department>(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 departments;
        }
    }
}