
using FGUI.ZM_Common_01;
using UnityEngine;
using FairyGUI;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using SGModule.Common.Extensions;
using DG.Tweening;
using UnityEngine.UIElements;
using SGModule.NetKit;

namespace RedHotRoast
{
    public class UnlockLevelNewUI : BaseUI
    {
        private UnlockLevelNewUICtrl ctrl;
        private UnlockLevelNewModel model;
        private FGUI.LG_End.com_unlockLevelNew ui;

        public UnlockLevelNewUI(UnlockLevelNewUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.UnlockLevelNewUI;
            this.ctrl = ctrl;
        }

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

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

        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 = null;
            //     }
            // }
            // loader_list.Clear();
            foreach (var loader in loader_list)
            {
                if (loader == null || loader.isDisposed) continue;

                // 释放材质到材质池
                if (loader.material != null)
                {
                    TextureHelper.CancelImageBlur(loader); // 自动返回材质池
                    loader.material = null;
                }

                // 清理 NTexture
                if (loader.texture != null)
                {
                    loader.texture.Dispose(); // 销毁 NTexture 对象
                    loader.texture = null;
                }
            }
            loader_list.Clear();
        }
        private List<GLoader> loader_list = new List<GLoader>();
        protected override void OnBind()
        {
            ui = baseUI as FGUI.LG_End.com_unlockLevelNew;
        }

