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

namespace UITypes
{

	public class Visualizer : VisualElement
	{
		[UnityEngine.Scripting.Preserve]
		public new class UxmlFactory : UxmlFactory<Visualizer> { }

		private List<VisualElement> bars = new();

		const int barCount = 128;

		public void InitVisualizer(string style)
		{
			for (int i = 0; i < barCount; i++)
			{
				VisualElement element = new();
				element.AddToClassList("Bar");
				element.AddToClassList(style);
				bars.Add(element);
				Add(element);
			}
		}

		public void SetBarColor(Color32 color)
		{
			bars.ForEach(bar => bar.style.backgroundColor = new StyleColor(color));
		}

		public void UpdateBars(float[] spectrumData)
		{
			for (int i = 0; i < barCount; i++)
			{
				VisualElement elem = bars[i];

				float width = Math.Min(spectrumData[i] * 50, 50);
				elem.style.width = Length.Percent(width);
			}
		}
	}
}
