local WS_OnIinitWorld = CharacterCreationProfession.initWorld

function WS_OnIinitWorld()
	print("||| WILDERNESS SURVIVAL SPAWN START |||")

	local spawnRegion = MapSpawnSelect.instance.selectedRegion

	if not (spawnRegion.name == "WS__Random") then
		print('||| Debug IGNORING WS__Random and using '..tostring(spawnRegion.name..'|||'))
		return
	end

	local DEBUG_SPWN = spawnRegion.points["DEBUG_SPWN"]
	local V_EASY = spawnRegion.points["V_EASY"]
	local V_NORMAL = spawnRegion.points["V_NORMAL"]
	local V_HARD = spawnRegion.points["V_HARD"]
	local C_EASY = spawnRegion.points["C_EASY"]
	local C_NORMAL = spawnRegion.points["C_NORMAL"]
	local C_HARD = spawnRegion.points["C_HARD"]

	local IsDifficulty = SandboxVars.WSSpawn.Diff
	local IsCabins = SandboxVars.WSSpawn.Cabins

	-- DEBUG SECTION

	local function DebugPrint(tbl, multi, name)
		print("||| WILDERNESS SURVIVAL SPAWN :: DEBUG PRINT START |||")

		local DiffName = nil
		if IsDifficulty == 1 then
			DiffName = "All"
		elseif IsDifficulty == 2 then
			DiffName = "Easy"
		elseif IsDifficulty == 3 then
			DiffName = "Easy & Normal"
		elseif IsDifficulty == 4 then
			DiffName = "Normal"
		elseif IsDifficulty == 5 then
			DiffName = "Normal & Hard"
		elseif IsDifficulty == 6 then
			DiffName = "Hard"
		end

		local Cabins = nil
		if IsCabins == 1 then
			Cabins = "enabled"
		elseif IsCabins == 2 then
			Cabins = "the only spawn"
		elseif IsCabins == 3 then
			Cabins = "disabled"
		end

		print(">>> Difficulty : "..DiffName)
		print(">>> Cabins are "..Cabins)
		if tbl ~= nil then
			if multi == true then
				local nb = ""
				if #tbl == 1 then
					nb = "is"
				else
					nb = "are"
				end
				print(">>> "..#tbl.." Spawn locations "..nb.." being called from "..name.." table")

				for i, d in ipairs(tbl) do
					print(">>> "..i.."/"..#tbl)

					for k, v in pairs(d) do
						print('\t',k, v)
					end
				end
			else
				print(">>> 1 spawn locations is being called from "..name.." table")
				for k, v in pairs(tbl) do
						print('\t',k, v)
					end
			end
		else
			print(">>> "..name.." is empty")
		end
		print("||| WILDERNESS SURVIVAL SPAWN :: DEBUG PRINT END |||")
	end

  	-- CALL SPAWN
	local CallSpwn = {}

	local function WPSpawn()
		if IsDifficulty ~= nil then
			-- ALL
			if IsDifficulty == 1 then
				if IsCabins >= 1 and IsCabins <= 2 then
					for _, v in ipairs(C_EASY) do
						table.insert(CallSpwn, v)
					end
					for _, v in ipairs(C_NORMAL) do
						table.insert(CallSpwn, v)
					end
					for _, v in ipairs(C_HARD) do
						table.insert(CallSpwn, v)
					end
				end
				if IsCabins ~= 2 then
					for _, v in ipairs(V_EASY) do
						table.insert(CallSpwn, v)
					end
					for _, v in ipairs(V_NORMAL) do
						table.insert(CallSpwn, v)
					end
					for _, v in ipairs(V_HARD) do
						table.insert(CallSpwn, v)
					end
				end
			end
			-- EASY
			if IsDifficulty >= 2 and IsDifficulty <= 3 then
				if IsCabins >= 1 and IsCabins <= 2 then
					for _, v in ipairs(C_EASY) do
						table.insert(CallSpwn, v)
					end
				end
				if IsCabins ~= 2 then
					for _, v in ipairs(V_EASY) do
						table.insert(CallSpwn, v)
					end
				end
				-- EASY & NORMAL
				if IsDifficulty == 3 then
					if IsCabins >= 1 and IsCabins <= 2 then
						for _, v in ipairs(C_NORMAL) do
							table.insert(CallSpwn, v)
						end
					end
					if IsCabins ~= 2 then
						for _, v in ipairs(V_NORMAL) do
							table.insert(CallSpwn, v)
						end
					end
				end
			end
			-- NORMAL
			if IsDifficulty >= 4 and IsDifficulty <= 5 then
				if IsCabins >= 1 and IsCabins <= 2 then
					for _, v in ipairs(C_NORMAL) do
						table.insert(CallSpwn, v)
					end
				end
				if IsCabins ~= 2 then
					for _, v in ipairs(V_NORMAL) do
						table.insert(CallSpwn, v)
					end
				end
				-- NORMAL & HARD
				if IsDifficulty == 5 then
					if IsCabins >= 1 and IsCabins <= 2 then
						for _, v in ipairs(C_HARD) do
							table.insert(CallSpwn, v)
						end
					end
					if IsCabins ~= 2 then
						for _, v in ipairs(V_HARD) do
							table.insert(CallSpwn, v)
						end
					end
				end
			end
			-- HARD
			if IsDifficulty == 6 then
				if IsCabins >= 1 and IsCabins <= 2 then
					for _, v in ipairs(C_HARD) do
						table.insert(CallSpwn, v)
					end
				end
				if IsCabins ~= 2 then
					for _, v in ipairs(V_HARD) do
						table.insert(CallSpwn, v)
					end
				end
			end
		else
			CallSpwn = DEBUG_SPWN
		end
		-- DebugPrint(CallSpwn, true, "CallSpwn")
		return CallSpwn
	end

	-- RANDOMIZE SPAWN TABLE
	local SpawnTbl = WPSpawn()
	local spwn = {}

	local function RandomSpwn()
		if #SpawnTbl >= 1 then
			if #SpawnTbl == 1 then
				DebugPrint(SpawnTbl, false, "#SpawnTbl == 1")
				spwn = SpawnTbl[1]
			else
				DebugPrint(SpawnTbl, true, "#SpawnTbl ~= 1")
				spwn = SpawnTbl[(ZombRand(#SpawnTbl)+1)]
			end
		elseif SpawnTbl == nil or #SpawnTbl == nil then
			print("SpawnTbl is returning nil >>> DEBUG_SPWN sent")
			spwn = DEBUG_SPWN[1]
		end
		-- DebugPrint(spwn, false, "spwn returned from RandomSpwn")
		return spwn
	end

	local Spawn = RandomSpwn()
	DebugPrint(Spawn, false, "Spawn")

	-- SET SPAWNPOINT
	getWorld():setLuaPosX(Spawn.posX)
	getWorld():setLuaPosY(Spawn.posY)
	getWorld():setLuaPosZ(Spawn.posZ)

	print("||| WILDERNESS SURVIVAL SPAWN END :: SUCCESS |||")
end

Events.OnInitWorld.Add(WS_OnIinitWorld)