:NAME options

:GLOBAL boolean		warning_reset_progress
:GLOBAL uint32		screen
:GLOBAL int32		resolution_index
:GLOBAL c_object*	music_mark
:GLOBAL c_object*	sound_mark
:GLOBAL c_object*	fullscreen_mark

:GLOBAL c_object*	nerd_mark0
:GLOBAL c_object*	nerd_mark1
:GLOBAL c_object*	nerd_mark2
:GLOBAL c_object*	nerd_mark3
:GLOBAL c_object*	nerd_mark4
:GLOBAL c_object*	nerd_mark5

:GLOBAL c_object*	resolution_text
:GLOBAL c_object*	apply_button
:GLOBAL c_sound*	sound_forward
:GLOBAL c_sound*	sound_back
:GLOBAL c_object*	scrim
:GLOBAL c_object*	scrim_highlight
:GLOBAL float32		dismiss_delay
:GLOBAL boolean		in_game_mode

k_window_resolutions= {}
k_window_resolutions[0]= {640, 480}
k_window_resolutions[1]= {800, 600}
k_window_resolutions[2]= {1024, 768}
k_window_resolutions[3]= {1280, 720}
k_window_resolutions[4]= {1360, 768}
k_window_resolutions[5]= {1680, 1050}
k_window_resolutions[6]= {1920, 1080}


k_escape_key= e_value("e_key", "_key_escape")

-- subscreens
k_screen_main= 0
k_screen_video= 1
k_screen_audio= 2
k_screen_nerd= 3


-- pause screen buttons
k_touch_music= "touch_music"
k_touch_sound= "touch_sound"
k_reset= "reset"
k_restore_iap= "restore_iap"
k_back= "back"
k_crash_me= "crash_me"
k_video_options= "video_options"
k_audio_options="audio_options"
k_nerd_options="nerd_options"
k_resolution_left="res_left"
k_resolution_right="res_right"
k_video_apply="apply"

k_music_check_mark= "music_mark"
k_sound_check_mark= "sound_mark"

-- nerd settings

k_nerd_count= 6

k_nerd_values={}
k_nerd_values[0]= e_value("e_render_debug_mode", "_render_occupancy_map_5_local_maxima")
k_nerd_values[1]= e_value("e_render_debug_mode", "_render_debug_arena_visibility")
k_nerd_values[2]= e_value("e_render_debug_mode", "_render_occupancy_map_spread")
k_nerd_values[3]= e_value("e_render_debug_mode", "_render_hide_occupancy_maps")
k_nerd_values[4]= e_value("e_render_debug_mode", "_render_ai_targets")
k_nerd_values[5]= e_value("e_render_debug_mode", "_render_paths")

k_nerd_touch={}
k_nerd_touch[0]= "touch5"			-- these make no sense! Live with it!
k_nerd_touch[1]= "touch6"
k_nerd_touch[2]= "touch_music"
k_nerd_touch[3]= "touch_sound"
k_nerd_touch[4]= "touch3"
k_nerd_touch[5]= "touch4"

-- progress reset warning

k_warning_plate = "warning"
k_warning_cancel= "warning_cancel"
k_warning_reset= "warning_reset"

k_dismiss_delay= 0.5

k_ui_time_mode= e_value("e_time_mode", "_time_mode_ui")

-- ------------------- initialization ------------------- 


function global_initialize(g)
	g.warning_reset_progress= false
	g.music_mark= nil
	g.sound_mark= nil
	
	g.nerd_mark0= nil
	g.nerd_mark1= nil
	g.nerd_mark2= nil
	g.nerd_mark3= nil
	g.nerd_mark4= nil
	g.nerd_mark5= nil
		
	g.sound_forward= nil
	g.sound_back= nil
	g.fullscreen_mark= nil
	g.resolution_text= nil
	g.apply_button= nil
	
	g.scrim= nil
	g.scrim_highlight= nil
	
	g.in_game_mode= false

end


-- ------------------- system ------------------- 

k_delay_factor= 0.5

