package main

import (
	"strconv"

	"github.com/awesome-gocui/gocui"
	"go.wit.com/log"
	"go.wit.com/toolkits/tree"
	"go.wit.com/widget"
)

func initWidget(n *tree.Node) *guiWidget {
	var w *guiWidget
	w = new(guiWidget)

	w.node = n
	w.cuiName = strconv.Itoa(w.node.WidgetId) + " TK"
	w.WidgetType = n.WidgetType
	w.labelN = n.State.Label
	if w.labelN == "" {
		// remove this debugging hack once things are stable and fixed
		w.labelN = n.GetProgName()
	}
	w.frame = true
	w.enable = n.State.Enable

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

	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
	}

	p := n.Parent
	if p == nil {
		log.Log(ERROR, "parent == nil", w.String(), n.WidgetId, w.WidgetType)
		return w
	}
	if p.TK == nil {
		log.Log(ERROR, "parent.TK == nil", w.String(), n.WidgetId, w.WidgetType)
		return w
	}

	// set the parent and append to parent children
	var ptk *guiWidget
	ptk = p.TK.(*guiWidget)
	w.parent = ptk
	ptk.children = append(ptk.children, w)
	return w
}

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

	me.ctrlDown = n
}

func (w *guiWidget) deleteView() {
	// make sure the view isn't really there
	log.Log(GOCUI, "deleteView()", w.cuiName, w.WidgetType, w.node.WidgetId)
	me.baseGui.DeleteView(w.cuiName)
	w.v = nil
}

func (w *guiWidget) IsCurrent() bool {
	if w.node.WidgetType == widget.Tab {
		return w.isCurrent
	}
	if w.node.WidgetType == widget.Window {
		log.Log(GOCUI, "IsCurrent() found current window", w.cuiName, w.String())
		log.Log(GOCUI, "IsCurrent() window w.isCurrent =", w.isCurrent)
		return w.isCurrent
	}
	if w.node.WidgetType == widget.Root {
		return false
	}
	return w.parent.IsCurrent()
}

func (tk *guiWidget) String() string {
	return tk.node.String()
}

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

func (w *guiWidget) Show() {
	// always should the dropdown widget
	if w == me.dropdownV {
		me.dropdownV.drawView()
		return
	}

	// don't display fake widgets
	if w.isFake {
		return
	}

	// if this isn't in the binary tree
	// it's some internal widget so always display those
	if w.node == nil {
		w.drawView()
		return
	}

	// always show window titles
	if w.node.WidgetType == widget.Window {
		w.drawView()
		return
	}

	if w.node.WidgetType == widget.Dropdown {
		log.Log(NOW, "Show() dropdown", w.cuiName, w.String())
		log.Log(NOW, "Show() dropdown n.Strings() =", w.node.Strings())
	}
	if w.node.WidgetType == widget.Combobox {
		log.Log(NOW, "Show() dropdown", w.cuiName, w.String())
		log.Log(NOW, "Show() dropdown n.Strings() =", w.node.Strings())
	}

	// if the widget is not in the current displayed windo
	// then ignore it
	log.Log(GOCUI, "Show() widget =", w.cuiName, w.String())
	log.Log(GOCUI, "Show() w.IsCurrent() returned", w.IsCurrent())
	if !w.IsCurrent() {
		log.Log(GOCUI, "Show() NOT drawing", w.cuiName, w.String())
		return
	}
	log.Log(GOCUI, "Show() drawing", w.cuiName, w.String())

	// finally, check if the widget State is hidden or not
	if w.node.Hidden() {
		// don't display hidden widgets
		return
	}

	// okay, if you made it this far, then display the widget
	w.drawView()
}

func (tk *guiWidget) Hide() {
	tk.deleteView()
}

func (tk *guiWidget) SetVisible(b bool) {
	if b {
		tk.Show()
	} else {
		tk.Hide()
	}
}

func (tk *guiWidget) findWidgetByName(name string) *guiWidget {
	if tk.cuiName == name {
		return tk
	}
	for _, child := range tk.children {
		found := child.findWidgetByName(name)
		if found != nil {
			return found
		}
	}
	return nil
}

func (tk *guiWidget) findWidgetByView(v *gocui.View) *guiWidget {
	if tk.v == v {
		return tk
	}
	if tk.cuiName == v.Name() {
		log.Log(NOW, "findWidget() error. names are mismatched or out of sync", tk.cuiName)
		log.Log(NOW, "findWidget() or maybe the view has been deleted")
		// return tk
	}
	for _, child := range tk.children {
		found := child.findWidgetByView(v)
		if found != nil {
			return found
		}
	}
	return nil
}