using System.Numerics;
using Voile.Input;
using Voile.Rendering;

namespace Voile.UI.Widgets;

public class RectangleWidget : Widget
{
    public override Rect MinimumSize { get; }
    public Color Color { get; set; } = Color.White;
    public RectangleWidget(Rect minimumRect, Color color) : base()
    {
        MinimumSize = minimumRect;
        Color = color;

        _defaultColor = color;

        _defaultSize = Size;

        _hoverColor = color.Lightened(0.25f);
    }

    protected override void OnRender(RenderSystem renderer, Style style)
    {
        renderer.SetTransform(GlobalPosition, Vector2.Zero);
        renderer.DrawRectangle(new Vector2(Size.Width, Size.Height), Color);
    }

    protected override void OnInput(UIInputContext inputContext)
    {
        if (_defaultSize == Rect.Zero)
        {
            _defaultSize = Size;
        }

        if (_downSize == Rect.Zero)
        {
            _downSize = new Rect(Size.Width * 0.75f, Size.Height * 0.75f);
        }

        var mouseInside = ContainsPoint(inputContext.MousePosition);

        if (mouseInside)
        {
            Color = _hoverColor;
        }
        else
        {
            Color = _defaultColor;
        }

        // if (mouseInside && inputContext.MouseDown)
        // {
        //     Size = _downSize;
        // }

        // if (mouseInside && inputContext.MouseReleased)
        // {
        //     Size = _defaultSize;
        // }

        if (mouseInside && inputContext.MousePressed)
        {
            Console.WriteLine("Hello, I was clicked!");
        }
    }

    protected override void OnUpdate()
    {

    }

    private Color _defaultColor;
    private Color _hoverColor;

    private Rect _defaultSize = Rect.Zero;
    private Rect _downSize = Rect.Zero;
}