using System;
using System.Collections.Generic;
using System.Linq;
using FairyGUI;
using SGModule.Common.Extensions;
using SGModule.Common.Helper;
using UnityEngine;

namespace RedHotRoast
{
    public class SecretDetailUI : BaseUI
    {
        private SecretDetailUICtrl ctrl;
        private SecretDetailModel model;
        private FGUI.LG_secretAlbums.com_secretDetail ui;

        public SecretDetailUI(SecretDetailUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.SecretDetailUI;
            this.ctrl = ctrl;
        }

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

        #region 生命周期

        protected override void OnInit()
        {
            //model = ModuleManager.Instance.GetModel(ModelConst.SecretDetailModel) as SecretDetailModel;
        }

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

        protected override void OnBind()
        {
            ui = baseUI as FGUI.LG_secretAlbums.com_secretDetail;
        }

        private DetailData _pictureName;

        protected override void OnOpenBefore(object args)
        {
            if (args == null) return;
            _pictureName = (DetailData)args;

            InitView();
        }

        protected override void OnOpen(object args)
        {
        }

        protected override void OnHide()
        {
        }

        protected override void OnDisplay(object args)
        {
        }

        #endregion

        #region 消息

        protected override void AddListener()
        {
        }

        protected override void RemoveListener()
        {
        }

        #endregion

        private GLoader _loader = new GLoader();
        //初始化页面逻辑
        private void InitView()
        {
            ui.btn_close.SetClick(CtrlCloseUI);
            _loader = ui.com_loader.GetChild("loader") as GLoader;

            TextureHelper.SetImgLoader(ui.com_loader.GetChild("loader") as GLoader, _pictureName.Name, null,
                _pictureName.Name1, FolderNames.SecretName);
            var btnDownCoin = ((FGUI.LG_Common.btn_unlock_1)ui.btn_download_coin);
            var downloadCoinNum = ConfigSystem.GetCommonConf().CoinsDownload;
            btnDownCoin.title = downloadCoinNum.ToString();
            if (GameHelper.GetVipPrivilege(Subscription.FreeDownImage.As<int>()))
            {
                (ui.btn_download as FGUI.LG_Common.btn_claim).have_vip.selectedIndex = 1;
                ui.is_vip.selectedIndex = 1;
            }
            else
            {
                ui.is_vip.selectedIndex = 0;
                btnDownCoin.down_load.selectedIndex = 1;
            }
            btnDownCoin.SetClick(() =>
            {
                if (GameHelper.Get101() >= downloadCoinNum)
                {
                    GameHelper.AddGold(-downloadCoinNum);
                    GameDispatcher.Instance.Dispatch(GameMsg.Update101);
                    TextureHelper.SaveImageToAlbum(_pictureName.Name, FolderNames.SecretName);
                }
                else
                {
                    GameHelper.ShowTips("no_enough_gold", true);
                    UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.BuygoldUI_Open);
                }
            });

            ui.btn_download.SetClick(() =>
            {
                if (GameHelper.GetVipPrivilege(Subscription.FreeDownImage.As<int>()))
                {
                    TextureHelper.SaveImageToAlbum(_pictureName.Name, FolderNames.SecretName);
                }
                else
                {
                    GameHelper.ShowVideoAd("secret_albnums", isSuccess =>
                    {
                        if (isSuccess)
                        {
                            TextureHelper.SaveImageToAlbum(_pictureName.Name, FolderNames.SecretName);
                        }
                    });
                }
            });
        }

        private void ItemRender(int index, GObject obj)
        {
        }


        private int GetPepleNum(float quantity)
        {
            // 获取当前时间
            DateTime now = DateTime.Now;

            // 计算当天已经过去的分钟数（从凌晨 0 点开始算）
            int minutesPassedToday = now.Hour * 60 + now.Minute;

            // 计算人数
            int num = Mathf.FloorToInt(quantity * minutesPassedToday);

            return num;
        }
    }
}