using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Configuration;


namespace AyaNova.Util
{

    /// <summary>
    /// Contains config values from bootup
    /// </summary>
    internal static class ServerBootConfig
    {
        //############################################################################################################
        //STATIC HARD CODED DEFAULTS NOT SET THROUGH CONFIG
        internal const int FAILED_AUTH_DELAY = 3000;//ms
        internal const int REPORT_RENDERING_OPERATION_TIMEOUT = 20000;//ms
        //############################################################################################################

        //Diagnostic static values used during development, may not be related to config at all, this is just a convenient class to put them in
#if (DEBUG)
        internal static List<string> TranslationKeysRequested { get; set; }

#endif

        //TEST MODE - BOOT WILL ERASE DB AND GENERATE SAMPLE DATA EVERY TIME
        //** Not intended for end users
        internal static bool AYANOVA_SERVER_TEST_MODE { get; set; }
        internal static decimal AYANOVA_SERVER_TEST_MODE_TZ_OFFSET { get; set; }
        internal static string AYANOVA_SERVER_TEST_MODE_SEEDLEVEL { get; set; }

        //CONTENTROOTPATH
        //** Not intended for end users
        internal static string AYANOVA_CONTENT_ROOT_PATH { get; set; } //Note: set in startup.cs, not in program.cs as it requires startup IHostingEnvironment


        //LANGUAGE / Translation
        internal static string AYANOVA_DEFAULT_TRANSLATION { get; set; }
        //** Not intended for end users
        internal static long AYANOVA_DEFAULT_TRANSLATION_ID { get; set; } //internal setting set at boot by TranslationBiz::ValidateTranslations

        //API
        internal static string AYANOVA_JWT_SECRET { get; set; }
        internal static string AYANOVA_USE_URLS { get; set; }

        //DATABASE
        internal static string AYANOVA_DB_CONNECTION { get; set; }
        //** Not intended for end users
        internal static bool AYANOVA_PERMANENTLY_ERASE_DATABASE { get; set; }

        //FILE FOLDERS
        internal static string AYANOVA_FOLDER_USER_FILES { get; set; }
        internal static string AYANOVA_FOLDER_BACKUP_FILES { get; set; }
        internal static string AYANOVA_FOLDER_TEMPORARY_SERVER_FILES { get; set; }

        //BACKUP PG_DUMP PATH (IF NOT IN PATH ALREADY)
        internal static string AYANOVA_BACKUP_PG_DUMP_PATH { get; set; }

        //LOGGING
        internal static string AYANOVA_LOG_PATH { get; set; }
        internal static string AYANOVA_LOG_LEVEL { get; set; }
        internal static bool AYANOVA_LOG_ENABLE_LOGGER_DIAGNOSTIC_LOG { get; set; }

        //SECURITY
        internal static string AYANOVA_SET_SUPERUSER_PW { get; set; }