function place_plate(plate_name, animation_name, staggered)
	local ws= world_state_get()
	local p= world_get().get_plate(plate_name)
	local ps= p.plate_state_get()
	ps.delete_all()
	ws.place_plate(p)
	
	if (animation_name) then
	
		if (staggered) then
			local object= ps.objects
			while (object) do
				if (not object.attachment.parent) then
					local offset= object.transform.offset
					local delay= k_delay_factor*(1-offset.y)
					object.position_set(offset.x, -3)
					object.tween_animation_play_after_delay(animation_name, delay)
				end
				object= object.plate_object_next
			end
		else
			ps.tween_animation_play(animation_name)
		end
	end
	return ps
end

function remove_plate(plate_name, animation_name)
	local ws= world_state_get()
	local ps= ws.plate_state_get(plate_name)
	
	local object= ps.objects
	while (object) do
		if (not object.tween_animation_play_direction(animation_name, false, false)) then
			object_delete(object)
		end
		object= object.plate_object_next
	end
end

function keyboard_dismiss()
	local keyboard= keyboard_get()
	local result= false
	if (keyboard) then
		if (keyboard.pressed[k_escape_key]) then
			result= true
		end
	end

	return result
end


function start()

	-- decide which options we want to display?
	local ws= world_state_get()	
	global.sound_forward= ws.sound_manager.get_sound_by_name("forward");
	global.sound_back= ws.sound_manager.get_sound_by_name("back");

	if (global.sound_forward) then
		global.sound_forward.play(k_ui_time_mode)
	end
	
	global.in_game_mode= (ws.plate_state_get("pause")~=nil)
	
	local scrim_anim= "start"
	if (global.in_game_mode) then
		scrim_anim= "start_fade"
	end
	
	place_plate("options_scrim", scrim_anim, false)
	place_plate("options", "start", true)
	place_plate("controls", "start", false)

	global.screen= k_screen_main
	
	sync_sound()
	sync_music()
	
	local store= store_get()
	store.request_refresh()
	
end

function set_mark(mark, enable)
	if (mark) then
		mark.hide(not enable)
	end
end

function sync_music()
	local sm= world_state_get().sound_manager
	set_mark(global.music_mark, not sm.music_is_muted())
end

function toggle_music()
	local sm= world_state_get().sound_manager
	local mute= not sm.music_is_muted()
	
	sm.music_mute(mute)
	set_mark(global.music_mark, not sm.music_is_muted())
end

function sync_sound()
	local sm= world_state_get().sound_manager
	set_mark(global.sound_mark, not sm.sfx_is_muted())
end

function toggle_sound()
	local sm= world_state_get().sound_manager
	local mute= not sm.sfx_is_muted()
	
	sm.sfx_mute(mute)
	set_mark(global.sound_mark, not sm.sfx_is_muted())
end

function unload_zone()
	if (global.sound_back) then
		global.sound_back.play(k_ui_time_mode)
	end

	world_get().zone_unload("options")
end

function warning_reset_start()
	global.warning_reset_progress= true
	place_plate(k_warning_plate, "start", true)
	place_plate("warning_scrim", "start", false)
end

function reset_execute()
	local player_profile= player_profile_get()
	player_profile.reset()
	local app_settings= app_settings_get()
	app_settings.reset()
	simulate_game_player_changed()
end

function warning_reset_update()

	local handled= false
	local close_warning= false

	local touch_interface= touch_get()
	if (touch_interface and touch_interface.touch_started()) then
		local touch_index= touch_interface.touch_start_index()
		local touch= touch_interface.touch[touch_index]
		local object= touch.touch_handler_object
		if (object) then
			local placement= object.placement
			if (placement) then
			
				local name= placement.name
				if (name == k_warning_cancel) then
					close_warning= true
					handled= true
				elseif (name == k_warning_reset) then
					close_warning= true
					reset_execute()
					handled= true
				end
			end
			
			if (close_warning) then
				object.button_press()
			end
		end
	end
	
	if (not handled) then
		local keyboard= keyboard_get()
		if (keyboard) then
			if (keyboard.pressed[k_escape_key]) then
				close_warning= true
			end
		end
	end
	
	if (close_warning) then
		remove_plate(k_warning_plate, "start")
		remove_plate("warning_scrim", "start")
		global.warning_reset_progress= false
	end
	
