using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace OCES.Audio
{
    /// <summary>
    /// 负责递归播放一个 MusicContainer 树。
    /// 被 MusicChannelPlayer 和 AmbienceChannelPlayer 共同使用。
    /// 不持有状态机逻辑，只负责"把这个 Container 播完"。
    /// </summary>
    public class MusicContainerPlayer
    {
        readonly MusicContainerConfig m_containerConfig;
        readonly MusicSegmentConfig m_segmentConfig;
        readonly AudioSourcePool m_pool;
        readonly MonoBehaviour m_coroutineHost;
        UnityEngine.Audio.AudioMixerGroup m_mixerGroup;


        // Sequence Step 模式的全局游标，key = containerId
        readonly Dictionary<uint, int> m_sequenceStepIndex = new();

        // Random 模式的不放回历史，key = containerId
        readonly Dictionary<uint, HashSet<uint>> m_randomHistory = new();

        public MusicContainerPlayer(
            MusicContainerConfig containerConfig,
            MusicSegmentConfig segmentConfig,
            AudioSourcePool pool,
            MonoBehaviour coroutineHost)
        {
            this.m_containerConfig = containerConfig;
            this.m_segmentConfig   = segmentConfig;
            this.m_pool            = pool;
            this.m_coroutineHost   = coroutineHost;
        }

        // ─────────────────────────────────────────────
        // 公开入口
        // ─────────────────────────────────────────────

        /// <summary>
        /// 开始播放指定 Container，播放完毕后调用 onFinished。
        /// 返回可用于外部停止的句柄。
        /// </summary>
        public ContainerPlayHandle Play(uint containerId, System.Action onFinished = null)
        {
            MusicContainer container = this.m_containerConfig.QueryById(containerId);
            if (container == null)
            {
                Debug.LogError($"[MusicContainerPlayer] 找不到 ContainerId: {containerId}");
                onFinished?.Invoke();
                return null;
            }

            ContainerPlayHandle handle = new();
            handle.TargetVolume = 1f;
            handle.Coroutine = this.m_coroutineHost.StartCoroutine(
                PlayContainerCoroutine(container, handle, onFinished));
            return handle;
        }

        /// <summary>
        /// 停止一个播放句柄（立即停止，不淡出）
        /// </summary>
        public void Stop(ContainerPlayHandle handle)
        {
            if (handle == null) return;
            handle.Cancelled = true;

            foreach (ContainerPlayHandle child in handle.ChildHandles)
            {
                Stop(child);
            }
            handle.ChildHandles.Clear();
            
            
            if (handle.Coroutine != null)
                this.m_coroutineHost.StopCoroutine(handle.Coroutine);
            foreach (AudioSource src in handle.ActiveSources)
            {
                ReturnSource(src);
            }
            handle.ActiveSources.Clear();
        }

        // ─────────────────────────────────────────────
        // 核心协程
        // ─────────────────────────────────────────────

        IEnumerator PlayContainerCoroutine(
            MusicContainer container,
            ContainerPlayHandle handle,
            System.Action onFinished)
        {
            int loopsCompleted = 0;

            while (true)
            {
                yield return this.m_coroutineHost.StartCoroutine(
                    PlayContainerOnce(container, handle.TargetVolume, handle));

                if (handle.Cancelled) yield break;

                loopsCompleted++;

                // -1 = 无限循环，一直重复
                if (container.LoopCount == -1)
                    continue;

                // 0 = 不循环，播一次就结束
                if (container.LoopCount == 0)
                    break;

                // >= 1，播满次数后结束
                if (loopsCompleted >= container.LoopCount)
                    break;
            }

            if (!handle.Cancelled)
                onFinished?.Invoke();
        }

        /// <summary>
        /// 播放一个 Container 一轮（不含循环逻辑）
        /// </summary>
        IEnumerator PlayContainerOnce(MusicContainer container, float volumeScale, ContainerPlayHandle handle)
        {
            if (container.Segments == null || container.Segments.Count == 0)
                yield break;

            switch (container.ContainerType)
            {
                case ContainerType.Blend:
                    yield return PlayBlend(container, volumeScale, handle);
                    break;

                case ContainerType.Sequence:
                    yield return PlaySequence(container, volumeScale, handle);
                    break;

                case ContainerType.Random:
                    yield return PlayRandom(container, volumeScale, handle);
                    break;
            }
        }

        // ─────────────────────────────────────────────
        // Blend（同时播放）
        // ─────────────────────────────────────────────

        IEnumerator PlayBlend(MusicContainer container, float volumeScale, ContainerPlayHandle handle)
        {
            // 同时启动所有子元素，等待全部结束
            var childHandles = new List<ContainerPlayHandle>();
            bool allDone = false;
            int remaining = container.Segments.Count;

            foreach (uint segId in container.Segments)
            {
                if (handle.Cancelled) yield break;

                ContainerPlayHandle childHandle = PlayChild(segId, volumeScale, () =>
                {
                    remaining--;
                    if (remaining <= 0) allDone = true;
                });
                if (childHandle != null)
                {
                    childHandles.Add(childHandle);
                    handle.ChildHandles.AddRange(childHandles);
                }
            }

            yield return new WaitUntil(() => allDone || handle.Cancelled);

            if (handle.Cancelled)
            {
                foreach (var ch in childHandles)
                    Stop(ch);
            }
        }

        // ─────────────────────────────────────────────
        // Sequence
        // ─────────────────────────────────────────────

        IEnumerator PlaySequence(MusicContainer container, float volumeScale, ContainerPlayHandle handle)
        {
            bool isStep = container.ContainerPlayMode; 

            if (isStep)
            {
                // Step: 每次只播一个，游标全局推进
                int index = GetNextSequenceIndex(container);
                yield return PlayChildAndWait(container.Segments[index], volumeScale, handle);
                
            }
            else
            {
                // Continuous: 顺序播完所有子元素，算一轮
                for (int i = 0; i < container.Segments.Count; i++)
                {
                    if (handle.Cancelled) yield break;

                    // StrategyParam 作为两段之间的间隔时间（秒）
                    if (i > 0 && container.StrategyParam > 0)
                        yield return new WaitForSeconds(container.StrategyParam);

                    yield return PlayChildAndWait(container.Segments[i], volumeScale, handle);
                }
            }
        }

        // ─────────────────────────────────────────────
        // Random
        // ─────────────────────────────────────────────

        IEnumerator PlayRandom(MusicContainer container, float volumeScale, ContainerPlayHandle handle)
        {
            bool isStep = container.ContainerPlayMode; // 同上，音乐系统默认 Continuous

            if (isStep)
            {
                // Step Random: 随机选一个播放，算一次 loopCount
                uint chosen = PickRandomChild(container);
                yield return PlayChildAndWait(chosen, volumeScale, handle);
            }
            else
            {
                // Continuous Random: 随机不放回地播完所有子元素，算一轮
                var remaining = new List<uint>(container.Segments);

                while (remaining.Count > 0)
                {
                    if (handle.Cancelled) yield break;

                    int idx = Random.Range(0, remaining.Count);
                    uint chosen = remaining[idx];
                    remaining.RemoveAt(idx);

                    if (container.StrategyParam > 0 && remaining.Count < container.Segments.Count - 1)
                        yield return new WaitForSeconds(container.StrategyParam);

                    yield return PlayChildAndWait(chosen, volumeScale, handle);
                }
            }
        }

        // ─────────────────────────────────────────────
        // 子元素分发（Segment 或嵌套 Container）
        // ─────────────────────────────────────────────

        /// <summary>
        /// 播放一个子元素（segment 或 container），等待其完成后返回。
        /// </summary>
        IEnumerator PlayChildAndWait(uint id, float volumeScale, ContainerPlayHandle parentHandle)
        {
            bool done = false;
            ContainerPlayHandle child = PlayChild(id, volumeScale, () => done = true);
            if (child != null)
                parentHandle.ChildHandles.Add(child);

            yield return new WaitUntil(() => done || parentHandle.Cancelled);

            if (parentHandle.Cancelled && child != null)
                Stop(child);
        }

        /// <summary>
        /// 启动一个子元素的播放，不等待，返回句柄。
        /// </summary>
        ContainerPlayHandle PlayChild(uint id, float volumeScale, System.Action onDone)
        {
            // ID < 1000000 是 MusicSegment，否则是嵌套 Container
            return id < 1000000u ? PlaySegment(id, volumeScale, onDone) : Play(id, onDone);
        }

        // ─────────────────────────────────────────────
        // Segment 播放
        // ─────────────────────────────────────────────

        ContainerPlayHandle PlaySegment(uint segmentId, float volumeScale, System.Action onFinished)
        {
            MusicSegment segment = this.m_segmentConfig.QueryById(segmentId);
            if (segment == null)
            {
                Debug.LogError($"[MusicContainerPlayer] 找不到 SegmentId: {segmentId}");
                onFinished?.Invoke();
                return null;
            }

            AudioClip clip = Resources.Load<AudioClip>($"Audios/{segment.Name}");
            if (!clip)
            {
                Debug.LogError($"[MusicContainerPlayer] 音频文件未找到: {segment.Name}");
                onFinished?.Invoke();
                return null;
            }

            AudioSource source = this.m_pool.AcquireAudioSource();
            source.clip   = clip;
            source.loop   = false;
            source.volume = volumeScale;
            source.Play();

            var handle = new ContainerPlayHandle();
            handle.ActiveSources.Add(source);
            handle.Coroutine = this.m_coroutineHost.StartCoroutine(
                WaitSegmentFinish(source, handle, onFinished));
            return handle;
        }

        IEnumerator WaitSegmentFinish(AudioSource source, ContainerPlayHandle handle, System.Action onFinished)
        {
            yield return new WaitWhile(() => source.isPlaying && !handle.Cancelled);
            
            
            source.Stop();
            ReturnSource(source);
            handle.ActiveSources.Remove(source);
            if (!handle.Cancelled)
                onFinished?.Invoke();
        }

        // ─────────────────────────────────────────────
        // 工具
        // ─────────────────────────────────────────────

        void ReturnSource(AudioSource source)
        {
            source.Stop();
            this.m_pool.ReturnToPool(source.gameObject);
        }

        int GetNextSequenceIndex(MusicContainer container)
        {
            int current = this.m_sequenceStepIndex.GetValueOrDefault(container.Id, 0);
            this.m_sequenceStepIndex[container.Id] = (current + 1) % container.Segments.Count;
            return current;
        }

        uint PickRandomChild(MusicContainer container)
        {
            if (!this.m_randomHistory.TryGetValue(container.Id, out HashSet<uint> history))
            {
                history = new HashSet<uint>();
                this.m_randomHistory[container.Id] = history;
            }

            List<uint> available = container.Segments.Where(id => !history.Contains(id)).ToList();
            if (available.Count == 0)
            {
                history.Clear();
                available = new List<uint>(container.Segments);
            }

            uint chosen = available[Random.Range(0, available.Count)];
            history.Add(chosen);
            return chosen;
        }
    }

    // ─────────────────────────────────────────────
    // 播放句柄
    // ─────────────────────────────────────────────

    /// <summary>
    /// 一次 Container 播放的句柄，用于外部停止或淡出时访问正在播放的 AudioSource。
    /// </summary>
    public class ContainerPlayHandle
    {
        public Coroutine Coroutine;
        public bool Cancelled;
        public float TargetVolume = 1f;
        public List<AudioSource> ActiveSources  = new();
        public List<ContainerPlayHandle> ChildHandles = new();

        /// <summary>
        /// 递归收集所有正在发声的 AudioSource（用于淡出）
        /// </summary>
        public void CollectActiveSources(List<AudioSource> result)
        {
            result.AddRange(this.ActiveSources);
            foreach (ContainerPlayHandle child in this.ChildHandles)
                child.CollectActiveSources(result);
        }
    }
}
