using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Reflection;

namespace Foo.Platform
{
    /// <summary>
    /// Installation Specific Settings. "HKLM\Software\Ooganizer",
    /// The Installer must put the correct values in there so that we know where our files
    /// are. ~Since now some of our assemblies are in the GAC, we require this.
    /// </summary>
    public class InstallationSpec
    {
        // Declare the Log4net Variable
        private static log4net.ILog Log = Logger.GetLog4NetInterface(MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// private statics
        /// </summary>
        private static string m_strInstallPath = "";
        //private static string m_strPlatformPath = "";
        //private static string m_strServerPath = "";
        //private static string m_strLogPath = "";
        private static RegistryKey s_OogyRootKey;

        /// <summary>
        /// Public Properties
        /// </summary>
        public static string InstallPath { get { return m_strInstallPath; } }
        //public static string PlatformPath { get { return m_strPlatformPath; } }
        //public static string ServerPath { get { return m_strServerPath; } }
        //public static string LogPath { get { return m_strLogPath; } }
 
        /// <summary>
        /// Static Constructor, immediatly gets our installation specific variables
        /// </summary>
        static InstallationSpec()
        {
            s_OogyRootKey = Registry.LocalMachine.OpenSubKey("Software\\Ooganizer",false);
            if (s_OogyRootKey != null)
            {
                object keyvalue = null;

                keyvalue = s_OogyRootKey.GetValue("InstallPath");
                if (isValidStr(keyvalue))
                    m_strInstallPath = keyvalue.ToString();

                //keyvalue = s_OogyRootKey.GetValue("PlatformPath");
                //if (isValidStr(keyvalue))
                //    m_strPlatformPath = keyvalue.ToString();

                //keyvalue = s_OogyRootKey.GetValue("ServerPath");
                //if (isValidStr(keyvalue))
                //    m_strServerPath = keyvalue.ToString();

                //keyvalue = s_OogyRootKey.GetValue("LogPath");
                //if (isValidStr(keyvalue))
                //    m_strLogPath = keyvalue.ToString();
            }
            else
            {                
                Log.Error(string.Format("{0}() - Couldn't Open Software\\Ooganizer Registry Key for Reading", MethodBase.GetCurrentMethod().Name));
            }
        }

        /// <returns>Returns true if passed in object is valid and not ""</returns>
        private static bool isValidStr(object oToValidate)
        {
            if ((oToValidate != null) && (oToValidate.ToString() != ""))
                return true;
            else
                return false;
        }
    }

    /// <summary>
    /// Use this class for Debug Specific Settings
    /// </summary>
    public class DebugSpec
    {
        public static bool SkipCaptionButtonStarter { get; private set; }

        private static RegistryKey s_DebugSpecRootKey;

        static DebugSpec()
        {
            SkipCaptionButtonStarter = false;

            #if DEBUG
            s_DebugSpecRootKey = Registry.LocalMachine.CreateSubKey("Software\\Ooganizer\\DebugSpec", RegistryKeyPermissionCheck.ReadWriteSubTree);
            SkipCaptionButtonStarter = GetValB("SkipCaptionButtonStarter");
            #endif 
        }

        private static bool GetValB(string strValKey)
        {
            try
            {
                string val = GetVal(strValKey);
                if (!String.IsNullOrEmpty(val))
                {
                    bool bVal = Boolean.Parse(val);
                    return bVal;
                }
            }
            catch (Exception) { /* ignore */ }
            return false;
        }

        private static string GetVal(string strValKey)
        {
            try
            {
                object keyvalue = null;
                keyvalue = s_DebugSpecRootKey.GetValue(strValKey);
                if (isValidStr(keyvalue))
                    return keyvalue.ToString();
            }
            catch (Exception) { }
            return String.Empty;
        }

        /// <returns>Returns true if passed in object is valid and not ""</returns>
        private static bool isValidStr(object oToValidate)
        {
            if ((oToValidate != null) && (oToValidate.ToString() != ""))
                return true;
            else
                return false;
        }
    }

    /// <summary>
    /// Temporary Values / "HKLM\Software\Ooganizer\TemporaryVal",
    /// This class is being used to temporarily store values in the registry and retrieve them.
    /// Useful for quick and dirty inter process communication. works out well for certain things.
    /// </summary>
    public class TemporaryVal
    {
        private static RegistryKey s_OogyRootKey;

        static TemporaryVal()
        {
            s_OogyRootKey = Registry.LocalMachine.OpenSubKey("Software\\Ooganizer", true);
        }

        public static void SetVal(string strValue)
        {
            try { s_OogyRootKey.SetValue("TemporaryVal", strValue); }
            catch (Exception) { }
        }

        public static void SetValI(int iValue)
        {
            try{ SetVal(iValue.ToString()); }
            catch (Exception){}
        }

        public static string GetVal()
        {
            try
            {
                object keyvalue = null;
                keyvalue = s_OogyRootKey.GetValue("TemporaryVal");
                if (isValidStr(keyvalue))
                    return keyvalue.ToString();
            }
            catch (Exception) { }
            return String.Empty;
        }

        public static int GetValI()
        {
            int I = 0;
            try
            {
                string strVal = GetVal();
                I = int.Parse(strVal); 
            }
            catch (Exception) { }
            return I;
        }

        public static void DeleteVal()
        {
            try { s_OogyRootKey.DeleteValue("TemporaryVal"); }
            catch (Exception) { }
        }

        /// <returns>Returns true if passed in object is valid and not ""</returns>
        private static bool isValidStr(object oToValidate)
        {
            if ((oToValidate != null) && (oToValidate.ToString() != ""))
                return true;
            else
                return false;
        }
    }
}
