using CloudGaming.DtoModel.Game;

using Microsoft.IdentityModel.Tokens;

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudGaming.Code.AppExtend;
/// <summary>
/// app多语言
/// </summary>
public static class AppLanguage
{
    /// <summary>
    /// 多语言管理
    /// </summary>
    public static ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, string>>>> AppConfigLanguages { get; set; } = new ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, string>>>>();

    /// <summary>
    /// 游戏特殊配置
    /// </summary>
    public static ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, AppGameLanguage>>> AppConfigGameLanguages { get; set; } = new ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, AppGameLanguage>>>();
    private static string RedisAppLanguageGame = "language:game";
    /// <summary>
    /// 获取当前项目的多语言配置
    /// </summary>
    /// <param name="appConfig"></param>
    /// <returns></returns>
    public static ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, string>>> GetAppConfigLanguage(this AppConfig appConfig)
    {
        if (!AppConfigLanguages.TryGetValue(appConfig.Identifier, out var language))
        {
            language = new ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, string>>>();
            AppConfigLanguages.TryAdd(appConfig.Identifier, language);
        }
        return language;
    }

    /// <summary>
    /// 获取当前接口的多语言配置
    /// </summary>
    /// <param name="appConfig"></param>
    /// <param name="language">api地址</param>
    /// <returns></returns>
    public static ConcurrentDictionary<string, ConcurrentDictionary<string, string>> GetAppLanguage(this AppConfig appConfig, string language)
    {
        var dic = GetAppConfigLanguage(appConfig);
        var languages = appConfig.GetLanguages();
        if (!languages.Contains(language))
        {
            language = appConfig.DefaultLanguage;
        }
        if (!dic.TryGetValue(language, out var languageDic))
        {
            languageDic = new ConcurrentDictionary<string, ConcurrentDictionary<string, string>>();
            dic.TryAdd(language, languageDic);
        }

        return languageDic;
    }

    /// <summary>
    /// 获取当前接口的多语言配置
    /// </summary>
    /// <param name="appConfig"></param>
    /// <param name="language">api地址</param>
    /// <returns></returns>
    public static ConcurrentDictionary<string, string> GetAppApiLanguage(this AppConfig appConfig, string language, string api)
    {

        var dic = GetAppLanguage(appConfig, language);

        if (!dic.TryGetValue(api, out var languageDic))
        {
            languageDic = new ConcurrentDictionary<string, string>();
            dic.TryAdd(api, languageDic);
        }

        return languageDic;
    }

    /// <summary>
    /// 获取多语言选项
    /// </summary>
    /// <param name="dic"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string GetAppLanguageKey(this ConcurrentDictionary<string, string> dic, string key)
    {
        if (string.IsNullOrEmpty(key) || dic == null) { return key; }
        return dic.GetOrAdd(key, (l) => key);
    }



    /// <summary>
    /// 加载多语言配置
    /// </summary>
    /// <param name="appConfig"></param>
    /// <param name="json"></param>
    public static void InitAppLanguage(this AppConfig appConfig, string _language, string json)
    {
        if (AppConfigLanguages == null)
        {
            AppConfigLanguages = new ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, string>>>>();
        }
        //获取当前项目的多语言配置
        var appLanguage = AppConfigLanguages.GetOrAdd(appConfig.Identifier, (t) => new ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, string>>>());
        //初始化json数据
        var cacheLanguage = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(json);
        //重新定义当前项目的多语言
        var innerDict = new ConcurrentDictionary<string, ConcurrentDictionary<string, string>>();
        foreach (var item in cacheLanguage)
        {

            var t = new ConcurrentDictionary<string, string>(item.Value);
            innerDict.TryAdd(item.Key, t);
        }
        appLanguage.AddOrUpdate(_language, (t) => innerDict, (t, o) => innerDict);
    }

    /// <summary>
    /// 保存多语言配置
    /// </summary>
    /// <param name="appConfig"></param>
    /// <returns></returns>
    public static string LanguageToJson(AppConfig appConfig, string language)
    {
        var settings = new JsonSerializerSettings
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver()
        };
        var appLanguage = GetAppLanguage(appConfig, language);
        var json = JsonConvert.SerializeObject(appLanguage, settings);
        return json;
    }

    /// <summary>
    /// 获取当前接口的多语言配置
    /// </summary>
    /// <param name="appConfig"></param>
    /// <param name="language">api地址</param>
    /// <returns></returns>
    public static ConcurrentDictionary<string, AppGameLanguage> GetAppLanguageGame(this AppConfig appConfig, string language)
    {
        if (!AppConfigGameLanguages.TryGetValue(appConfig.Identifier, out var dic))
        {
            dic = new ConcurrentDictionary<string, ConcurrentDictionary<string, AppGameLanguage>>();
            AppConfigGameLanguages.TryAdd(appConfig.Identifier, dic);
        }
        var languages = appConfig.GetLanguages();
        if (!languages.Contains(language))
        {
            language = appConfig.DefaultLanguage;
        }
        if (!dic.TryGetValue(language, out var languageDic))
        {
            languageDic = new ConcurrentDictionary<string, AppGameLanguage>();
            dic.TryAdd(language, languageDic);
        }
        return languageDic;
    }

    /// <summary>
    /// 获取游戏多语言配置
    /// </summary>
    /// <param name="dic"></param>
    /// <param name="gameInfo"></param>
    /// <returns></returns>

    public static AppGameLanguage GetAppGameLanguage(this ConcurrentDictionary<string, AppGameLanguage> dic, GameInfo gameInfo)
    {
        if (gameInfo == null || string.IsNullOrEmpty(gameInfo.GameId))
        {
            return null;
        }
        if (dic == null)
        {
            return null;
        }
        return dic.GetOrAdd(gameInfo.GameId, (x) => new AppGameLanguage()
        {
            GameIntroduce = gameInfo.GameIntroduce,
            GameName = gameInfo.GameName,
            GameShare = gameInfo.GameShare
        });
    }

    /// <summary>
    /// 保存多语言配置
    /// </summary>
    /// <param name="appConfig"></param>
    /// <returns></returns>
    public static string LanguageGameToJson(AppConfig appConfig, string language)
    {
        var settings = new JsonSerializerSettings
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver()
        };
        var appLanguage = GetAppLanguageGame(appConfig, language);
        var json = JsonConvert.SerializeObject(appLanguage, settings);
        return json;
    }

    public static void SaveLanguageCache(CloudGamingBase cloudGamingBase)
    {
        var languages = cloudGamingBase.AppConfig.GetLanguages();
        foreach (var language in languages)
        {
            var json = LanguageToJson(cloudGamingBase.AppConfig, language);
            cloudGamingBase.RedisCache.StringSet($"language:common:{language}", json);
            var json1 = LanguageGameToJson(cloudGamingBase.AppConfig, language);
            cloudGamingBase.RedisCache.StringSet($"{RedisAppLanguageGame}:{language}", json1);
        }
    }
}
