using System;
using System.Collections.Generic;
using Ichni.RhythmGame;
namespace Ichni
{
///
/// 集中式元素更新调度器(ichni Official)。
/// 将所有 GameElement 的帧更新按 9 个阶段有序执行,
/// 替代原先分散在 GameManager.Update/LateUpdate()、各子管理器、MonoBehaviour.Update() 中的零散更新。
///
/// TickEarly (GameManager.Update)
/// Phase 0 TimeDuration — 判定元素激活/隐藏(Legacy timeDurations 列表)
/// Phase 1 Animation — 更新动画值,设置脏标记(AnimationBase 通过 IScheduledElement 注册)
/// Phase 2 Apply — 执行 Color + Transform(Legacy activeColorSubmodules / activeTransformSubmodules)
///
/// --- Unity 执行其他脚本的 Update(SplineComputer 检测 transform.hasChanged,重新采样)---
///
/// TickLate (GameManager.LateUpdate)
/// DirtyMark — ExecuteDeferredRefresh(Legacy,isStarting || isPlaying 条件)
/// Phase 3 SplineRebuild — 占位(SplineComputer 已在 Update 中自行处理)
/// Phase 4 TrackCore — 更新轨道时间、裁剪区间(Track 通过 IScheduledElement 注册)
/// Phase 5 TrackFollower — CrossTrackPoint / HeadPoint / PercentPoint
/// [TrackManager.ManualLateUpdate — 清除 refreshedThisFrame 标记]
/// Phase 6 Note — NoteManager 内部驱动(激活 + 更新 + 重置)
/// Phase 7 Effect — ParticleEmitter / TimeEffectsCollection / ParticleTracker / DTM 元素 / LookAt 旋转覆盖
/// Phase 8 Misc — SkyboxSubsetter / JudgeTrigger / LowPriorityActions
///
/// 此分离设计消除了 Dreamteck Spline 的一帧延迟:
/// Phase 2 (Update) 修改 Transform → SplineComputer.Update() 检测 hasChanged 并重采样
/// → Phase 4/5 (LateUpdate) 调用 SetPercent() 时获取最新采样数据。
///
public class ElementUpdateScheduler
{
#region [常量] Constants
/// 阶段总数(TimeDuration..Misc,步长 10,共 9 个)
private const int PhaseCount = 9;
/// 阶段枚举值到数组索引的步长除数
private const int PhaseStep = 10;
private static readonly UpdatePhase[] AllPhases = (UpdatePhase[])Enum.GetValues(typeof(UpdatePhase));
#endregion
#region [阶段元素列表] Phase Element Lists
///
/// 每个阶段的已注册 IScheduledElement 列表。
/// 使用数组替代 Dictionary<UpdatePhase, ...>,
/// 索引通过 (int)phase / 10 直接映射,消除哈希查找开销。
///
private readonly List[] _phaseElements;
/// 将 UpdatePhase 枚举值转换为数组索引。
private static int PhaseIndex(UpdatePhase phase) => (int)phase / PhaseStep;
#endregion
#region [轨道 Spline 列表] Track Spline List
/// Phase 3 专用:需要手动重建 SplineComputer 的 Track 列表(保留供后续优化)
private readonly List