// This is a simple example
package main

import 	(
	"os/user"
	"embed"

	"go.wit.com/log"

	"go.wit.com/gui/gui"
	"go.wit.com/gui/gadgets"
	"go.wit.com/gui/lib/repostatus"
)

//go:embed resources/*
var resToolkit embed.FS

func main() {
	myGui = gui.New()
	myGui.InitEmbed(resToolkit)
	myGui.Default()

	autotypistWindow()
	repoworld()
	log.Sleep(.3)
	hidePerfect()
	gui.Watchdog()
}

func addRepo(grid *gui.Node, path string, master string, devel string, user string) {
	newRepo := new(repo)

	if repostatus.VerifyLocalGoRepo(path) {
		log.Warn("newRepo actually exists", newRepo.getPath())
	} else {
		log.Warn("newRepo does not exist", newRepo.getPath())
		return
	}

	newRepo.path = path
	newRepo.pLabel = grid.NewLabel(path).SetProgName("path")
	newRepo.lastTag = grid.NewLabel("").SetProgName("lastTag")
	newRepo.vLabel = grid.NewLabel("").SetProgName("current")

	newRepo.masterName = grid.NewLabel("").SetProgName("masterName")
	newRepo.masterVersion = grid.NewLabel("").SetProgName("masterVersion")
	newRepo.develName = grid.NewLabel("").SetProgName("develName")
	newRepo.develVersion = grid.NewLabel("").SetProgName("develVersion")
	newRepo.userName = grid.NewLabel("").SetProgName("userName")
	newRepo.userVersion = grid.NewLabel("").SetProgName("userVersion")
	newRepo.dirtyLabel = grid.NewLabel("")

	newRepo.showButton = grid.NewButton("Show()", func () {
		if newRepo.status == nil {
			log.Warn("status window doesn't exist")
			return
		}
		log.Warn("status window exists. trying TestDraw() here")
		newRepo.status.Show()
		newRepo.status.Update()
	})
	if path == "" {
		newRepo.cButton.Hide()
		newRepo.pButton.Hide()
	}
	newRepo.status = repostatus.New(myGui, newRepo.path)
	newRepo.status.SetMasterName(master)
	newRepo.status.SetDevelName(devel)
	newRepo.status.SetUserName(user)
	newRepo.hidden = false
	newRepo.status.Update()
	newRepo.newScan()
	allrepos = append(allrepos, newRepo)
}

func autotypistWindow() {
	me.autotypistWindow = gadgets.NewBasicWindow(myGui, "autotypist for GO & git. it types faster than you can.")
	me.autotypistWindow.Make()
	me.autotypistWindow.StandardExit()

	box := me.autotypistWindow.Box()
	globalDisplayOptions(box)
	globalBuildOptions(box)
	globalResetOptions(box)
	me.autotypistWindow.Draw()

}

// This creates a window
func repoworld() {
	reposwin = gadgets.NewBasicWindow(myGui, "All git repositories in ~/go/src/")
	reposwin.Make()
	// reposwin.StandardExit()

	reposbox = reposwin.Box().NewBox("bw vbox", false)
	reposgroup = reposbox.NewGroup("go repositories (read from ~/.config/myrepolist)")
	reposgrid = reposgroup.NewGrid("test", 11, 1)

	reposgrid.NewLabel("")
	reposgrid.NewLabel("last tag").SetProgName("last tag")
	reposgrid.NewLabel("Current Version").SetProgName("Current Version")
	reposgrid.NewLabel("master")
	reposgrid.NewLabel("version")
	reposgrid.NewLabel("devel")
	reposgrid.NewLabel("version")
	reposgrid.NewLabel("user")
	reposgrid.NewLabel("version")
	reposgrid.NewLabel("Status")
	reposgrid.NewLabel("Show()")

	repos := myrepolist()
	for _, line := range repos {
		log.Warn("repo =", line)
		path, mbranch, dbranch, ubranch := splitLine(line)
		if mbranch == "" { mbranch = "master" }
		if dbranch == "" { dbranch = "devel" }
		usr, _ := user.Current()
		if ubranch == "" { ubranch = usr.Username }
		addRepo(reposgrid, path, mbranch, dbranch, ubranch)
	}
}
