// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"errors"
	"fmt"
	"slices"
	"strings"

	"github.com/awesome-gocui/gocui"

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

func createStdout(g *gocui.Gui) bool {
	if me.stdout.tk == nil {
		makeOutputWidget(g, "this is a create before a mouse click")
		// me.logStdout.v.Write([]byte(msg))

		// this will show very early debugging output
		// keep this code commented out but do not remove it. when it doubt, this will be the Light of Elendil
		// NEVER REMOVE THIS CODE

		msg := fmt.Sprintf("test out gocuiEvent() %d\n", me.ecount)
		me.stdout.tk.Write([]byte(msg))

		log.Log(NOW, "logStdout test out")
	}
	return true
}

func coreStdout() {
	if me.stdout.tk != nil {
		return
	}
	a := new(widget.Action)
	a.ProgName = "2stdout2"
	a.WidgetType = widget.Stdout
	a.WidgetId = me.stdout.wId
	a.ParentId = 0
	// n := addNode(a)
	n := me.myTree.AddNode(a)
	me.stdout.tk = initWidget(n)

	tk := me.stdout.tk
	tk.gocuiSize.w0 = me.stdout.lastW
	tk.gocuiSize.h0 = me.stdout.lastH
	tk.gocuiSize.w1 = tk.gocuiSize.w0 + me.stdout.w
	tk.gocuiSize.h1 = tk.gocuiSize.h0 + me.stdout.h
}

func makeOutputWidget(g *gocui.Gui, stringFromMouseClick string) *gocui.View {
	if me.treeRoot == nil {
		// keep skipping this until the binary tree is initialized
		return nil
	}

	coreStdout()

	if me.stdout.tk == nil {
		return nil
	}

	v, err := g.View("msg")
	if v == nil {
		log.Log(NOW, "makeoutputwindow() this is supposed to happen. v == nil", err)
	} else {
		log.Log(NOW, "makeoutputwindow() msg != nil. WTF now? err =", err)
		return v
	}

	rect := me.stdout.tk.gocuiSize
	v, err = g.SetView("msg", rect.w0, rect.h0, rect.w1, rect.h1, 0)

	if errors.Is(err, gocui.ErrUnknownView) {
		log.Log(NOW, "makeoutputwindow() this is supposed to happen?", err)
	}

	if err != nil {
		log.Log(NOW, "makeoutputwindow() create output window failed", err)
		return nil
	}

	if v == nil {
		log.Log(NOW, "makeoutputwindow() msg == nil. WTF now? err =", err)
		return nil
	} else {
		me.stdout.tk.v = v
	}

	v.Clear()
	v.SelBgColor = gocui.ColorCyan
	v.SelFgColor = gocui.ColorBlack
	fmt.Fprintln(v, "figure out how to capture STDOUT to here\n"+stringFromMouseClick)
	// g.SetViewOnBottom("msg")
	// setBottomBG()

	me.stdout.tk.v = v
	me.stdout.tk.DrawAt(me.stdout.lastW, me.stdout.lastH)
	relocateStdoutOffscreen()
	/*
		if me.stdout.outputOffscreen {
			me.stdout.tk.relocateStdout(me.stdout.lastW, me.stdout.lastH)
		} else {
			relocateStdoutOffscreen()
		}
	*/
	return v
}

func relocateStdoutOffscreen() {
	if me.stdout.tk == nil {
		return
	}
	log.CaptureMode(me.stdout.tk)
	// log.Log(ERROR, "setting log.CaptureMode(tk.v) in relocateStdoutOffscreen()")
	newW := 10
	newH := 0 - me.stdout.h - 4
	me.stdout.tk.relocateStdout(newW, newH)
}

func (tk *guiWidget) relocateStdout(w int, h int) {
	w0 := w
	h0 := h
	w1 := w + me.stdout.w
	h1 := h + me.stdout.h

	tk.gocuiSize.w0 = w0
	tk.gocuiSize.w1 = w1
	tk.gocuiSize.h0 = h0
	tk.gocuiSize.h1 = h1

	tk.full.w0 = w0
	tk.full.w1 = w1
	tk.full.h0 = h0
	tk.full.h1 = h1

	me.baseGui.SetView("msg", w0, h0, w1, h1, 0)
	// me.baseGui.SetViewOnBottom("msg")
}

// from the gocui devs:
// Write appends a byte slice into the view's internal buffer. Because
// View implements the io.Writer interface, it can be passed as parameter
// of functions like fmt.Fprintf, fmt.Fprintln, io.Copy, etc. Clear must
// be called to clear the view's buffer.

func (w stdout) Height() int {
	if w.tk == nil {
		return 40
	}
	return w.tk.gocuiSize.Height() - 2
}

func (w stdout) Write(p []byte) (n int, err error) {
	me.writeMutex.Lock()
	defer me.writeMutex.Unlock()

	lines := strings.Split(strings.TrimSpace(string(p)), "\n")
	me.stdout.outputS = append(me.stdout.outputS, lines...)
	if me.outf != nil {
		fmt.Fprint(me.outf, string(p))
	}

	return len(p), nil
}

func (w *guiWidget) Write(p []byte) (n int, err error) {
	lines := strings.Split(strings.TrimSpace(string(p)), "\n")
	if me.outf != nil {
		fmt.Fprint(me.outf, string(p))
	}

	if w == nil {
		me.stdout.outputS = append(me.stdout.outputS, lines...)
		return len(p), nil
	}
	w.tainted = true
	me.writeMutex.Lock()
	defer me.writeMutex.Unlock()

	me.stdout.outputS = append(me.stdout.outputS, lines...)

	tk := me.stdout.tk
	if tk == nil {
		return len(p), nil
	}
	if tk.v == nil {
		// redo this old code
		v, _ := me.baseGui.View("msg")
		if v != nil {
			tk.v = v
		}
		return len(p), nil
	}
	tk.refreshStdout()
	return len(p), nil
}

// lets the user page up and down through the stdout buffer
func (tk *guiWidget) refreshStdout() {
	if len(me.stdout.outputS) < me.stdout.h+me.stdout.pager {
		// log.Info(fmt.Sprintf("buffer too small=%d len(%d)", me.stdout.pager, len(me.stdout.outputS)))
		var cur []string
		cur = append(cur, me.stdout.outputS...)
		slices.Reverse(cur)
		tk.v.Clear()
		fmt.Fprintln(tk.v, strings.Join(cur, "\n"))
		return
	}

	var cur []string
	// chop off the last lines in the buffer
	chop := len(me.stdout.outputS) - (me.stdout.pager + me.stdout.h)
	cur = append(cur, me.stdout.outputS[chop:chop+me.stdout.h]...)
	slices.Reverse(cur)
	tk.v.Clear()
	fmt.Fprintln(tk.v, strings.Join(cur, "\n"))
}