package layouts

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

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

// InputControl is a layout that has a label and an a widget associated with it.
type InputControl struct {
	*base.WithFocus
	label   *widgets.Label
	widget  ui.InputWidget
	spacing int
}

func NewInputControl(label *widgets.Label, wiget ui.InputWidget) *InputControl {
	return &InputControl{
		WithFocus: &base.WithFocus{},
		label:     label,
		widget:    wiget,
		spacing:   10,
	}
}

func (ic InputControl) Size() core.Vec2i {
	return core.Vec2i{
		X: ic.label.Size().X + ic.widget.Size().X + ic.spacing,
		Y: max(ic.label.Size().Y, ic.widget.Size().Y),
	}
}

func (ic *InputControl) SetFocus(value bool) {
	ic.WithFocus.SetFocus(value)
	ic.widget.SetFocus(value)

	// TODO: use a theme system here so colors are not hardcoded.
	if value {
		ic.label.SetColor(rl.Red)
	} else {
		ic.label.SetColor(rl.White)
	}
}

func (ic InputControl) HandleInput() {
	ic.widget.HandleInput()
}

func (ic InputControl) Draw(x, y int32) {
	size := ic.Size()
	label_y := (int32(size.Y) - int32(ic.label.Size().Y)) / 2
	ic.label.Draw(x, y+label_y)
	ic.widget.Draw(x+int32(ic.label.Size().X+ic.spacing), y)
}
