local S = minetest.get_translator("the_end_game_master")
local intramod = the_end_game_master.intramod



intramod.huds = {}

local function set_pregame_hud(player)

	local name = player:get_player_name()

	local hud = intramod.huds[name]
	if not hud
	then
		hud = {}
		intramod.huds = hud
	end
	table.insert(hud,
		player:hud_add(
			{
				hud_elem_type = "image",
				position = {x = 0.5, y = 0.5},
				scale = {x = -100, y = -100},
				name = "pregame_background",
				text = "the_end_game_master_pregame_background.png",
			})
		)
	table.insert(hud,
		player:hud_add(
			{
				hud_elem_type = "image",
				position = {x = 0.5, y = 0.15},
				scale = {x = -40, y = -15},
				name = "pregame_logo",
				text = "the_end_game_master_header.png",
			})
		)
	table.insert(hud,
		player:hud_add(
			{
				hud_elem_type = "text",
				position = {x = 0.5, y = 0.75},
				name = "pregame_inv_hint",
				text = S("Open your inventory to start"),
				number = 0xFFFFFF
			})
		)

	intramod.huds[name] = hud
	player:hud_set_flags(
		{
			hotbar = false,
			healthbar = false,
			crosshair = false,
			wielditem = false,
			breathbar = false,
		})
end
minetest.register_on_leaveplayer(function(player)
		intramod.huds[player:get_player_name()] = nil
	end)

local ready_players = {}

local function set_pregame_inv(player)

	local playername = player:get_player_name()
	local ready_affecting_button = ready_players[playername] and
		"button[4.5,5;3,1;vote_not_ready;" ..
			minetest.formspec_escape(S("No longer Ready")) .. "]" or
		"button[4.5,5;3,1;vote_ready;" ..
			minetest.formspec_escape(S("Ready")) .. "]"


	local formspec = "size[12,8]" ..
		"real_coordinates[true]" ..
		ready_affecting_button..
		--"button[1,6;3,1;choose_appearance;Choose Appearance]" ..
		--"button[4.5,6;3,1;choose_beard;Choose Beard]" ..
		"button[4.5,3;3,1;guide;" .. S("Guide") .. "]"


	player:set_inventory_formspec(formspec)
	return formspec
end
local function show_404_page(player)
	local formspec = "size[12,12]" ..
		"real_coordinates[true]" ..
		"image[0,0.5;12,2;the_end_game_master_header.png]" ..
		"label[2,6;404 Error: This page does not exist and is propably under construction]"

	minetest.show_formspec(player:get_player_name(),
		"the_end_game_master:404_page",
		formspec)
end

local music = {}

local function start_pregame_music(player)
	local name = player:get_player_name()
	music[name] = minetest.sound_play("mood_music_level1",
		{
			to_player = name,
			loop = true,
		})
end
function the_end_game_master.stop_pregame_music(player)
	local name = player:get_player_name()
	if music[name]
	then
		minetest.sound_stop(music[name])
		music[name] = nil
	end
end
minetest.register_on_leaveplayer(function(player)
		music[player:get_player_name()] = nil
	end)


local function vote_ready(player)
	local name = player:get_player_name()
	ready_players[name] = true

	local player_count = #minetest.get_connected_players()

	local ready_count = 0
	for _, _ in pairs(ready_players)
	do
		ready_count = ready_count + 1
	end

	local ready_string = S("@1 is ready!\n"..
		"@2 / @3 players are ready.", name, ready_count, player_count)

	if ready_count >= player_count * 0.75 --start game
	then
		minetest.chat_send_all(ready_string .. S("\nLoading map..."))

		the_end_game_master.start_game()
		return
	end
	minetest.chat_send_all(ready_string)
	set_pregame_inv(player)

end

local function vote_not_ready(player)
	local name = player:get_player_name()
	ready_players[name] = nil

	local player_count = #minetest.get_connected_players()

	local ready_count = 0
	for _, _ in pairs(ready_players)
	do
		ready_count = ready_count + 1
	end


	minetest.chat_send_all(S("@1 is no longer ready.\n@2 / @3 players are ready.",
		name, ready_count, player_count))
	set_pregame_inv(player)
end
minetest.register_on_leaveplayer(vote_not_ready)




minetest.register_on_player_receive_fields(function(player, formname, fields)
		local stage = the_end_game_master.get_stage()
		if formname ~= "" or sfinv.enabled or not (stage == "pregame" or stage == "loading")
		then
			return false
		end
		if fields.vote_ready
		then
			vote_ready(player)

		elseif fields.vote_not_ready
		then
			vote_not_ready(player)

		elseif fields.choose_appearance
		then
			show_404_page(player)

		elseif fields.choose_beard
		then
			show_404_page(player)

		elseif fields.guide
		then
			in_game_guide.show_root_page(player:get_player_name(), "return_to_pregame_inv")
		end
		return false
	end)
minetest.register_on_player_receive_fields(function(player, formname, fields)
		local stage = the_end_game_master.get_stage()
		if fields.return_to_pregame_inv and (stage == "pregame" or stage == "loading")
		then
			minetest.show_formspec(player:get_player_name(),
				"", set_pregame_inv(player))

			return true
		end
		return false
	end)


local function clear_inv(player)
	local inv = player:get_inventory()
	if inv
	then
		inv:set_list("hand", {ItemStack("limbo_respawning:limbohand")})
		inv:set_list("main", {})
	end
end

local function set_pregame_physics(player)
	player:set_physics_override(
		{
			speed = 0,
			jump = 0,
			gravity = 0,
			sneak = false,
		})
end


local function set_player_to_pregame(player)
	set_pregame_hud(player)
	set_pregame_inv(player)
	clear_inv(player)
	set_pregame_physics(player)
	start_pregame_music(player)
end

function the_end_game_master.set_pregame()
	the_end_game_master.set_stage("pregame")
	ready_players = {}
	--move players to lobby box
	movable_spawnpoint.set_spawnpoint({x = 0, y = -24, z = 0})
	in_game_guide.set_root_page("the_end_gameplay_guide_master:home")
	in_game_guide.set_root_page_back_button_name("return_to_pregame_inv")

	--TODO: give options like appearance editor and beard/kit chooser
	--remove status effects

	mob_spawning_director.disable()
	sfinv.enabled = false



end



minetest.register_on_joinplayer(function(player)
		local stage = the_end_game_master.get_stage()
		if stage == "pregame" or stage == "loading"
		then
			movable_spawnpoint.spawn_player(player)
			set_player_to_pregame(player)
		end
	end)




limbo_respawning.register_on_respawn(function(playername)
		local player = minetest.get_player_by_name(playername)
		local stage = the_end_game_master.get_stage()
		if player
		then
			movable_spawnpoint.spawn_player(player)
			if stage ~= "game"
			then
				set_player_to_pregame(player)
			end
		end
	end)
