package widgets

import (
	"tetris/engine/core"
	"tetris/engine/render"
	"tetris/game/ui/base"

	rl "github.com/gen2brain/raylib-go/raylib"
)

type Button struct {
	*base.WithFocus
	Text     string
	TextSize int32
	Action   func()
}

func NewButton(text string, size int32, action func()) Button {
	return Button{
		WithFocus: &base.WithFocus{},
		Text:      text,
		TextSize:  size,
		Action:    action,
	}
}

func (b Button) Size() core.Vec2i {
	s := int(b.TextSize)
	return core.Vec2i{
		X: len(b.Text) * s,
		Y: s,
	}
}

func (b Button) HandleInput() {
	if rl.IsKeyPressed(rl.KeyEnter) {
		b.Action()
	}
}

func (b Button) Draw(x, y int32) {
	// TODO: use a theme system here so colors are not hardcoded.
	col := rl.White
	if b.HasFocus() {
		col = rl.Red
	}
	render.DrawTextCenter(x, y, b.TextSize, b.Text, col)
}
