using Misc;
using MultiLayout.UiModule;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace FLY.Thick.Base.UI.OnInit
{
    public class OnInitLanguage : IOnInit
    {
        ParamDictionary paramDictionary;
        public OnInitLanguage(ParamDictionary paramDictionary, int lv=0)
        {
            this.paramDictionary = paramDictionary;
            Level = lv;
            UpdateCulture();
        }

        public int Level { get; }

        public void OnInit()
        {
            paramDictionary.ValueChanged += ParamDictionary_ValueChanged;
        }

        private void ParamDictionary_ValueChanged(object sender, ParamDictionaryValueChangedEventArgs e)
        {
            if (e.Key == ParamDistItemKeys.Language) 
            {
                UpdateCulture();
            }
        }

        private void UpdateCulture()
        {
            CultureInfo info;
            string language = paramDictionary.GetValue(ParamDistItemKeys.Language, "zh-CN");

            if (language == "en-US")
            {
                info = new CultureInfo(language);
            }
            else
            {
                info = new CultureInfo("zh-CN");
            }
  
            Thread.CurrentThread.CurrentCulture = info;
            Thread.CurrentThread.CurrentUICulture = info;
        }
    }
}
