local S = minetest.get_translator("map_making_toolkit")





local function highlight_volume(pos1, pos2)
	for i = pos1.x, pos2.x, (pos1.x < pos2.x and 1 or -1)
	do
		for ii = pos1.y, pos2.y, (pos1.y < pos2.y and 1 or -1)
		do
			for iii = pos1.z, pos2.z, (pos1.z < pos2.z and 1 or -1)
			do
				minetest.add_particle(
					{
						pos = {x = i, y = ii, z = iii},
						texture = "smoke_puff_white.png",
						expirationtime = 30,
						size = 10,
						glow = 14,
					})
			end
		end
	end
end


local function highlight_cmd_func(name, param)

	local worked, pos1, offset = map_making_toolkit.parse_args(param)
	if not worked or offset.x == 0 or offset.y == 0 or offset.z == 0
	then
		return true, S("Invalid coordinates or offset")
	end

	local pos2 =
	{
		x = pos1.x + offset.x * 16,
		y = pos1.y + offset.y * 16,
		z = pos1.z + offset.z * 16
	}

	highlight_volume({x = pos1.x, y = pos1.y, z = pos1.z},
		{x = pos2.x, y = pos1.y, z = pos1.z})
	highlight_volume({x = pos1.x, y = pos1.y, z = pos1.z},
		{x = pos1.x, y = pos2.y, z = pos1.z})
	highlight_volume({x = pos1.x, y = pos1.y, z = pos1.z},
		{x = pos1.x, y = pos1.y, z = pos2.z})

	highlight_volume({x = pos2.x, y = pos2.y, z = pos1.z},
		{x = pos2.x, y = pos2.y, z = pos2.z})
	highlight_volume({x = pos2.x, y = pos2.y, z = pos1.z},
		{x = pos2.x, y = pos1.y, z = pos1.z})
	highlight_volume({x = pos2.x, y = pos2.y, z = pos1.z},
		{x = pos1.x, y = pos2.y, z = pos1.z})

	highlight_volume({x = pos1.x, y = pos2.y, z = pos2.z},
		{x = pos2.x, y = pos2.y, z = pos2.z})
	highlight_volume({x = pos1.x, y = pos2.y, z = pos2.z},
		{x = pos1.x, y = pos1.y, z = pos2.z})
	highlight_volume({x = pos1.x, y = pos2.y, z = pos2.z},
		{x = pos1.x, y = pos2.y, z = pos1.z})

	highlight_volume({x = pos2.x, y = pos1.y, z = pos2.z},
		{x = pos2.x, y = pos2.y, z = pos2.z})
	highlight_volume({x = pos2.x, y = pos1.y, z = pos2.z},
		{x = pos1.x, y = pos1.y, z = pos2.z})
	highlight_volume({x = pos2.x, y = pos1.y, z = pos2.z},
		{x = pos2.x, y = pos1.y, z = pos1.z})

	return true, S("The highlighted box would be saved as @1 schematics" ..
		" with size 16x16x16 each", offset.x * offset.y * offset.z)
end

minetest.register_chatcommand("highlightmapbounds",
	{
		params = S("<x> <y> <z> <offset x> <offset y> <offset z>"),
		privs = {server = true},
		description = S("Highlights the edges of a box specified in the arguments.\n"..
			"Offset is in map blocks."),
		func = highlight_cmd_func
	})
