package main

import (
	"errors"

	"go.wit.com/lib/widget"
	"go.wit.com/log"
	"go.wit.com/toolkits/tree"
)

// this will check to make sure that the node
// is valid for making a New TK andlabs widget
// Basically, it makes sure there is a parent ID
// and that there already a widget created
func notNew(n *tree.Node) bool {
	if n == nil {
		log.Warn("ready() n = nil")
		return true
	}
	if n.TK != nil {
		log.Warn("ready() n.TK = nil", n.WidgetId, n.GetProgName())
		return true
	}
	if n.Parent == nil {
		log.Warn("ready() n.Parent = nil", n.WidgetId, n.GetProgName())
		return true
	}
	if n.Parent.TK == nil {
		log.Warn("ready() n.Parent.TK = nil", n.WidgetId, n.GetProgName())
		log.Warn("ready() n.Parent.TK = nil", n.Parent.WidgetId, n.Parent.GetProgName())
		return true
	}
	// this means you can add a new widgets
	return false
}

func ready(n *tree.Node) bool {
	if n == nil {
		log.Warn("ready() n = nil")
		return false
	}
	if n.TK == nil {
		log.Warn("ready() n.TK = nil", n.WidgetId, n.GetProgName())
		return false
	}
	if n.Parent == nil {
		log.Warn("ready() n.Parent = nil", n.WidgetId, n.GetProgName())
		return false
	}
	if n.Parent.TK == nil {
		log.Warn("ready() n.Parent.TK = nil", n.WidgetId, n.GetProgName())
		log.Warn("ready() n.Parent.TK = nil", n.Parent.WidgetId, n.Parent.GetProgName())
		return false
	}
	return true
}
func (n *node) ready() bool {
	if n == nil {
		return false
	}
	if n.tk == nil {
		return false
	}
	return true
}

func show(n *tree.Node, b bool) {
	var tk *guiWidget
	tk = n.TK.(*guiWidget)
	// tk = getTK(n)

	if tk == nil {
		return
	}
	if tk.uiControl == nil {
		return
	}
	if b {
		tk.uiControl.Show()
	} else {
		tk.uiControl.Hide()
	}
}

func enable(n *tree.Node, b bool) {
	var tk *guiWidget
	tk = n.TK.(*guiWidget)
	if n == nil {
		panic("WHAT? enable was passed nil. How does this even happen?")
	}
	if tk == nil {
		return
	}
	if tk.uiControl == nil {
		return
	}
	if b {
		tk.uiControl.Enable()
	} else {
		tk.uiControl.Disable()
	}
}

func (n *node) pad(b bool) {
	log.Warn("pad() on WidgetId =", n.WidgetId)

	t := n.tk
	if t == nil {
		log.Log(ERROR, "pad() toolkit struct == nil. for", n.WidgetId)
		return
	}

	switch n.WidgetType {
	case widget.Window:
		t.uiWindow.SetMargined(b)
		t.uiWindow.SetBorderless(b)
	case widget.Tab:
		tabSetMargined(t.uiTab, b)
	case widget.Group:
		t.uiGroup.SetMargined(b)
	case widget.Grid:
		t.uiGrid.SetPadded(b)
	case widget.Box:
		t.uiBox.SetPadded(b)
	default:
		log.Log(ERROR, "TODO: implement pad() for", n.WidgetType, n.progname)
	}
}

func (n *node) move(newParent *node) {
	p := n.parent

	switch p.WidgetType {
	case widget.Group:
	case widget.Tab:
		// tabSetMargined(tParent.uiTab, true)
	case widget.Window:
		// t.uiWindow.SetBorderless(false)
	case widget.Grid:
		// t.uiGrid.SetPadded(true)
	case widget.Box:
		log.Log(INFO, "TODO: move() where =", p.ParentId)
		log.Log(INFO, "TODO: move() for widget =", n.WidgetId)

		stretchy = true
		if p.tk.uiBox != nil {
			p.tk.uiBox.Append(n.tk.uiControl, stretchy)
		}
	default:
		log.Log(ERROR, "TODO: need to implement move() for type =", n.WidgetType)
		log.Log(ERROR, "TODO: need to implement move() for where =", p.ParentId)
		log.Log(ERROR, "TODO: need to implement move() for widget =", n.WidgetId)
	}
}

