import os
import sys
import tkinter as tk
from typing import Callable

#"pip install pygame-ce"
with open( os.devnull, "w" ) as f:
	oldstdout=sys.stdout; sys.stdout=f; import pygame; sys.stdout=oldstdout

from . import config

import src.game.render_lowlevel as game_render_lowlevel



surf:pygame.Surface|None = None



tkroot:tk.Tk|None = None

def init_tkinter():
	global tkroot
	tkroot = tk.Tk()
	tkroot.withdraw()

	icon = tk.PhotoImage( file=config.FILEPATH_ICON )
	tkroot.wm_iconphoto( True, icon )

	tkroot.tk.call( "tk", "scaling", config.tk_gui_scale )

def set_caption( caption:str ):
	pygame.display.set_caption(caption)
	tkroot.title(caption)



def main_window_remake_as_pygame():
	global surf
	surf = pygame.display.set_mode( config.window_res, flags=pygame.RESIZABLE )

def _main_window_remake_helper( game_mode:bool ):
	game_render_lowlevel.ensure_deinit()

	pygame.display.quit()

	if sys.platform in ( "win32", "win64" ):
		os.environ["SDL_VIDEO_CENTERED"] = "1"

	pygame.display.init()
	#print(pygame.display.get_driver())

	icon = pygame.image.load(config.FILEPATH_ICON).convert( 32, pygame.SRCALPHA )
	pygame.display.set_icon(icon)

	if game_mode and config.window_msaa_nspp > 1:
		pygame.display.gl_set_attribute( pygame.GL_MULTISAMPLEBUFFERS, 1                       )
		pygame.display.gl_set_attribute( pygame.GL_MULTISAMPLESAMPLES, config.window_msaa_nspp )
	else:
		pygame.display.gl_set_attribute( pygame.GL_MULTISAMPLEBUFFERS, 0 )
		pygame.display.gl_set_attribute( pygame.GL_MULTISAMPLESAMPLES, 0 )

	if game_mode and config.render_advanced:
		pygame.display.gl_set_attribute( pygame.GL_CONTEXT_MAJOR_VERSION, 3 )
		pygame.display.gl_set_attribute( pygame.GL_CONTEXT_MINOR_VERSION, 2 )
		pygame.display.gl_set_attribute( pygame.GL_CONTEXT_PROFILE_MASK, pygame.GL_CONTEXT_PROFILE_CORE )
	else:
		pygame.display.gl_set_attribute( pygame.GL_CONTEXT_MAJOR_VERSION, 2 )
		pygame.display.gl_set_attribute( pygame.GL_CONTEXT_MINOR_VERSION, 1 )
		pygame.display.gl_set_attribute( pygame.GL_CONTEXT_PROFILE_MASK, pygame.GL_CONTEXT_PROFILE_COMPATIBILITY )

def main_window_remake_as_gl_game():
	global surf
	surf = None

	_main_window_remake_helper(True)

	flags = pygame.OPENGL|pygame.DOUBLEBUF | pygame.RESIZABLE
	if config.window_fullscreen:
		flags |= pygame.FULLSCREEN
	pygame.display.set_mode( config.window_res, flags, vsync=config.window_swap_interval )
	game_render_lowlevel.config_context()

	game_render_lowlevel.ensure_init()

def main_window_remake_as_gl_menu():
	global surf
	surf = None

	_main_window_remake_helper(False)

	flags = pygame.OPENGL|pygame.DOUBLEBUF | pygame.RESIZABLE #note un-fullscreen
	pygame.display.set_mode( config.window_res, flags, vsync=config.window_swap_interval )
	game_render_lowlevel.config_context()

def tk_main( fn_cleanup:Callable[[],None], confirm_close:Callable[[],bool]=lambda:True ):
	tkroot.eval("tk::PlaceWindow . center")

	tkroot.deiconify()

	looping = True
	def on_close():
		nonlocal looping
		if confirm_close():
			looping = False
	tkroot.protocol( "WM_DELETE_WINDOW", on_close )
	tkroot.bind( "<Escape>", lambda evt: on_close() )

	while looping:
		tkroot.update_idletasks()
		tkroot.update()

	fn_cleanup()

	tkroot.withdraw()
