
using FGUI.ZM_Common_01;
using UnityEngine;
using FairyGUI;
using FGUI.LG_Common;
using DG.Tweening;
using SGModule.NetKit;
using System.Collections.Generic;
using SGModule.Common.Extensions;

namespace RedHotRoast
{
    public class UnlockLevelUI : BaseUI
    {
        private UnlockLevelUICtrl ctrl;
        private UnlockLevelModel model;
        private FGUI.LG_End.com_unlockLevel ui;

        public UnlockLevelUI(UnlockLevelUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.UnlockLevelUI;
            this.ctrl = ctrl;
        }

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

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

        protected override void OnClose()
        {
            for (int i = 0; i < loader_list.Count; i++)
            {
                if (loader_list[i] != null && !loader_list[i].isDisposed && loader_list[i].texture != null)
                {
                    loader_list[i].texture.Dispose();
                    loader_list[i].texture = null;
                }
            }
            loader_list.Clear();
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.LG_End.com_unlockLevel;
        }
        private LevelUnlock levelUnlock_;
        private int level_;
        protected override void OnOpenBefore(object args)
        {
            level_ = (int)args;
            Debug.Log(level_);

            if (!loader_list.Contains(ui.com_loader.GetChild("loader") as GLoader))
            {
                loader_list.Add(ui.com_loader.GetChild("loader") as GLoader);
            }

            levelUnlock_ = ConfigSystem.GetConfig<LevelUnlock>()[level_];
            TextureHelper.SetImgLoader(ui.com_loader.GetChild("loader") as GLoader, levelUnlock_.Name, (s) =>
            {
                TextureHelper.SetImageBlur(ui.com_loader.GetChild("loader") as GLoader);
            }, "LevelAlbums/", FolderNames.AlbumName);
            InitView();
            if (levelUnlock_.LeveType == 1)
            {
                TrackKit.SendEvent(ADEventTrack.Special, ADEventTrack.Property.speciallevel);
            }
            else if (levelUnlock_.LeveType == 2)
            {
                TrackKit.SendEvent(ADEventTrack.VipLevel, ADEventTrack.Property.viplevel);
            }

        }
        private List<GLoader> loader_list = new List<GLoader>();
        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)
        {
            DOVirtual.DelayedCall(0.5f, () =>
            {
                InitView();
            });

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


            if (GameHelper.GetVipPrivilege(Subscription.VipLevel.As<int>()))
            {
                (ui.btn_vip as btn_claim_2).have_vip.selectedIndex = 1;
            }
            if (GameHelper.GetVipPrivilege(Subscription.SLVLevel.As<int>()))
            {
                (ui.btn_watch as btn_claim_1).have_vip.selectedIndex = 1;
            }

            if (levelUnlock_.LeveType == 1) //特殊关
            {
                ui.type.selectedIndex = 0;
                ui.btn_pay.title = levelUnlock_.GoldCoins.ToString();

                ui.btn_pay.SetClick(() =>
                {
                    if (DataMgr.Coin.Value >= levelUnlock_.GoldCoins)
                    {
                        DataMgr.Coin.Value -= levelUnlock_.GoldCoins;
                        GameDispatcher.Instance.Dispatch(GameMsg.Gold_refresh);
                        DataMgr.LevelUnlockList.Value.Add(level_);
                        GameHelper.ShowTips("unlocked", true);
                        DataMgr.LevelUnlockList.Save();
                        GameDispatcher.Instance.Dispatch(GameMsg.UnlockLevelsuccess);
                        CtrlCloseUI();
                        TrackKit.SendEvent(ADEventTrack.Special, ADEventTrack.Property.speciallevelcoin);
                    }
                    else
                    {
                        GameHelper.ShowTips("no_enough_gold", true);
                        UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.BuygoldUI_Open);
                    }
                });

                ui.btn_watch.SetClick(() =>  //7天都不包含。30天包含特殊，365全部包含
                {
                    if (GameHelper.GetVipPrivilege(Subscription.SLVLevel.As<int>()))
                    {
                        DataMgr.LevelUnlockList.Value.Add(level_);
                        GameHelper.ShowTips("unlocked", true);
                        DataMgr.LevelUnlockList.Save();
                        GameDispatcher.Instance.Dispatch(GameMsg.UnlockLevelsuccess);
                        CtrlCloseUI();

                    }
                    else
                    {
                        GameHelper.ShowVideoAd("UnlockSpecialLevel", isSuccess =>
                        {
                            if (isSuccess)
                            {
                                DataMgr.LevelUnlockList.Value.Add(level_);
                                GameHelper.ShowTips("unlocked", true);
                                DataMgr.LevelUnlockList.Save();
                                GameDispatcher.Instance.Dispatch(GameMsg.UnlockLevelsuccess);
                                CtrlCloseUI();
                            }
                            TrackKit.SendEvent(ADEventTrack.Special, ADEventTrack.Property.speciallevelvideo);
                        });
                    }

                });
            }
            else if (levelUnlock_.LeveType == 2)//vip关
            {
                ui.type.selectedIndex = 1;
                ui.btn_pay.title = levelUnlock_.GoldCoins.ToString();

                ui.btn_pay.SetClick(() =>
                {
                    if (DataMgr.Coin.Value >= levelUnlock_.GoldCoins)
                    {
                        DataMgr.Coin.Value -= levelUnlock_.GoldCoins;
                        GameDispatcher.Instance.Dispatch(GameMsg.Gold_refresh);
                        DataMgr.LevelUnlockList.Value.Add(level_);
                        GameHelper.ShowTips("unlocked", true);
                        DataMgr.LevelUnlockList.Save();
                        GameDispatcher.Instance.Dispatch(GameMsg.UnlockLevelsuccess);
                        TrackKit.SendEvent(ADEventTrack.VipLevel, ADEventTrack.Property.viplevelcoin);
                        CtrlCloseUI();
                    }
                    else
                    {
                        GameHelper.ShowTips("no_enough_gold", true);
                        UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.BuygoldUI_Open);
                    }
                });
                ui.btn_vip.SetClick(() =>
                {
                    if (GameHelper.GetVipPrivilege(Subscription.VipLevel.As<int>()))
                    {
                        DataMgr.LevelUnlockList.Value.Add(level_);
                        GameHelper.ShowTips("unlocked", true);
                        DataMgr.LevelUnlockList.Save();
                        GameDispatcher.Instance.Dispatch(GameMsg.UnlockLevelsuccess);
                        CtrlCloseUI();
                    }
                    else
                    {
                        uiCtrlDispatcher.Dispatch(UICtrlMsg.VipClubViewUI_Open, 2);
                        TrackKit.SendEvent(ADEventTrack.VipLevel, ADEventTrack.Property.viplevelsub);
                    }


                });


            }
            ui.btn_close.SetClick(() =>
            {
                GameHelper.SetLevel(GameHelper.GetLevel() + 1);
                if (UIManager.Instance.IsExistUI(UIConst.NewEndUI))
                {
                    UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.NewEndUI_Close);
                }
                else
                {
                    UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.OpenGameUI_Open);
                }

                CtrlCloseUI();
            });

        }
    }
}
