using GraphicsManager.Interfaces;
using GraphicsManager.Objects;
using OpenTK.Mathematics;

namespace Luski.GUI.MainScreen.UI.LuskiControls;

public class ToggleSwitch : UserControl
{
    public ToggleSwitch()
    :base(Globals.ms.TextureManager.GetTextureResource("Toggle.png"))
    {
        base.Size = new(40.ScaleInt(), 24.ScaleInt());
        base.BackgroundColor = OffBackgroundColor;
        Shader = Rectangle.DefaultAlphaShader[Globals.ms.Context];
        Clicked += o =>
        {
            Value = !Value;
            return Task.CompletedTask;
        };
    }

    public event Func<ToggleSwitch, Task>? ValueChanged; 

    public override void LoadToParent(IParent Parent, IWindow Window)
    {
        Value = !Value;
        Value = !Value;
        base.LoadToParent(Parent, Window);
    }

    public new Vector2i Size
    {
        get => base.Size;
        private set => base.Size = value;
    }

    public bool Value
    {
        get => val;
        set
        {
            if (value) BackgroundColor = OnBackgroundColor;
            else BackgroundColor = OffBackgroundColor;
            val = value;
            if (Loaded && ValueChanged is not null) ValueChanged.Invoke(this);
        }
    }

    public Color4 OnBackgroundColor { get; set; }= new(35, 165, 90, 255); 
    public Color4 OffBackgroundColor { get; set; } = new(128, 132, 142, 255);
    private bool val = true;
}