package main

import "log"
import "fmt"

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

//
// THIS IS THE STANDARD VM DISPLAY TABLE
// This maps the 'human' indexed cells in the table
// to the machine's andlabs/libui values. That way
// if you want to work against column 4, then you
// can just reference 4 instead of the internal number
// which could be anything since TEXTCOLOR, TEXT, BG, etc
// fields use between 1 and 3 values internally
//
func addVmsTab(gw *gui.GuiWindow, name string, count int, a *pb.Account) *gui.TableData {
	var parts []gui.TableColumnData

	human := 0

	tmp := gui.TableColumnData{}
	tmp.CellType = "BG"
	tmp.Heading  = "background"
	tmp.Index    = human
	parts = append(parts, tmp)
	human += 1

	tmp = gui.TableColumnData{}
	tmp.CellType = "TEXTCOLOR"
	tmp.Heading  = "name"
	tmp.Index    = human
	parts = append(parts, tmp)
	human += 1

	tmp = gui.TableColumnData{}
	tmp.CellType = "TEXTCOLOR"
	tmp.Heading  = "hostname"
	tmp.Index    = human
	parts = append(parts, tmp)
	human += 1

	tmp = gui.TableColumnData{}
	tmp.CellType = "TEXTCOLOR"
	tmp.Heading  = "IPv6"
	tmp.Index    = human
	parts = append(parts, tmp)
	human += 1

	tmp = gui.TableColumnData{}
	tmp.CellType = "TEXTCOLOR"
	tmp.Heading  = "cpus"
	tmp.Index    = human
	parts = append(parts, tmp)
	human += 1

	tmp = gui.TableColumnData{}
	tmp.CellType = "TEXTCOLOR"
	tmp.Heading  = "memory"
	tmp.Index    = human
	parts = append(parts, tmp)
	human += 1

	tmp = gui.TableColumnData{}
	tmp.CellType = "BUTTON"
	tmp.Heading  = "Details"
	tmp.Index    = human
	parts = append(parts, tmp)
	human += 1

	mh := gui.AddTableTab(gw, name, count, parts)
	makeButton(mh.Box, a, nil, "Add Virtual Machine", "JUNK", createAddVmBox)

	makeButton(mh.Box, a, nil, "Login", "JUNK", login)
	makeButton(mh.Box, a, nil, "Configure", "JUNK", showAccountClick)
	return mh
}

func createAddVmBox(b *gui.GuiButton) {
	log.Println("createAddVmBox() START")
	gw := b.Box.Window
	log.Println("createAddVmBox() gw =", gw)
	values, _ := b.Values.(*myButtonInfo)
	name := "(" + values.Account.Nick + ")"

	// gw.BoxMap["ADD VM" + name] = box

	txt := "ADD VM " + name
	gw.Name = txt
	gw = gui.InitGuiWindow(gw)
	box := gui.HardBox(gw, gui.Yaxis, txt)
	box  = gui.HardBox(gw, gui.Xaxis, txt)
	// box := gui.AddGenericBox(gw, name)

	// Add hostname entry box
	hostname := gui.MakeEntryHbox(box, "Hostname:", "testhost", true, "Hostname")
	memory   := gui.MakeEntryHbox(box, "Memory:",   "512", true, "Memory")
	disk     := gui.MakeEntryHbox(box, "Disk:",     "20", true, "Disk")

	log.Println("createAddVmBox() hostname, memory, disk =", hostname, memory, disk)

	// gui.HorizontalBreak(box)
	box  = gui.HardBox(gw, gui.Xaxis, txt)

	makeButton(box, values.Account, nil, "Add Virtual Machine", "CREATE", createVmClick)
	makeButton(box, values.Account, nil, "Cancel", "CLOSE", nil)
}

func showVmMouseClick(b *gui.GuiButton) {
	log.Println("showVmMouseClick() START b.Box.Window =", b.Box.Window)
	values, _ := b.Values.(*myButtonInfo)
	createVmBox(b.Box.Window, values.VM)
	log.Println("showVmMouseClick() END")
}

func createVmBox(gw *gui.GuiWindow, vm *pb.Event_VM) {
	log.Println("CreateVmBox() START vm.Name =", vm.Name)

	txt := "VM " + vm.Name
	gw.Name = txt
	gw = gui.InitGuiWindow(gw)
	box := gui.HardBox(gw, gui.Yaxis, txt)
	box  = gui.HardBox(gw, gui.Xaxis, txt)

	// Add hostname entry box
	gui.MakeEntryVbox(box, "hostname:",	vm.Hostname,			true, "Hostname")
	gui.MakeEntryVbox(box, "IPv6:",		vm.IPv6,			true, "IPv6")
	gui.MakeEntryVbox(box, "RAM:",		fmt.Sprintf("%d",vm.Memory),	true, "Memory")
	gui.MakeEntryVbox(box, "CPU:",		fmt.Sprintf("%d",vm.Cpus),	true, "Cpus")
	gui.MakeEntryVbox(box, "Disk (GB):",	fmt.Sprintf("%d",vm.Disk),	true, "Disk")
	gui.MakeEntryVbox(box, "OS Image:",	vm.BaseImage,			true, "BaseImage")

	box = gui.HardBox(gw, gui.Xaxis, "VM BUTTONS")

	makeButton(box, nil, vm, "Power On",  "POWERON",  nil)
	makeButton(box, nil, vm, "Power Off", "POWEROFF", nil)
	makeButton(box, nil, vm, "Destroy",   "DESTROY",  nil)
	makeButton(box, nil, vm, "ping",      "PING",     runPingClick)
	makeButton(box, nil, vm, "Console",   "XTERM",    runTestExecClick)
	makeButton(box, nil, vm, "Save",      "SAVE",     nil)
	makeButton(box, nil, vm, "Done",      "DONE",     nil)
}
