using System;
using DG.Tweening;
using BingoBrain.Core;
using FGUI.JRewardPop;
using DontConfuse;

namespace BingoBrain
{
    public class GodPleUI : BaseUI
    {
        private GodPleUICtrl ctrl;
        private GodPleModel model;
        private com_reward ui;

        private Action closeCallback;

        public GodPleUI(GodPleUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.GodPleUI;
            this.ctrl = ctrl;
        }

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

        #region 生命周期

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

        protected override void OnClose()
        {
            if (UI.Instance.IsExistUI(UIConst.H5UI))
            {
                WebviewManager.Instance.SetDarkThough(true);

            }

        }

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

        protected override void OnOpenBefore(object args)
        {
            if (GameHelper.IsGiftSwitch()) ui.gift.selectedIndex = 1;
            if (args != null)
            {
                model.rewardInfo = (RewardData)args;
            }
            if (UI.Instance.IsExistUI(UIConst.H5UI))
            {
                WebviewManager.Instance.SetDarkThough(false);
            }
            InitView();
        }

        protected override void OnOpen(object args)
        {
        }

        #endregion


        private void InitView()
        {
            DOVirtual.DelayedCall(0.10f, () =>
            {
                var piaodai = GameHelper.SetSpine("Effect.spine.fx_piaodai", "fx_piaodai", ui.fx_PiaoDai, "enter",
                    false);
                DOVirtual.DelayedCall(0.8f, () => { piaodai.state.SetAnimation(0, "idle", true); });
                DOVirtual.DelayedCall(0.2f,
                    () => { GameHelper.SetParticleSystem("Effect.sys.fx_lihua", "fx_lihua", ui.fx_lihua); });
            });

            var rewardValue = model.rewardInfo.GetRewardDataList()[0].GetTotalValue();
            ui.text_num.text = $"+{GameHelper.Get102Str(rewardValue)}";

            Out102 item = GameHelper.GetDynamicVersion_Cash();
            ui.rate_text.text = "UP TO+" + item.cashmultiple + "00%";
            ui.btn_collect.title = "Get " + (rewardValue * item.cashmultiple).ToString("0.00");

            var isFree = model.rewardInfo.condition == RewardCondition.None;

            if (ui.btn_collect is btn_collect btn_collect)
            {
                btn_collect.cont_state.selectedIndex = isFree ? btn_collect._State_free : btn_collect._State_video;
            }


            ui.btn_no.visible = false;

            // if (!isFree)
            // {
                DOVirtual.DelayedCall(2, () => { ui.btn_no.visible = true; });

                ui.btn_collect.SetClick(() =>
                {
                    OnClickAdCollect(item.cashmultiple);
                });
                ui.btn_no.SetClick(() =>
                {
                    OnClickCancel();
                });

            // }
            // else
            // {
            //     ui.btn_collect.SetClick(GetReward);
            // }
        }

        private void OnClickAdCollect(int rate)
        {
            GameHelper.GetVideo("reward_playcash", isSuccess =>
            {
                if (isSuccess)
                {
                    model.rewardInfo.GetRewardDataList()[0].value = model.rewardInfo.GetRewardDataList()[0].value * rate;
                    GetReward();
                }
            });
        }

        private void GetReward()
        {
            UsaSystem.GetReward(model.rewardInfo);
            CtrlCloseUI();
        }

        private void OnClickCancel()
        {
            model.rewardInfo.GetRewardDataList()[0].value = model.rewardInfo.GetRewardDataList()[0].value / 10;
            UsaSystem.GetReward(model.rewardInfo);
            CtrlCloseUI();
            GameHelper.ShowInterstitial("interstitial_skipAd");
        }
    }
}