func (n *node) Delete() {
	p := n.parent
	log.Log(NOW, "uiDelete()", n.WidgetId, "to", p.WidgetId)

	if n.WidgetType == widget.Window {
		log.Warn("DESTROY uiWindow here")
		log.Warn("NEED TO REMOVE n from parent.Children")
		n.tk.uiWindow.Destroy()
		n.tk.uiWindow = nil
		for i, child := range p.children {
			log.Warn("parent has child:", i, child.WidgetId, child.progname)
			if n == child {
				log.Warn("Found child ==", i, child.WidgetId, child.progname)
				log.Warn("Found n ==", i, n.WidgetId, n.progname)
				p.children = append(p.children[:i], p.children[i+1:]...)
			}
			// t.uiWindow.SetBorderless(false)
		}
		for i, child := range p.children {
			log.Warn("parent now has child:", i, child.WidgetId, child.progname)
		}
		return
	}

	switch p.WidgetType {
	case widget.Group:
		// tParent.uiGroup.SetMargined(true)
	case widget.Tab:
		// tabSetMargined(tParent.uiTab, true)
	case widget.Window:
	case widget.Grid:
		// t.uiGrid.SetPadded(true)
	case widget.Box:
		log.Log(NOW, "tWidget.boxC =", p.progname)
		log.Log(NOW, "is there a tParent parent? =", p.parent)
		if p.tk.boxC < 1 {
			log.Log(NOW, "Can not delete from Box. already empty. tWidget.boxC =", p.tk.boxC)
			return
		}
		p.tk.uiBox.Delete(0)
		p.tk.boxC -= 1

		// this didn't work:
		// tWidget.uiControl.Disable()
		// sleep(.8)
		// tParent.uiBox.Append(tWidget.uiControl, stretchy)
	default:
		log.Log(ERROR, "TODO: need to implement uiDelete() for widget =", n.WidgetId, n.WidgetType)
		log.Log(ERROR, "TODO: need to implement uiDelete() for parent =", p.WidgetId, p.WidgetType)
	}
}

func rawAction(a *widget.Action) {
	log.Log(INFO, "rawAction() START a.ActionType =", a.ActionType, "a.Value", a.Value)

	if a.ActionType == widget.ToolkitInit {
		Init()
		return
	}
	switch a.WidgetType {
	case widget.Root:
		me.treeRoot = me.myTree.AddNode(a)
		log.Log(INFO, "doAction() found treeRoot")
		return
	}

	log.Warn("andlabs rawAction() START a.WidgetId =", a.WidgetId, "a.ParentId =", a.ParentId, a.ActionType)
	switch a.WidgetType {
	case widget.Flag:
		log.Log(ERROR, "rawAction() RE-IMPLEMENT LOG FLAGS")
		return
	}

	if me.treeRoot == nil {
		panic("me.treeRoot == nil")
	}

	n := me.treeRoot.FindWidgetId(a.WidgetId)

	if a.ActionType == widget.Add {
		me.treeRoot.ListWidgets()
		// ui.QueueMain(func() {
		add(a)
		// })
		// TODO: remove this artificial delay
		// sleep(.001)
		return
	}

	if a.ActionType == widget.Dump {
		log.Log(NOW, "rawAction() Dump =", a.ActionType, a.WidgetType, n.State.ProgName)
		// me.rootNode.listChildren(true)
		return
	}

	if n == nil {
		log.Error(errors.New("andlabs rawAction() ERROR findWidgetId found nil"), a.ActionType, a.WidgetType)
		log.Log(NOW, "rawAction() ERROR findWidgetId found nil for id =", a.WidgetId)
		log.Log(NOW, "rawAction() ERROR findWidgetId found nil", a.ActionType, a.WidgetType)
		log.Log(NOW, "rawAction() ERROR findWidgetId found nil for id =", a.WidgetId)
		me.treeRoot.ListWidgets()
		return
		panic("findWidgetId found nil for id = " + string(a.WidgetId))
	}

	switch a.ActionType {
	case widget.Show:
		show(n, true)
		// n.show(true)
	case widget.Hide:
		show(n, false)
		//n.show(false)
	case widget.Enable:
		enable(n, true)
		// n.enable(true)
	case widget.Disable:
		log.Warn("andlabs got disable for", n.WidgetId, n.State.ProgName)
		enable(n, false)
		// n.enable(false)
	case widget.Get:
		// n.setText(a)
		setText(n, a)
	case widget.GetText:
		switch a.WidgetType {
		case widget.Textbox:
			a.Value = n.State.Value
		}
	case widget.Set:
		setText(n, a)
		// n.setText(a)
	case widget.SetText:
		setText(n, a)
		// n.setText(a)
	case widget.AddText:
		addText(n, a)
		// n.addText(a)
		/*
			case widget.Margin:
				n.pad(true)
			case widget.Unmargin:
				n.pad(false)
			case widget.Pad:
				n.pad(true)
			case widget.Unpad:
				n.pad(false)
			case widget.Delete:
				n.Delete()
			case widget.Move:
				log.Log(NOW, "rawAction() attempt to move() =", a.ActionType, a.WidgetType)
		*/
	default:
		log.Log(ERROR, "rawAction() Unknown =", a.ActionType, a.WidgetType)
	}
	log.Log(INFO, "rawAction() END =", a.ActionType, a.WidgetType)
}
