using System;
using Wayne.Lib.Log;


namespace Wayne.ForecourtControl.Fusion
{
    class ConfigurationParams
    {

        private static string _inifile = getSINPPath("ini\\") + @"ForecourtServer.ini";
        // TODO read path from registry
        //private static string _inifile = @".\ForecourtServer.ini";
        public static string inifile
        {
            get { return _inifile; }
        }
        // TODO read path from registry
        private static string _tracePath = getSINPPath("trace\\");
        public static string tracePath
        {
            get { return _tracePath; }
        }
        private string _applicationSender = "";
        public string applicationSender
        {
            get { return _applicationSender; }
        }
        private string _workstationID = "";
        public string workstationID
        {
            get { return _workstationID; }
        }

        private string _sIPAddress = "";
        public string sIPAddress
        {
            get { return _sIPAddress; }
            set { _sIPAddress = value; }
        }
        private string _sIPAddress2 = "";
        public string sIPAddress2
        {
            get { return _sIPAddress2; }
            set { _sIPAddress2 = value; }
        }

        private int _iIPPortA = 0;
        public int iIPPortA
        {
            get { return _iIPPortA; }
            set { _iIPPortA = value; }
        }

        private int _iIPPortB = 0;
        public int iIPPortB
        {
            get { return _iIPPortB; }
            set { _iIPPortB = value; }
        }

        private int _iIPPortC = 0;
        public int iIPPortC
        {
            get { return _iIPPortC; }
            set { _iIPPortC = value; }
        }

        private long _heartbeatInterval = 10000;
        public long heartbeatInterval
        {
            get { return _heartbeatInterval; }
            set { _heartbeatInterval = value; }
        }

        private long _heartbeatTimeout = 30000;
        public long heartbeatTimeout
        {
            get { return _heartbeatTimeout; }
            set { _heartbeatTimeout = value; }
        }

        private long _headerEncryption = 1;
        public long headerEncryption
        {
            get { return _headerEncryption; }
            set { _headerEncryption = value; }
        }

        public static string getSINPPath(string subpath)
        {
            string path = "";
            //Microsoft.Win32.RegistryKey rk;
            //try
            //{
            //    using (rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Nuovo Pignone\\SINP"))
            //    {
            //        if (rk != null) path = (string)rk.GetValue("Directory", "");
            //    }
            //}
            //catch (Exception ex)
            //{
            //}
            //if (path == "")
            //{
            //    try
            //    {
            //        using (rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Dresser Wayne\\FUSION"))
            //        {
            //            if (rk != null) path = (string)rk.GetValue("Directory", "");
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //    }
            //}

            if (path == "")
            {
                if (path == "")
                {
                    try
                    {
                        System.Reflection.Assembly assembly = System.Reflection.Assembly.GetCallingAssembly();
                        path = System.IO.Path.GetDirectoryName(assembly.GetName().Name);
                    }
                    catch (Exception ex)
                    {
                    }
                }
                if (path == "")
                    path = ".\\";
                return path;
            }

            return path += "\\" + subpath;
        }


        public ConfigurationParams(DebugLogger debugLogger)
        {
            string sValue = "";
            if (debugLogger.IsActive())
                debugLogger.Add(string.Format("inifile='{0}'", inifile));

            // read applicationSender config parameter
            sValue = IniFile.IniReadValue(inifile, "FUSION-Connection", "applicationSender");
            try
            {
                if (sValue.Length > 0)
                    _applicationSender = sValue;
            }
            catch (Exception ex)
            {
                if (debugLogger.IsActive())
                    debugLogger.Add(string.Format("Exception reading applicationSender: {0}", ex.ToString()));
            }

            // read workstationID config parameter
            sValue = IniFile.IniReadValue(inifile, "FUSION-Connection", "workstationID");
            try
            {
                if (sValue.Length > 0)
                    _workstationID = sValue;
            }
            catch (Exception ex)
            {
                if (debugLogger.IsActive())
                    debugLogger.Add(string.Format("Exception reading workstationID: {0}", ex.ToString()));
            }

            // read IPPortA config parameter
            sValue = IniFile.IniReadValue(inifile, "FUSION-Connection", "Host");
            if (sValue.Length > 0) sIPAddress = sValue;
            sValue = IniFile.IniReadValue(inifile, "FUSION-Connection", "Host2");
            if (sValue.Length > 0) sIPAddress2 = sValue;
            if (debugLogger.IsActive()) 
                debugLogger.Add(string.Format("Host='{0}', Host2='{1}'", sIPAddress, sIPAddress2));

            // read IPPortA config parameter
            sValue = IniFile.IniReadValue(inifile, "FUSION-Connection", "IPPortA");
            try
            {
                if (sValue.Length > 0)
                    iIPPortA = Convert.ToInt16(sValue);
            }
            catch (Exception ex)
            {
                if (debugLogger.IsActive()) 
                    debugLogger.Add(string.Format("Exception reading IPPortA: {0}", ex.ToString()));
            }

            // read IPPortB config parameter
            sValue = IniFile.IniReadValue(inifile, "FUSION-Connection", "IPPortB");
            try
            {
                if (sValue.Length > 0)
                    iIPPortB = Convert.ToInt16(sValue);
            }
            catch (Exception ex)
            {
                if (debugLogger.IsActive()) 
                    debugLogger.Add(string.Format("Exception reading IPPortB: {0}", ex.ToString()));
            }

            // read IPPortC config parameter
            sValue = IniFile.IniReadValue(inifile, "FUSION-Connection", "IPPortC");
            try
            {
                if (sValue.Length > 0)
                    iIPPortC = Convert.ToInt16(sValue);
            }
            catch (Exception ex)
            {
                if (debugLogger.IsActive()) 
                    debugLogger.Add(string.Format("Exception reading IPPortC: {0}", ex.ToString()));
            }

            // read HeartbeatInterval config parameter
            sValue = IniFile.IniReadValue(inifile, "FUSION-Connection", "HeartbeatInterval");
            try
            {
                if (sValue.Length > 0)
                    heartbeatInterval = Convert.ToInt64(sValue);
            }
            catch (Exception ex)
            {
                if (debugLogger.IsActive()) 
                    debugLogger.Add(string.Format("Exception reading HeartbeatInterval: {0}", ex.ToString()));
            }

            // read HeartbeatTimeout config parameter
            sValue = IniFile.IniReadValue(inifile, "FUSION-Connection", "HeartbeatTimeout");
            try
            {
                if (sValue.Length > 0)
                    heartbeatTimeout = Convert.ToInt64(sValue);
            }
            catch (Exception ex)
            {
                if (debugLogger.IsActive()) 
                    debugLogger.Add(string.Format("Exception reading heartbeatTimeout: {0}", ex.ToString()));
            }

            // read headerEncryption config parameter
            sValue = IniFile.IniReadValue(inifile, "FUSION-Connection", "headerEncryption");
            try
            {
                if (sValue.Length > 0)
                    headerEncryption = Convert.ToInt32(sValue);
            }
            catch (Exception ex)
            {
                if (debugLogger.IsActive()) 
                    debugLogger.Add(string.Format("Exception reading headerEncryption: {0}", ex.ToString()));
            }
        }
    }
}