using System.Numerics;

namespace Voile.UI.Containers;

public class MarginContainer : Container
{
    /// <summary>
    /// The margin to apply around the contents of this container.
    /// </summary>
    public Size Margin { get; set; }

    /// <summary>
    /// Specifies if this <see cref="MarginContainer"/> will fill to parent size.
    /// </summary>
    public bool Fill { get; set; } = true;

    public MarginContainer() : this(new Size()) { }

    public MarginContainer(Size margin)
    {
        Margin = margin;
    }

    protected override void OnUpdate()
    {
        base.OnUpdate();
        if (Parent == null) return;

        if (Size != Parent.Size)
        {
            Size = Parent.Size;
        }
    }

    public override void Arrange()
    {
        foreach (var child in Children)
        {
            var newPosition = new Vector2(Margin.Left, Margin.Top);
            var newSize = new Rect(
                Size.Width - Margin.Left - Margin.Right,
                Size.Height - Margin.Top - Margin.Bottom
            );

            child.Size = newSize;
            child.LocalPosition = newPosition;
        }
    }
}