from tkinter import ttk

from .. import config
from .. import display

from . import tk_util



def main_gui():
	def send_done():
		display.tkroot.event_generate("<Escape>")



	tabs = ttk.Notebook( display.tkroot, padding=5 )
	tabs.pack( expand=True, fill="both" )
	panel_window   = ttk.Frame( tabs, padding=5 )
	panel_graphics = ttk.Frame( tabs, padding=5 )
	panel_audio    = ttk.Frame( tabs, padding=5 )
	#panel_game     = ttk.Frame( tabs, padding=5 )
	#panel_misc     = ttk.Frame( tabs, padding=5 )
	tabs.add( panel_window  , text="Window"   )
	tabs.add( panel_graphics, text="Graphics" )
	tabs.add( panel_audio   , text="Audio"    )
	#tabs.add( panel_game    , text="Game"     )
	#tabs.add( panel_misc    , text="Misc."    )



	tk_util.Label( panel_window, "Window resolution: " ).set_grid( 0, 0 )
	window_res_x = tk_util.IntegerEntry( panel_window, 1,9999, str(config.window_res[0]) ).set_grid( 1, 0 )
	window_res_y = tk_util.IntegerEntry( panel_window, 1,9999, str(config.window_res[1]) ).set_grid( 2, 0 )

	tk_util.Label( panel_window, "MSAA samples: " ).set_grid( 0, 1 )
	window_msaa_nspp = tk_util.IntegerEntry( panel_window, 1,64, str(config.window_msaa_nspp) ).set_grid( 1, 1 )

	tk_util.Label( panel_window, "Fullscreen: " ).set_grid( 0, 2 )
	window_fullscreen = tk_util.Checkbox( panel_window, config.window_fullscreen ).set_grid( 1, 2 )

	tk_util.Label( panel_window, "VSync (Swap-Interval): " ).set_grid( 0, 3 )
	window_swap_interval = tk_util.IntegerEntry( panel_window, -5,5, str(config.window_swap_interval) ).set_grid( 1, 3 )

	tk_util.Label( panel_window, "GUI scale: " ).set_grid( 0, 4 )
	tk_gui_scale = tk_util.FloatEntry( panel_window, 0.5,4.0, config.tk_gui_scale ).set_grid( 1, 4 )

	tk_util.Button( panel_window, "Done", send_done ).set_grid( 0, 5 )



	y = 0

	tk_util.Label( panel_graphics, "Use advanced renderer: " ).set_grid( 0, y )
	render_advanced = tk_util.Checkbox( panel_graphics, config.render_advanced ).set_grid( 1, y ); y += 1

	tk_util.Label( panel_graphics, "Render framerate: " ).set_grid( 0, y )
	render_fps = tk_util.Checkbox( panel_graphics, config.render_fps ).set_grid( 1, y ); y += 1

	tk_util.Label( panel_graphics, "Render shadows: " ).set_grid( 0, y )
	render_shadows = tk_util.Checkbox( panel_graphics, config.render_shadows ).set_grid( 1, y ); y += 1

	tk_util.Label( panel_graphics, "Shadow level: " ).set_grid( 0, y )
	render_shadow_level = tk_util.FloatEntry( panel_graphics, 0.0,1.0, 1.0-config.render_shadow_darkness ).set_grid( 1, y ); y += 1

	tk_util.Label( panel_graphics, "(Basic only) render particle shadows: " ).set_grid( 0, y )
	renderbasic_shadows_particles = tk_util.Checkbox( panel_graphics, config.renderbasic_shadows_particles ).set_grid( 1, y ); y += 1

	tk_util.Label( panel_graphics, "(Adv. only) G-buffer texture AA: " ).set_grid( 0, y )
	renderadv_gbuf_texaa = tk_util.IntegerEntry( panel_graphics, 1,16, config.renderadv_gbuf_texaa ).set_grid( 1, y ); y += 1

	tk_util.Label( panel_graphics, "(Adv. only) shadow mode: " ).set_grid( 0, y )
	renderadv_shadow_mode = tk_util.Radio( panel_graphics,
		{ 0 : "Shadow volume", 1 : "Shadowmap" },
		config.renderadv_shadow_mode
	).set_grid( 1, y ); y += 1

	tk_util.Label( panel_graphics, "(Adv. only) shadowmap size: " ).set_grid( 0, y )
	renderadv_smap_res = tk_util.IntegerEntry( panel_graphics, 64,16_384, config.renderadv_smap_res ).set_grid( 1, y ); y += 1

	tk_util.Label( panel_graphics, "(Adv. only) shadowmap PCF (sqrt): " ).set_grid( 0, y )
	renderadv_smap_pcf_rt = tk_util.IntegerEntry( panel_graphics, 0,16, config.renderadv_smap_pcf_rt ).set_grid( 1, y ); y += 1

	tk_util.Label( panel_graphics, "Camera convergence: " ).set_grid( 0, y )
	camera_converge = tk_util.FloatEntry( panel_graphics, 0.0,1.0, config.camera_converge ).set_grid( 1, y ); y += 1

	tk_util.Label( panel_graphics, "Camera FoV (°): " ).set_grid( 0, y )
	camera_fov_deg = tk_util.FloatEntry( panel_graphics, 0.0,1.0, config.camera_fov_deg ).set_grid( 1, y ); y += 1

	tk_util.Label( panel_graphics, "Camera clip near / far: " ).set_grid( 0, y )
	camera_near = tk_util.FloatEntry( panel_graphics, 0.0,1000.0, config.camera_near ).set_grid( 1, y )
	camera_far  = tk_util.FloatEntry( panel_graphics, 0.0,1000.0, config.camera_far  ).set_grid( 2, y ); y += 1

	tk_util.Label( panel_graphics, "Detail of marbles: " ).set_grid( 0, y )
	detail_marble = tk_util.IntegerEntry( panel_graphics, 5,100, config.detail_marble ).set_grid( 1, y ); y += 1

	tk_util.Label( panel_graphics, "Detail of circular shadows: " ).set_grid( 0, y )
	detail_shadow_circle = tk_util.IntegerEntry( panel_graphics, 10,100, config.detail_shadow_circle ).set_grid( 1, y ); y += 1

	tk_util.Label( panel_graphics, "Detail of particles: " ).set_grid( 0, y )
	detail_particle = tk_util.IntegerEntry( panel_graphics, 5,50, config.detail_particle ).set_grid( 1, y ); y += 1

	tk_util.Label( panel_graphics, "Anisotropic texture filtering: " ).set_grid( 0, y )
	texture_anisotropic = tk_util.FloatEntry( panel_graphics, 1.0,16.0, config.texture_anisotropic ).set_grid( 1, y ); y += 1

	tk_util.Label( panel_graphics, "Explosion particles: " ).set_grid( 0, y )
	particle_num_explode = tk_util.IntegerEntry( panel_graphics, 0,1000, config.particle_num_explode ).set_grid( 1, y ); y += 1

	tk_util.Button( panel_graphics, "Done", send_done ).set_grid( 0, y ); y += 1



	tk_util.Label( panel_audio, "Volume (FX): " ).set_grid( 0, 0 )
	volume_fx = tk_util.FloatEntry( panel_audio, 0.0,1.0, config.volume_fx ).set_grid( 1, 0 )

	#tkgui.Label( panel_audio, "Volume (Music): " ).set_grid( 0, 1 )
	#volume_music = tkgui.FloatEntry( panel_audio, 0.0,1.0, config.volume_music ).set_grid( 1, 1 )

	tk_util.Button( panel_audio, "Done", send_done ).set_grid( 0, 1 )



	#ttk.Separator(panel_window).grid( column=0,row=4, columnspan=3, sticky="we" )

	def on_done():
		config.window_res[0]        = window_res_x        .get_value()
		config.window_res[1]        = window_res_y        .get_value()
		config.window_msaa_nspp     = window_msaa_nspp    .get_value()
		config.window_fullscreen    = window_fullscreen   .get_value()
		config.window_fullscreen    = window_fullscreen   .get_value()
		config.window_swap_interval = window_swap_interval.get_value()
		config.tk_gui_scale         = tk_gui_scale        .get_value()

		config.render_advanced               = render_advanced              .get_value()
		config.render_fps                    = render_fps                   .get_value()
		config.render_shadows                = render_shadows               .get_value()
		config.render_shadow_darkness        = 1.0 - render_shadow_level.get_value()
		config.renderbasic_shadows_particles = renderbasic_shadows_particles.get_value()
		config.renderadv_gbuf_texaa          = renderadv_gbuf_texaa         .get_value()
		config.renderadv_shadow_mode         = renderadv_shadow_mode        .get_value()
		config.renderadv_smap_res            = renderadv_smap_res           .get_value()
		config.renderadv_smap_pcf_rt         = renderadv_smap_pcf_rt        .get_value()
		config.camera_converge               = camera_converge              .get_value()
		config.camera_fov_deg                = camera_fov_deg               .get_value()
		config.camera_near                   = camera_near                  .get_value()
		config.camera_far                    = camera_far                   .get_value()
		config.detail_marble                 = detail_marble                .get_value()
		config.detail_shadow_circle          = detail_shadow_circle         .get_value()
		config.detail_particle               = detail_particle              .get_value()
		config.texture_anisotropic           = texture_anisotropic          .get_value()
		config.particle_num_explode          = particle_num_explode         .get_value()

		config.volume_fx    = volume_fx   .get_value()
		#config.volume_music = volume_music.get_value()

		tabs.destroy()

	display.tk_main(on_done)
