package main

import "log"
import "fmt"
import "runtime"
import "os/exec"
import "strings"

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

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

//
// 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)
	gui.CreateButton(mh.Box, a, nil, "Add Virtual Machine", "JUNK", createAddVmBox)
	return mh
}

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

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

	txt := "ADD VM " + name
	gw = gui.InitGuiWindow(config, txt, gw)
	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)

	gui.CreateButton(box, b.Account, nil, "Add Virtual Machine", "CREATE", nil)
	gui.CreateButton(box, b.Account, nil, "Cancel", "CLOSE", nil)
}

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

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

	txt := "createVmBox() " + vm.Name
	gw = gui.InitGuiWindow(config, txt, gw)
	box := gui.AddGenericBox(gw, vm.Name)

	// 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")

	gui.HorizontalBreak(box)

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

func runTestHide(b *gui.GuiButton) {
	/*
	log.Println("runTestHide START")
	Data.Window1.Box1.Hide()
	Data.Window1.Box2.Hide()
	// time.Sleep(2000 * time.Millisecond)
	Data.State = "HIDE"
	log.Println("runTestHide END")
	*/
}

func runPingClick(b *gui.GuiButton) {
	log.Println("runPingClick START")
	log.Println("runTestExecClick b.VM", b.VM)
	hostname := "localhost"
	if (b.VM != nil) {
		hostname = b.VM.Hostname
	}
	spew.Dump(b)
	var tmp []string
	tmp = append(tmp, "xterm", "-geometry", "120x30", "-e", "ping " + hostname + ";sleep 3")
	go runCommand(tmp)
	log.Println("runPingClick END")
}

func runTestExecClick(b *gui.GuiButton) {
	log.Println("runTestExecClick START")
	if runtime.GOOS == "linux" {
		go runSimpleCommand("xterm -report-fonts")
	} else if runtime.GOOS == "windows" {
		go runSimpleCommand("mintty.exe")
	} else {
		go runSimpleCommand("xterm")
	}
	log.Println("runTestExecClick END")
}

func runSimpleCommand(s string) {
	cmd := strings.TrimSpace(s) // this is like 'chomp' in perl
        cmdArgs := strings.Fields(cmd)
	runCommand(cmdArgs)
}

func runCommand(cmdArgs []string) {
	log.Println("runCommand() START", cmdArgs)
	process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
	// process := exec.Command("xterm", "-e", "ping localhost")
	log.Println("runCommand() process.Start()")
	process.Start()
	log.Println("runCommand() process.Wait()")
	process.Wait()
	log.Println("runCommand() NEED TO CHECK THE TIME HERE TO SEE IF THIS WORKED")
	log.Println("runCommand() OTHERWISE INFORM THE USER")
	log.Println("runCommand() END")
}