using System;
using UnityEngine;

namespace MeidoPhotoStudio.Plugin;

public class SelectionGrid : BaseControl
{
    private SimpleToggle[] toggles;
    private int selectedItemIndex;

    public int SelectedItemIndex
    {
        get => selectedItemIndex;
        set
        {
            selectedItemIndex = Mathf.Clamp(value, 0, toggles.Length - 1);

            foreach (var toggle in toggles)
                toggle.value = toggle.toggleIndex == selectedItemIndex;

            OnControlEvent(EventArgs.Empty);
        }
    }

    public SelectionGrid(string[] items, int selected = 0)
    {
        selectedItemIndex = Mathf.Clamp(selected, 0, items.Length - 1);
        toggles = MakeToggles(items);
    }

    public void SetItems(string[] items, int selectedItemIndex = -1)
    {
        if (selectedItemIndex < 0)
            selectedItemIndex = SelectedItemIndex;

        if (items.Length != toggles.Length)
            toggles = MakeToggles(items);
        else
        {
            for (var i = 0; i < items.Length; i++)
            {
                var item = items[i];

                toggles[i].value = i == SelectedItemIndex;
                toggles[i].label = item;
            }
        }

        SelectedItemIndex = Mathf.Clamp(selectedItemIndex, 0, items.Length - 1);
    }

    public override void Draw(params GUILayoutOption[] layoutOptions)
    {
        GUILayout.BeginHorizontal();

        foreach (var toggle in toggles)
            toggle.Draw(layoutOptions);

        GUILayout.EndHorizontal();
    }

    private SimpleToggle[] MakeToggles(string[] items)
    {
        var toggles = new SimpleToggle[items.Length];

        for (var i = 0; i < items.Length; i++)
        {
            var toggle = new SimpleToggle(items[i], i == SelectedItemIndex)
            {
                toggleIndex = i
            };

            toggle.ControlEvent += (s, a) =>
            {
                var value = (s as SimpleToggle).toggleIndex;

                if (value != SelectedItemIndex)
                    SelectedItemIndex = value;
            };

            toggles[i] = toggle;
        }

        return toggles;
    }

    private class SimpleToggle
    {
        public int toggleIndex;
        public bool value;
        public string label;
        public event EventHandler ControlEvent;

        public SimpleToggle(string label, bool value = false)
        {
            this.label = label;
            this.value = value;
        }

        public void Draw(params GUILayoutOption[] layoutOptions)
        {
            var value = GUILayout.Toggle(this.value, label, layoutOptions);

            if (value == this.value)
                return;

            if (!value)
                this.value = true;
            else
            {
                this.value = value;

                ControlEvent?.Invoke(this, EventArgs.Empty);
            }
        }
    }
}
