using System;
using FairyGUI;
using Spine.Unity;
using BingoBrain.Core;
using UnityEngine;
using BingoBrain.HotFix;

namespace BingoBrain
{
    public class BingoEndUI : BaseUI
    {
        private BingoEndUICtrl ctrl;
        private BingoEndModel model;
        private FGUI.JBingoPlay.com_end ui;
        private Action closeCb;

        public BingoEndUI(BingoEndUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.BingoEndUI;
            this.ctrl = ctrl;
        }

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

        #region 生命周期

        protected override void OnInit()
        {
        }

        protected override void OnClose()
        {
            ui?.FadeOut();
            closeCb?.Invoke();
           
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.JBingoPlay.com_end;
        }

        protected override void OnOpenBefore(object args)
        {
            // FX.Instance.GetFx<SkeletonAnimation>(Fx_Type.spine_reward, sk =>
            // {
            //     var skeletal = sk;
            //     var goWrapper = new GoWrapper(skeletal.gameObject);
            //     // ui.com_endPop.gp_fx.SetNativeObject(goWrapper);
            //     skeletal.transform.localScale = Vector3.one * 110;
            //     skeletal.state.SetAnimation(0, "animation", true);
            //     // closeCb += () =>
            //     // {
            //     //     goWrapper.wrapTarget = null;
            //     //     ui.com_endPop.gp_fx.SetNativeObject(null);
            //     //     FX.Instance.RecFx(Fx_Type.spine_reward, skeletal);
            //     // };
            // });
        }

        protected override void OnOpen(object args)
        {
            ui?.FadeIn();
            InitView();
        }

        private void PlayAgain()
        {
            if (PreferencesMgr.Instance.CardBoardSum >= BingoCell.CardBoardCount)
            {
                PreferencesMgr.Instance.CardBoardSum -= BingoCell.CardBoardCount;
                CtrlCloseUI();
                GameDispatcher.Instance.Dispatch(BingoInfo.EndBingoGame, false);
                GameDispatcher.Instance.Dispatch(BingoInfo.StartBingoGame);
            }
            else
            {
                uiCtrlDispatcher.Dispatch(SkinInfo.MoreCardUI_Open);
            }
        }

        private void BackHome()
        {
            CtrlCloseUI();
            GameDispatcher.Instance.Dispatch(BingoInfo.EndBingoGame, true);
             GameHelper.ShowInterstitial("interstitial_endgameAd");
        }

        #endregion

        private void InitView()
        {
            if (BingoCell.isFirstGame)
            {
                PreferencesMgr.Instance.CardBoardIndex = 1;
            }
            else
            {
                if (PreferencesMgr.Instance.IsSecondEndShow)
                {
                    PreferencesMgr.Instance.IsSecondEndShow = false;
                }
            }
            if (GameHelper.IsGiftSwitch()) ui.com_endPop.gift.selectedIndex = 1;
            ui.com_endPop.cont_giftSwitch.selectedIndex = GameHelper.IsGiftSwitch() ? 0 : 1;
            ui.btn_home.onClick.Set(BackHome);
            ui.btn_playagain.onClick.Set(PlayAgain);
            ui.com_endPop.text_coin.text = GameHelper.GetItemSum(101, BingoCell.GetCoinSum);
            ui.com_endPop.text_cash.text = GameHelper.GetItemSum(102, BingoCell.GetCashSum);
            ui.btn_playagain.text_cardnum.text =
                string.Format(ui.btn_playagain.text_cardnum.text, BingoCell.CardBoardCount);
            Audio.Instance.PlayDynamicEffect("game_win");
            GameHelper.SetGObject("fourth", ui.btn_playagain);
            GameHelper.SetGuidePos("fourth",
                ui.btn_playagain.position + new Vector3(ui.btn_playagain.size.x, ui.btn_playagain.size.y) / 2);
        }
    }
}