using FairyGUI;
using UnityEngine;
using BingoBrain.Core;
using BingoBrain.HotFix;
using Object = UnityEngine.Object;
using DG.Tweening;
using System.Collections;

namespace BingoBrain
{
    public class ReadyBingoUI : BaseUI
    {
        private ReadyBingoUICtrl ctrl;
        private ReadyBingoModel model;
        private FGUI.JLoading.com_loading ui;

        protected GProgressBar pb_loading;

        public ReadyBingoUI(ReadyBingoUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.ReadyBingoUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "JLoading";
            uiInfo.assetName = "com_loading";
            uiInfo.layerType = UILayerType.Loading;
            uiInfo.isNeedOpenAnim = false;
            uiInfo.isNeedCloseAnim = false;
            uiInfo.isNeedUIMask = false;
        }

        #region 生命周期

        protected override void OnInit()
        {
        }

        protected override void OnClose()
        {
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.JLoading.com_loading;
        }

        protected override void OnOpenBefore(object args)
        {
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.ReadyBingoyUI_Open);
            ui.pb_loading.max = 100;
            ui.pb_loading.value = 0;

            if (!GameHelper.IsConnect())
            {
                return;
            }

            IsfvKit.StartCoroutine(SetProgress());


            var splashCanvas = GameObject.Find("SplashCanvas");
            if (splashCanvas != null)
            {
                splashCanvas.SetActive(false);
                Object.Destroy(splashCanvas);
            }
        }
        private Tweener tweener;

        public IEnumerator SetProgress()
        {
            tweener = DOTween.To(() => ui.pb_loading.value,
                x => ui.pb_loading.value = x, 97, 3.0f);

            yield return tweener;
        }

        protected override void OnOpen(object args)
        {
        }

        #endregion

        private void Reconnection(object obj = null) 
        {
            IsfvKit.StartCoroutine(SetProgress());
        }


        #region 消息

        protected override void AddListener()
        {
            GameDispatcher.Instance.AddListener(BingoInfo.UpdateHotFixMax, OnUpdateHotFixMax);
            GameDispatcher.Instance.AddListener(BingoInfo.UpdateHotFixProgress, OnUpdateHotFixProgress);
            GameDispatcher.Instance.AddListener(BingoInfo.Network_reconnection, Reconnection);

        }

        protected override void RemoveListener()
        {
            GameDispatcher.Instance.RemoveListener(BingoInfo.UpdateHotFixMax, OnUpdateHotFixMax);
            GameDispatcher.Instance.RemoveListener(BingoInfo.UpdateHotFixProgress, OnUpdateHotFixProgress);
            GameDispatcher.Instance.RemoveListener(BingoInfo.Network_reconnection, Reconnection);

        }

        #endregion

        private bool isAgree = true;

        public void CheckAgree()
        {
            if (!isAgree) return;
            ctrl.CloseUI();
        }

        bool isShowed = false;

        private void OnUpdateHotFixMax(object obj)
        {
            if (obj == null) return;
            var max = (int)obj;
            ui.pb_loading.max = max;
        }

        private void OnUpdateHotFixProgress(object obj)
        {
            if (obj != null)
            {
                var value = (int)obj;
                pb_loading.value = value;
            }
        }
    }
}