using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace betrace_carfi_erp_driver.Common
{
    public static class SystemInfo
    {
        static public string BeTrace_SQL_Server = string.Empty;
        static public string BeTrace_SQL_Database = string.Empty;
        static public string BeTrace_SQL_System = string.Empty;
        static public string BeTrace_SQL_User = string.Empty;
        static public string BeTrace_SQL_Password = string.Empty;
        static public string BeTrace_SQL_Database_ConnectionString = string.Empty;
        static public string BeTrace_SQL_System_ConnectionString = string.Empty;

        static public string ERP_Type = string.Empty;
        static public string ERP_SQL_Server = string.Empty;
        static public string ERP_SQL_Database = string.Empty;
        static public string ERP_SQL_System = string.Empty;
        static public string ERP_SQL_User = string.Empty;
        static public string ERP_SQL_Password = string.Empty;
        static public string ERP_Auth_User = string.Empty;
        static public string ERP_Auth_Password = string.Empty;
        static public string ERP_Enterprise = string.Empty;
        static public string ERP_Exercise = string.Empty;
        static public string ERP_Section = string.Empty;
        static public string ERP_URL = string.Empty;
        static public string ERP_SQL_Database_ConnectionString = string.Empty;
        static public string ERP_SQL_System_ConnectionString = string.Empty;

        static public string LOGPATH = string.Empty;
        static public string LogDev = string.Empty;

        public static bool ReloadSystemInfo()
        {
            try
            {
                using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
                using (var key = hklm.OpenSubKey(@"SOFTWARE\BeTrace\ERP", RegistryKeyPermissionCheck.ReadSubTree))
                {
                    if (key != null)
                    {
                        LOGPATH = key.GetValue("LOGPATH") as string;
                        LogDev = key.GetValue("LogDev") as string;

                        BeTrace_SQL_Server = key.GetValue("BeTrace_SQL_Server") as string;
                        BeTrace_SQL_Database = key.GetValue("BeTrace_SQL_Database") as string;
                        BeTrace_SQL_System = key.GetValue("BeTrace_SQL_System") as string;
                        BeTrace_SQL_User = key.GetValue("BeTrace_SQL_User") as string;
                        BeTrace_SQL_Password = key.GetValue("BeTrace_SQL_Password") as string;

                        ERP_Type = key.GetValue("ERP_Type") as string;
                        ERP_SQL_Server = key.GetValue("ERP_SQL_Server") as string;
                        ERP_SQL_Database = key.GetValue("ERP_SQL_Database") as string;
                        ERP_SQL_System = key.GetValue("ERP_SQL_System") as string;
                        ERP_SQL_User = key.GetValue("ERP_SQL_User") as string;
                        ERP_SQL_Password = key.GetValue("ERP_SQL_Password") as string;
                        ERP_Auth_User = key.GetValue("ERP_Auth_User") as string;
                        ERP_Auth_Password = key.GetValue("ERP_Auth_Password") as string;
                        ERP_Enterprise = key.GetValue("ERP_Enterprise") as string;
                        ERP_Exercise = key.GetValue("ERP_Exercise") as string;
                        ERP_Section = key.GetValue("ERP_Section") as string;
                        ERP_URL = key.GetValue("ERP_URL") as string;

                        BeTrace_SQL_Database_ConnectionString = $"Server={BeTrace_SQL_Server};Database={BeTrace_SQL_Database};User Id={BeTrace_SQL_User};Password={BeTrace_SQL_Password};";
                        BeTrace_SQL_System_ConnectionString = $"Server={BeTrace_SQL_Server};Database={BeTrace_SQL_System};User Id={BeTrace_SQL_User};Password={BeTrace_SQL_Password};";

                        ERP_SQL_Database_ConnectionString = $"Server={ERP_SQL_Server};Database={ERP_SQL_Database};User Id={ERP_SQL_User};Password={ERP_SQL_Password};";
                        ERP_SQL_System_ConnectionString = $"Server={ERP_SQL_Server};Database={ERP_SQL_System};User Id={ERP_SQL_User};Password={ERP_SQL_Password};";

                        Logger.WriteEntry("Registry read successfully.", EventLogEntryType.Information);
                    }
                    else
                    {
                        Logger.WriteEntry("Error reading registry contents. Key was Null.", EventLogEntryType.Error);
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteEntry("Error reading registry contents. \n" + ex.Message, EventLogEntryType.Error);
                return false;
            }
            return true;
        }

    }
}