        /// <summary>
        /// Populate the config from the configuration found at boot
        /// called by program.cs
        /// </summary>
        /// <param name="config"></param>
        internal static void SetConfiguration(IConfigurationRoot config)
        {

#if (DEBUG)
            TranslationKeysRequested = new List<string>();
#endif

            bool? bTemp = null;

            #region SERVER BASICS

            //TEST MODE?
            bTemp = config.GetValue<bool?>("AYANOVA_SERVER_TEST_MODE");
            AYANOVA_SERVER_TEST_MODE = (null == bTemp) ? false : (bool)bTemp;
            AYANOVA_SERVER_TEST_MODE_SEEDLEVEL = config.GetValue<string>("AYANOVA_SERVER_TEST_MODE_SEEDLEVEL");
            AYANOVA_SERVER_TEST_MODE_SEEDLEVEL = string.IsNullOrWhiteSpace(AYANOVA_SERVER_TEST_MODE_SEEDLEVEL) ? "small" : AYANOVA_SERVER_TEST_MODE_SEEDLEVEL;
            decimal? dTemp = config.GetValue<decimal?>("AYANOVA_SERVER_TEST_MODE_TZ_OFFSET");
            AYANOVA_SERVER_TEST_MODE_TZ_OFFSET = (null == dTemp) ? 0 : (decimal)dTemp;

            //LANGUAGE
            //TranslationBiz will validate this later at boot pfc and ensure a sane default is set (English)
            AYANOVA_DEFAULT_TRANSLATION = config.GetValue<string>("AYANOVA_DEFAULT_TRANSLATION");
            AYANOVA_DEFAULT_TRANSLATION = string.IsNullOrWhiteSpace(AYANOVA_DEFAULT_TRANSLATION) ? "en" : AYANOVA_DEFAULT_TRANSLATION;
            string lowTranslation = AYANOVA_DEFAULT_TRANSLATION.ToLowerInvariant();
            switch (lowTranslation)
            {
                case "en":
                case "english":
                    AYANOVA_DEFAULT_TRANSLATION = "en";
                    break;
                case "de":
                case "deutsch":
                case "german":
                    AYANOVA_DEFAULT_TRANSLATION = "de";
                    break;
                case "es":
                case "español":
                case "spanish":
                    AYANOVA_DEFAULT_TRANSLATION = "es";
                    break;
                case "fr":
                case "français":
                case "french":
                    AYANOVA_DEFAULT_TRANSLATION = "fr";
                    break;
                default:
                    AYANOVA_DEFAULT_TRANSLATION = "en";
                    break;
            }




            //LOGLEVEL
            AYANOVA_LOG_LEVEL = config.GetValue<string>("AYANOVA_LOG_LEVEL");
            AYANOVA_LOG_LEVEL = string.IsNullOrWhiteSpace(AYANOVA_LOG_LEVEL) ? "Info" : AYANOVA_LOG_LEVEL;

            //LOGGING DIAGNOSTIC LOG
            bTemp = config.GetValue<bool?>("AYANOVA_LOG_ENABLE_LOGGER_DIAGNOSTIC_LOG");
            AYANOVA_LOG_ENABLE_LOGGER_DIAGNOSTIC_LOG = (null == bTemp) ? false : (bool)bTemp;

            //PORT / API
            AYANOVA_USE_URLS = config.GetValue<string>("AYANOVA_USE_URLS");
            AYANOVA_USE_URLS = string.IsNullOrWhiteSpace(AYANOVA_USE_URLS) ? "http://*:7575" : AYANOVA_USE_URLS;

            AYANOVA_JWT_SECRET = config.GetValue<string>("AYANOVA_JWT_SECRET");

            //DB
            AYANOVA_DB_CONNECTION = config.GetValue<string>("AYANOVA_DB_CONNECTION");
            bTemp = config.GetValue<bool?>("AYANOVA_PERMANENTLY_ERASE_DATABASE");
            AYANOVA_PERMANENTLY_ERASE_DATABASE = (null == bTemp) ? false : (bool)bTemp;


            //FOLDERS
            //Log folder
            AYANOVA_LOG_PATH = config.GetValue<string>("AYANOVA_LOG_PATH");

            if (AYANOVA_LOG_PATH == null)
            {
                //DEFAULT LOG PATH
                var currentDir = Directory.GetCurrentDirectory();
                AYANOVA_LOG_PATH = Path.Combine(currentDir, "logs");
            }
            else
            {
                AYANOVA_LOG_PATH = Path.Combine(AYANOVA_LOG_PATH, "logs");
            }

            //(note, startup.cs ensures these folders exist via FileUtil because we need IHostingEnvironment)
            //UserFiles
            AYANOVA_FOLDER_USER_FILES = config.GetValue<string>("AYANOVA_FOLDER_USER_FILES");

            //BackupFiles
            AYANOVA_FOLDER_BACKUP_FILES = config.GetValue<string>("AYANOVA_FOLDER_BACKUP_FILES");

            //TemporaryFiles
            AYANOVA_FOLDER_TEMPORARY_SERVER_FILES = config.GetValue<string>("AYANOVA_FOLDER_TEMPORARY_SERVER_FILES");

            //pgdump backup utility path
            AYANOVA_BACKUP_PG_DUMP_PATH = config.GetValue<string>("AYANOVA_BACKUP_PG_DUMP_PATH");

            //backdoor back door password superuser reset
            AYANOVA_SET_SUPERUSER_PW = config.GetValue<string>("AYANOVA_SET_SUPERUSER_PW");

            #endregion server BASICS



        }


        //Fetch first url from list of urls (used by generator)
        internal static string FirstOfAyaNovaUseUrls
        {
            get
            {
                if (string.IsNullOrWhiteSpace(AYANOVA_USE_URLS))
                { return null; }

                if (!AYANOVA_USE_URLS.Contains(";"))
                {
                    return AYANOVA_USE_URLS.Replace("*", "localhost");
                }
                var s = AYANOVA_USE_URLS.Split(';');
                return s[0].Replace("*", "localhost");

            }
        }





    }//eoc


}//eons