local modpath = minetest.get_modpath("story")
local currentPage = 0
storyText = {}
storyText[0] = "Click next to continue. Do NOT close the formspec by pressing the Escape key, or the game may break. To my knowledge, this is impossible to solve with the current Lua API."
storyText[1] = "Your name is Vyacheslav Mikhaylov and you live in the Eastern European country of Absurdistan..."
storyText[2] = "The government of your country has a long history of corruption and oppressing its citizens."
storyText[3] = "It didn't bother you much until last week when you were watching the news on TV..."
storyText[4] = "The government started forcefully drafting people into the military."
storyText[5] = "You were among the first people to get their draft notice in the mail."
storyText[6] = "You were devastated when you realized that you'll be sent away to die in a war that doesn't even concern you."
storyText[7] = "\"I've got nothing to lose at this point, how about I teach them a lesson.\", you thought to yourself."
storyText[8] = "Click \"Next\" to generate the world."
story = {}

function getStoryFormspec(number)
	storyFormspec =
	{
	"formspec_version[4]",
	"size[18, 16]",
	"bgcolor[#3A41EA]",
	"position[0.5, 0.5]",
	"style_type[button;bgcolor=#1010FF;textcolor=yellow]",
	"hypertext[1, 12.5; 12, 5;;<global halign=center color=yellow size=20 font=Regular>" .. storyText[number] .. "]\n",
	"image[1, .2; 16, 12;" .. number .. ".png]",
	"button[13, 12.3; 4, 1;back;Back]",
	"button[13, 13.6; 4, 1;next;Next]",
	"button[13, 14.9; 4, 1;exit;Quit]"
	}
	return storyFormspec
end



minetest.register_on_joinplayer(function(player)
	
	
	player:set_physics_override({speed = 2.5, jump = 1, gravity = 1.2})
		
	minetest.show_formspec(player:get_player_name(), "story:story_formspec", table.concat(getStoryFormspec(0), "\n"))
	music = minetest.sound_play("4K", {
        gain = 0, 
		pitch = 1,
        loop = true,
    })
	
	minetest.sound_fade(music, .1, 1)
	
	player:get_inventory():add_item("main", "more_fire:pipebomb 48")
	player:get_inventory():add_item("main", "more_fire:molotov_cocktail 48")
	minetest.setting_set("time_speed", 0)
	minetest.set_timeofday(.2)

end)

minetest.register_on_newplayer(function(ObjectRef)
	minetest.place_schematic({x = 0, y = -1, z = 0}, minetest.get_modpath("ip_story") .. "/schems/citymap-v3.mts", "0", nil, true)
	minetest.after(4, function() minetest.fix_light({x = 20, y = 129, z = 191}, {x = 236, y = 12, z = 6}) end)
	ObjectRef:set_pos({x = 185, y = 24, z = 152})
end)

minetest.register_on_respawnplayer(function(player)
	minetest.after(.1, function()
		player:set_pos({x = 185, y = 24, z = 152})
	end)
end)

minetest.register_on_player_receive_fields(function(player, formname, fields)
    if formname ~= "story:story_formspec" then
        return
    end

	if formname == "story:story_formspec" then	
		if fields.next then
			if currentPage >= 8 then
				minetest.close_formspec(player:get_player_name(), "story:story_formspec")
				return
			end
			currentPage = currentPage + 1 -- Fuck Lua and its lack of arithmetic assignment operators
			minetest.show_formspec(player:get_player_name(), "story:story_formspec", table.concat(getStoryFormspec(currentPage), "\n"))
			
		elseif fields.back and currentPage >= 0 then
			currentPage = currentPage - 1 -- Fuck Lua and its lack of arithmetic assignment operators
			minetest.show_formspec(player:get_player_name(), "story:story_formspec", table.concat(getStoryFormspec(currentPage), "\n"))
		
		elseif fields.exit then
			--minetest.kick_player(player:get_player_name(), "Fuck you in particular")
			minetest.request_shutdown("The game has crashed due to the citizen's neglience.\n This incident will be reported.")
		
		elseif fields.quit then
			minetest.after(.2, function() minetest.show_formspec(player:get_player_name(), "story:story_formspec", table.concat(getStoryFormspec(currentPage), "\n")) end)
		end
	end
end)


