package main

import "log"
import "runtime"

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

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

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

func showSplashBox(box *gui.GuiBox) *gui.GuiBox {
	log.Println("ShowSplashBox() START")

	// TODO: turn 'Welcome' into a i18n for translations
	text := getNEWTEXT()
	gui.ShowTextBox(box, text, splashClick, "Welcome")
	box.Window.UiWindow.SetTitle("Cloud Control Panel")

	gui.HorizontalBreak(box)

	if runtime.GOOS == "linux" {
		gui.NewLabel(box,"OS: Linux")
	} else if runtime.GOOS == "windows" {
		gui.NewLabel(box,"OS: Windows")
	} else {
		gui.NewLabel(box,"OS: " + runtime.GOOS)
	}

	if (config.Dirty) {
		gui.NewLabel(box, "Version: " + config.Version + " (dirty)")
	} else {
		gui.NewLabel(box, "Version: " + config.Version)
	}

	if (config.Debug) {
		gui.NewLabel(box, "git rev-list: " + config.Gitref)
		gui.NewLabel(box, "go build version: " + config.Goversion)
		gui.NewLabel(box, "build date: " + shell.Chomp(config.Builddate))
	}

	button := makeButton(box, nil, nil, "OK", "AREA", splashClick)
	if (config.Debug) {
		debugClick(button)
	}
	log.Println("ShowSplashBox() END box =", box)
	return box
}

func getNEWTEXT() *ui.AttributedString {
	var aText  *ui.AttributedString
	aText = ui.NewAttributedString("")

	gui.AreaAppendText(aText, "Welcome to the Cloud Control Panel\n", ui.TextSize(16), ui.TextColor{0.0, 0.0, 0.8, .8})
	gui.AreaAppendText(aText, "(alpha)\n\n", ui.TextSize(10))

	gui.AreaAppendText(aText, "This control panel was designed to be an interface to your 'private' cloud. ", ui.TextWeightBold)
	gui.AreaAppendText(aText, "The concept of a private cloud means that you can use a providers system, or, seemlessly, use your own hardware in your own datacenter. ", ui.TextWeightBold)

	aText.AppendUnattributed("\n")
	aText.AppendUnattributed("\n")
	gui.AreaAppendText(aText, "This control panel requires:\n")
	aText.AppendUnattributed("\n")
	gui.AreaAppendText(aText, "IPv6\n")
	gui.AreaAppendText(aText, "Your hostname in DNS\n")
	aText.AppendUnattributed("\n\n\n\n\n")

	gui.AreaAppendText(aText, "<click or press any key>\n", ui.TextSize(10))

	return aText
}

func splashClick(b *gui.GuiButton) {
	log.Println("splashClick() START")
	gw := b.Box.Window

	// remove the splash screen
	gui.DeleteWindow("Splash")

	tmp := len(config.Accounts)
	log.Println("splashClick() There are", tmp, "accounts")
	if (tmp == 1) {
		// Alfonso and Christina suggested auto login here
		makeAccountWindow(gw, config.Accounts[0])
		// makeCloudInfoBox(gw)
		return
	}

	// if there is already an account, skip straight to the main screen
	for key, _ := range config.Accounts  {
		log.Println("gui.State = splash BUT THERE IS AN ACCOUNT  Account = ", config.Accounts[key])
		makeCloudInfoBox(gw)
		return
	}

	createAccount(gw)
	log.Println("splashClick() END")
}