
using FGUI.Common_01;
using UnityEngine;
using FairyGUI;
using IgnoreOPS;
using System;
using Spine.Unity;
using FGUI.AddCell_12;
using SGModule.Net;
using SGModule.NetKit;

namespace ChillConnect
{
    public class AddviewnewUI : BaseUI
    {
        private AddviewnewUICtrl ctrl;
        private AddviewnewModel model;
        private FGUI.ZM_AddCell_12.com_addView_new ui;
        private bool is_off = false;

        public AddviewnewUI(AddviewnewUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.AddviewnewUI;
            this.ctrl = ctrl;
        }

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

        #region 生命周期
        protected override void OnInit()
        {
            //model = ModuleManager.Instance.GetModel(ModelConst.AddviewnewModel) as AddviewnewModel;
        }
        private Action closeCallback;
        protected override void OnClose()
        {
            GameHelper.showGameUI = true;
            HallManager.Instance.UpdateSecondEvent -= updateWatchCD;
            closeCallback?.Invoke();
            AdRedeemManager.Instance.Destroy();

        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.ZM_AddCell_12.com_addView_new;
        }

        protected override void OnOpenBefore(object args)
        {
            // if (ui.btn_watch is btn_watchAd watchAdBtn)
            // {
            //     CommonTools.GetInstance.InitAdBtnAnim(watchAdBtn.icon_Parent);
            // }
            if (SaveData.GetSaveObject().addview_off_time > GameHelper.GetNowTime()) is_off = true;
            ui.text_out.text = "x" + GameHelper.GetItemNumber(0);
            ui.text_back.text = "x" + GameHelper.GetItemNumber(1);
            ui.text_refresh.text = "x" + GameHelper.GetItemNumber(2);
            ui.text_level.text = "Level " + GameHelper.GetLevel();

            TrackKit.SendEvent(GameHelper.getTrackEvenName(),Property.BuyOneNewShow);
            // NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.BuyOneNewShow, 1);

            ui.btn_play.SetClick(() =>
            {
                GameHelper.gameType = 0;
                GameDispatcher.Instance.Dispatch(GameMsg.OpenGame);
                CtrlCloseUI();
            });
            if (GameHelper.IsGiftSwitch() && ConfigSystem.GetConfig<CommonModel>().PiggyBankSwitch == 1)
            {
                (ui.btn_max_pay.GetChild("img_saveingpot") as GImage).visible = true;
            }

            // (ui.btn_watch.GetChild("btn_text") as GTextField).scale = new Vector2(0.7f, 0.7f);
            // (ui.btn_watch.GetChild("title1") as GTextField).scale = new Vector2(0.7f, 0.7f);
            // ui.btn_watch.GetChild("img_saveingpot").x += 20;
            // ui.btn_watch.GetChild("img_saveingpot").y -= 33;
            // ad_cool_down = ConfigSystem.GetConfig<CommonModel>().exchangeCD;
            // btn_WatchAd = ui.btn_watch as btn_watchAd;
            if (GameHelper.IsAdModelOfPay())
            {
                if (is_off)
                {
                    AdRedeemManager.Instance.SetWatchAd(PurchasingManager.GetPaySku(PayType.buy_one_off), ui.btn_watch as btn_watchAd, () =>
                    {
                        TrackKit.SendEvent(GameHelper.getTrackEvenName(),Property.BuyOneNewClick);
                        //   NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.BuyOneNewClick, 1);
                        SetTextString();
                    });
                    ui.pay_type.selectedIndex = 0;
                    AdRedeemManager.Instance.Start();
                }
                else
                {
                    AdRedeemManager.Instance.SetWatchAd(PurchasingManager.GetPaySku(PayType.buy_one), ui.btn_watch as btn_watchAd, () =>
                    {
                        TrackKit.SendEvent(GameHelper.getTrackEvenName(),Property.BuyOneNewClick);
                        //  NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.BuyOneNewClick, 1);
                        SetTextString();
                    });
                    ui.pay_type.selectedIndex = 0;
                    AdRedeemManager.Instance.Start();
                }

                SetTextString();
            }
            else
            {
                ui.pay_type.selectedIndex = 1;
                if (is_off)
                {
                    decimal price = (decimal)GameHelper.GetCommonModel().AddDiscount2;
                    ui.btn_max_pay.title = GameHelper.getPrice(price);
                    ui.btn_max_pay.SetClick(() =>
                    {
                        TrackKit.SendEvent(GameHelper.getTrackEvenName(),Property.BuyOneNewClick);
                        // NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.BuyOneNewClick, 1);
                        ApplePayClass maxPayData = new ApplePayClass
                        {
                            amount = (int)Math.Round(price * 100),
                            sku = PurchasingManager.GetPaySku(PayType.buy_one_off),
                            currency = "USD"
                        };
                        MaxPayManager.Instance.Buy(maxPayData);
                    });
                }
                else
                {
                    decimal price = (decimal)GameHelper.GetCommonModel().addspace2;
                    ui.btn_max_pay.title = GameHelper.getPrice(price);
                    ui.btn_max_pay.SetClick(() =>
                    {
                        TrackKit.SendEvent(GameHelper.getTrackEvenName(),Property.BuyOneNewClick);
                        //  NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.BuyOneNewClick, 1);
                        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);
            GameDispatcher.Instance.AddListener(GameMsg.Sheep_item_refresh, SetItemNumber);
        }
        protected override void RemoveListener()
        {
            GameDispatcher.Instance.RemoveListener(GameMsg.apple_pay_success, pay_success);
            GameDispatcher.Instance.RemoveListener(GameMsg.Sheep_item_refresh, SetItemNumber);
        }
        #endregion
        void SetItemNumber(object a)
        {
            ui.text_out.text = "x" + GameHelper.GetItemNumber(0);
            ui.text_back.text = "x" + GameHelper.GetItemNumber(1);
            ui.text_refresh.text = "x" + GameHelper.GetItemNumber(2);
        }
        void pay_success(object str)
        {
            string type = (string)str;
            if (type == PurchasingManager.GetPaySku(PayType.buy_one) || type == PurchasingManager.GetPaySku(PayType.buy_one_off))
            {
                TrackKit.SendEvent(GameHelper.getTrackEvenName(),Property.BuyOneNewSuccess);
                // NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.BuyOneNewSuccess, 1);
                SaveData.GetSaveObject().have_slot = true;
                SaveData.saveDataFunc();
                GameDispatcher.Instance.Dispatch(GameMsg.Slot_refresh);
                ui.pay_type.selectedIndex = 2;
                SkeletonAnimation addeffect = FXManager.Instance.SetFx<SkeletonAnimation>(ui.ani_parent1, Fx_Type.fx_addeffect, ref closeCallback);
                addeffect.state.SetAnimation(0, "animation", true);
                SkeletonAnimation addeffect1 = FXManager.Instance.SetFx<SkeletonAnimation>(ui.ani_parent2, Fx_Type.fx_addeffect_1, ref closeCallback);
                addeffect1.state.SetAnimation(0, "animation", true);
                // CtrlCloseUI();
            }


        }

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

            // var tAnimation = FXManager.Instance.SetFx<SkeletonAnimation>(ui.tip_parent, Fx_Type.Fx_AddBoxTip, ref _closeCallback);
            // tAnimation.state.AddAnimation(0, "animation", true, 0);

            ui.btn_close.SetClick(() =>
            {
                GameDispatcher.Instance.Dispatch(GameMsg.OpenGame);
                CtrlCloseUI();
            });

            HallManager.Instance.UpdateSecondEvent += updateWatchCD;
            ui.btn_addback.SetClick(() =>
            {
                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.SheepwindowUI_Open, 1);
            });
            ui.btn_addout.SetClick(() =>
            {
                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.SheepwindowUI_Open, 0);
            });
            ui.btn_addrefresh.SetClick(() =>
            {
                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.SheepwindowUI_Open, 2);
            });
            // updateWatchCD();

            // checkBtnState();
        }
        void updateWatchCD()
        {

        }
        public void SetTextString()
        {
            int need = 0;
            if (is_off) need = AdRedeemManager.Instance.GetCeilingNeedAds(PurchasingManager.GetPaySku(PayType.buy_one_off));
            else need = AdRedeemManager.Instance.GetCeilingNeedAds(PurchasingManager.GetPaySku(PayType.buy_one));
            var myAd = AdRedeemManager.Instance.GetLookRewardADNum();

            // ui.tips.SetVar("num", need.ToString()).FlushVars();
            ui.ads.SetVar("num", myAd + "/").FlushVars();
            ui.ads.SetVar("need", need.ToString()).FlushVars();
        }

    }
}