package tk

import (
	
)

// ComboBox is a Widget that combines a ListBox and a CodeArea.
type ComboBox interface {
	Widget
	// Returns the embedded codearea widget.
	CodeArea() CodeArea
	// Returns the embedded listbox widget.
	ListBox() ListBox
	// Forces the filtering to rerun.
	Refilter()
}

// ComboBoxSpec specifies the configuration and initial state for ComboBox.
type ComboBoxSpec struct {
	CodeArea CodeAreaSpec
	ListBox  ListBoxSpec
	OnFilter func(ComboBox, string)
}

type comboBox struct {
	codeArea CodeArea
	listBox  ListBox
	OnFilter func(ComboBox, string)

	// Last filter value.
	lastFilter string
}

// NewComboBox creates a new ComboBox from the given spec.
func ( ComboBoxSpec) ComboBox {
	if .OnFilter == nil {
		.OnFilter = func(ComboBox, string) {}
	}
	 := &comboBox{
		codeArea: NewCodeArea(.CodeArea),
		listBox:  NewListBox(.ListBox),
		OnFilter: .OnFilter,
	}
	.OnFilter(, "")
	return 
}

// Render renders the codearea and the listbox below it.
func ( *comboBox) (,  int) *term.Buffer {
	 := .codeArea.Render(, )
	 := .listBox.Render(, -len(.Lines))
	.Extend(, false)
	return 
}

// Handle first lets the listbox handle the event, and if it is unhandled, lets
// the codearea handle it. If the codearea has handled the event and the code
// content has changed, it calls OnFilter with the new content.
func ( *comboBox) ( term.Event) bool {
	if .listBox.Handle() {
		return true
	}
	if .codeArea.Handle() {
		 := .codeArea.CopyState().Buffer.Content
		if  != .lastFilter {
			.OnFilter(, )
			.lastFilter = 
		}
		return true
	}
	return false
}

func ( *comboBox) () {
	.OnFilter(, .codeArea.CopyState().Buffer.Content)
}

func ( *comboBox) () CodeArea { return .codeArea }
func ( *comboBox) () ListBox   { return .listBox }