using FGUI.JSettings;
using BingoBrain.Core;
using BingoBrain.HotFix;
using UnityEngine.Device;

namespace BingoBrain
{
    public class JThinkUI : BaseUI
    {
        private JThinkUICtrl ctrl;
        private JThinkModel model;
        private com_setting ui;

        public JThinkUI(JThinkUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.JThinkUI;
            this.ctrl = ctrl;
        }

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

        #region 生命周期

        protected override void OnInit()
        {
            model = ModuleBoardk.GetModel(ModelConst.JThinkModel) as JThinkModel;
        }

        protected override void OnClose()
        {
        }

        protected override void OnBind()
        {
            ui = baseUI as com_setting;
        }

        protected override void OnOpenBefore(object args)
        {
            Audio.Instance.PlayDynamicEffect("pop_open");
            ui.closeButton.SetClick(() =>
            {
                Audio.Instance.PlayDynamicEffect("button");
                CtrlCloseUI();
            });
            ui.btn_pri.SetClick(() => { UICtrlDispatcher.Instance.Dispatch(SkinInfo.JTermUI_Open); });
            ui.btn_terms.SetClick(() => { UICtrlDispatcher.Instance.Dispatch(SkinInfo.JTermUI_Open, true); });

            ui.btn_music.SetClick(OnClickMusicBtn);
            ui.btn_sound.SetClick(OnClickSoundBtn);
            ui.btn_how.SetClick(() =>
            {
                Audio.Instance.PlayDynamicEffect("button");
                uiCtrlDispatcher.Dispatch(SkinInfo.BingoWhatUI_Open);
            });
            ui.btn_contact.SetClick(GameHelper.OpenEmail);
            if (GameHelper.IsGiftSwitch())
            {
                ui.btn_record.visible = true;
                ui.btn_record.SetClick(() =>
                {
                    uiCtrlDispatcher.Dispatch(SkinInfo.MakeupRecordUI_Open);
                    CtrlCloseUI();
                });
            }
            ui.btn_website.SetClick(() =>
            {
                OpenBrowser.OpenURL("https://bingotor.com/");
            });
        }

        private void OnClickSoundBtn()
        {
            Audio.Instance.PlayDynamicEffect("button");
            model.IsOpenEffect = !model.IsOpenEffect;
            RefreshUI();
        }

        private void OnClickMusicBtn()
        {
            Audio.Instance.PlayDynamicEffect("button");
            model.IsOpenMusic = !model.IsOpenMusic;
            RefreshUI();
        }

        protected override void OnOpen(object args)
        {
            RefreshUI();
        }

        private void RefreshUI()
        {
            ui.text_version.SetVar("version", Application.version).FlushVars();

            ui.text_uid.text = "UID:" + GameHelper.GetLoginModel().uid;
            ui.btn_music.btn_on.cont_btn.selectedIndex = model.IsOpenMusic ? btn_on.Btn_on : btn_on.Btn_off;
            ui.btn_sound.btn_on.cont_btn.selectedIndex = model.IsOpenEffect ? btn_on.Btn_on : btn_on.Btn_off;
        }

        #endregion
    }
}