using DG.Tweening;

namespace ScrewsMaster
{
    using System;
    using System.Collections;
    using FairyGUI;
    using Spine.Unity;
    using UnityEngine;
    using Object = UnityEngine.Object;

    public class LoadingUI : BaseUI
    {
        private LoadingUICtrl ctrl;
        private LoadingModel model;
        private FGUI.SLoading.com_loading ui;

        protected GProgressBar pb_loading;
        private Tweener tweener;
        private Action closeCallback;


        protected int currValue;

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

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

        protected override void OnOpenBefore(object args)
        {

            // var login = FXManager.Instance.SetFx<SkeletonAnimation>(ui.title_parent, Fx_Type.fx_title, ref closeCallback);
            // login.state.SetAnimation(0, "animation", true);
            // var rabbit = FXManager.Instance.SetFx<SkeletonAnimation>(ui.logo_parent, Fx_Type.fx_rabbit, ref closeCallback);
            // rabbit.state.SetAnimation(0, "animation", true);
            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.OpenBgUI_Open);
            pb_loading = this.ui.pb_loading;
            pb_loading.max = 100;
            pb_loading.value = 0;
            // DOVirtual.Float(0, 100, 2.8f, (value) =>
            // {
            //     // ui.pb_loading.title = $"Loading...{(int)value}%";

            //     ui.pb_loading.value = (int)value;
            // });
            CrazyAsyKit.StartCoroutine(SetProgressState());

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

        public IEnumerator SetProgressState()
        {
            tweener = DOTween.To(() => ui.pb_loading.value,
                x => ui.pb_loading.value = x, 97, 5.0f).SetEase(Ease.Linear);

            // RotateImageObject();
            ui.pb_loading.grip.TweenRotate(1800, 5) // 1 秒内旋转 360 度
                .SetEase(EaseType.Linear);
            ui.pb_loading.grip.TweenMoveX(ui.pb_loading.grip.x + ui.pb_loading.Progress.width - ui.pb_loading.grip.width, 5).SetEase(EaseType.Linear);
           

            yield return tweener;
        }

        
        private int _loopCount = 0;
        private const int maxLoops = 5; // 设定最大循环次数
        void RotateImageObject()
        {
            // 如果旋转次数还没有达到最大值，继续旋转
            if (_loopCount < maxLoops)
            {
                // 使用 FairyGUI 自带的 TweenRotate 让图片旋转 360 度
                ui.pb_loading.grip.TweenRotate(359, 1f) // 1 秒内旋转 360 度
                    .SetEase(EaseType.Linear)     // 匀速旋转
                    .OnComplete(() =>
                    {
                        _loopCount++; // 每次完成后递增计数
                        RotateImageObject(); // 递归调用实现循环
                    });
            }
            else
            {
                Debug.Log("Rotation completed 5 times");
            }
        }

        protected override void OnOpen(object args)
        {
        }

        #endregion

        #region 消息

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

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

        #endregion

        private bool isAgree = true;

        public void CheckAgree()
        {
            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;
            }
        }
    }
}