using System.Collections;
using System.Globalization;
using DG.Tweening;
using FairyGUI;
using FGUI.Loading_25;
using UnityEngine;

namespace ChillConnect
{
    public class LoadingUI : BaseUI
    {
        private LoadingUICtrl ctrl;
        private LoadingModel model;
        private com_loading ui;

        protected GProgressBar pb_loading;
        private Tweener tweener;


        protected int currValue;

        public LoadingUI(LoadingUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.LoadingUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "Loading_25";
            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 com_loading;
        }

        protected override void OnOpenBefore(object args)
        {
            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.OpenBgUI_Open);
            ui.com_pb.max = 100;
            ui.com_pb.value = 0;

            ui.com_pb.text_per.text = ui.com_pb.value + "%";
            // if (!GameHelper.IsConnect()) {
            //     return;
            // }

            // CrazyAsyKit.StartCoroutine(SetProgress(50));

            // var splashCanvas = GameObject.Find("SplashCanvas");
            // if (splashCanvas != null)
            // {
            //     splashCanvas.SetActive(false);
            //     Object.Destroy(splashCanvas);
            // }
            if (PlayerPrefs.GetInt("OpenAD", 1) == 0) closeMask();

        }

        public IEnumerator SetProgress(int max)
        {
            tweener.Kill();
            tweener = DOTween.To(() => ui.com_pb.value,
                x => ui.com_pb.value = x, max, 12f)
                .OnUpdate(() =>
                {
                    ui.com_pb.text_per.text = ui.com_pb.value + "%";
                });

            yield return tweener;
        }

        private void Reconnection(object obj = null)
        {

            //CrazyAsyKit.StartCoroutine(SetProgress(97));
        }

        protected override void OnOpen(object args)
        {
        }

        #endregion

        #region 消息

        protected override void AddListener()
        {
            GameDispatcher.Instance.AddListener(GameMsg.UpdateHotFixMax, OnUpdateHotFixMax);
            // GameDispatcher.Instance.AddListener(GameMsg.UpdateHotFixProgress, OnUpdateHotFixProgress);
            GameDispatcher.Instance.AddListener(GameMsg.Network_reconnection, Reconnection);
            GameDispatcher.Instance.AddListener(GameMsg.CloseMask, closeMask);
        }

        protected override void RemoveListener()
        {
            GameDispatcher.Instance.RemoveListener(GameMsg.UpdateHotFixMax, OnUpdateHotFixMax);
            // GameDispatcher.Instance.RemoveListener(GameMsg.UpdateHotFixProgress, OnUpdateHotFixProgress);
            GameDispatcher.Instance.RemoveListener(GameMsg.Network_reconnection, Reconnection);
            GameDispatcher.Instance.RemoveListener(GameMsg.CloseMask, closeMask);
        }

        #endregion
        private void closeMask(object a = null)
        {
            ui.com_pb.visible = true;
            OnUpdateHotFixProgress(97);
        }
        private bool loading = false;
        private bool isAgree = true;

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

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

        private void OnUpdateHotFixProgress(object obj)
        {
            if (loading) return;

            if (obj != null)
            {
                loading = true;
                tweener.Kill();
                var value = (int)obj;
                // pb_loading.value = value;
                tweener = DOTween.To(() => ui.com_pb.value,
                    x => ui.com_pb.value = x, value, 12f)
                    .OnUpdate(() =>
                    {
                        ui.com_pb.text_per.text = (int)(ui.com_pb.value) + "%";
                    });
            }
        }
    }
}