end

function get_resolution_index_fs(app_settings)
	local iterator= app_settings.available_video_modes.iterator_get()
	local current_mode= app_settings.last_fullscreen_video_mode
	local index= 0
	local result= 0
	while (true) do
		local resmode= iterator.next()
		if (resmode) then
			if (resmode.width==current_mode.width and resmode.height==current_mode.height) then
				result= index
				break
			else
				index = index+1
			end
		else
			break
		end
	end
	return result
end

function get_resolution_index_w(app_settings)
	local current_mode= app_settings.last_windowed_video_mode
	local result= 0
	for i= 0, #k_window_resolutions do
		local wres= k_window_resolutions[i]
		if (wres[1]==current_mode.width and wres[2]==current_mode.height) then
			result= i
			break
		end
	end
	
	return result
end

function sync_real_video_mode()
	local app_settings= app_settings_get()
	if (app_settings.is_full_screen()) then
		global.resolution_index= get_resolution_index_fs(app_settings)
		global.fullscreen_mark.hide(false)
	else
		global.resolution_index= get_resolution_index_w(app_settings)
		global.fullscreen_mark.hide(true)
	end	
end

function sync_resolution()
	local app_settings= app_settings_get()
	local text= ""
	if (global.fullscreen_mark and not global.fullscreen_mark.is_hidden()) then
		if (global.resolution_index < app_settings.available_video_modes.count) then
			local video_mode= app_settings.available_video_modes[global.resolution_index]
			text= text..video_mode.width.." x "..video_mode.height
		else
			text= "invalid"
		end
	else
		local res= k_window_resolutions[global.resolution_index]
		text= text..res[1].." x "..res[2]
	end
	
	if (global.resolution_text) then
		local text_component= global.resolution_text.text_get()
		text_component.text_set(text)
	end
end

function check_for_apply_button()
	if (global.apply_button==nil) then
		global.apply_button = world_state_get().place_by_plate("video_options", "apply");
		if (global.apply_button) then
			global.apply_button.tween_animation_play("start")
		end
	end
end

function update_resolution(delta)
	local app_settings= app_settings_get()
	local list= nil
	local length= 0

	if (global.fullscreen_mark and not global.fullscreen_mark.is_hidden()) then
		list= app_settings.available_video_modes
		length= list.count
	else
		list= k_window_resolutions
		length= 1+#k_window_resolutions
	end

	if (length > 0) then
		local newindex= global.resolution_index + delta
		if (newindex >=length) then
			newindex= 0
		elseif (newindex < 0) then
			newindex= length-1
		end	
		
		global.resolution_index= newindex

		check_for_apply_button()
	end
end

function toggle_full_screen()
	if (global.fullscreen_mark) then
		if (global.fullscreen_mark.is_hidden()) then
			global.fullscreen_mark.hide(false)
		else
			global.fullscreen_mark.hide(true)
		end
		check_for_apply_button()

		local app_settings= app_settings_get()
		if (app_settings.is_full_screen()==not global.fullscreen_mark.is_hidden()) then
			-- we're returning to the state we're actually in
			sync_real_video_mode()
		else
			global.resolution_index= 0
		end
		sync_resolution()
		
	end
end



function video_options_start()
	remove_plate("options", "start")
	local ps= place_plate("video_options", "start", true)
	global.screen= k_screen_video
	
	global.resolution_text= ps.object_get("resolution")
	global.fullscreen_mark= ps.object_get("fullscreen_mark")
	sync_real_video_mode()
	
	local apply_button= ps.object_get("apply")
	object_delete(apply_button)
	
	sync_resolution()
	
end

function apply_video_changes()
	local app_settings= app_settings_get()
	local fullscreen= false
	if (global.fullscreen_mark) then
		fullscreen= (not global.fullscreen_mark.is_hidden())
	end
	
	if (fullscreen) then
		if (global.resolution_index < app_settings.available_video_modes.count) then
			app_settings.set_fullscreen_mode(app_settings.available_video_modes[global.resolution_index])
		end
	else
		local res= k_window_resolutions[global.resolution_index]
		app_settings.set_windowed_mode(res[1], res[2])
	end

end

