using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.UIElements;

namespace RebootKit.Engine.UI {
    public static class Classes {
        public const string k_Base = "rr__base";
        public const string k_Label = "rr__label";
        public const string k_Button = "rr__button";
    }

    public interface IVisualElementBuilder {
        VisualElement Build();
    }

    public class VisualElementBuilder : IVisualElementBuilder {
        readonly List<string> m_ClassNames = new List<string>();

        Color? m_BackgroundColor;
        float2? m_Size;

        VisualElementBuilder() {
        }

        public VisualElementBuilder Class(string className) {
            m_ClassNames.Add(className);
            return this;
        }

        public VisualElementBuilder Class(params string[] classNames) {
            foreach (string className in classNames) {
                m_ClassNames.Add(className);
            }

            return this;
        }

        public VisualElementBuilder BackgroundColor(Color color) {
            m_BackgroundColor = color;
            return this;
        }
        
        public VisualElementBuilder Size(float2 size) {
            m_Size = size;
            return this;
        }

        public VisualElement Build() {
            VisualElement element = new VisualElement();
            element.AddToClassList(Classes.k_Base);

            foreach (string className in m_ClassNames) {
                element.AddToClassList(className);
            }

            if (m_BackgroundColor.HasValue) {
                element.style.backgroundColor = m_BackgroundColor.Value;
            }
            
            if (m_Size.HasValue) {
                element.style.width = m_Size.Value.x;
                element.style.height = m_Size.Value.y;
            }

            return element;
        }

        public static VisualElementBuilder New() {
            return new VisualElementBuilder();
        }
    }

    public readonly struct LabelBuilder : IVisualElementBuilder {
        readonly string m_Text;

        LabelBuilder(string text) {
            m_Text = text;
        }

        public VisualElement Build() {
            Label label = new Label(m_Text);
            label.AddToClassList(Classes.k_Base);
            label.AddToClassList(Classes.k_Label);
            return label;
        }
        
        public static LabelBuilder New(string text) => new LabelBuilder(text);
    }
}