using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Yaulw.Assembly;
using Yaulw.Tools;

namespace PlutoServer.MSL
{
    public static class Configuration 
    {
        private const string DEFAULT_REGISTRATION_HOST_N_PORT_URL = "http://services.ndchealthvar.com/mobile1/REGURL.htm";
        private const string DEFAULT_REGISTRATION_HOST_URL = "services.ndchealthvar.com";
        private const int DEFAULT_REGISTRATION_CHANNEL_PORT = 443;
        private const string DEFAULT_CHECK_VERSION_URL = "http://services.ndchealthvar.com/mobile1/PUBLISH.htm";
        private const string DEFAULT_DOWNLOAD_SETUP_URL = "http://services.ndchealthvar.com/mobile1/Setup.exe";
        private const string DEFAULT_DOWNLOAD_MINISETUP_URL = "http://services.ndchealthvar.com/mobile1/SetupMini.exe";
        private const string DEFAULT_DETERMINE_EXTERNAL_IP_ADDRESS_URL = "http://services.ndchealthvar.com/GetIP/default.aspx";
        private static System.Configuration.Configuration _Configuration = null;        

        /// <summary>
        /// Load the PlutoServer.MSL.Exe configuration (writing Configuration class this way) allows
        /// DiagnoseMobile.exe (RegistrationAPI_Tester to reuse this code/link this class) and is therefore
        /// more generic way of retrieving the settings
        /// </summary>
        /// <returns></returns>
        public static bool Load(bool bReload)
        {
            try
            {
                if (_Configuration == null || bReload)
                {
                    string curPath = AssemblyW.SpecializedAssemblyInfo.GetAssemblyPath(AssemblyW.AssemblyST.Executing);
                    string ConfigNameNPath = PathNaming.PathEndsWithSlash(curPath) + "PlutoServer.MSL.exe";
                    _Configuration = ConfigurationManager.OpenExeConfiguration(ConfigNameNPath);
                    return (_Configuration != null);
                }
            }
            catch (Exception) { /* ignore */}
            return false;
        }

        #region Setting Getters

        /// <summary>
        /// Returns true if passed in object is valid and is not empty
        /// </summary>
        /// <param name="oToValidate">an object to validate</param>
        /// <returns>true if valid, false otherwise</returns>
        private static bool IsNotNullAndNotEmpty(object oToValidate)
        {
            if ((oToValidate != null) && (oToValidate.ToString() != String.Empty))
                return true;
            else
                return false;
        }

        /// <summary>
        /// Check to make sure the configuration is set and that there is a setting that is not blank
        /// </summary>
        /// <param name="setting"></param>
        /// <returns></returns>
        private static bool IsValidSetting(string setting)
        {
            if (_Configuration != null && !String.IsNullOrEmpty(setting) && IsNotNullAndNotEmpty(_Configuration.AppSettings.Settings[setting]))
            {
                var v = _Configuration.AppSettings.Settings[setting];
                return IsNotNullAndNotEmpty(v.Value);
            }
            return false;
        }

        /// <summary>
        /// Retrieves a setting or "" if setting is not valid/not found/not configured
        /// </summary>
        /// <param name="setting"></param>
        /// <returns></returns>
        private static string RetrieveSetting(string setting)
        {
            if (IsValidSetting(setting))
            {
                var v = _Configuration.AppSettings.Settings[setting];
                return v.Value.ToString();
            }
            return String.Empty;
        }

        #endregion

        /// <summary>
        /// Default Registration URL to determine what Registration server to use (allows us to be more dynamic)
        /// </summary>
        public static string REGISTRATION_HOST_N_PORT_URL
        {
            get
            {
                if (IsValidSetting("REGISTRATION_HOST_N_PORT_URL"))
                    return RetrieveSetting("REGISTRATION_HOST_N_PORT_URL");
                else
                    return DEFAULT_REGISTRATION_HOST_N_PORT_URL;
            }
        }

        /// <summary>
        /// Default Registration HOST URL To Use (can be configured via App.Config)
        /// </summary>
        public static string REGISTRATION_HOST_URL
        {
            get 
            {                
                if (IsValidSetting("REGISTRATION_HOST_URL"))
                    return RetrieveSetting("REGISTRATION_HOST_URL");
                else
                    return DEFAULT_REGISTRATION_HOST_URL;                                
            }
        }

        /// <summary>
        /// Default Registration Port To Use (can be configured via App.Config)
        /// </summary>
        public static int REGISTRATION_CHANNEL_PORT
        {
            get 
            {
                if (IsValidSetting("REGISTRATION_CHANNEL_PORT"))
                {
                    int nPort = 0;
                    if (int.TryParse(RetrieveSetting("REGISTRATION_CHANNEL_PORT"), out nPort) && nPort > 0)
                        return nPort;
                }                
                return DEFAULT_REGISTRATION_CHANNEL_PORT;                
            }
        }

        /// <summary>
        /// Default CHECK_VERSION_URL To Use (can be configured via App.Config)
        /// </summary>
        public static string CHECK_VERSION_URL
        {
            get
            {
                if (IsValidSetting("CHECK_VERSION_URL"))
                    return RetrieveSetting("CHECK_VERSION_URL");
                else
                    return DEFAULT_CHECK_VERSION_URL;
            }
        }

        /// <summary>
        /// Default DOWNLOAD_SETUP_URL To Use (can be configured via App.Config)
        /// </summary>
        public static string DOWNLOAD_SETUP_URL
        {
            get
            {
                if (IsValidSetting("DOWNLOAD_SETUP_URL"))
                    return RetrieveSetting("DOWNLOAD_SETUP_URL");
                else
                    return DEFAULT_DOWNLOAD_SETUP_URL;
            }            
        }

        /// <summary>
        /// Default DOWNLOAD_MINISETUP_URL To Use (can be configured via App.Config)
        /// </summary>
        public static string DOWNLOAD_MINISETUP_URL
        {
            get
            {
                if (IsValidSetting("DOWNLOAD_MINISETUP_URL"))
                    return RetrieveSetting("DOWNLOAD_MINISETUP_URL");
                else
                    return DEFAULT_DOWNLOAD_MINISETUP_URL;
            }             
        }

        /// <summary>
        /// Default DETERMINE_EXTERNAL_IP_ADDRESS_URL to use (can be configured via App.Config)
        /// </summary>
        public static string DETERMINE_EXTERNAL_IP_ADDRESS_URL
        {
            get
            {
                if (IsValidSetting("DETERMINE_EXTERNAL_IP_ADDRESS_URL"))
                    return RetrieveSetting("DETERMINE_EXTERNAL_IP_ADDRESS_URL");
                else
                    return DEFAULT_DOWNLOAD_MINISETUP_URL;
            }             
        }

    }
}
