using NBC;
using UnityEngine;

namespace NBF.Setting
{
    [Sort(100)]
    public class LanguageSetting : MultiOption<int>
    {
        private int _defaultLanguage;
        public override string Name => "Language";
        public override string Group => SettingsDef.Group.Language;

        public override string Tab => SettingsDef.Tab.SoundAndLanguage;

        protected override int DefaultValue => (int)_defaultLanguage;

        protected override void OnInitialize()
        {
            var list = LanguageConst.LanguageList;
            var systemLanguage = Application.systemLanguage;
            var systemIndex = list.FindIndex(t => t.Language == systemLanguage);
            if (systemIndex < 0)
            {
                systemIndex = 0;
            }

            _defaultLanguage = (int)Lan.Inst.GetCurrentLanguage();
            for (int i = 0; i < list.Count; i++)
            {
                var lang = list[i];
                AddOption(lang.Name, i);
            }

            _defaultLanguage = systemIndex;
            var current = GetValue();
            if (current < 0 || current >= LanguageConst.LanguageList.Count)
            {
                SetValue(_defaultLanguage);
            }
        }

        protected override void OnApply()
        {
            var lang = LanguageConst.LanguageList[GetValue()];
            Lan.Inst.UseLanguage(lang.Language);
        }
    }
}