using Voile.Rendering;

namespace Voile.UI.Containers;

/// <summary>
/// A special container that occupies the entire available size of the parent. <br />
/// Usually used as a root element for the UI system.
/// </summary>
public class FillContainer : Container
{
    public FillContainer()
    {

    }

    public FillContainer(Rect minimumSize) : base(minimumSize)
    {

    }

    public override void Arrange()
    {

    }

    protected override void OnRender(RenderSystem renderer, Style style)
    {
        base.OnRender(renderer, style);

        Rect parentSize;

        if (Parent != null)
        {
            parentSize = Parent.Size;
        }
        else
        {
            var windowSize = renderer.WindowSize;
            var windowRect = new Rect(windowSize.X, windowSize.Y);

            parentSize = windowRect;
        }

        if (_lastParentSize != parentSize)
        {
            Size = parentSize;
            _lastParentSize = parentSize;
        }
    }

    protected override void OnUpdate()
    {
        base.OnUpdate();
        Size = _lastParentSize;

        if (Children.Count != 0)
        {
            Children[0].Size = Size;
        }
    }

    private Rect _lastParentSize = Rect.Zero;
}