
using FGUI.ZM_Common_01;
using UnityEngine;
using FairyGUI;
using System.Collections.Generic;
using FGUI.LG_albums;
using FGUI.LG_Common;
using SGModule.DataStorage;
using DG.Tweening;
using SGModule.Common.Extensions;
using System.Linq;
using SGModule.NetKit;
namespace RedHotRoast
{
    public class AlbumDetailUI : BaseUI
    {
        private AlbumDetailUICtrl ctrl;
        private AlbumDetailModel model;
        private FGUI.LG_albums.com_albumsDetail ui;

        public AlbumDetailUI(AlbumDetailUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.AlbumDetailUI;
            this.ctrl = ctrl;
        }

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

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

        protected override void OnClose()
        {
            if (loader != null && !loader.isDisposed && loader.texture != null)
            {
                loader.texture.Dispose();
                loader.texture = null;
            }
            loader = null;

            if (new_index != -1)
            {
                GameDispatcher.Instance.Dispatch(GameMsg.UnlockAlbums, index);
            }
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.LG_albums.com_albumsDetail;
        }
        private List<LevelUnlock> LevelData;
        private int new_index = -1;
        private int index;

        private GLoader loader = new GLoader();
        protected override void OnOpenBefore(object args)
        {
            index = (int)args;
            Debug.Log(index);
            ui.btn_close.SetClick(() =>
            {
                CtrlCloseUI();
            });
            LevelData = ConfigSystem.GetConfig<LevelUnlock>();
            // ui.list_.SetVirtual();
            // ui.list_.itemRenderer = RendererList;
            // ui.list_.numItems = GameHelper.GetLevel() - 1;
            // ui.list_.ScrollToView(new_index);

            RendererList();
        }

        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, () =>
            {
                // ui.list_.numItems = GameHelper.GetLevel() - 1;
                RendererList();
            });

        }
        // void RendererList(int index, GObject obj)
        void RendererList()
        {

            item_albumsDetails item = (item_albumsDetails)ui.com_item;

            loader = item.com_loader.GetChild("loader") as GLoader;

            // if (GameHelper.GetVipPrivilege(Subscription.VipLevel.As<int>()))
            // {
            //     (item.btn_vip as btn_claim_2).have_vip.selectedIndex = 1;

            // }
            // if (GameHelper.GetVipPrivilege(Subscription.SLVLevel.As<int>()))
            // {
            //     (item.btn_watch as btn_claim_1).have_vip.selectedIndex = 1;

            // }


            var downloadCoinNum = ConfigSystem.GetCommonConf().CoinsDownload;
            var btn_down_coin = (FGUI.LG_Common.btn_unlock_1)item.btn_download_coin;
            btn_down_coin.title = downloadCoinNum.As<string>();
            btn_down_coin.down_load.selectedIndex = GameHelper.GetVipPrivilege(Subscription.FreeDownImage.As<int>()) ? 0 : 1;


            if (GameHelper.GetVipPrivilege(Subscription.FreeDownImage.As<int>()))
            {
                (item.btn_download as FGUI.LG_Common.btn_claim).have_vip.selectedIndex = 1;
                item.is_vip.selectedIndex = 1;
            }
            else
            {
                item.is_vip.selectedIndex = 0;
            }
            if (index < GameHelper.GetCommonModel().MultiModal - 1)
            {
                TextureHelper.SetImgLoader(item.com_loader.GetChild("loader") as GLoader, LevelData[index].Name, null, "LevelAlbums/", FolderNames.AlbumName);
                item.btn_download.SetClick(() =>
                {
                    if (GameHelper.GetVipPrivilege(Subscription.FreeDownImage.As<int>()))
                    {
                        TextureHelper.SaveImageToAlbum(LevelData[index].Name, FolderNames.AlbumName);
                    }
                    else
                    {
                        GameHelper.ShowVideoAd("DownloadImage", isSuccess =>
                        {
                            if (isSuccess)
                            {
                                TextureHelper.SaveImageToAlbum(LevelData[index].Name, FolderNames.AlbumName);
                            }
                        });
                    }

                });
                btn_down_coin.SetClick(() =>
                {
                    if (GameHelper.Get101() >= downloadCoinNum)
                    {
                        GameHelper.AddGold(-downloadCoinNum);
                        TextureHelper.SaveImageToAlbum(LevelData[index].Name, FolderNames.AlbumName);
                        TrackKit.SendEvent(ADEventTrack.Coinsbuy, ADEventTrack.Property.download);
                    }
                    else
                    {
                        GameHelper.ShowTips("no_enough_gold", true);

                        uiCtrlDispatcher.Dispatch(UICtrlMsg.BuygoldUI_Open);
                    }
                });
            }
            else
            {
                Levelunlock levelunlock_ = DataMgr.LevelUnlockListNew.Value.FirstOrDefault(x => x.level_ == index + 1);
                string file_name = "";
                if (levelunlock_ != null)
                {
                    if (levelunlock_.type == 0)
                    {
                        if (levelunlock_.config_index >= ConfigSystem.GetConfig<FreeImageLibrary>().Count) levelunlock_.config_index = ConfigSystem.GetConfig<FreeImageLibrary>().Count - 1;
                        FreeImageLibrary _leveldata = ConfigSystem.GetConfig<FreeImageLibrary>()[levelunlock_.config_index];
                        TextureHelper.SetImgLoader(item.com_loader.GetChild("loader") as GLoader, _leveldata.Name, null, "LevelAlbums/", FolderNames.AlbumName);
                        file_name = _leveldata.Name;
                    }
                    else if (levelunlock_.type == 1)
                    {
                        if (levelunlock_.config_index >= ConfigSystem.GetConfig<ADImageLibrary>().Count) levelunlock_.config_index = ConfigSystem.GetConfig<ADImageLibrary>().Count - 1;
                        ADImageLibrary _leveldata = ConfigSystem.GetConfig<ADImageLibrary>()[levelunlock_.config_index];
                        TextureHelper.SetImgLoader(item.com_loader.GetChild("loader") as GLoader, _leveldata.Name, null, "LevelAlbums/", FolderNames.AlbumName);
                        file_name = _leveldata.Name;
                    }
                    else if (levelunlock_.type == 2)
                    {
                        if (levelunlock_.config_index >= ConfigSystem.GetConfig<SpecialImageLibrary>().Count) levelunlock_.config_index = ConfigSystem.GetConfig<SpecialImageLibrary>().Count - 1;
                        SpecialImageLibrary _leveldata = ConfigSystem.GetConfig<SpecialImageLibrary>()[levelunlock_.config_index];
                        TextureHelper.SetImgLoader(item.com_loader.GetChild("loader") as GLoader, _leveldata.Name, null, "LevelAlbums/", FolderNames.AlbumName);
                        file_name = _leveldata.Name;
                    }
                    else if (levelunlock_.type == 3)
                    {
                        if (levelunlock_.config_index >= ConfigSystem.GetConfig<VIPImageLibrary>().Count) levelunlock_.config_index = ConfigSystem.GetConfig<VIPImageLibrary>().Count - 1;
                        VIPImageLibrary _leveldata = ConfigSystem.GetConfig<VIPImageLibrary>()[levelunlock_.config_index];
                        TextureHelper.SetImgLoader(item.com_loader.GetChild("loader") as GLoader, _leveldata.Name, null, "LevelAlbums/", FolderNames.AlbumName);
                        file_name = _leveldata.Name;
                    }
                    item.btn_download.SetClick(() =>
                    {
                        if (GameHelper.GetVipPrivilege(Subscription.FreeDownImage.As<int>()))
                        {
                            TextureHelper.SaveImageToAlbum(file_name, FolderNames.AlbumName);
                        }
                        else
                        {
                            GameHelper.ShowVideoAd("DownloadImage", isSuccess =>
                            {
                                if (isSuccess)
                                {
                                    TextureHelper.SaveImageToAlbum(file_name, FolderNames.AlbumName);
                                }
                            });
                        }

                    });
                    btn_down_coin.SetClick(() =>
                    {
                        if (GameHelper.Get101() >= downloadCoinNum)
                        {
                            GameHelper.AddGold(-downloadCoinNum);
                            TextureHelper.SaveImageToAlbum(file_name, FolderNames.AlbumName);
                            TrackKit.SendEvent(ADEventTrack.Coinsbuy, ADEventTrack.Property.download);
                        }
                        else
                        {
                            GameHelper.ShowTips("no_enough_gold", true);

                            uiCtrlDispatcher.Dispatch(UICtrlMsg.BuygoldUI_Open);
                        }
                    });
                }
                else
                {
                    TextureHelper.SetImgLoader(item.com_loader.GetChild("loader") as GLoader, LevelData[index].Name, null, "LevelAlbums/", FolderNames.AlbumName);
                    item.btn_download.SetClick(() =>
                    {
                        if (GameHelper.GetVipPrivilege(Subscription.FreeDownImage.As<int>()))
                        {
                            TextureHelper.SaveImageToAlbum(LevelData[index].Name, FolderNames.AlbumName);
                        }
                        else
                        {
                            GameHelper.ShowVideoAd("DownloadImage", isSuccess =>
                            {
                                if (isSuccess)
                                {
                                    TextureHelper.SaveImageToAlbum(LevelData[index].Name, FolderNames.AlbumName);
                                }
                            });
                        }

                    });
                    btn_down_coin.SetClick(() =>
                    {
                        if (GameHelper.Get101() >= downloadCoinNum)
                        {
                            GameHelper.AddGold(-downloadCoinNum);
                            TextureHelper.SaveImageToAlbum(LevelData[index].Name, FolderNames.AlbumName);
                            TrackKit.SendEvent(ADEventTrack.Coinsbuy, ADEventTrack.Property.download);
                        }
                        else
                        {
                            GameHelper.ShowTips("no_enough_gold", true);

                            uiCtrlDispatcher.Dispatch(UICtrlMsg.BuygoldUI_Open);

                        }
                    });
                }
            }
        }
    }
}