
using FGUI.A000_common;
using UnityEngine;
using FairyGUI;
using DG.Tweening;
using Roy.Datas;
using SGame;

namespace ScrewsMaster
{
    public class ReviveUI : BaseUI
    {
        private ReviveUICtrl ctrl;
        private ReviveModel model;
        private FGUI.G008_reward.com_revive ui;

        private bool isRevive;

        public ReviveUI(ReviveUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.ReviveUI;
            this.ctrl = ctrl;
        }

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

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

        protected override void OnClose()
        {
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.G008_reward.com_revive;
        }

        protected override void OnOpenBefore(object args)
        {
            isRevive = false;
            ui.cont_iswacth.selectedIndex = GameHelper.GetIsWacthAd();

            InitView();
        }

        protected override void OnOpen(object args)
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDisplay(object args)
        {

        }

        protected override void OnCloseUIAnimEnd()
        {
            base.OnCloseUIAnimEnd();
            if (isRevive)
            {
                ShowScrews.Instance.OnResurrect();
            }

        }

        #endregion

        #region 消息
        protected override void AddListener()
        {

        }
        protected override void RemoveListener()
        {

        }
        #endregion

        //初始化页面逻辑
        private void InitView()
        {
            ui.cont_iswacth.selectedIndex = GameHelper.GetIsWacthAd();
            if (GameHelper.IsGiftSwitch() && ConfigSystem.GetConfig<CommonModel>().PiggyBankSwitch == 1) (ui.btn_AdRevive.GetChild("img_saveingpot") as GImage).visible = true;

            ui.btn_AdRevive.onClick.Add(() =>
            {
                //观看广告复活
                GameHelper.ShowVideoAd("reward_lobby", isSuccess =>
                {
                    if (isSuccess)
                    {
                        ui.btn_back.SetClick(() => { });
                        ui.btn_close.SetClick(() => { });
                      //  ui.btn_revive.SetClick(() => { });
                        GameDispatcher.Instance.Dispatch(GameMsg.Gold_refresh);
                        GameHelper.SetReviveCount();
                        GameHelper.SetReviveCount();
                        isRevive = true;
                        ctrl.CloseUI();
                    }
                });
            });

            // var revivalCoins = ConfigSystem.GetConfig<CommonModel>().RevivalCoins;
            // if (ui.btn_revive is btn_revive btnRevive) btnRevive.txt_coin.text = revivalCoins.ToString();
            // ui.btn_revive.onClick.Add(() =>
            // {
            //     if (!GameHelper.CheckGoldNumber(revivalCoins))
            //     {
            //         GameHelper.ShowTips("Not Enough Coins");
            //         return;
            //     }
            //     //消耗金币复活
            //     GameHelper.AddGoldNumber(-revivalCoins);
            //     GameDispatcher.Instance.Dispatch(GameMsg.Gold_refresh);
            //     //CtrlCloseUI();
            //     GameHelper.SetIsWatchAd(2);
            //     GameHelper.SetReviveCount();
            //     isRevive = true;
            //     ctrl.CloseUI();
            // });

            ui.btn_close.onClick.Add(() =>
            {
                ctrl.CloseUI();
                float[] cash_array = GameHelper.GetRewardValue(2);

                var temp = new SettlementData
                { is_success = false, cash_number = cash_array[0], is_level_success = true, is_h5_reward = false };

                DOVirtual.DelayedCall(0.5f, () =>
                {
                    UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.LevelSuccessUI_Open, temp);
                });
            });

            ui.btn_back.onClick.Add(() =>
            {
                ctrl.CloseUI();
                float[] cash_array = GameHelper.GetRewardValue(2);

                var temp = new SettlementData
                { is_success = false, cash_number = cash_array[0], is_level_success = true, is_h5_reward = false };

                DOVirtual.DelayedCall(0.5f, () =>
                {
                    UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.LevelSuccessUI_Open, temp);
                });
            });

        }

    }
}