package main

import (
	// "os"
	"go.wit.com/dev/andlabs/ui"
	_ "go.wit.com/dev/andlabs/ui/winmanifest"

	"go.wit.com/lib/widget"
	"go.wit.com/log"

	"go.wit.com/toolkits/tree"
)

// This routine is very specific to this toolkit
// It's annoying and has to be copied to each widget when there are changes
// it could be 'simplfied' maybe or made to be more generic, but this is as far as I've gotten
// it's probably not worth working much more on this toolkit, the andlabs/ui has been great and got me here!
// but it's time to write direct GTK, QT, macos and windows toolkit plugins
// -- jcarr 2023/03/09

// Grid numbering examples by (X,Y)
// ---------
// -- (1) --
// -- (2) --
// ---------
//
// -----------------------------
// -- (1,1) -- (1,2) -- (1,3) --
// -- (2,1) -- (2,2) -- (2,3) --
// -----------------------------

// internally for andlabs/ui
// (x&y flipped and start at zero)
// -----------------------------
// -- (0,0) -- (1,0) -- (1,0) --
// -- (0,1) -- (1,1) -- (1,1) --
// -----------------------------

func place(p *tree.Node, n *tree.Node) bool {
	log.Warn("SPEEDY newplace() 1 START", n.WidgetId, n.GetProgName(), n.GetLabel(), n.String())
	log.Warn("SPEEDY newplace() n.State.Strings =", n.State.Strings)
	log.Log(INFO, "place() 1 START", n.WidgetType, n.GetProgName(), n.GetLabel())
	if !ready(n) {
		log.Warn("place() 1 START not ready()")
		return false
	}
	log.Log(INFO, "place() 1 START ready()")
	var tk, ptk *guiWidget
	tk = n.TK.(*guiWidget)
	ptk = p.TK.(*guiWidget)
	log.Warn("SPEEDY newplace() 2 START", n.WidgetId, n.GetProgName(), n.GetLabel())

	if ptk == nil {
		log.Log(ERROR, "ptk == nil", p.GetProgName(), p.ParentId, p.WidgetType, ptk)
		log.Log(ERROR, "n = ", n.GetProgName(), n.ParentId, n.WidgetType, tk)
		log.Warn("SPEEDY ptk == nil", n.WidgetId, n.GetProgName())
		log.Sleep(1)
		panic("ptk == nil")
	}

	log.Log(INFO, "place() switch", p.WidgetType)
	log.Warn("SPEEDY newplace() before switch", n.WidgetId, n.GetProgName())
	switch p.WidgetType {
	case widget.Grid:
		tk.gridX = n.State.GridOffset.X - 1
		tk.gridY = n.State.GridOffset.Y - 1
		log.Warn("place() on Grid at gridX,gridY", tk.gridX, tk.gridY)
		ptk.uiGrid.Append(tk.uiControl,
			tk.gridX, tk.gridY, 1, 1,
			false, ui.AlignFill, false, ui.AlignFill)
		return true
	case widget.Group:
		if ptk.uiBox == nil {
			log.Log(WARN, "place() andlabs hack group to use add a box", n.GetProgName(), n.WidgetType)
			ptk.uiBox = rawBox(n)
			ptk.uiGroup.SetChild(ptk.uiBox)
		}
		ptk.uiBox.Append(tk.uiControl, stretchy)
		return true
	case widget.Tab:
		if ptk.uiTab == nil {
			log.Log(ERROR, "ptk.uiTab == nil for n.WidgetId =", n.WidgetId, "ptk =", ptk)
			panic("ptk.uiTab == nil")
		}
		if tk.uiControl == nil {
			log.Log(ERROR, "tk.uiControl == nil for n.WidgetId =", n.WidgetId, "tk =", tk)
			panic("tk.uiControl == nil")
		}
		log.Log(ERROR, "CHECK LOGIC ON THIS. APPENDING directly into a window without a tab")
		// log.Log(ERROR, "THIS SHOULD NEVER HAPPEN ??????? trying to place() node=", n.WidgetId, n.GetProgName(), n.Text, n.WidgetType)
		// log.Log(ERROR, "THIS SHOULD NEVER HAPPEN ??????? trying to place() on parent=", p.WidgetId, p.GetProgName(), p.Text, p.WidgetType)
		// panic("tk.uiControl == nil")
		ptk.uiTab.Append(widget.GetString(n.State.Value), tk.uiControl)
		ptk.boxC += 1
		return true
	case widget.Box:
		log.Warn("SPEEDY Add Something to Box", n.WidgetId, n.GetProgName())
		log.Log(INFO, "place() uiBox =", ptk.uiBox)
		log.Log(INFO, "place() uiControl =", tk.uiControl)
		ptk.uiBox.Append(tk.uiControl, stretchy)
		ptk.boxC += 1
		return true
	case widget.Window:
		log.Warn("SPEEDY Add Something to Window", n.WidgetId, n.GetProgName())
		ptk.uiWindow.SetChild(tk.uiControl)
		return true
	default:
		log.Log(ERROR, "place() how? Parent =", p.WidgetId, p.WidgetType)
	}
	log.Warn("SPEEDY newplace() return", n.WidgetId, n.GetProgName())
	return false
}
