
using FGUI.ZM_Common_01;
using UnityEngine;
using FairyGUI;

namespace RedHotRoast
{
    public class SubUnlockUI : BaseUI
    {
        private SubUnlockUICtrl ctrl;
        private SubUnlockModel model;
        private FGUI.LG_Unlock.com_SubUnlock ui;

        public SubUnlockUI(SubUnlockUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.SubUnlockUI;
            this.ctrl = ctrl;
        }

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

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

        protected override void OnClose()
        {
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.LG_Unlock.com_SubUnlock;
        }

        protected override void OnOpenBefore(object args)
        {
            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.BuyVip, refrsh);
        }
        protected override void RemoveListener()
        {
            GameDispatcher.Instance.RemoveListener(GameMsg.BuyVip, refrsh);
        }
        #endregion
        private void refrsh(object a = null)
        {
            GameHelper.ShowTips("unlock_vip", true);
            CtrlCloseUI();
        }

        //初始化页面逻辑
        private void InitView()
        {
            ui.btn_close.SetClick(() =>
            {
                CtrlCloseUI();
            });
            var vip_list = ConfigSystem.GetConfig<VipClub>();
            ui.text_unlockLive.SetVar("num", GameHelper.GetCommonModel().UnlockLive[1].ToString()).FlushVars();
            ui.text_unlockAlbum.SetVar("num", GameHelper.GetCommonModel().UnlockSecret[1].ToString()).FlushVars();
            ui.btn_vip0.text_price.text = GameHelper.getPrice((decimal)vip_list[0].DiscountPrice);
            ui.btn_vip1.text_price.text = GameHelper.getPrice((decimal)vip_list[1].DiscountPrice);
            ui.btn_vip2.text_price.text = GameHelper.getPrice((decimal)vip_list[2].DiscountPrice);

            if (GameHelper.GetVipLevel() - 1 >= 0)
            {
                ui.btn_vip0.enabled = false;
            }

            ui.btn_vip0.SetClick(() =>
            {
                string _type = "vip_club" + 0;
                ApplePayClass maxPayData = new ApplePayClass
                {
                    amount = (int)System.Math.Round(vip_list[0].DiscountPrice * 100),
                    sku = PurchasingManager.GetPaySku(PayType.weekly_subscription),
                    currency = "USD",
                    shopName = _type
                };
                MaxPayManager.Instance.Buy(maxPayData);
            });

            if (GameHelper.GetVipLevel() - 1 >= 1)
            {
                ui.btn_vip1.enabled = false;
            }

            ui.btn_vip1.SetClick(() =>
            {
                string _type = "vip_club" + 1;
                ApplePayClass maxPayData = new ApplePayClass
                {
                    amount = (int)System.Math.Round(vip_list[1].DiscountPrice * 100),
                    sku = PurchasingManager.GetPaySku(PayType.monthly_subscription),
                    currency = "USD",
                    shopName = _type
                };
                MaxPayManager.Instance.Buy(maxPayData);
            });

            if (GameHelper.GetVipLevel() - 1 >= 2)
            {
                ui.btn_vip2.enabled = false;
            }

            ui.btn_vip2.SetClick(() =>
            {
                string _type = "vip_club" + 2;
                ApplePayClass maxPayData = new ApplePayClass
                {
                    amount = (int)System.Math.Round(vip_list[2].DiscountPrice * 100),
                    sku = PurchasingManager.GetPaySku(PayType.yearly_subscription),
                    currency = "USD",
                    shopName = _type
                };
                MaxPayManager.Instance.Buy(maxPayData);
            });

        }

    }
}