function video_options_update()

	local touch_interface= touch_get()
	local handled= false

	if (touch_interface and touch_interface.touch_started()) then
		local touch_index= touch_interface.touch_start_index()
		local touch= touch_interface.touch[touch_index]
		local object= touch.touch_handler_object
		if (object) then
			local placement= object.placement
			if (placement) then
				local name= placement.name
				if (name == k_back) then
					dismiss_video_options()
					object.tween_animation_play("pressed")
					handled= true
				elseif (name == k_resolution_left) then
					update_resolution(-1)
					sync_resolution()
					handled= true
				elseif (name == k_resolution_right) then
					update_resolution(1)
					sync_resolution()
					handled= true
				elseif (name == k_video_apply) then
					apply_video_changes()
					dismiss_video_options()
					object.button_press()
					handled= true
				elseif (name == k_touch_sound) then
					toggle_full_screen()
				end
			end
		end
	end

	if ((not handled) and keyboard_dismiss()) then
		dismiss_video_options()
	end
end

function dismiss_video_options()
	place_plate("options", "start", true)
	remove_plate("video_options", "start")
	global.screen= k_screen_main
	global.fullscreen_mark= nil
	global.resolution_text= nil
	global.apply_button= nil
end



function audio_options_start()
	remove_plate("options", "start")
	local ps= place_plate("audio_options", "start", true)
	global.screen= k_screen_audio
	
	global.music_mark= ps.object_get(k_music_check_mark)
	global.sound_mark= ps.object_get(k_sound_check_mark)

	sync_music()
	sync_sound()
	
end

function audio_options_update()

	local touch_interface= touch_get()
	local handled= false

	if (touch_interface and touch_interface.touch_started()) then
		local touch_index= touch_interface.touch_start_index()
		local touch= touch_interface.touch[touch_index]
		local object= touch.touch_handler_object
		if (object) then
			local placement= object.placement
			if (placement) then
				local name= placement.name
				if (name == k_back) then
					dismiss_audio_options()
					object.tween_animation_play("pressed")
					handled= true
				elseif (name == k_touch_music) then
					toggle_music()
					handled= true
				elseif (name == k_touch_sound) then
					toggle_sound()
					handled= true
				end
			end
		end
	end

	if ((not handled) and keyboard_dismiss()) then
		dismiss_audio_options()
	end
end

function dismiss_audio_options()
	place_plate("options", "start", true)
	remove_plate("audio_options", "start")
	global.screen= k_screen_main
	
	global.music_mark= nil
	global.sound_mark= nil
	
end


function sync_nerd_marks()
	for i=0,5 do
		set_mark(global["nerd_mark"..i], render_debug_mode_is_enabled(k_nerd_values[i]))
	end
end

function toggle_nerd_mark(i)

	local is_enabled= render_debug_mode_is_enabled(k_nerd_values[i])
	is_enabled = not is_enabled
	
	render_debug_mode_enable(k_nerd_values[i], is_enabled)
	
	if (is_enabled) then
		-- some special cases
		--k_nerd_values[0]= e_value("e_render_debug_mode", "_render_occupancy_map_5_local_maxima")
		--k_nerd_values[1]= e_value("e_render_debug_mode", "_render_debug_arena_visibility")
		--k_nerd_values[2]= e_value("e_render_debug_mode", "_render_occupancy_map_spread")
		--k_nerd_values[3]= e_value("e_render_debug_mode", "_render_hide_occupancy_maps")
		--k_nerd_values[4]= e_value("e_render_debug_mode", "_render_ai_targets")
		--k_nerd_values[5]= e_value("e_render_debug_mode", "_render_paths")
		
		if (i ==2) then											-- _render_occupancy_map_spread ON
			render_debug_mode_enable(k_nerd_values[3], false)	-- _render_hide_occupancy_maps OFF
			render_debug_mode_enable(k_nerd_values[1], false)	-- _render_debug_arena_visibility OFF
		elseif (i ==3) then										-- _render_hide_occupancy_maps ON
			render_debug_mode_enable(k_nerd_values[2], false)	-- _render_occupancy_map_spread OFF
		elseif (i ==1) then										-- _render_debug_arena_visibility ON
			render_debug_mode_enable(k_nerd_values[2], false)	-- _render_occupancy_map_spread OFF
			render_debug_mode_enable(k_nerd_values[3], true)	-- _render_hide_occupancy_maps ON
		end
	end
	
	sync_nerd_marks()
	
