using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Base
{
    public interface ITick
    {
        public void Tick()
        {
        }
    }

    public interface ITickPhysics
    {
        public void TickPhysics()
        {
        }
    }

    public interface ITickUI
    {
        public void TickUI()
        {
        }
    }

    public class Clock : MonoBehaviour
    {
        private static Clock _instance;
        public bool pause;
        public List<ITickPhysics> tickPhysics = new();
        public List<ITick> ticks = new();
        public List<ITickUI> tickUIs = new();

        //float timer = 0;
        public static Clock Instance
        {
            get
            {
                if (_instance == null)
                {
                    // 检查场景中是否已存在实例
                    _instance = FindAnyObjectByType<Clock>();

                    // 如果不存在，创建一个新的
                    if (_instance == null)
                    {
                        var clockObject = new GameObject("[Clock]");
                        _instance = clockObject.AddComponent<Clock>();
                        DontDestroyOnLoad(clockObject); // 确保对象不被销毁
                    }
                }

                return _instance;
            }
        }


        private void Awake()
        {
            // 确保只有一个实例存在
            if (_instance != null && _instance != this)
            {
                Destroy(gameObject);
                return;
            }

            _instance = this; // 设置当前实例为静态变量
            SceneManager.sceneLoaded += OnSceneLoaded; // 注册场景加载事件
        }

        private void Update()
        {
            if (!pause)
                foreach (var tick in ticks)
                    tick.Tick();

            foreach (var uiTick in tickUIs) uiTick.TickUI();
            //if (timer > 1)
            //{
            //    timer -= 1;
            //    Debug.Log("滴答");
            //}
            //timer += Time.deltaTime;
        }

        private void FixedUpdate()
        {
            if (!pause)
                foreach (var physicsTick in tickPhysics)
                    physicsTick.TickPhysics();
        }

        private void OnDestroy()
        {
            // 移除事件监听
            SceneManager.sceneLoaded -= OnSceneLoaded;
        }

        private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
        {
            // 场景加载完成后调用 Init 方法
            Init();
        }

        /// <summary>
        ///     初始化方法
        /// </summary>
        public void Init()
        {
            ticks.Clear();
            tickPhysics.Clear();
            tickUIs.Clear();

            foreach (var obj in FindObjectsByType<MonoBehaviour>(FindObjectsSortMode.None))
            {
                if (obj is ITick tickObj) ticks.Add(tickObj);
                if (obj is ITickPhysics physicsObj) tickPhysics.Add(physicsObj);
                if (obj is ITickUI uiObj) tickUIs.Add(uiObj);
            }
        }
    }
}