package main

import (
	"bufio"
	"strings"

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

func splitLines(s string) []string {
	var lines []string
	sc := bufio.NewScanner(strings.NewReader(s))
	for sc.Scan() {
		lines = append(lines, sc.Text())
	}
	return lines
}

func (w *guiWidget) textResize() bool {
	// w := n.tk
	var width, height int = 0, 0
	var changed bool = false

	for i, s := range splitLines(w.labelN) {
		log.Log(INFO, "textResize() len =", len(s), i, s)
		if width < len(s) {
			width = len(s)
		}
		height += 1
	}
	if w.gocuiSize.w1 != w.gocuiSize.w0+width+me.FramePadW {
		w.gocuiSize.w1 = w.gocuiSize.w0 + width + me.FramePadW
		changed = true
	}
	if w.gocuiSize.h1 != w.gocuiSize.h0+height+me.FramePadH {
		w.gocuiSize.h1 = w.gocuiSize.h0 + height + me.FramePadH
		changed = true
	}
	if changed {
		// w.showWidgetPlacement("textResize() changed")
	}
	return changed
}

func (w *guiWidget) hideWidgets() {
	if w == nil {
		return
	}
	w.isCurrent = false
	switch w.node.WidgetType {
	case widget.Root:
	case widget.Flag:
	case widget.Window:
	case widget.Box:
	case widget.Grid:
	default:
		w.Hide()
	}
	for _, child := range w.children {
		child.hideWidgets()
	}
}

func hideFake() {
	var w *guiWidget
	w = me.treeRoot.TK.(*guiWidget)
	w.hideFake()
}

func showFake() {
	var w *guiWidget
	w = me.treeRoot.TK.(*guiWidget)
	w.showFake()
}

func (w *guiWidget) hideFake() {
	if w.isFake {
		w.Hide()
	}
	for _, child := range w.children {
		child.hideFake()
	}
}

// shows the 'fake' widgets for widgets that
// are not normally displayed (like a grid widget)
func (w *guiWidget) showFake() {
	if w.isFake {
		w.drawView()
		w.showWidgetPlacement("showFake:")
	}
	for _, child := range w.children {
		child.showFake()
	}
}

func (w *guiWidget) showWidgets() {
	w.Show()

	for _, child := range w.children {
		child.showWidgets()
	}
}