package main

import (
	"go.wit.com/dev/andlabs/ui"
	_ "go.wit.com/dev/andlabs/ui/winmanifest"

	"go.wit.com/log"
	"go.wit.com/gui/widget"
)

func (p *node) newCombobox(n *node) {
	newt := new(guiWidget)

	cb := ui.NewEditableCombobox()
	newt.uiEditableCombobox = cb
	newt.uiControl = cb

	// initialize the index
	newt.c = 0
	newt.val = make(map[int]string)

	cb.OnChanged(func(spin *ui.EditableCombobox) {
		n.value = spin.Text()
		log.Warn("combobox changed =" + spin.Text() + ".")
		n.doUserEvent()
	})

	n.tk = newt
	p.place(n)

	// add the initial combobox entries
	for i, s := range n.strings {
		log.Warn("add combobox entries on create", n.progname, i, s)
		n.addComboboxName(s)
	}
	cur := widget.GetString(n.value)
	log.Warn("add combobox: TODO: set default value on create", n.progname, cur)
	n.setComboboxName(cur)
}

func (n *node) addComboboxName(s string) {
	if ! n.ready() { return }
	log.Log(INFO, "addComboboxName()", n.WidgetId, "add:", s)

	n.tk.uiEditableCombobox.Append(s)
	if (n.tk.val == nil) {
		log.Log(INFO, "make map didn't work")
		return
	}
	n.tk.val[n.tk.c] = s

	// If this is the first menu added, set the dropdown to it
	if (n.tk.c == 0) {
		log.Log(INFO, "THIS IS THE FIRST combobox", s)
		n.tk.uiEditableCombobox.SetText(s)
	}
	n.tk.c = n.tk.c + 1
}

func (n *node) setComboboxName(s string) bool {
	if ! n.ready() { return false}
	log.Log(INFO, "SetComboboxName()", n.WidgetId, ",", s)
	n.tk.uiEditableCombobox.SetText(s)
	return false
}
