using Common.Global;
using Database.Database;
using Microsoft.Identity.Client;

namespace Esim.Apis.Singleton
{
    public class ConfigManager
    {

        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ConfigManager));

        private ModelContext dbContext;
        private List<Config> appConfigs = new List<Config>();

        private ConfigManager()
        {
            dbContext = new DbHelper().GetDbContext(GlobalConfig.Configuration!);
            log.Debug("ConfigManager initialized");
        }

        private static readonly object lockObj = new object();

        private static ConfigManager? instance;
        public static ConfigManager Instance => instance ??= new ConfigManager();

        public void Initialize()
        {
            appConfigs = dbContext.Configs.ToList();
            log.Debug("ConfigManager initialized");
        }

        public string GetConfigWebValue(string configName, string lang = "lo")
        {
            var config = appConfigs.FirstOrDefault(c => c.Name == configName && c.Type == "WEB");
            if (config != null)
            {
                log.Debug($"Config found: {configName} = {config.ValueGlobal}");
                return lang == "en" ? config.ValueGlobal : config.ValueLocal;
            }
            log.Warn($"Config not found: {configName}");
            return string.Empty;
        }

        public string GetConfigSmsValue(string configName, string lang = "lo")
        {
            var config = appConfigs.FirstOrDefault(c => c.Name == configName && c.Type == "SMS");
            if (config != null)
            {
                log.Debug($"Config found: {configName} = {config.ValueGlobal}");
                return lang == "en" ? config.ValueGlobal : config.ValueLocal;
            }
            log.Warn($"Config not found: {configName}");
            return string.Empty;
        }

        public string GetConfigAppValue(string configName, string lang = "te")
        {
            var config = appConfigs.FirstOrDefault(c => c.Name == configName && c.Type == "APP");
            if (config != null)
            {
                log.Debug($"Config found: {configName} = {config.ValueGlobal}");
                return lang == "en" ? config.ValueGlobal : config.ValueLocal;
            }
            log.Warn($"Config not found: {configName}");
            return string.Empty;
        }

        // loop 5p to refresh
        public void RefreshConfigs()
        {
            while (true)
            {
                try
                {
                    log.Debug($"ConfigManager refreshing...");
                    appConfigs = dbContext.Configs.ToList();
                    log.Debug("ConfigManager refreshed");
                }
                catch (Exception ex)
                {
                    log.Error("Error refreshing configs", ex);
                }
                Thread.Sleep(5 * 60 * 1000); // 5 minutes
            }
        }
    }
}
