using System;
using System.Collections.Generic;
using FairyGUI;
using FGUI.AddCell_12;
using FGUI.Common_01;
using IgnoreOPS;
using SGModule.Net;
using SGModule.NetKit;

namespace ChillConnect
{
    public class AddViewUI : BaseUI
    {
        private AddViewUICtrl ctrl;
        private AddViewModel model;
        private 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 = "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())
            {
                AdRedeemManager.Instance.Destroy();
            }
            if (need_open_turn) GameHelper.CallShowTurn();

        }
        private bool need_open_turn = false;
        protected override void OnBind()
        {
            ui = baseUI as com_addView;
        }

        protected override void OnOpenBefore(object args)
        {
            TrackKit.SendEvent(GameHelper.getTrackEvenName(),Property.BuyOneShow);
            if (args != null) need_open_turn = (bool)args;
            // ad_cool_down = ConfigSystem.GetConfig<CommonModel>().exchangeCD;
            // btn_WatchAd = ui.btn_watch as btn_watchAd;

            if (GameHelper.IsAdModelOfPay())
            {
                if (GameHelper.IsGiftSwitch() && ConfigSystem.GetConfig<CommonModel>().PiggyBankSwitch == 1)
                {
                    ui.btn_watch.GetChild("img_saveingpot").visible = true;
                }
                btn_watchAd btn = ui.btn_watch as btn_watchAd;
                btn.buy_type.selectedIndex = 0;
                AdRedeemManager.Instance.SetWatchAd(PurchasingManager.GetPaySku(PayType.buy_one), ui.btn_watch as btn_watchAd, SetTextString);
                ui.pay_type.selectedIndex = 0;


                AdRedeemManager.Instance.Start();
                SetTextString();
            }
            else
            {
                if (GameHelper.IsGiftSwitch() && ConfigSystem.GetConfig<CommonModel>().PiggyBankSwitch == 1)
                {
                    ui.btn_max_pay.GetChild("img_saveingpot").visible = true;
                }
                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();
                GameDispatcher.Instance.Dispatch(GameMsg.resurgence_close);
            }


        }

        //初始化页面逻辑
        private void InitView()
        {
            ui.btn_close.SetClick(() =>
            {
                CtrlCloseUI();
                GameDispatcher.Instance.Dispatch(GameMsg.resurgence_close);
            });

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

            // checkBtnState();
            InitBroadCast();
            HallManager.Instance.UpdateSecondEvent += updateWatchCD;
            updateWatchCD();
            if (GameHelper.IsGiftSwitch())
            {
                ui.broad.visible = true;
            }
        }

        public void SetTextString()
        {
            var 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.ToString()).FlushVars();
            ui.ads.SetVar("need", need.ToString()).FlushVars();
        }
        void updateWatchCD()
        {
            broadtime++;
            BroadCast();
        }
        private int broadtime = 1;
        private List<string> config_name_list = ConfigSystem.GetConfig<LevelAttemptsModel>().config_name_list;
        private List<string> config_money_list = ConfigSystem.GetConfig<LevelAttemptsModel>().config_money_list;
        private List<string[]> broad_list = new List<string[]>();
        private void BroadCast()
        {
            if (broadtime % 3 == 0)
            {
                (ui.broad as com_broadcast_new).t1.Play(() =>
                {
                    broad_list.RemoveAt(0);
                    int name_index = UnityEngine.Random.Range(0, config_name_list.Count);
                    int money_index = UnityEngine.Random.Range(0, config_money_list.Count);
                    broad_list.Add(new string[2] { config_name_list[name_index], config_money_list[money_index] });
                    for (int i = 0; i < 4; i++)
                    {
                        text_list[i].text = "Congratulations,User [color=#ad4800][size=36]" + broad_list[i][0] + "[/size][/color] !After purchasing the +1 Block Pack,play [color=#ad4800]" + broad_list[i][1] + "[/color] matcher to clear the level!";
                    }
                });


            }

        }
        private List<GTextField> text_list = new List<GTextField>();
        private void InitBroadCast()
        {
            text_list.Add((ui.broad as com_broadcast_new).text_0);
            text_list.Add((ui.broad as com_broadcast_new).text_1);
            text_list.Add((ui.broad as com_broadcast_new).text_2);
            text_list.Add((ui.broad as com_broadcast_new).text_3);
            for (int i = 0; i < 4; i++)
            {

                int name_index = UnityEngine.Random.Range(0, config_name_list.Count);
                int money_index = UnityEngine.Random.Range(0, config_money_list.Count);
                broad_list.Add(new string[2] { config_name_list[name_index], config_money_list[money_index] });
                text_list[i].text = "Congratulations,User [color=#ad4800][size=36]" + broad_list[i][0] + "[/size][/color] !After purchasing the +1 Block Pack,play [color=#ad4800]" + broad_list[i][1] + "[/color] matcher to clear the level!";

            }
        }
    }
}