using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using System;
using System.Data;
using HRM.BO;

namespace HRM.DA
{
    #region SystemInformation Service

    public class SystemInformationService : ServiceTemplate, ISystemInformationService
    {
        public SystemInformationService()
        {
        }

        private void MapObject(SystemInformation oSystemInformation, DataReader oReader)
        {
            base.SetObjectID(oSystemInformation, 1);
            oSystemInformation.name = oReader.GetString("NAME");
            oSystemInformation.Code = oReader.GetString("Code");
            oSystemInformation.corporateAddress = oReader.GetString("CORPORATEADDRESS");
            oSystemInformation.factoryAddress = oReader.GetString("FACTORYADDRESS");
            oSystemInformation.TelephoneNo = oReader.GetString("TELEPHONE");
            oSystemInformation.email = oReader.GetString("EMAIL");
            //oSystemInformation.webAddress = oReader.GetString("WEBADDRESS");
            oSystemInformation.systemStartDate = oReader.GetDateTime("SYSTEMSTARTDATE").Value;
            oSystemInformation.TaxYearEndDate = oReader.GetDateTime("TAXYEARENDDATE").Value;
            oSystemInformation.NextPayProcessDate = oReader.GetDateTime("NEXTPAYPROCESSDATE").Value;
            oSystemInformation.maxYearOfService = oReader.GetInt32("MAXYEAROFSERVICE").Value;
            oSystemInformation.pFContriStaff = oReader.GetDouble("PFCONTRISTAFF").Value;
            oSystemInformation.pFContriCompany = oReader.GetDouble("PFCONTRICOMPANY").Value;
            oSystemInformation.pFInterest = oReader.GetDouble("PFINTEREST").Value;
            oSystemInformation.TaxParamID = oReader.GetInt32("PARAMID", 0);
            oSystemInformation.oTFilePath = oReader.GetString("OTFILEPATH");
            oSystemInformation.PFYearEndDate = oReader.GetDateTime("PFYEARENDDATE").Value;
            this.SetObjectState(oSystemInformation, Ease.Core.ObjectState.Saved);
        }

        public void MapHardPasswordSetup(HardPasswordSetup oItem, DataReader reader)
        {
            oItem.MaxLength = reader.GetInt32("MaxLength").GetValueOrDefault();
            oItem.MinLength = reader.GetInt32("MinLength").GetValueOrDefault();
            oItem.PasswordExpireNotificationDays =
                reader.GetInt32("PasswordExpireNotificationDays").GetValueOrDefault();
            oItem.PasswordExpireDays = reader.GetInt32("PasswordExpireDays").GetValueOrDefault();
            oItem.ContainLowercase = reader.GetBoolean("ContainLowercase").GetValueOrDefault();
            oItem.ContainNumber = reader.GetBoolean("ContainNumber").GetValueOrDefault();
            oItem.ContainSpecialCharacter = reader.GetBoolean("ContainSpecialCharacter").GetValueOrDefault();
            oItem.ContainUppercase = reader.GetBoolean("ContainUppercase").GetValueOrDefault();
            oItem.ContainLetter = reader.GetBoolean("ContainLetter").GetValueOrDefault();
            oItem.UserPasswordSame = reader.GetBoolean("UserPasswordSame").GetValueOrDefault();
        }

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

        public string GetDatabaseName()
        {
            string s = "";
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                //object obj = tc.ExecuteScalar("Select user from dual");
                //if (obj == DBNull.Value) s = "";
                //else
                //{
                s = Convert.ToString(tc.Connection.Database);
                //}
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return s;
        }

        #region Service implementation

        public SystemInformation Get()
        {
            SystemInformation oSystemInformation = new SystemInformation();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(SystemInformationDA.Get(tc));
                if (oreader.Read())
                {
                    oSystemInformation = this.CreateObject<SystemInformation>(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 oSystemInformation;
        }

        public void Get(TransactionContext tc, out SystemInformation sysInfo)
        {
            sysInfo = new SystemInformation();
            DataReader oreader = new DataReader(SystemInformationDA.Get(tc));

            if (oreader.Read())
                sysInfo = this.CreateObject<SystemInformation>(oreader);

            oreader.Close();
        }

        public void UpdCurrYerTaxParamter(int taxParameterID, int payrollTypeID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                SystemInformationDA.UpdateTaxParamId(tc, taxParameterID, payrollTypeID);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public DateTime GetServerDate()
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                string s = Convert.ToString(tc.Connection);
                DateTime ServerDate = SystemInformationDA.GetServerDate(tc, s);
                tc.End();
                return ServerDate;
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();

                ExceptionLog.Write(e);
                throw new ServiceException("Failed to Insert This System Information", e);

                #endregion
            }
        }

        public static DateTime GetServerDate(TransactionContext tc)
        {
            try
            {
                string s = Convert.ToString(tc.Connection);
                DateTime ServerDate = SystemInformationDA.GetServerDate(tc, s);
                return ServerDate;
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();

                ExceptionLog.Write(e);
                throw new ServiceException("Failed to Insert This System Information", e);

                #endregion
            }
        }

        public int Update(SystemInformation oSystemInformation)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                SystemInformationDA.Update(tc, oSystemInformation);
                tc.End();
                return oSystemInformation.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(HardPasswordSetup oItem)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                SystemInformationDA.Save(tc, oItem);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public DataTable GetPasswordSetup()
        {
            DataTable dt = new DataTable();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                dt = SystemInformationDA.GetPasswordSetup(tc);
                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;
        }

        public void GetHardPasswordSetup(TransactionContext tc, out HardPasswordSetup hardPasswordRule)
        {
            hardPasswordRule = new HardPasswordSetup();
            DataReader reader = new DataReader(SystemInformationDA.GetHardPasswordSetup(tc));

            while (reader.Read())
            {
                this.MapHardPasswordSetup(hardPasswordRule, reader);
            }

            reader.Close();
        }

        public string GetConnectionString()
        {
            TransactionContext tc = null;
            string connection = string.Empty;
            try
            {
                tc = TransactionContext.Begin(true);
                connection = tc.Connection.ConnectionString;


                if (connection != null)
                    return connection;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return connection;
        }

        public DataSet GetSystemInfo()
        {
            DataSet dsetDSB = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                dsetDSB = SystemInformationDA.GetSystemInfo(tc);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return dsetDSB;
        }

        #endregion
    }

    #endregion
}