package main

import "log"
import "fmt"

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

func makeCloudInfoBox(gw *gui.GuiWindow) {
	log.Println("makeCloudInfoBox() START gw =", gw)

	gw.UiTab.Delete(0) // does this make things more or less stable or neither?
	gw.Name = "Cloud Accounts"
	gw = gui.InitGuiWindow(gw)
	// TODO: make this text i18n
	box := gui.HardBox(gw, gui.Yaxis, "Cloud Accounts")

	log.Println("makeCloudInfoBox() START GW IS NOW: gw =", gw)
	log.Println("makeCloudInfoBox() box =", box)

	gui.NewLabel(box, "Hostname:")

	tmp := config.Hostname + " (" + config.IPv6 + ")"
	gui.MakeEntryHbox(box, "hostname:", tmp, true, "Hostname")

	// box = gui.HardBox(gw, gui.Yaxis)
	for key, a := range config.Accounts {
		vbox := gui.NewBox(box, gui.Xaxis, fmt.Sprintf("ACCOUNT %d", key))
		log.Println("account =          ", key, a)
		log.Println("Accounts[key] =    ", config.Accounts[key])
		log.Println("account.Nick =     ", config.Accounts[key].Nick)
		log.Println("account.Username = ", config.Accounts[key].Username)
		log.Println("account.Token =    ", config.Accounts[key].Token)

		// name := "Login" // + config.Accounts[key].Nick
		// makeButton(vbox, config.Accounts[key], nil, name, "LOGIN", login)

		// name  = "Show " + config.Accounts[key].Nick
		// name  = "Show"
		// makeButton(vbox, config.Accounts[key], nil, name, "SHOW", showAccountClick)

		// name  = "Configure"
		makeButton(vbox, config.Accounts[key], nil, "Configure", "CONFIG", showAccountClick)

		name := config.Accounts[key].Nick + " (" + config.Accounts[key].Domain + ")"
		gui.NewLabel(vbox, name)
	}

	Xbox := gui.HardBox(gw, gui.Xaxis, "subdomain test")
	makeButton(Xbox, nil, nil, "Add Account", "ADD TAB", func (*gui.GuiButton) {
		log.Println("makeCloudInfoBox() Add Account")
		createAccount(gw)
	})
	makeButton(Xbox, nil, nil, "Quit", "QUIT", nil)
	makeButton(Xbox, nil, nil, "Debug", "DEBUG", debugClick)
	log.Println("makeCloudInfoBox() END")
}

// func createAccount(gw *gui.GuiWindow) {
func initWindow(gw *gui.GuiWindow, name string, axis int) *gui.GuiBox {
	window := gui.Data.WindowMap[name]
	if (window != nil) {
		box := window.BoxMap["MAINBOX"]
		log.Println("initWindow() tab already exists name =", name)
		gui.ErrorWindow(box.Window, "Create Window Error", "Window " + name + " already exists")
		return nil
	}

	// if there is not an account, then go to 'make account'
	gw.Name = name
	newWindow := gui.InitGuiWindow(gw)

	var box *gui.GuiBox
	if (axis == gui.Xaxis) {
		box = gui.HardBox(newWindow, gui.Xaxis, name)
	} else {
		box = gui.HardBox(newWindow, gui.Yaxis, name)
	}
	return box
}

func createAccount(gw *gui.GuiWindow) {
	box := initWindow(gw, "Create New Account", gui.Yaxis)
	if (box == nil) { return }

	gw = box.Window

	// make a place for someone to enter their subdomain
	hardXbox := gui.HardBox(gw, gui.Xaxis, "subdomain test")
	makeButton(hardXbox, nil, nil, "Generate a Subdomain", "SUBDOMAIN", generateSubdomain)
	gui.NewLabel(hardXbox, "")

	gui.AddEntry(box, "SUBDOMAIN")
	// gui.HorizontalBreak(box)

	hardXbox1 := gui.HardBox(gw, gui.Xaxis, "subdomain test")
	makeButton(hardXbox1, nil, nil, "Create Account", "ADD", addSubdomainClick)
	gui.NewLabel(hardXbox, "")

	log.Println("splashClick() END box =", box)
}

func addSubdomainClick(b *gui.GuiButton) {
	log.Println("addSubdomainClick() START")
	subdomain := gui.GetText(b.Box, "SUBDOMAIN")
	if (subdomain == "") {
		gui.ErrorWindow(b.Box.Window, "Blank Name", "You must have a valid subdomain")
		return
	}
	log.Println("\tsubdomain =", subdomain)

	acc := new(pb.Account)
	acc.Nick	= subdomain
	acc.Domain	= subdomain
	acc.Username	= "jcarr@wit.com"
	acc.Email	= "jcarr@wit.com"
	acc.Password	= "badpass"
	acc.URL		= "http://stackapi-api1.stackapi.customers.wprod.wit.com:4000/"
	config.Accounts = append(config.Accounts, acc)

	makeCloudInfoBox(b.Box.Window)
	State = "done"
	log.Println("addSubdomainClick() END")
}

func generateSubdomain(b *gui.GuiButton) {
	log.Println("generateSubdomain START")
	if values, ok := b.Values.(*myButtonInfo); ! ok {
		log.Println("\tvalues.Accounts error")
	} else {
		log.Println("\tvalues.Accounts =", values.Accounts)
		log.Println("\tvalues.Name =", values.Name)
	}
	// use this way?
	// values, _ := b.Values.(*myButtonInfo)

	if (b == nil) {
		log.Println("generateSubdomain ERROR b == nil")
		return
	}
	// subdomain.SetText("cust00013.wit.dev")

	txt := gui.SetText(b.Box, "SUBDOMAIN", "yourname.customers.wprod.wit.com")
	log.Println("generateSubdomain subdomain = ", txt)
	log.Println("generateSubdomain END")
}
