using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

namespace OCES.Audio.Editor
{
#if UNITY_EDITOR || DEVELOPMENT_BUILD
    public sealed class DebugInfoCollector : MonoBehaviour
    {
        
        public static DebugInfoCollector Instance { get; private set; }
        
        public Dictionary<uint, int> ClipConcurrentCount = new();
        public List<ActiveSound> ActiveSounds = new();
        public Dictionary<Type, Enum> ActiveStates = new();        
        readonly StringBuilder m_stringBuilder = new();
        Text m_textComponent;

        void Awake()
        {
            if ((bool)Instance && Instance != this)
            {
                Destroy(gameObject);
                return;
            }
            Instance = this;

            this.m_textComponent = GetComponent<Text>();
        }
        
        void Update()
        {
            this.m_stringBuilder.Clear();

            this.m_stringBuilder.AppendLine("Current States:");
            foreach (KeyValuePair<Type, Enum> activeState in this.ActiveStates)
            {
                this.m_stringBuilder.AppendLine($"{activeState.Key.Name} is {activeState.Value}");
            }
            this.m_stringBuilder.AppendLine();
            
            this.m_stringBuilder.AppendLine("Clip Concurrent Count:");
            foreach (KeyValuePair<uint, int> clipCount in this.ClipConcurrentCount)
            {
                this.m_stringBuilder.AppendLine($"{clipCount.Key} is playing {clipCount.Value}");
            }
            this.m_stringBuilder.AppendLine();
            
            this.m_stringBuilder.Append("ActiveSounds:");
            foreach (ActiveSound activeSound in this.ActiveSounds)
            {
                this.m_stringBuilder.AppendLine($"{activeSound.AudioObject.Id}");
            }

            this.m_textComponent.text = this.m_stringBuilder.ToString();
        }
    }
#endif
}
