package main

import (
	// if you include more than just this import
	// then your plugin might be doing something un-ideal (just a guess from 2023/02/27)
	"go.wit.com/log"
	"go.wit.com/widget"
)

func action(a widget.Action) {
	log.Log(INFO, "action() START", a.WidgetId, a.ActionType, a.WidgetType, a.ProgName)
	// n := me.rootNode.findWidgetId(a.WidgetId)
	n := me.treeRoot.FindWidgetId(a.WidgetId)
	var w *guiWidget
	if n != nil {
		w = n.TK.(*guiWidget)
	}
	switch a.ActionType {
	case widget.Add:
		if w == nil {
			n := me.myTree.AddNode(&a)
			if n == nil {
				log.Warn("WTF")
				panic("WTF")
			}
			n.TK = initWidget(n)
			if n.WidgetType == widget.Root {
				me.treeRoot = n
			}
			addWidget(n)
		} else {
			// this is done to protect the plugin being 'refreshed' with the
			// widget binary tree. TODO: find a way to keep them in sync
			log.Log(ERROR, "action() Add ignored for already defined widget",
				a.WidgetId, a.ActionType, a.WidgetType, a.ProgName)
		}
	case widget.Show:
		if widget.GetBool(a.Value) {
			w.showView()
		} else {
			w.hideWidgets()
		}
	case widget.Set:
		if a.WidgetType == widget.Flag {
			log.Log(NOW, "TODO: set flag here", a.ActionType, a.WidgetType, a.ProgName)
			log.Log(NOW, "TODO: n.WidgetType =", n.WidgetType, "n.String() =", a.ProgName)
		} else {
			if a.Value == nil {
				log.Log(ERROR, "TODO: Set here. a == nil id =", a.WidgetId, "type =", a.WidgetType, "Name =", a.ProgName)
				log.Log(ERROR, "TODO: Set here. id =", a.WidgetId, "n.String() =", n.String())
			} else {
				w.Set(a.Value)
			}
		}
	case widget.SetText:
		w.SetText(widget.GetString(a.Value))
	case widget.AddText:
		w.AddText(widget.GetString(a.Value))
	case widget.Move:
		log.Log(NOW, "attempt to move() =", a.ActionType, a.WidgetType, a.ProgName)
	case widget.ToolkitClose:
		log.Log(NOW, "attempting to close the plugin and release stdout and stderr")
		standardExit()
	case widget.Enable:
		if w.Visible() {
			// widget was already shown
		} else {
			log.Log(INFO, "Setting Visible to true", a.ProgName)
			w.SetVisible(true)
		}
	case widget.Disable:
		if w.Visible() {
			log.Log(INFO, "Setting Visible to false", a.ProgName)
			w.SetVisible(false)
		} else {
			// widget was already hidden
		}
	default:
		log.Log(ERROR, "action() ActionType =", a.ActionType, "WidgetType =", a.WidgetType, "Name =", a.ProgName)
	}
	log.Log(INFO, "action() END")
}

func (w *guiWidget) AddText(text string) {
	if w == nil {
		log.Log(NOW, "widget is nil")
		return
	}
	w.vals = append(w.vals, text)
	for i, s := range w.vals {
		log.Log(NOW, "AddText()", w.String(), i, s)
	}
	w.SetText(text)
}

func (w *guiWidget) SetText(text string) {
	var changed bool = false
	if w == nil {
		log.Log(NOW, "widget is nil")
		return
	}
	if w.labelN != text {
		w.labelN = text
		changed = true
	}
	if !changed {
		return
	}

	if w.Visible() {
		w.textResize()
		w.deleteView()
		w.showView()
	}
}

func (w *guiWidget) Set(val any) {
	log.Log(INFO, "Set() value =", val)

	w.value = val.(string)
	if w.node.WidgetType != widget.Checkbox {
		w.setCheckbox(val)
	}
}