local repairable_hashtable
local repairing_goal =  minetest.get_content_id("degradable_walls:wall")
local floor = math.floor
local ceil = math.ceil
local sound_play = minetest.sound_play

local S = minetest.get_translator("degradable_walls")

local radius = 3
local voxel_buffer = {}
minetest.register_craftitem("degradable_walls:mortar",
{
	description = S("Mortar"),
	inventory_image = "degradable_walls_mortar.png",
	max_stack = 128,
	on_use = function(itemstack, user, pointed_thing)
		if pointed_thing.type == "node"
		then
			local pos = pointed_thing.under
			local vm = minetest.get_voxel_manip()
			local minpos = {
				x = floor(pos.x - radius),
				y = floor(pos.y - radius),
				z = floor(pos.z - radius)
				}
			local maxpos = {
				x = ceil(pos.x + radius),
				y = ceil(pos.y + radius),
				z = ceil(pos.z + radius)
				}
	
			local emin, emax = vm:read_from_map(minpos, maxpos)
			local area = VoxelArea:new{MinEdge = emin, MaxEdge = emax}
			local data = vm:get_data(voxel_buffer)
			
			for iz = minpos.z, maxpos.z
			do
				for iy = minpos.y, maxpos.y
				do
					for ix = minpos.x, maxpos.x
					do
						local position_vector = {x = ix, y = iy, z = iz}
						local dist = vector.distance(pos, position_vector)
						
						if dist < radius and
							repairable_hashtable[data[area:index(ix, iy, iz)]]
						then
							data[area:index(ix, iy, iz)] = repairing_goal
						end
					end
				end
			end
			vm:set_data(data)
			vm:write_to_map(true)
			
			sound_play("degradable_walls_mortar_application",
					{
						gain = 0.5,
						pitch = 1 + (math.random() - 0.5) / 8
					})
			
			itemstack:take_item()
			return itemstack
		end
	end
})
minetest.register_alias("mortar", "degradable_walls:mortar")
local cobble_name = minetest.registered_aliases["cobble"]
assert(type(cobble_name) == "string", "There is no node with alias " ..
	"'cobble' registered.")


minetest.register_node("degradable_walls:mortar_and_pestle",
	{
		description = S("Mortar And Pestle\n@1",
			minetest.colorize("#90FFFF", S("Right click with cobble to make mortar for repairing walls"))),
		inventory_image = "degradable_walls_mortar_and_pestle_inv.png",
		drawtype = "mesh",
		mesh = "degradable_walls_mortar_and_pestle.obj",
		tiles =
		{
			"degradable_walls_mortar_and_pestle_m.png",
			"degradable_walls_mortar_and_pestle_p.png"
		},
		
		collision_box = 
		{
			type = "fixed",
			fixed = {
				{-0.5, -0.5, -0.5, 0.5, 0, 0.5},
				
			},
		},
		selection_box = 
		{
			type = "fixed",
			fixed = {
				{-0.5, -0.5, -0.5, 0.5, 0, 0.5},
				
			},
		},
		is_ground_content = false,
		groups = {cracky = 2, stone = 1},
		sounds = generic_media.node_sound_stone_defaults(),
		stack_max = 1,
		on_rightclick = function(pos, node, clicker, itemstack)
			local itemname = itemstack:get_name()
			if itemname == cobble_name and clicker
			then
				--TODO: play sound
				local inv = clicker:get_inventory()
				local leftover = inv:add_item("main", "degradable_walls:mortar")
				if leftover:is_empty()
				then
					itemstack:take_item()
					sound_play("degradable_walls_mortar_use",
						{
							gain = 0.2,
							pitch = 1 + (math.random() - 0.5) / 16
						})
					return itemstack
				end
			end
		end,
	})
minetest.register_alias("mortar_and_pestle", "degradable_walls:mortar_and_pestle")
crafting.register_recipe(
	{
		output = "mortar_and_pestle",
		type = "inv",
		items = {"cobble 8", "stick 1"},
		always_known = true,
	})

return function(h)
		repairable_hashtable = h
	end
