import tkinter.messagebox as messagebox
from tkinter import ttk

from .. import display

from . import tk_util



def main_gui():
	window = ttk.Frame( display.tkroot, padding=5 )
	window.grid()

	tk_util.Label( window, "Congratulations, you got a highscore!" ).set_grid( 0, 0 )

	tk_util.Label( window, "Please enter your name: " ).set_grid( 0, 1 )
	entry = tk_util.TextEntry(window).set_grid( 1, 1 )

	name = ""
	def on_submit():
		nonlocal name
		name = entry.get_text()
		display.tkroot.event_generate("<Escape>")
	tk_util.Button( window, "Submit", on_submit ).set_grid( 0, 2 )

	display.tkroot.bind( "<Return>", lambda evt: on_submit() )

	def confirm():
		if name != "": return True
		return messagebox.askyesno(
			"Unsaved Name",
			"No name provided. Lose your highscore and quit anyway?",
			icon = messagebox.WARNING
		)
	display.tk_main( window.destroy, confirm )

	display.tkroot.unbind("<Return>")

	return name