        private bool isHallEnter = false;
        protected override void OnOpenBefore(object args)
        {
            if (args != null)
            {
                isHallEnter = (bool)args;
            }
            InitView();
            ui.btn_close.SetClick(() =>
            {
                GameDispatcher.Instance.Dispatch(GameMsg.ExitGame);
                uiCtrlDispatcher.Dispatch(UICtrlMsg.NewEndUI_Close);
                CtrlCloseUI();
            });
        }

        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, RefreshVip);
        }
        protected override void RemoveListener()
        {
            GameDispatcher.Instance.RemoveListener(GameMsg.BuyVip, RefreshVip);
        }
        #endregion
        private void RefreshVip(object a = null)
        {
            bool have_special = GameHelper.GetVipPrivilege(Subscription.SLVLevel.As<int>());
            SpecialImageLibrary special_ImageLibrary = ConfigSystem.GetConfig<SpecialImageLibrary>()[DataMgr.LevelUnlockSpecial.Value];
            if (special_ImageLibrary.State == 1 && !have_special) TextureHelper.SetImageBlur(ui.item_2.com_pic.picture);
            else TextureHelper.CancelImageBlur(ui.item_2.com_pic.picture);

            if (have_special)
            {
                ui.item_2.btn_unlock.visible = false;
                ui.item_2.type.selectedIndex = 0;
                ui.item_2.btn_skip.SetClick(() =>
                {
                    SelectLevel(2, DataMgr.LevelUnlockSpecial.Value);
                });
            }
            else
            {
                ui.item_2.type.selectedIndex = 2;
                ui.item_2.btn_unlock.visible = true;
                ui.item_2.btn_skip.SetClick(() =>
                {
                    uiCtrlDispatcher.Dispatch(UICtrlMsg.VipClubViewUI_Open);
                });

                if (special_ImageLibrary.GoldCoins > 0)
                {
                    ui.item_2.btn_unlock.pay_type.selectedIndex = 1;
                    ui.item_2.btn_unlock.title = special_ImageLibrary.GoldCoins.ToString();
                    ui.item_2.btn_unlock.SetClick(() =>
                    {
                        if (DataMgr.Coin.Value >= special_ImageLibrary.GoldCoins)
                        {
                            GameHelper.AddGold(-special_ImageLibrary.GoldCoins);
                            SelectLevel(2, DataMgr.LevelUnlockSpecial.Value);
                            Debug.Log("///////金币解锁");
                        }
                        else
                        {
                            GameHelper.ShowTips("no_enough_gold", true);

                            uiCtrlDispatcher.Dispatch(UICtrlMsg.BuygoldUI_Open);

                        }
                    });
                }
                else
                {
                    ui.item_2.btn_unlock.visible = false;
                }
            }

            bool have_vip = GameHelper.GetVipPrivilege(Subscription.VipLevel.As<int>());
            VIPImageLibrary vip_ImageLibrary = ConfigSystem.GetConfig<VIPImageLibrary>()[DataMgr.LevelUnlockVIP.Value];
            if (vip_ImageLibrary.State == 1 && !have_vip) TextureHelper.SetImageBlur(ui.item_3.com_pic.picture);
            else TextureHelper.CancelImageBlur(ui.item_3.com_pic.picture);
            if (have_vip)
            {
                ui.item_3.btn_unlock.visible = false;
                ui.item_3.type.selectedIndex = 0;
                ui.item_3.btn_skip.SetClick(() =>
                {
                    SelectLevel(3, DataMgr.LevelUnlockVIP.Value);
                });
            }
            else
            {
                ui.item_3.type.selectedIndex = 3;
                ui.item_3.btn_unlock.visible = true;
                ui.item_3.btn_skip.SetClick(() =>
                {
                    uiCtrlDispatcher.Dispatch(UICtrlMsg.VipClubViewUI_Open);
                });

                if (vip_ImageLibrary.GoldCoins > 0)
                {
                    ui.item_3.btn_unlock.pay_type.selectedIndex = 1;
                    ui.item_3.btn_unlock.title = vip_ImageLibrary.GoldCoins.ToString();
                    ui.item_3.btn_unlock.SetClick(() =>
                    {
                        if (DataMgr.Coin.Value >= vip_ImageLibrary.GoldCoins)
                        {
                            GameHelper.AddGold(-vip_ImageLibrary.GoldCoins);
                            SelectLevel(3, DataMgr.LevelUnlockVIP.Value);
                            Debug.Log("///////金币解锁");
                        }
                        else
                        {
                            GameHelper.ShowTips("no_enough_gold", true);

                            uiCtrlDispatcher.Dispatch(UICtrlMsg.BuygoldUI_Open);

                        }
                    });
                }
                else
                {
                    ui.item_3.btn_unlock.visible = false;
                }
            }
        }

        //初始化页面逻辑
        private void InitView()
        {
            Debug.Log(JsonConvert.SerializeObject(ConfigSystem.GetConfig<FreeImageLibrary>()) + "..................");
            Debug.Log(JsonConvert.SerializeObject(ConfigSystem.GetConfig<ADImageLibrary>()) + "..................");
            Debug.Log(JsonConvert.SerializeObject(ConfigSystem.GetConfig<SpecialImageLibrary>()) + "..................");
            Debug.Log(JsonConvert.SerializeObject(ConfigSystem.GetConfig<VIPImageLibrary>()) + "..................");

            ConfigSystem.GetConfig<VIPImageLibrary>();

            loader_list.Add(ui.item_0.com_pic.picture);
            loader_list.Add(ui.item_1.com_pic.picture);
            loader_list.Add(ui.item_2.com_pic.picture);
            loader_list.Add(ui.item_3.com_pic.picture);

            // List<int> free_level_list = new List<int>();
            // List<int> ad_level_list = new List<int>();
            // List<int> special_level_list = new List<int>();
            // List<int> vip_level_list = new List<int>();
            // for (int i = 0; i < DataMgr.LevelUnlockListNew.Value.Count; i++)
            // {
            //     if (DataMgr.LevelUnlockListNew.Value[i].type == 0) free_level_list.Add(DataMgr.LevelUnlockListNew.Value[i].config_index);
            //     else if (DataMgr.LevelUnlockListNew.Value[i].type == 1) ad_level_list.Add(DataMgr.LevelUnlockListNew.Value[i].config_index);
            //     else if (DataMgr.LevelUnlockListNew.Value[i].type == 2) special_level_list.Add(DataMgr.LevelUnlockListNew.Value[i].config_index);
            //     else if (DataMgr.LevelUnlockListNew.Value[i].type == 3) vip_level_list.Add(DataMgr.LevelUnlockListNew.Value[i].config_index);
            // }
            int Free_Alubum_0 = PlayerPrefs.GetInt("Free_Alubum_0", -1);

            int AD_Alubum = PlayerPrefs.GetInt("AD_Alubum", -1);
            int Spec_Alubum = PlayerPrefs.GetInt("Spec_Alubum", -1);
            int Vip_Alubum = PlayerPrefs.GetInt("Vip_Alubum", -1);

            if (DataMgr.LevelUnlockFree.Value < 0)
            {
                DataMgr.LevelUnlockFree.Value = Free_Alubum_0;
            }

            if (DataMgr.LevelUnlockAD.Value < 0)
            {
                DataMgr.LevelUnlockAD.Value = AD_Alubum;
            }

            if (DataMgr.LevelUnlockSpecial.Value < 0)
            {
                DataMgr.LevelUnlockSpecial.Value = Spec_Alubum;
            }

            if (DataMgr.LevelUnlockVIP.Value < 0)
            {
                DataMgr.LevelUnlockVIP.Value = Vip_Alubum;
            }


            FreeImageLibrary free_ImageLibrary = ConfigSystem.GetConfig<FreeImageLibrary>()[DataMgr.LevelUnlockFree.Value];
            ui.item_0.btn_unlock.pay_type.selectedIndex = 2;
            ui.item_0_re.btn_unlock.visible = false;
            TextureHelper.SetImgLoader(ui.item_0.com_pic.picture, free_ImageLibrary.Name, (s) =>
            {
                if (free_ImageLibrary.State == 1) TextureHelper.SetImageBlur(ui.item_0.com_pic.picture);
            }, "LevelAlbums/", FolderNames.AlbumName);

            int Free_Alubum_1 = PlayerPrefs.GetInt("Free_Alubum_1", -1);
            FreeImageLibrary free_ImageLibrary_re = ConfigSystem.GetConfig<FreeImageLibrary>()[Free_Alubum_1];
            TextureHelper.SetImgLoader(ui.item_0_re.com_pic.picture, free_ImageLibrary_re.Name, (s) =>
            {
                if (free_ImageLibrary_re.State == 1) TextureHelper.SetImageBlur(ui.item_0_re.com_pic.picture);
            }, "LevelAlbums/", FolderNames.AlbumName);

            ui.item_0.btn_unlock.SetClick(() =>
            {
                PlayerPrefs.SetInt("unlock_refresh", 0);
                DataMgr.LevelUnlockFree.Value = Free_Alubum_1;
                ui.item_0.visible = false;
                ui.item_0_re.visible = true;
            });

            if (PlayerPrefs.GetInt("unlock_refresh", 1) == 1)
            {
                ui.item_0.visible = true;
                ui.item_0_re.visible = false;
            }
            else
            {
                ui.item_0.visible = false;
                ui.item_0_re.visible = true;
            }

            ui.item_0.btn_skip.SetClick(() =>
            {
                SelectLevel(0, DataMgr.LevelUnlockFree.Value);
            });
            ui.item_0_re.btn_skip.SetClick(() =>
            {
                SelectLevel(0, DataMgr.LevelUnlockFree.Value);
            });



            ADImageLibrary ad_ImageLibrary = ConfigSystem.GetConfig<ADImageLibrary>()[DataMgr.LevelUnlockAD.Value];
            TextureHelper.SetImgLoader(ui.item_1.com_pic.picture, ad_ImageLibrary.Name, (s) =>
            {
                if (ad_ImageLibrary.State == 1) TextureHelper.SetImageBlur(ui.item_1.com_pic.picture);
            }, "LevelAlbums/", FolderNames.AlbumName);

            ui.item_1.btn_unlock.pay_type.selectedIndex = 3;

            ui.item_1.btn_skip.SetClick(() =>
            {
                Debug.Log("///////播放广告解锁");
                GameHelper.ShowVideoAd("UnlockLevel", isSuccess =>
                {
                    if (isSuccess)
                    {
                        SelectLevel(1, DataMgr.LevelUnlockAD.Value);
                    }
                });
            });


            bool have_special = GameHelper.GetVipPrivilege(Subscription.SLVLevel.As<int>());
            SpecialImageLibrary special_ImageLibrary = ConfigSystem.GetConfig<SpecialImageLibrary>()[DataMgr.LevelUnlockSpecial.Value];
            TextureHelper.SetImgLoader(ui.item_2.com_pic.picture, special_ImageLibrary.Name, (s) =>
            {
                if (special_ImageLibrary.State == 1 && !have_special) TextureHelper.SetImageBlur(ui.item_2.com_pic.picture);
            }, "LevelAlbums/", FolderNames.AlbumName);
            if (have_special)
            {
                ui.item_2.btn_unlock.visible = false;
                ui.item_2.type.selectedIndex = 0;
                ui.item_2.btn_skip.SetClick(() =>
                {
                    SelectLevel(2, DataMgr.LevelUnlockSpecial.Value);
                });
            }
            else
            {
                ui.item_2.type.selectedIndex = 2;
                ui.item_2.btn_unlock.visible = true;
                ui.item_2.btn_skip.SetClick(() =>
                {
                    uiCtrlDispatcher.Dispatch(UICtrlMsg.VipClubViewUI_Open);
                });

                if (special_ImageLibrary.GoldCoins > 0)
                {
                    ui.item_2.btn_unlock.pay_type.selectedIndex = 1;
                    ui.item_2.btn_unlock.title = special_ImageLibrary.GoldCoins.ToString();
                    ui.item_2.btn_unlock.SetClick(() =>
                    {
                        if (DataMgr.Coin.Value >= special_ImageLibrary.GoldCoins)
                        {
                            GameHelper.AddGold(-special_ImageLibrary.GoldCoins);
                            SelectLevel(2, DataMgr.LevelUnlockSpecial.Value);
                            TrackKit.SendEvent(ADEventTrack.Coinsbuy, ADEventTrack.Property.unlock_svip);
                            Debug.Log("///////金币解锁");
                        }
                        else
                        {
                            GameHelper.ShowTips("no_enough_gold", true);

                            uiCtrlDispatcher.Dispatch(UICtrlMsg.BuygoldUI_Open);

                        }
                    });
                }
                else
                {
                    ui.item_2.btn_unlock.visible = false;
                }
            }

            bool have_vip = GameHelper.GetVipPrivilege(Subscription.VipLevel.As<int>());
            VIPImageLibrary vip_ImageLibrary = ConfigSystem.GetConfig<VIPImageLibrary>()[DataMgr.LevelUnlockVIP.Value];

            TextureHelper.SetImgLoader(ui.item_3.com_pic.picture, vip_ImageLibrary.Name, (s) =>
            {
                if (vip_ImageLibrary.State == 1 && !have_vip) TextureHelper.SetImageBlur(ui.item_3.com_pic.picture);
            }, "LevelAlbums/", FolderNames.AlbumName);
            if (have_vip)
            {
                ui.item_3.btn_unlock.visible = false;
                ui.item_3.type.selectedIndex = 0;
                ui.item_3.btn_skip.SetClick(() =>
                {
                    SelectLevel(3, DataMgr.LevelUnlockVIP.Value);
                });
            }
            else
            {
                ui.item_3.type.selectedIndex = 3;
                ui.item_3.btn_unlock.visible = true;
                ui.item_3.btn_skip.SetClick(() =>
                {
                    uiCtrlDispatcher.Dispatch(UICtrlMsg.VipClubViewUI_Open);
                });

                if (vip_ImageLibrary.GoldCoins > 0)
                {
                    ui.item_3.btn_unlock.pay_type.selectedIndex = 1;
                    ui.item_3.btn_unlock.title = vip_ImageLibrary.GoldCoins.ToString();
                    ui.item_3.btn_unlock.SetClick(() =>
                    {
                        if (DataMgr.Coin.Value >= vip_ImageLibrary.GoldCoins)
                        {
                            GameHelper.AddGold(-vip_ImageLibrary.GoldCoins);
                            SelectLevel(3, DataMgr.LevelUnlockVIP.Value);
                            Debug.Log("///////金币解锁");
                            TrackKit.SendEvent(ADEventTrack.Coinsbuy, ADEventTrack.Property.unlock_vip);
                        }
                        else
                        {
                            GameHelper.ShowTips("no_enough_gold", true);

                            uiCtrlDispatcher.Dispatch(UICtrlMsg.BuygoldUI_Open);

                        }
                    });
                }
                else
                {
                    ui.item_3.btn_unlock.visible = false;
                }
            }


        }

        public void SelectLevel(int type_, int id)
        {

            DataMgr.LevelUnlockListNew.Value.Add(new Levelunlock()
            {
                level_ = GameHelper.GetLevel(),
                type = type_,
                config_index = id
            });
            DataMgr.LevelUnlockListNew.Save();
            DataMgr.LevelUnlockFree.Value = -1;
            DataMgr.LevelUnlockAD.Value = -1;
            DataMgr.LevelUnlockSpecial.Value = -1;
            DataMgr.LevelUnlockVIP.Value = -1;

            PlayerPrefs.SetInt("Free_Alubum_0", -1);
            PlayerPrefs.SetInt("Free_Alubum_1", -1);
            PlayerPrefs.SetInt("AD_Alubum", -1);
            PlayerPrefs.SetInt("Spec_Alubum", -1);
            PlayerPrefs.SetInt("Vip_Alubum", -1);

            PlayerPrefs.SetInt("unlock_refresh", 1);
            GameDispatcher.Instance.Dispatch(GameMsg.UnlockLevelsuccess);
            CtrlCloseUI();
            PreDownloadManager.StartDownloadAlbumsPicture();

            if (isHallEnter)
            {
                GameHelper.gameType = 0;
                GameDispatcher.Instance.Dispatch(GameMsg.OpenGame, false);
            } else
            {
                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.OpenGameUI_Open, true);
            }

        }
    }
    public class Levelunlock
    {
        public int level_;
        public int type;
        public int config_index;
    }
}
