package alui

import (
	"opslag.de/schobers/allg5"
	"opslag.de/schobers/geom"
)

type Container struct {
	ControlBase

	Children []Control
}

func (c *Container) AddChild(child ...Control) {
	c.Children = append(c.Children, child...)
}

func (c *Container) DesiredSize(ctx *Context) geom.PointF32 {
	var size geom.PointF32
	for _, child := range c.Children {
		s := child.DesiredSize(ctx)
		if geom.IsNaN32(s.X) || geom.IsNaN32(size.X) {
			size.X = geom.NaN32()
		} else {
			size.X = geom.Max32(s.X, size.X)
		}
		if geom.IsNaN32(s.Y) || geom.IsNaN32(size.Y) {
			size.Y = geom.NaN32()
		} else {
			size.Y = geom.Max32(s.Y, size.Y)
		}
	}
	return size
}

func (c *Container) Handle(e allg5.Event) {
	c.ControlBase.Handle(e)
	for _, child := range c.Children {
		child.Handle(e)
	}
}

func (c *Container) Layout(ctx *Context, bounds geom.RectangleF32) {
	c.ControlBase.Layout(ctx, bounds)
	for _, child := range c.Children {
		child.Layout(ctx, bounds)
	}
}

func (c *Container) Render(ctx *Context, bounds geom.RectangleF32) {
	c.ControlBase.Render(ctx, bounds)
	for _, child := range c.Children {
		child.Render(ctx, bounds)
	}
}
