--[[
	Customized functions for controlling what happens for a particular loaded level.
	These classes should define any  of

	init(level)
	start(level)
	update(level, dt)
	shutdown(level)
	render(level)
]]--

local Util = require 'core/appkit/lua/util'
local SimpleProject = require 'core/appkit/lua/simple_project'

Project.MainMenu = Project.MainMenu or {}
local MainMenu = Project.MainMenu
MainMenu.custom_listener = MainMenu.custom_listener or nil

local start_time = 0
local play_sound = false;

player_camera_fov = 0.78
volume = 0.5
audio_on = true;

-- Audio functions
local function play_main_menu_ambient()
	if stingray.Wwise then
		stingray.Wwise.load_bank("content/audio/default")
		local wwise_world = stingray.Wwise.wwise_world(SimpleProject.world)
		stingray.WwiseWorld.trigger_event(wwise_world, "sfx_UI_music_start")
	end
end

local function stop_main_menu_ambient()
	if stingray.Wwise then
		stingray.Wwise.load_bank("content/audio/default")
		local wwise_world = stingray.Wwise.wwise_world(SimpleProject.world)
		stingray.WwiseWorld.stop_event(wwise_world, "sfx_UI_music_start")
	end
end

local function play_switch_game()
	if stingray.Wwise then
		stingray.Wwise.load_bank("content/audio/default")
		local wwise_world = stingray.Wwise.wwise_world(SimpleProject.world)
		stingray.WwiseWorld.trigger_event(wwise_world, "sfx_UI_music_switch_game")
	end
end

local function toggle_audio_on()
	if stingray.Wwise then
		stingray.Wwise.load_bank("content/audio/default")
		local wwise_world = stingray.Wwise.wwise_world(SimpleProject.world)
		stingray.WwiseWorld.trigger_event(wwise_world, "UI_Audio_Toggle_ON")
	end
end

local function toggle_audio_off()
	if stingray.Wwise then
		stingray.Wwise.load_bank("content/audio/default")
		local wwise_world = stingray.Wwise.wwise_world(SimpleProject.world)
		stingray.WwiseWorld.trigger_event(wwise_world, "UI_Audio_Toggle_OFF")
	end
end

function MainMenu.start()
	if stingray.Window then
		stingray.Window.set_show_cursor(true)
	end

	if scaleform then
		if stingray.Wwise ~= nil then
		stingray.Wwise.load_bank("content/audio/default") -- temporarily necessary for autoload mode to work
		end
		scaleform.Stingray.load_project_and_scene("content/ui/advanced_menu.s2d/advanced_menu")
		--Register menu button mouse listener
		local custom_listener = MainMenu.custom_listener
		custom_listener = scaleform.EventListener.create(custom_listener, MainMenu.on_custom_event)
		MainMenu.custom_listener = custom_listener
		scaleform.EventListener.connect(custom_listener, scaleform.EventTypes.Custom)
	end

	local level = SimpleProject.level
	start_time = stingray.World.time(SimpleProject.world)
	-- make sure camera is at correct location
	local camera_unit = SimpleProject.camera_unit
	local camera = stingray.Unit.camera(camera_unit, 1)
	stingray.Unit.set_local_pose(camera_unit, 1, stingray.Matrix4x4.identity())
	stingray.Camera.set_local_pose(camera, camera_unit, stingray.Matrix4x4.identity())

	Appkit.manage_level_object(level, MainMenu, nil)

	-- Main Menu Start Ambient Audio
	play_main_menu_ambient()
end

function MainMenu.shutdown(object)
	if scaleform then
		scaleform.EventListener.disconnect(MainMenu.custom_listener)
		scaleform.Stingray.unload_project()
		stop_main_menu_ambient()
	end

	MainMenu.evt_listener_handle = nil
	Appkit.unmanage_level_object(SimpleProject.level, MainMenu, nil)
	if stingray.Window then
		stingray.Window.set_show_cursor(false)
	end
end

MainMenu.action = nil
function MainMenu.on_custom_event(evt)
	if evt.name == "action" then
		if evt.data ~= nil then
			MainMenu.action = evt.data
		end
	end
end

local function perform_action()
	if MainMenu.action == nil then
		return
	end

	-- Load empty level
	if MainMenu.action.message == "change_level" then
		SimpleProject.change_level(Project.level_names.basic)
		-- Audio for player level
		play_main_menu_ambient()
		play_switch_game()

	-- Exit the program
	elseif MainMenu.action.message == "exit" then
		stingray.Application.quit()
	-- Play Sound
	elseif MainMenu.action.message == "play_sound" then
		play_sound = true
	elseif MainMenu.action.message == "stop_sound" then
		play_sound = false
	elseif MainMenu.action.message == "fov_changed" then
		player_camera_fov = MainMenu.action.value * 0.01 + 0.4
	elseif MainMenu.action.message == "volume_changed" then
		volume = MainMenu.action.value/100;
	elseif MainMenu.action.message == "sound_toggled" then
		audio_on = MainMenu.action.value == 1;
		if not audio_on then
			toggle_audio_off()
		else
			toggle_audio_on()
		end
	end
	MainMenu.action = nil
end

-- [[Main Menu custom functionality]]--
function MainMenu.update(object, dt)
	if MainMenu.action == nil  then
		local time = stingray.World.time(SimpleProject.world)
		local p = stingray.Application.platform()
		if time - start_time > 1 then
			if Appkit.Util.is_pc() then
				if stingray.Keyboard.pressed(stingray.Keyboard.button_id("1")) then
					MainMenu.action = "start"
				elseif stingray.Keyboard.pressed(stingray.Keyboard.button_id("esc")) then
					MainMenu.action = "exit"
				end
			elseif p == stingray.Application.XB1 or p == stingray.Application.PS4 then 
				if stingray.Pad1.pressed(stingray.Pad1.button_id(Appkit.Util.plat(nil, "a", nil, "cross"))) then
    				MainMenu.action = "start"
    			elseif stingray.Pad1.pressed(stingray.Pad1.button_id(Appkit.Util.plat(nil, "b", nil, "circle"))) then
    				MainMenu.action = "exit"
    			end
    		end

		end
	end
	perform_action()

	if play_sound then
		--local wwise_world = stingray.Wwise.wwise_world(SimpleProject.world)
		--local is_playing = stingray.WwiseWorld.is_playing(wwise_world, "Spawn_Event")
		--if not is_playing and audio_on then
		--	stingray.WwiseWorld.set_global_parameter(wwise_world, "bus_Volume", volume)
		--	stingray.WwiseWorld.trigger_event(wwise_world, "sfx_spawn_sound")
		--else
		--	prevVolume = volume
		--end
	else
		--local wwise_world = stingray.Wwise.wwise_world(SimpleProject.world)
		--local is_playing = stingray.WwiseWorld.is_playing(wwise_world, "sfx_spawn_sound")
		--if is_playing then
		--	stingray.WwiseWorld.stop_event(wwise_world, "sfx_spawn_sound")
		--end
	end
end

return MainMenu
