package main

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

var muAction sync.Mutex

func catchActionChannel() {
	log.Log(NOW, "catchActionChannel() START")
	for {
		log.Log(NOW, "catchActionChannel() for loop")
	    	select {
		case a := <-pluginChan:
			log.Log(NOW, "catchActionChannel() SELECT widget id =", a.WidgetId, a.ProgName)
			log.Log(NOW, "catchActionChannel() STUFF", a.WidgetId, a.ActionType, a.WidgetType)
			muAction.Lock()
			doAction(&a)
			muAction.Unlock()
			log.Log(NOW, "catchActionChannel() STUFF END", a.WidgetId, a.ActionType, a.WidgetType)
		}
	}
}

/*
// Other goroutines must use this to access the GUI
//
// You can not acess / process the GUI thread directly from
// other goroutines. This is due to the nature of how
// Linux, MacOS and Windows work (they all work differently. suprise. surprise.)
//
// this sets the channel to send user events back from the plugin
func Callback(guiCallback chan widget.Action) {
	callback = guiCallback
}

func PluginChannel() chan widget.Action {
	return pluginChan
}
*/

// This is important. This sets the defaults for the gui. Without this, there isn't correct padding, etc
func init() {
	log.Log(INFO, "Init()")

	// andlabs = make(map[int]*andlabsT)
	pluginChan = make(chan widget.Action, 1)

	log.Log(NOW, "Init() start channel reciever")
	go catchActionChannel()
	go simpleStdin()
	log.Log(NOW, "Init() END")
}
