using PublicTools.XMLDataBase;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEngine;
using XRTool.UI;

namespace XRTool.Util
{
    /// <summary>
    /// 语言包的配置
    /// </summary>
    public class LanguagePackConf
    {
        private string language = "简体中文";
        private string packagePath;
        /// 语言包的名称
        /// </summary>
        public string Language { get => language; set => language = value; }
        /// <summary>
        /// 语言包的路径
        /// </summary>
        public string PackagePath { get => packagePath; set => packagePath = value; }
        public Dictionary<string, LanguageConf> LanguageMap
        {
            get
            {
                if (languageMap == null)
                {
                    languageMap = new Dictionary<string, LanguageConf>();
                }
                return languageMap;
            }
        }
        public Dictionary<string, LanguageConf> ChineseMap
        {
            get
            {
                if (chineseMap == null)
                {
                    chineseMap = new Dictionary<string, LanguageConf>();
                }
                return chineseMap;
            }
        }
        /// <summary>
        /// 语言文本映射
        /// </summary>
        private Dictionary<string, LanguageConf> languageMap;
        /// <summary>
        /// 语言文本映射
        /// </summary>
        private Dictionary<string, LanguageConf> chineseMap;
        public event Action<LanguagePackConf> OnReadComplete;
        /// <summary>
        /// 新旧编号
        /// </summary>
        public static Dictionary<string, string> numMap;
        public static string NULL = "null";
        /// <summary>
        /// 读取语言包，异步操作，读完后有对应的数据
        /// </summary>
        public void ReadLanguageMap()
        {
            LanguageMap.Clear();
            ChineseMap.Clear();
            string path = Path.Combine(Application.streamingAssetsPath, "Language", packagePath);
            DataConfMgr<LanguageConf>.Instance.OpenData(path, true);
            DataConfMgr<LanguageConf>.Instance.TableHelper.readConfComplete -= OnReadConfComplete;
            DataConfMgr<LanguageConf>.Instance.TableHelper.readConfComplete += OnReadConfComplete;
            DataConfMgr<LanguageConf>.Instance.OpenTable("PriKey");
        }
        private void OnReadConfComplete()
        {
            InitLanguage(DataConfMgr<LanguageConf>.Instance.TableHelper.DataList);
            OnReadComplete?.Invoke(this);
        }

        public void InitLanguage(List<LanguageConf> list)
        {
            LanguageMap.Clear();
            ChineseMap.Clear();
            bool isInitNum = (numMap == null);
            if (isInitNum)
            {
                numMap = new Dictionary<string, string>();
            }
            if (list != null)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (!LanguageMap.ContainsKey(list[i].PriKey))
                    {
                        LanguageMap.Add(list[i].PriKey, list[i]);
                        string[] strs = list[i].AudioPath.Split('；');
                        if (isInitNum && !string.IsNullOrEmpty(list[i].AudioPath) && list[i].AudioPath != NULL)
                        {
                            for (int j = 0; j < strs.Length; j++)
                            {
                                    if (strs[j].Contains("T"))
                                    {

                                        if (!numMap.ContainsKey(strs[j]))
                                        {
                                            numMap.Add(strs[j], list[i].PriKey);
                                        }
                                    }
                                    else
                                    {
                                        if (!numMap.ContainsKey("T" + strs[j]))
                                        {
                                            numMap.Add("T"+strs[j], list[i].PriKey);
                                        }
                                    }
                            }
                        }
                    }
                    if (!ChineseMap.ContainsKey(list[i].Message))
                    {
                        ChineseMap.Add(list[i].Message, list[i]);
                    }
                }
            }
        }
        /// <summary>
        /// 获取对应的语言文本的配置
        /// 
        /// </summary>
        /// <param name="prikey"></param>
        public LanguageConf GetLanguage(string prikey)
        {
            if (!string.IsNullOrEmpty(prikey))
            {
                if (LanguageMap.ContainsKey(prikey))
                {
                    return LanguageMap[prikey];
                }
                else if (ChineseMap.ContainsKey(prikey))
                {
                    return ChineseMap[prikey];
                }
                else if (numMap != null && numMap.ContainsKey(prikey))
                {
                    return GetLanguage(numMap[prikey]);
                }
            }
            //UnityLog.LogError("GetLanguage=>Error:" + prikey);
            return null;
        }
    }
}