using DG.Tweening;

namespace RedHotRoast
{
    using System.Collections;
    using FairyGUI;
    using UnityEngine;
    using Object = UnityEngine.Object;

    public class LoadingUI : BaseUI
    {
        private LoadingUICtrl ctrl;
        private LoadingModel model;
        private FGUI.ZM_Loading_25.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 = "ZM_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()
        {
            tweener.Kill();
        }

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

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

        public IEnumerator SetProgress()
        {
            tweener = DOTween.To(() => ui.com_pb.value,
                x => ui.com_pb.value = x, 98, 12.0f);

            yield return tweener;
        }

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

        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);
        }

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

        #endregion

        private bool isAgree = true;

        public void CheckAgree()
        {
            tweener.Kill();
            tweener = DOTween.To(() => ui.com_pb.value,
                x => ui.com_pb.value = x, 100, 0.1f).OnComplete(() =>
            {
                if (!isAgree) return;
                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 (obj != null)
            {
                tweener.Kill();
                var value = (int)obj;
                pb_loading.value = value;
            }
        }
    }
}