using System;
using BingoBrain.Core;
using BingoBrain.HotFix;
using DG.Tweening;
using FGUI.ACommon;
using Spine.Unity;
using UnityEngine;

namespace BingoBrain
{
    public class StarRewardUI : BaseUI
    {
        private StarRewardUICtrl ctrl;
        private StarRewardModel model;
        private FGUI.JRewardPop.com_StarReward ui;

        public StarRewardUI(StarRewardUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.StarRewardUI;
            this.ctrl = ctrl;
        }

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

        #region 生命周期
        protected override void OnInit()
        {
            //model = ModuleManager.Instance.GetModel(ModelConst.StarRewardModel) as StarRewardModel;
        }

        protected override void OnClose()
        {
            CloseCb?.Invoke();
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.JRewardPop.com_StarReward;
        }
        private bool is_three = false;
        protected override void OnOpenBefore(object args)
        {
            if (args != null && (bool)args) is_three = true;
            InitView();
        }

        protected override void OnOpen(object args)
        {
        }

        protected override void OnHide()
        {
        }

        protected override void OnDisplay(object args)
        {
        }
        #endregion

        #region 消息
        protected override void AddListener()
        {

        }
        protected override void RemoveListener()
        {

        }
        #endregion

        //初始化页面逻辑
        private void InitView()
        {

            if (Screen.safeArea.y != 0)
            {//刘海屏
                ui.reward_end.y += Screen.safeArea.y;
            }
            decimal cash = CardEntity.GetSumToType(CardPropType.cash);
            if (is_three) cash = CardEntity.GetSumToType(CardPropType.cashx3);
            ui.cash_text.text = cash.ToString();
            Out102 item = GameHelper.GetDynamicVersion_Cash();
            ui.rate_text.text = "UP TO+" + item.cashmultiple + "00%";
            ui.btn_adget.title = "$" + cash * item.cashmultiple;
            ui.btn_get.SetClick(() =>
            {
                CheckReward(cash / 10);
                DOVirtual.DelayedCall(1.7f, () =>
                {
                    UICtrlDispatcher.Instance.Dispatch(SkinInfo.StarRewardUI_Close);
                    GameHelper.ShowInterstitial("interstitial_skipAd");
                });

            });

            ui.btn_adget.SetClick(() =>
            {
                GameHelper.GetVideo("reward_fly", isCompleted =>
                {
                    if (isCompleted)
                    {
                        CheckReward(cash * item.cashmultiple);
                    }
                });

            });
            FX.Instance.SetFx<SkeletonAnimation>(ui.spine_parent, Fx_Type.spine_starreward_bg, sk =>
                    {
                        SkeletonAnimation cardSpine1 = sk;
                        NAAVsa.PlayAnim(cardSpine1, "b2", true);


                        // NAAVsa.AddCompleteEvent(cardSpine, OnChangeCardFinish);
                        // OnChangeCardFinish(null);
                    }, CloseCb);
            FX.Instance.SetFx<SkeletonAnimation>(ui.spine_parent, Fx_Type.spine_starreward_bg, sk =>
                    {
                        SkeletonAnimation cardSpine = sk;
                        NAAVsa.PlayAnim(cardSpine, "b", false);


                        // NAAVsa.AddCompleteEvent(cardSpine, OnChangeCardFinish);
                        // OnChangeCardFinish(null);
                    }, CloseCb);



        }
        private Action CloseCb;

        private void CheckReward(decimal cash)
        {
            var startPot = GameHelper.GetUICenterPosition(ui.cash_text);
            var endPos = GameHelper.GetUICenterPosition(ui.reward_end);
            var rewardData = new RewardData
            {
                displayType = RewardDisplayType.RewardFly | RewardDisplayType.ValueChange
            };
            var rewardSingleData = new Goda(101, cash, RewardOrigin.Play)
            {
                startPosition = startPot,
                endPosition = endPos,

            };
            rewardSingleData.id = 102;
            rewardData.AddReward(rewardSingleData);
            GameDispatcher.Instance.Dispatch(BingoInfo.GetReward, rewardData);
            ui.btn_get.SetClick(() => { });
            ui.btn_adget.SetClick(() => { });
            DOVirtual.DelayedCall(1.7f, () =>
            {
                UICtrlDispatcher.Instance.Dispatch(SkinInfo.StarRewardUI_Close);
            });
        }
    }
}