package alui

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

var _ Control = &Label{}

type Label struct {
	ControlBase

	Text      string
	TextAlign allg5.HorizontalAlignment
}

func (l *Label) DesiredSize(ctx *Context) geom.PointF32 {
	font := ctx.Fonts.Get(l.Font)
	_, _, w, h := font.TextDimensions(l.Text)
	return geom.PtF32(w+8, h+8)
}

func (l *Label) Render(ctx *Context, bounds geom.RectangleF32) {
	back := l.Background
	fore := l.Foreground
	if fore == nil {
		fore = &ctx.Palette.Text
	}

	if back != nil {
		allg5.DrawFilledRectangle(bounds.Min.X, bounds.Min.Y, bounds.Max.X, bounds.Max.Y, *back)
	}
	ctx.Fonts.DrawAlign(l.Font, bounds.Min.X+4, bounds.Min.Y+4, bounds.Max.X-4, *fore, l.TextAlign, l.Text)
}
