using System;
using System.Linq;
using DG.Tweening;
using FairyGUI;
using FGUI.ZM_Setting_07;
using UnityEngine;

namespace RedHotRoast
{
    public class LanguageViewUI : BaseUI
    {
        private LanguageViewUICtrl ctrl;
        private LanguageViewModel model;

        private int total_item = 8;
        // 8种语言
        private SystemLanguage[] languages = new SystemLanguage[8] { 
            SystemLanguage.English, SystemLanguage.French, SystemLanguage.German,
            SystemLanguage.Spanish, SystemLanguage.Portuguese, SystemLanguage.Japanese, 
            SystemLanguage.Korean, SystemLanguage.Russian
        };

        private string[] language_names = new string[8] {
            "English", "Français", "Deutsch", "Español", "Português", "日本語", "한국어", "Русский язык"
        };

        private FGUI.ZM_Setting_07.com_language ui;

        public LanguageViewUI(LanguageViewUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.MenuUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "ZM_Setting_07";
            uiInfo.assetName = "com_language";
            uiInfo.layerType = UILayerType.Popup;
            uiInfo.isNeedOpenAnim = false;
            uiInfo.isNeedCloseAnim = false;
            uiInfo.isNeedUIMask = true;
        }

        #region 生命周期

        protected override void OnInit()
        {
            model = moduleManager.GetModel(ModelConst.LanguageViewModel) as LanguageViewModel;
        }

        protected override void OnClose()
        {
            CommonHelper.FadeOut(ui);
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.ZM_Setting_07.com_language;
        }

        protected override void OnOpenBefore(object args)
        {
            InitView();
            ui.btn_close.SetClick(CtrlCloseUI);
        }

        protected override void OnOpen(object args)
        {
            // CommonHelper.FadeIn(ui);

        }

        protected override void OnHide()
        {
        }

        protected override void OnDisplay(object args)
        {
        }

        #endregion

        #region 消息

        protected override void AddListener()
        {
        }

        protected override void RemoveListener()
        {
        }

        #endregion

        private void InitView()
        {
            ui.list.itemRenderer = UpdateItem;
            ui.list.numItems = total_item;
        }

        private void UpdateItem(int index, GObject items)
        {
            var currentIndex = index + 1;
            var btn_language = items as btn_language;

            btn_language.select.selectedIndex = 0;
            btn_language.lab_name.text = language_names[index];

            SystemLanguage code = Language.GetSystemLanguageCode(Language.getCurrentLanguage());

            if (code == languages[index])
            {
                btn_language.select.selectedIndex = 1;
            }

            btn_language.SetClick(() =>
            {
                string code = Language.getISOCode(languages[index]);
                Language.setCurrentLanguage(code);
                InitView();
            });
        }

        
    }
}