package tree

/*
	These code should be common to all gui plugins

	There are some helper functions that are probably going to be
	the same everywhere. Mostly due to handling the binary tree structure
	and the channel communication

	For now, it's just a symlink to the 'master' version in
	./toolkit/nocui/common.go
*/

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

func (me *TreeInfo) SendEnableDebugger() {
	if me.callback == nil {
		log.Log(TREEWARN, "SendEnableDebugger() callback == nil")
		return
	}
	var a widget.Action
	a.ActionType = widget.EnableDebug
	a.ProgName = me.PluginName
	me.callback <- a
	return
}

func (me *TreeInfo) SendToolkitLoad(s string) {
	if me.callback == nil {
		log.Log(TREEWARN, "SendToolkitLoad() callback == nil")
		return
	}
	log.Log(TREEWARN, "SendToolkitLoad() START: toolkit load", s)
	var a widget.Action
	a.ActionType = widget.ToolkitLoad
	a.Value = s
	a.ProgName = me.PluginName
	me.callback <- a
	log.Log(TREEWARN, "SendToolkitLoad() END:   toolkit load", s)
	return
}

func (me *TreeInfo) SendToolkitPanic() {
	if me.callback == nil {
		log.Log(TREEWARN, "SendToolkitPanic() callback == nil")
		return
	}
	log.Log(TREEWARN, "SendToolkitPanic() START")
	var a widget.Action
	a.ActionType = widget.ToolkitPanic
	a.ProgName = me.PluginName
	me.callback <- a
	log.Log(TREEWARN, "SendToolkitPanic() END")
	return
}

func (me *TreeInfo) SendWindowCloseEvent(n *Node) {
	if me.callback == nil {
		log.Log(TREEWARN, "SendWindowClose() callback == nil", n.WidgetId)
		return
	}
	log.Log(TREE, "SendWindowClose() START: user closed the window", n.GetProgName())
	var a widget.Action
	a.WidgetId = n.WidgetId
	a.ActionType = widget.CloseWindow
	a.ProgName = me.PluginName
	me.callback <- a
	log.Log(TREE, "SendWindowClose() END:   user closed the window", n.GetProgName())
	return
}

// this name is better, but I can't use it
// is multiple toolkit support worth this sacrifice?
// func (n *Node) SendWidgetEvent() {
// }

// The user clicked on something. Or typed something
func (me *TreeInfo) SendWidgetEvent(n *Node) {
	me.SendUserEvent(n)
}

func (me *TreeInfo) SendFromUser(n *Node) {
	me.SendUserEvent(n)
}

// Other goroutines must use this to access the GUI
func (me *TreeInfo) SendUserEvent(n *Node) {
	if me.callback == nil {
		log.Log(TREEWARN, "SendUserEvent() callback == nil", n.WidgetId)
		return
	}
	log.Log(TREE, "SendUserEvent() START: send a user event to the callback channel")
	var a widget.Action
	a.WidgetId = n.WidgetId
	a.State = n.State
	a.ActionType = widget.User
	a.ProgName = me.PluginName
	me.callback <- a
	log.Log(TREE, "SendUserEvent() END:   sent a user event to the callback channel")
	return
}