using System;
using FGUI.ZM_Common_01;
using SGModule.NetKit;
using Spine.Unity;

namespace RedHotRoast
{
    public class AddViewUI : BaseUI
    {
        private AddViewUICtrl ctrl;
        private AddViewModel model;
        private FGUI.ZM_AddCell_12.com_addView ui;
        public int ad_cool_down = 120;
        public btn_watchAd btn_WatchAd;

        public AddViewUI(AddViewUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.AddViewUI;
            this.ctrl = ctrl;
        }

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

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

        protected override void OnClose()
        {
            GameHelper.showGameUI = true;
            // HallManager.Instance.UpdateSecondEvent -= updateWatchCD;
            if (GameHelper.IsAdModelOfPay())
            {
                AdExchangeManager.Instance.Destroy();
            }
            if (need_call) GameHelper.CallShowTurn();
        }
        private bool need_call;
        protected override void OnBind()
        {
            ui = baseUI as FGUI.ZM_AddCell_12.com_addView;
        }

        protected override void OnOpenBefore(object args)
        {
            TrackKit.SendEvent(GameHelper.GetEventName(), ADEventTrack.Property.buy_one_show);
            if (args != null) need_call = (bool)args;
            // ad_cool_down = ConfigSystem.GetConfig<CommonModel>().exchangeCD;
            // btn_WatchAd = ui.btn_watch as btn_watchAd;
            if (GameHelper.IsAdModelOfPay())
            {
                AdExchangeManager.Instance.SetWatchAd(PurchasingManager.GetPaySku(PayType.buy_one), ui.btn_watch as btn_watchAd, SetTextString);
                ui.pay_type.selectedIndex = 0;


                AdExchangeManager.Instance.Start();
                SetTextString();
            }
            else
            {
                ui.pay_type.selectedIndex = 1;
                decimal price = (decimal)GameHelper.GetCommonModel().addspace2;
                ui.btn_max_pay.title = GameHelper.getPrice(price);
                ui.btn_max_pay.SetClick(() =>
                {
                    ApplePayClass maxPayData = new ApplePayClass
                    {
                        amount = (int)Math.Round(price * 100),
                        sku =PurchasingManager.GetPaySku(PayType.buy_one),
                        currency = "USD"
                    };
                    MaxPayManager.Instance.Buy(maxPayData);
                });
            }

            InitView();
        }

        protected override void OnOpen(object args)
        {
        }

        protected override void OnHide()
        {
        }

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

        #region 消息
        protected override void AddListener()
        {
            GameDispatcher.Instance.AddListener(GameMsg.apple_pay_success, pay_success);

        }
        protected override void RemoveListener()
        {
            GameDispatcher.Instance.RemoveListener(GameMsg.apple_pay_success, pay_success);

        }
        #endregion
        void pay_success(object str)
        {
            string type = (string)str;

            if (type == PurchasingManager.GetPaySku(PayType.buy_one))
            {
                CtrlCloseUI();
            }

        }

        private Action closeCallback = null;

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


            // var add = FXManager.Instance.SetFx<SkeletonAnimation>(ui.anim_bg, Fx_Type.fx_add_box, ref closeCallback);
            // add.state.SetAnimation(0, "animation", true);
            //

            ui.btn_close.SetClick(() =>
            {
                CtrlCloseUI();
            });

            // HallManager.Instance.UpdateSecondEvent += updateWatchCD;
            // updateWatchCD();

            // checkBtnState();

        }

        public void SetTextString()
        {
            var need =  AdExchangeManager.Instance.GetCeilingNeedAds(PurchasingManager.GetPaySku(PayType.buy_one));
            var myAd = AdExchangeManager.Instance.GetLookRewardADNum();

            ui.text_watch_ad.SetVar("needNum", need.ToString()).FlushVars();
            ui.tips.SetVar("needAds", need.ToString()).FlushVars();
            ui.tips.SetVar("myAds", myAd.ToString()).FlushVars();
        }
    }
}