end

function nerd_options_start()
	remove_plate("options", "start")
	local ps= place_plate("nerd_options", "start", true)
	global.screen= k_screen_nerd

	global.nerd_mark0= ps.object_get("nerd_mark0")
	global.nerd_mark1= ps.object_get("nerd_mark1")
	global.nerd_mark2= ps.object_get("nerd_mark2")
	global.nerd_mark3= ps.object_get("nerd_mark3")
	global.nerd_mark4= ps.object_get("nerd_mark4")
	global.nerd_mark5= ps.object_get("nerd_mark5")

	sync_nerd_marks()

end

function nerd_options_update()
	local touch_interface= touch_get()
	local handled= false

	if (touch_interface and touch_interface.touch_started()) then
		local touch_index= touch_interface.touch_start_index()
		local touch= touch_interface.touch[touch_index]
		local object= touch.touch_handler_object
		if (object) then
			local placement= object.placement
			if (placement) then
				local name= placement.name
				if (name == k_back) then
					dismiss_nerd_options()
					object.tween_animation_play("pressed")
					handled= true
				else
					for i=0,5 do
						if (name == k_nerd_touch[i]) then
							toggle_nerd_mark(i)
							handled= true
							break
						end
					end
				end
			end
		end
	end

	if ((not handled) and keyboard_dismiss()) then
		dismiss_nerd_options()
	end

end

function dismiss_nerd_options()
	place_plate("options", "start", true)
	remove_plate("nerd_options", "start")
	global.screen= k_screen_main
	
	for i=0,5 do
		global["nerd_mark"..i]= nil
	end
end



function start_dismiss()
	local scrim_anim= "start"
	if (global.in_game_mode) then
		scrim_anim= "start_fade"
	end

	remove_plate("options_scrim", scrim_anim)
	remove_plate("options", "start")
	remove_plate("controls", "start")
	global.dismiss_delay= k_dismiss_delay
end

function dismiss()
	unload_zone()
end

--[[
function restore_purchases()
	local ws= world_state_get()
	local store= store_get()

	store.restore_purchases()
	ws.place_by_plate("notification", "restore_notification")		-- react to the player!
end
--]]


function update()
	if (global.warning_reset_progress) then
		warning_reset_update()
	elseif (global.screen == k_screen_video) then
		video_options_update()
	elseif (global.screen == k_screen_audio) then
		audio_options_update()
	elseif (global.screen == k_screen_nerd) then
		nerd_options_update()
	elseif (global.dismiss_delay > 0) then
		local dt= world_state_get().master_time.delta_t()
		global.dismiss_delay= global.dismiss_delay-dt
		if (global.dismiss_delay<= 0) then
			-- finish
			dismiss()
		end
	else
		local touch_interface= touch_get()
		local handled= false

		if (touch_interface and touch_interface.touch_started()) then
			local touch_index= touch_interface.touch_start_index()
			local touch= touch_interface.touch[touch_index]
			local object= touch.touch_handler_object
			if (object) then
				local placement= object.placement
				if (placement) then
					local name= placement.name
					
					if (name == k_reset) then
						warning_reset_start()
						handled= true
					--elseif (name == k_restore_iap) then
					--	restore_purchases()
					--	handled= true
					elseif (name == k_video_options) then
						video_options_start()
						handled= true
					elseif (name == k_audio_options) then
						audio_options_start()
						handled= true
					elseif (name == k_nerd_options) then
						nerd_options_start()
						handled= true
					elseif (name == k_back) then
						start_dismiss()
						--object.tween_animation_play("pressed")
						handled= true
					elseif (name == k_crash_me) then
						crash_me()
						handled= true
					end
					
					if (handled) then
						object.button_press()
					end
				end
			end
		end
		
		if ((not handled) and keyboard_dismiss()) then
			start_dismiss()
		end
	end
end


system_register(start, update, nil, nil)
