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

namespace HRM.DA
{
    [Serializable]
    public class EmpMobileService : ServiceTemplate, IEmpMobileService
    {
        protected override T CreateObject<T>(DataReader dr)
        {
            EmpMobile attendanceTrack = new EmpMobile();
            MapObject(attendanceTrack, dr);
            return attendanceTrack as T;
        }

        private void MapObject(EmpMobile attendanceTrack, DataReader oReader)
        {
            base.SetObjectID(attendanceTrack, (oReader.GetInt32("EmpMobID").Value));
            attendanceTrack.EmpID = oReader.GetInt32("EmpID", 0);
            attendanceTrack.DeviceNO = oReader.GetString("DeviceNO");
            attendanceTrack.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            attendanceTrack.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            attendanceTrack.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            attendanceTrack.ModifiedDate = oReader.GetDateTime("ModifiedDate").HasValue
                ? oReader.GetDateTime("ModifiedDate").Value
                : DateTime.MinValue;
            attendanceTrack.IsActive = oReader.GetBoolean("IsActive", false);
            this.SetObjectState(attendanceTrack, Ease.Core.ObjectState.Saved);
        }

        #region IUserLocationService Members

        public List<EmpMobile> Get()
        {
            TransactionContext tc = null;
            List<EmpMobile> listAttendanceTrack = new List<EmpMobile>();
            
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(EmpMobileDA.Get(tc));

                listAttendanceTrack = this.CreateObjects<EmpMobile>(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 listAttendanceTrack;
        }
        public List<EmpMobile> Get(bool isActive)
        {
            TransactionContext tc = null;
            List<EmpMobile> empMobiles = new List<EmpMobile>();
            
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(EmpMobileDA.Get(tc, isActive));

                empMobiles = this.CreateObjects<EmpMobile>(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 empMobiles;
        }
        public DataSet getActiveDevice(bool isActive)
        {
            TransactionContext tc = null;
            DataSet ds = new DataSet();
            try
            {
                tc = TransactionContext.Begin(true);
                ds = EmpMobileDA.getActiveDevice(tc, isActive);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return ds;
        }
        
        public EmpMobile Get(int nID)
        {
            TransactionContext tc = null;
            EmpMobile attendanceTrack = new EmpMobile();
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(EmpMobileDA.Get(tc, nID));
                if (oreader.Read())
                {
                    attendanceTrack = this.CreateObject<EmpMobile>(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 attendanceTrack;
        }

        public List<EmpMobile> GetByEmployeeID(int employeeID)
        {
            TransactionContext tc = null;
            List<EmpMobile> listAttendanceTrack = new List<EmpMobile>();
            try
            {
                tc = TransactionContext.Begin(true);
                DataReader oreader = new DataReader(EmpMobileDA.GetByEmployeeID(tc, employeeID));

                listAttendanceTrack = this.CreateObjects<EmpMobile>(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 listAttendanceTrack;
        }

        public EmpMobile Get(string deviceNo)
        {
            TransactionContext tc = null;
            EmpMobile attendanceTrack = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(EmpMobileDA.Get(tc, deviceNo));
                if (oreader.Read())
                {
                    attendanceTrack = this.CreateObject<EmpMobile>(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 attendanceTrack;
        }

        public EmpMobile Get(int EmployeeID, string deviceNo)
        {
            TransactionContext tc = null;
            EmpMobile attendanceTrack = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(EmpMobileDA.Get(tc, EmployeeID, deviceNo));
                if (oreader.Read())
                {
                    attendanceTrack = this.CreateObject<EmpMobile>(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 attendanceTrack;
        }

        public int Save(EmpMobile obj)
        {
            TransactionContext tc = null;
            int oId = 0;
            try
            {
                tc = TransactionContext.Begin(true);
                if (obj.IsNew)
                {
                    int id = tc.GenerateID("EMPMOBILE", "EmpMobID");
                    base.SetObjectID(obj, id);
                    obj.SetAuditTrails();
                    oId = EmpMobileDA.Insert(tc, obj);
                }
                else
                {
                    oId = EmpMobileDA.Update(tc, obj);
                }

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

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

                #endregion
            }

            return oId;
        }
        public int Save(string ids)
        {
            TransactionContext tc = null;
            int oId = 0;
            try
            {
                tc = TransactionContext.Begin(true);
                EmpMobileDA.Update(tc, ids);
                tc.End();
                oId = 1;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return oId;
        }
        public int Delete(EmpMobile obj)
        {
            TransactionContext tc = null;
            int oId = 0;
            try
            {
                tc = TransactionContext.Begin(true);
                oId = EmpMobileDA.Delete(tc, obj);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return oId;
        }
        public int Delete(string ids)
        {
            TransactionContext tc = null;
            int oId = 0;
            try
            {
                tc = TransactionContext.Begin(true);
                EmpMobileDA.Delete(tc, ids);
                tc.End();
                oId = 1;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return oId;
        }
        public DataTable GetEmpForMobileLogin(int EmployeeID)
        {
            DataTable dataTableOfEmployee = new DataTable("Employee");
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataSet dataSet = EmpMobileDA.GetEmpForMobileLogin(tc, EmployeeID);
                dataTableOfEmployee = dataSet.Tables[0];
                dataSet.Dispose();
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }
            return dataTableOfEmployee;
        }

        public DataTable GetEmpForMobileLogin(string DeviceNo)
        {
            DataTable dataTableOfEmployee = new DataTable("Employee");
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataSet dataSet = EmpMobileDA.GetEmpForMobileLogin(tc, DeviceNo);
                dataTableOfEmployee = dataSet.Tables[0];
                dataSet.Dispose();
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }
            return dataTableOfEmployee;
        }

        public DataTable GetEmpForMobileLogin(string DeviceNo, int EmpID)
        {
            DataTable dataTableOfEmployee = new DataTable("Employee");
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataSet dataSet = EmpMobileDA.GetEmpForMobileLogin(tc, DeviceNo, EmpID);
                dataTableOfEmployee = dataSet.Tables[0];
                dataSet.Dispose();
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }
            return dataTableOfEmployee;
        }

        public DataTable GetEmpForMobileLoginByEmpNo(string  EmpNo)
        {
            DataTable dataTableOfEmployee = new DataTable("Employee");
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataSet dataSet = EmpMobileDA.GetEmpForMobileLoginByEmpNo(tc, EmpNo);
                dataTableOfEmployee = dataSet.Tables[0];
                dataSet.Dispose();
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }
            return dataTableOfEmployee;
        }

        public bool UnRegisterUserDevice( int EmpID, string DeviceNo)
        {
            bool success = false;
            TransactionContext tc = null;

            try
            {
                tc = TransactionContext.Begin();

                EmpMobileDA.UnRegisterUserDevice(tc, EmpID, DeviceNo);

                success = true;

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

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return success;
        }

        #endregion
    }
}