using System;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
using Core;

namespace Script.Gameplay.Global
{
    /// <summary>
    /// 全局关卡倒计时管理器（UniTimer 版）
    /// </summary>
    public sealed class GameCountdownManager : MonoSingleton<GameCountdownManager>
    {

        [Header("默认关卡时长（秒）")] [SerializeField] private float defaultDuration = 60f;

        [Header("是否受 Time.timeScale 影响")] [SerializeField]
        private bool useUnscaledTime = false;

        [Header("事件：每秒刷新（剩余秒数）")] public UnityEvent<float> OnTick;

        [Header("事件：倒计时结束")] public UnityEvent OnFinish;

        private UniTimer _timer = new UniTimer();

        /*------------------------------------------------------*/
        protected override void Awake()
        {
            base.Awake();
            // 确保事件实例化，避免其他脚本订阅时报 NullReferenceException
            OnTick ??= new UnityEvent<float>();
            OnFinish ??= new UnityEvent();
        }

        /// <summary>
        /// 启动关卡倒计时（默认时长）
        /// </summary>
        public UniTask StartLevelTimer() => StartLevelTimer(defaultDuration);

        /// <summary>
        /// 启动关卡倒计时（指定时长）
        /// </summary>
        /// <param name="duration">秒</param>
        public UniTask StartLevelTimer(float duration)
        {
            StopTimer(); // 先清掉旧计时器

            // 用 unscaledDeltaTime 就不受 Time.timeScale 影响
            Time.timeScale = 1f; // 可根据需求保留或去掉
            return _timer.StartAsync(
                duration,
                isCountDown: true,
                onTick: left => { OnTick?.Invoke(left); },
                onComplete: () =>
                {
                    OnFinish?.Invoke();
                    // 这里可以统一处理“时间到”逻辑
                    TimeOutHandle();
                });
        }

        /// <summary> 暂停 </summary>
        public void Pause() => _timer.Pause();

        /// <summary> 继续 </summary>
        public void Resume() => _timer.Resume();

        /// <summary> 立即结束并触发 OnFinish </summary>
        public void StopTimer() => _timer.Stop();

        /// <summary> 剩余时间（秒） </summary>
        public float Remaining => _timer.Remaining;

        /// <summary> 是否正在跑 </summary>
        public bool IsRunning => _timer.IsRunning;

        /*------------------------------------------------------*/
        // 统一处理“时间到”
        private void TimeOutHandle()
        {
            Debug.Log("[GameCountdown] 关卡时间到！");
            // 可以在这里弹 UI、播放音效、加载结算场景等
        }

        /*------------------------------------------------------*/
        // 方便外部 await 等待结束
        public UniTask WaitForTimeOutAsync() => _timer.WaitForFinishAsync();
    }
}