using Engine.Scene;
using OpenTK.Mathematics;

namespace DoomDeathmatch.Component.UI;

public class StackComponent : UiContainerComponent
{
  public List<UiComponent> Children => _children;
  public Orientation Orientation { get; set; } = Orientation.Vertical;

  private readonly List<UiComponent> _children = [];

  public override void Update(double parDeltaTime)
  {
    base.Update(parDeltaTime);

    var size = GameObject.Transform.Size.Xy;
    var count = _children.Count;

    size *= Orientation == Orientation.Horizontal ? new Vector2(1, 0) : new Vector2(0, 1);

    var offset = new Vector2(-size.X / 2 + size.X / count / 2, -size.Y / 2 + size.Y / count / 2);

    for (var i = count - 1; i >= 0; i--)
    {
      var child = _children[i];

      child.Offset = offset;
      offset += size / count;
    }
  }
}