using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;

namespace HRD.WebApi
{
    public class MonitoringWebRequest
    {
        public string WebRequestMethod { get; set; }
        public string WebRequestPath { get; set; }
        public LogLevel LoggingLevel { get; set; } = LogLevel.Information;
    }

    //TODO: remove this. configure each with iconfiguretion using ioptions interface
    public static class WebApiConfig
    {
        private static string _dalConnectionstring;

        private static Dictionary<string, string> _customConfig = new Dictionary<string, string>();

        public static int ConnectionstringTimeoutInMin { get; set; } = 5;
        public static bool RaiseRepositoryExceptions { get; set; } = false; //if true, all Repository-Exception are thrown

        public static void Init(dynamic appSettings, System.Reflection.AssemblyName assemblyName = null)
        {
            AssemblyName = assemblyName?.Name ?? string.Empty;
            AssemblyVersion = assemblyName?.Version.ToString() ?? string.Empty;

            Connectionstring = appSettings["ConnectionStrings:sqlConnection"];
            ClientVersion = appSettings["AppConfig:ClientVersion"];
            OfficeFileServerUrl = appSettings["AppConfig:OfficeFileServerUrl"];
            IsLive = bool.Parse(appSettings["AppConfig:LDAP_WebAppGroup_Is_Live"]);

            //custom
            var list = appSettings.GetSection("CustomConfig").GetChildren();
            foreach (var item in list)
            {
                _customConfig.Add(item.Key, item.Value);
            }
        }

        public static LogLevel ToEnumLoggingLevel(string enumString)
        {
            if (string.IsNullOrEmpty(enumString))
            {
                return LogLevel.None;
            }

            return (LogLevel)Enum.Parse(typeof(LogLevel), enumString);
        }

        internal static void InitCustomSetting(Dictionary<string, string> customConfig)
        {
            _customConfig = customConfig;
        }

        public static string Connectionstring
        {
            get { return _dalConnectionstring; }
            set
            {
                _dalConnectionstring = value;
            }
        }

        public static string GetCustomconfig(string key)
        {
            _customConfig.TryGetValue(key, out string result);
            return result;
        }

        public static string AssemblyName { get; set; }
        public static string AssemblyVersion { get; set; }

        //OfficeFileServer
        public static string OfficeFileServerUrl { get; set; }

        //DAL

        public static string ClientVersion { get; set; }
        public static bool IsLive { get; set; }
        
        public static string ConnectionString(EN_ConnectionType connectionType = EN_ConnectionType.SQLServer)
        {
            switch (connectionType)
            {
                case EN_ConnectionType.SQLServer:
                    return Connectionstring;

                default:

                    return String.Empty;
            }
        }
    }
}