package ui

import (
	"code.ur.gs/lupine/ordoor/internal/menus"
)

// An inventory select is a sort of radio button. If 2 share the same menu,
// selecting one deselects the other. Otherwise, they act like checkboxes.
//
// TODO: wrap all the behaviour in a single struct to make it easier to drive
type inventorySelect struct {
	checkbox

	parentPath string
	others     []*inventorySelect
}

// Called from the menu, which fills "others" for us
func (d *Driver) buildInventorySelect(p *menus.Properties) (*inventorySelect, *Widget, error) {
	c, _, err := d.buildCheckbox(p)
	if err != nil {
		return nil, nil, err
	}

	// In an inventorySelect, the frozen and click sprites are reversed
	c.clickSpr, c.frozenSpr = c.frozenSpr, c.clickSpr

	element := &inventorySelect{checkbox: *c}
	widget := &Widget{
		Active:        p.Active,
		ownClickables: []clickable{element},
		ownFreezables: []freezable{element},
		ownHoverables: []hoverable{element},
		ownPaintables: []paintable{element},
		ownValueables: []valueable{element},
	}

	return element, widget, nil
}

func (i *inventorySelect) registerMouseClick() {
	// Do nothing if we're already selected
	if i.value() == "1" {
		return
	}

	// Turn us on, turn everyone else off
	for _, other := range i.others {
		other.setValue("0")
	}

	i.setValue("1")
}
