import GemRB

StartWindow = 0
QuitWindow = 0
ExitButton = 0
SinglePlayerButton = 0
MultiPlayerButton = 0
MoviesButton = 0

def OnLoad():
	global StartWindow, QuitWindow
	global ExitButton, MultiPlayerButton, MoviesButton, SinglePlayerButton

	skip_videos = GemRB.GetVar ("SkipIntroVideos")
	if not skip_videos:
		GemRB.PlayMovie ('BG4LOGO',1)
		GemRB.PlayMovie ('TSRLOGO',1)
		GemRB.PlayMovie ('BILOGO',1)
		GemRB.PlayMovie ('INFELOGO',1)
		GemRB.PlayMovie ('INTRO',1)
		GemRB.SetVar ("SkipIntroVideos", 1)

	GemRB.LoadWindowPack("START")

	#quit subwindow
	QuitWindow = GemRB.LoadWindowObject(3)
	QuitTextArea = QuitWindow.GetControl(0)
	CancelButton = QuitWindow.GetControl(2)
	ConfirmButton = QuitWindow.GetControl(1)
	QuitTextArea.SetText(19532)
	CancelButton.SetText(13727)
	ConfirmButton.SetText(15417)
	ConfirmButton.SetEvent(0, "ExitConfirmed")
	CancelButton.SetEvent(0, "ExitCancelled")

	#main window
	StartWindow = GemRB.LoadWindowObject(0)
	SinglePlayerButton = StartWindow.GetControl(0)
	MultiPlayerButton = StartWindow.GetControl(1)
	MoviesButton = StartWindow.GetControl(2)
	ExitButton = StartWindow.GetControl(3)

	BackToMain()
	
	GemRB.LoadMusicPL("Theme.mus")
	return
	
def SinglePlayerPress():
	
	SinglePlayerButton.SetText(13728)
	MultiPlayerButton.SetText(13729)
	MoviesButton.SetText(24110)
	ExitButton.SetText(15416)
	MultiPlayerButton.SetEvent(0, "LoadSingle")
	SinglePlayerButton.SetEvent(0, "NewSingle")
	MoviesButton.SetEvent(0, "MissionPack")
	ExitButton.SetEvent(0, "BackToMain")
	if GemRB.GetString(24110) == "": # TODO: better way to detect lack of mission pack?
		MoviesButton.SetFlags(IE_GUI_BUTTON_NO_IMAGE, OP_OR)
	return

def MultiPlayerPress():
	
	SinglePlayerButton.SetText(11825)
	MultiPlayerButton.SetText(20642)
	MoviesButton.SetText(15416)
	ExitButton.SetText("")
	SinglePlayerButton.SetEvent(0, "PregenPress")
	MultiPlayerButton.SetEvent(0, "ConnectPress")
	MoviesButton.SetEvent(0, "BackToMain")
	ExitButton.SetEvent(0, "")
	ExitButton.SetStatus(IE_GUI_BUTTON_DISABLED)
	ExitButton.SetFlags(IE_GUI_BUTTON_NO_IMAGE, OP_OR)
	return

def ConnectPress():
#well...
	return

def PregenPress():
	if StartWindow:
		StartWindow.Unload()
	if QuitWindow:
		QuitWindow.Unload()
	GemRB.SetVar("PlayMode",0) #loadgame needs this hack
	GemRB.GameSetExpansion(0)
	GemRB.LoadGame(-1)
	GemRB.SetVar("PlayMode",-1)
	GemRB.SetNextScript("GUIMP")
	return

def LoadSingle():
	if StartWindow:
		StartWindow.Unload()
	if QuitWindow:
		QuitWindow.Unload()
	GemRB.SetVar("PlayMode",0)
	GemRB.GameSetExpansion(0)
	GemRB.SetNextScript("GUILOAD")
	return

def MissionPack():
	if StartWindow:
		StartWindow.Unload()
	if QuitWindow:
		QuitWindow.Unload()
	GemRB.SetVar("PlayMode",3) #use mpsave for saved games
	GemRB.GameSetExpansion(1)
	GemRB.SetNextScript("GUILOAD")
	return

def NewSingle():
	if StartWindow:
		StartWindow.Unload()
	if QuitWindow:
		QuitWindow.Unload()
	GemRB.SetVar("PlayMode",0)
	GemRB.GameSetExpansion(0)
	GemRB.SetVar("Slot",1)
	GemRB.LoadGame(-1)
	GemRB.SetNextScript("CharGen") #temporarily
	return

def ExitPress():
	StartWindow.SetVisible(0)
	QuitWindow.SetVisible(1)
	return
	
def ExitConfirmed():
	GemRB.Quit()
	return

def MoviesPress():
#apparently the order is important
	if StartWindow:
		StartWindow.Unload()
	if QuitWindow:
		QuitWindow.Unload()
	GemRB.SetNextScript("GUIMOVIE")
	return

def ExitCancelled():
	QuitWindow.SetVisible(0)
	StartWindow.SetVisible(1)
	return
	
def BackToMain():
	SinglePlayerButton.SetStatus(IE_GUI_BUTTON_ENABLED)
	MultiPlayerButton.SetStatus(IE_GUI_BUTTON_ENABLED)
	MoviesButton.SetStatus(IE_GUI_BUTTON_ENABLED)
	ExitButton.SetStatus(IE_GUI_BUTTON_ENABLED)
	SinglePlayerButton.SetText(15413)
	MultiPlayerButton.SetText(15414)
	MoviesButton.SetText(15415)
	ExitButton.SetText(15417)
	SinglePlayerButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, "SinglePlayerPress")
	MultiPlayerButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, "MultiPlayerPress")
	MoviesButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, "MoviesPress")
	ExitButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, "ExitPress")
	MoviesButton.SetFlags(IE_GUI_BUTTON_NO_IMAGE, OP_NAND)
	ExitButton.SetFlags(IE_GUI_BUTTON_NO_IMAGE, OP_NAND)
	QuitWindow.SetVisible(0)
	StartWindow.SetVisible(1)
	return
