package main

import (
	"go.wit.com/lib/widget"
	"go.wit.com/log"
)

func initWidget(n *node) *guiWidget {
	var w *guiWidget
	w = new(guiWidget)
	// Set(w, "default")

	w.frame = true

	// set the name used by gocui to the id
	w.cuiName = string(n.WidgetId)

	if n.WidgetType == widget.Root {
		log.Log(INFO, "setupWidget() FOUND ROOT w.id =", n.WidgetId)
		n.WidgetId = 0
		me.rootNode = n
		return w
	}

	if n.WidgetType == widget.Grid {
		w.widths = make(map[int]int)  // how tall each row in the grid is
		w.heights = make(map[int]int) // how wide each column in the grid is
	}

	return w
}

func setupCtrlDownWidget() {
	a := new(widget.Action)
	a.ProgName = "ctrlDown"
	a.WidgetType = widget.Dialog
	a.WidgetId = -1
	a.ParentId = 0
	n := addNode(a)

	me.ctrlDown = n
}

func (n *node) deleteView() {
	w := n.tk
	if w.v != nil {
		w.v.Visible = false
		return
	}
	// make sure the view isn't really there
	me.baseGui.DeleteView(w.cuiName)
	w.v = nil
}

// searches the binary tree for a WidgetId
func (n *node) findWidgetName(name string) *node {
	if n == nil {
		return nil
	}

	if n.tk.cuiName == name {
		return n
	}

	for _, child := range n.children {
		newN := child.findWidgetName(name)
		if newN != nil {
			return newN
		}
	}
	return nil
}

func (n *node) IsCurrent() bool {
	w := n.tk
	if n.WidgetType == widget.Tab {
		return w.isCurrent
	}
	if n.WidgetType == widget.Window {
		return w.isCurrent
	}
	if n.WidgetType == widget.Root {
		return false
	}
	return n.parent.IsCurrent()
}

func (n *node) Visible() bool {
	if n == nil {
		return false
	}
	if n.tk == nil {
		return false
	}
	if n.tk.v == nil {
		return false
	}
	return n.tk.v.Visible
}

func (n *node) SetVisible(b bool) {
	if n == nil {
		return
	}
	if n.tk == nil {
		return
	}
	if n.tk.v == nil {
		return
	}
	n.tk.v.Visible = b
}

func addDropdown() *node {
	n := new(node)
	n.WidgetType = widget.Flag
	n.WidgetId = -2
	n.ParentId = 0

	// copy the data from the action message
	n.progname = "DropBox"
	n.tk.label = "DropBox text"

	// store the internal toolkit information
	n.tk = new(guiWidget)
	n.tk.frame = true

	// set the name used by gocui to the id
	n.tk.cuiName = "-1 dropbox"

	n.tk.color = &colorFlag

	// add this new widget on the binary tree
	n.parent = me.rootNode
	if n.parent != nil {
		n.parent.children = append(n.parent.children, n)
	}
	return n
}
