package main

import "log"
import "time"
import "fmt"

import "github.com/gookit/config"

import "github.com/andlabs/ui"
import _ "github.com/andlabs/ui/winmanifest"

import "git.wit.com/wit/gui"

// import "github.com/davecgh/go-spew/spew"

var jwcmainwin *ui.Window
var jwcmaintab *ui.Tab
var tabcount int

func makeCloudInfoBox() ui.Control {
	hbox := ui.NewHorizontalBox()
	hbox.SetPadded(true)

	vbox := ui.NewVerticalBox()
	vbox.SetPadded(true)
	hbox.Append(vbox, false)

	vbox.Append(ui.NewDatePicker(), false)
	vbox.Append(ui.NewTimePicker(), false)
	vbox.Append(ui.NewDateTimePicker(), false)
	vbox.Append(ui.NewFontButton(), false)
	vbox.Append(ui.NewColorButton(), false)

	mybutton := ui.NewButton("Test List VMs")
	mybutton.OnClicked(func(*ui.Button) {
		log.Println("send over socket")
	})
	vbox.Append(mybutton, false)
	vbox.Append(ui.NewLabel("Hostname:"), false)

	// ATTEMPT TO ADD THE TABLE HERE
	add2button := ui.NewButton("Add a Test Table")
	add2button.OnClicked(func(*ui.Button) {
		log.Println("send over socket")
		add2()
	})
	vbox.Append(add2button, false)
	// ATTEMPT TO ADD THE TABLE HERE END

	hbox.Append(ui.NewVerticalSeparator(), false)

	vbox = ui.NewVerticalBox()
	vbox.SetPadded(true)
	hbox.Append(vbox, true)

	grid := ui.NewGrid()
	grid.SetPadded(true)
	vbox.Append(grid, false)

	button := ui.NewButton("Open File")
	entry := ui.NewEntry()
	entry.SetReadOnly(true)
	button.OnClicked(func(*ui.Button) {
		/*
		filename := ui.OpenFile(mainwin)
		if filename == "" {
			filename = "(cancelled)"
		}
		*/
		entry.SetText("FIXME")
	})
	grid.Append(button,
		0, 0, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	grid.Append(entry,
		1, 0, 1, 1,
		true, ui.AlignFill, false, ui.AlignFill)

	button = ui.NewButton("Save File")
	entry2 := ui.NewEntry()
	entry2.SetReadOnly(true)
	button.OnClicked(func(*ui.Button) {
		/*
		filename := ui.SaveFile(mainwin)
		if filename == "" {
			filename = "(cancelled)"
		}
		entry2.SetText(filename)
		*/
		entry.SetText("FIXME")
	})
	grid.Append(button,
		0, 1, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	grid.Append(entry2,
		1, 1, 1, 1,
		true, ui.AlignFill, false, ui.AlignFill)

	msggrid := ui.NewGrid()
	msggrid.SetPadded(true)
	grid.Append(msggrid,
		0, 2, 2, 1,
		false, ui.AlignCenter, false, ui.AlignStart)

	button = ui.NewButton("Message Box")
	button.OnClicked(func(*ui.Button) {
		log.Println("FIXEME")
		/*
		ui.MsgBox(mainwin,
			"This is a normal message box.",
			"More detailed information can be shown here.")
		*/
	})
	msggrid.Append(button,
		0, 0, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	button = ui.NewButton("Error Box")
	button.OnClicked(func(*ui.Button) {
		log.Println("FIXEME")
		/*
		ui.MsgBoxError(mainwin,
			"This message box describes an error.",
			"More detailed information can be shown here.")
		*/
	})
	msggrid.Append(button,
		1, 0, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)

	vbox.Append(ui.NewLabel("Hostname:"), false)
	vbox.Append(ui.NewLabel("librem15.lab.wit.com"), false)


	entryForm := ui.NewForm()
	entryForm.SetPadded(true)
	vbox.Append(entryForm, false)

	hostnameEntry :=  ui.NewEntry()
	entryForm.Append("hostname:", hostnameEntry, false)
	hostnameEntry.SetText("librem15.lab.wit.com")

	IPv6entry :=  ui.NewEntry()
	entryForm.Append("IPv6:", IPv6entry, false)
	IPv6entry.SetText("2604:bbc0:3:3:0:10:0:1003")

	return hbox
}

func setupCloudUI() {
	jwcmainwin = ui.NewWindow("Cloud Control Panel", config.Int("width"), config.Int("height"), false)
	jwcmainwin.OnClosing(func(*ui.Window) bool {
		ui.Quit()
		return true
	})
	ui.OnShouldQuit(func() bool {
		jwcmainwin.Destroy()
		return true
	})

	jwcmaintab = ui.NewTab()
	jwcmainwin.SetChild(jwcmaintab)
	jwcmainwin.SetMargined(true)

	tabcount = 0
	jwcmaintab.Append("Cloud Info", makeCloudInfoBox())
	jwcmaintab.SetMargined(tabcount, true)


	jwcmainwin.Show()
}

func add2() {
	var parts []gui.InputData

	for key, foo := range []string{"BG", "TEXTCOLOR", "BUTTON", "TEXTCOLOR", "TEXTCOLOR", "TEXT", "BUTTON", "TEXT", "BUTTON"} {
		log.Println(key, foo)

		var b gui.InputData
		b.CellType = foo
		b.Heading  = fmt.Sprintf("heading%d", key)
		parts = append(parts, b)
	}

	log.Println("Sleep for 2 seconds, then try to add new tabs")
	time.Sleep(1 * 1000 * 1000 * 1000)
	gui.AddTableTab(jwcmaintab, 1, "test seven", 7, parts)
}