using DataModel.Database; using DataModel.Database.Common; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; namespace Processor; public static class AppSettings { #region DeepL Settings #region DeepL Mode private static SettingDeepLMode CACHE_DEEPL_MODE = SettingDeepLMode.DISABLED; private static bool CACHE_DEEPL_MODE_IS_LOADED = false; public static async Task SetDeepLMode(SettingDeepLMode mode) { // Convert the enum to its int value: var intValue = (int)mode; // Update the cache: CACHE_DEEPL_MODE = mode; CACHE_DEEPL_MODE_IS_LOADED = true; // Get the database: await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); // Check, if the setting is already set: if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_MODE) is {} existingSetting) { existingSetting.IntegerValue = intValue; await db.SaveChangesAsync(); } // Does not exist, so create it: else { var setting = new Setting { Code = SettingNames.DEEPL_MODE, IntegerValue = intValue, }; await db.Settings.AddAsync(setting); await db.SaveChangesAsync(); } } public static async Task GetDeepLMode() { if (CACHE_DEEPL_MODE_IS_LOADED) return CACHE_DEEPL_MODE; // Get the database: await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); // Check, if the setting is already set: if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_MODE) is { } existingSetting) return (SettingDeepLMode) existingSetting.IntegerValue; // Does not exist, so create it: var setting = new Setting { Code = SettingNames.DEEPL_MODE, IntegerValue = (int)SettingDeepLMode.DISABLED, }; await db.Settings.AddAsync(setting); await db.SaveChangesAsync(); var mode = (SettingDeepLMode) setting.IntegerValue; CACHE_DEEPL_MODE = mode; CACHE_DEEPL_MODE_IS_LOADED = true; return mode; } #endregion #region DeepL API Key private static string CACHE_DEEPL_API_KEY = string.Empty; private static bool CACHE_DEEPL_API_KEY_IS_LOADED = false; public static async Task SetDeepLAPIKey(string apiKey) { // Update the cache: CACHE_DEEPL_API_KEY = apiKey; CACHE_DEEPL_API_KEY_IS_LOADED = true; // Get the database: await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); // Check, if the setting is already set: if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_API_KEY) is {} existingSetting) { existingSetting.TextValue = apiKey; await db.SaveChangesAsync(); } // Does not exist, so create it: else { var setting = new Setting { Code = SettingNames.DEEPL_API_KEY, TextValue = apiKey, }; await db.Settings.AddAsync(setting); await db.SaveChangesAsync(); } } public static async Task GetDeepLAPIKey() { // Get the database: await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); // Check, if the setting is already set: if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_API_KEY) is { } existingSetting) return existingSetting.TextValue; // Does not exist, so create it: var setting = new Setting { Code = SettingNames.DEEPL_API_KEY, TextValue = string.Empty, }; await db.Settings.AddAsync(setting); await db.SaveChangesAsync(); var key = setting.TextValue; CACHE_DEEPL_API_KEY = key; CACHE_DEEPL_API_KEY_IS_LOADED = true; return key; } #endregion #region DeepL Action private static SettingDeepLAction CACHE_DEEPL_ACTION = SettingDeepLAction.MANUAL; private static bool CACHE_DEEPL_ACTION_IS_LOADED = false; public static async Task SetDeepLAction(SettingDeepLAction action) { // Convert the enum to its int value: var intValue = (int)action; // Update the cache: CACHE_DEEPL_ACTION = action; CACHE_DEEPL_ACTION_IS_LOADED = true; // Get the database: await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); // Check, if the setting is already set: if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_ACTION) is {} existingSetting) { existingSetting.IntegerValue = intValue; await db.SaveChangesAsync(); } // Does not exist, so create it: else { var setting = new Setting { Code = SettingNames.DEEPL_ACTION, IntegerValue = intValue, }; await db.Settings.AddAsync(setting); await db.SaveChangesAsync(); } } public static async Task GetDeepLAction() { // Get the database: await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); // Check, if the setting is already set: if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_ACTION) is { } existingSetting) return (SettingDeepLAction) existingSetting.IntegerValue; // Does not exist, so create it: var setting = new Setting { Code = SettingNames.DEEPL_ACTION, IntegerValue = (int)SettingDeepLAction.MANUAL, }; await db.Settings.AddAsync(setting); await db.SaveChangesAsync(); var action = (SettingDeepLAction) setting.IntegerValue; CACHE_DEEPL_ACTION = action; CACHE_DEEPL_ACTION_IS_LOADED = true; return action; } #endregion #endregion #region Translation Settings #region Number of cultures private static int CACHE_NUMBER_OF_CULTURES = -1; public static async Task GetNumberCultures() { // When possible, use the cache: if (CACHE_NUMBER_OF_CULTURES > -1) return CACHE_NUMBER_OF_CULTURES; // Get the database: await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); // Count the number of cultures: var count = await db.Settings.CountAsync(n => n.Code == SettingNames.CULTURE); // We have at least one default culture: if(count == 0) { CACHE_NUMBER_OF_CULTURES = 1; return 1; } CACHE_NUMBER_OF_CULTURES = count; return count; } #endregion #region Get a culture // Cache the cultures: private static readonly Dictionary CACHE_CULTURES = new(); public static async Task GetCultureCode(int index) { // Check the cache: if (CACHE_CULTURES.TryGetValue(index, out var cultureCode)) return cultureCode; // Get the database: await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); // Get the culture code: var code = await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.CULTURE && n.IntegerValue == index); // When the culture code is not set & index = 1, use the default culture en-US: if(code is null && index == 1) { CACHE_CULTURES.Add(index, "en-US"); return "en-US"; } // Update the cache & return the code: var codeText = code?.TextValue ?? string.Empty; CACHE_CULTURES[index] = codeText; return codeText; } #endregion #region Set a culture public static async Task SetCultureCode(int index, string code) { // Update the cache: CACHE_CULTURES[index] = code; // Get the database: await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); // Check, if the setting is already set: if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.CULTURE && n.IntegerValue == index) is {} existingSetting) { existingSetting.TextValue = code; await db.SaveChangesAsync(); } // Does not exist, so create it: else { var setting = new Setting { Code = SettingNames.CULTURE, IntegerValue = index, TextValue = code, }; await db.Settings.AddAsync(setting); await db.SaveChangesAsync(); // Update the number of cultures cache: if(CACHE_NUMBER_OF_CULTURES > -1) CACHE_NUMBER_OF_CULTURES++; } } #endregion #endregion }