--- gemstones
local shards_per_gem = 9

sorcery.register_gem = function(name,gem)
	local itemname = gem.foreign or 'sorcery:gem_' .. name
	local shardname = gem.foreign_shard or 'sorcery:gem_' .. name .. '_shard'
	local amuletname = gem.foreign_amulet or 'sorcery:gem_' .. name .. '_amulet'

	local tools, armors = sorcery.matreg.tools, sorcery.matreg.armors
	if gem.tools then for t,c in pairs(tools) do
		sorcery.matreg.lookup[(gem.items and gem.items[t]) or ('sorcery:' .. t .. '_' .. name)] = {
			gem = true;
			id = name; data = gem;
			value = c.cost*shards_per_gem;
		}
	end end
	if gem.armor then for a,c in pairs(armors) do
		sorcery.matreg.lookup[(gem.items and gem.items[a]) or ('sorcery:' .. a .. '_' .. name)] = {
			gem = true;
			id = name; data = gem;
			value = c.cost*shards_per_gem;
		}
	end end

	if gem.foreign_shard then
		minetest.clear_craft {output=shardname}
	else
		minetest.register_craftitem(shardname, {
			description = sorcery.lib.str.capitalize(name) .. ' shard';
			inventory_image = 'sorcery_gem_' .. name .. '_shard.png';
			groups = { sorcery_shard = 1; };
			_proto = gem;
		})
	end
	if not gem.foreign_amulet then
		minetest.register_craftitem(amuletname, {
			description = sorcery.lib.str.capitalize(name) .. ' amulet';
			inventory_image = sorcery.lib.image('sorcery_amulet.png'):multiply(sorcery.lib.color(gem.tone)):render();
			_proto = {
				id = name;
				data = gem;
			};
		}) 
	end
	minetest.register_craft {
		type = 'shapeless';
		recipe = (minetest.get_modpath('xdecor') and {
			'xdecor:hammer', itemname;
		}) or { itemname };
		output = shardname .. ' 9';
		replacements = {
			{'xdecor:hammer','xdecor:hammer'};
		};
	}
	minetest.register_craft {
		type = 'shapeless';
		recipe = {
			shardname, shardname, shardname;
			shardname, shardname, shardname;
			shardname, shardname, shardname;
		};
		output = itemname;
	};
	minetest.register_craft {
		recipe = {
			{shardname,itemname,shardname};
			{itemname,itemname,itemname};
			{shardname,itemname,shardname};
		};
		output = amuletname;
	};

	-- generate lenses and crafting recipes
	for _, kind in pairs { 'amplifier','rectifier','concave','convex' } do
		local id = 'sorcery:lens_' .. kind .. '_' .. name
		minetest.register_tool(id, {
			inventory_image = sorcery.lib.image('sorcery_lens_overlay_gold.png'):
				blit(sorcery.lib.image('sorcery_lens_' .. kind .. '.png'):
					multiply(sorcery.lib.color(gem.tone):brighten(1.1))):
				render();
			description = sorcery.lib.str.capitalize(name) .. ' ' .. kind .. ' lens';
			groups = { sorcery_enchanting_lens = 1 };
			_proto = {
				gem = name;
				kind = kind;
			};
		})
	end
	do local casing = 'sorcery:fragment_gold'
		minetest.register_craft {
			output = 'sorcery:lens_convex_' .. name;
			recipe = {
				{'',       casing, ''};
				{itemname, casing, itemname};
				{'',       casing, ''};
			};
		}
		minetest.register_craft {
			output = 'sorcery:lens_concave_' .. name;
			recipe = {
				{itemname, casing, itemname};
				{'',       casing, ''};
				{itemname, casing, itemname};
			};
		}
		minetest.register_craft {
			output = 'sorcery:lens_amplifier_' .. name;
			recipe = {
				{itemname, casing, itemname};
				{shardname,casing, shardname};
				{shardname,casing, shardname};
			};
		}
		minetest.register_craft {
			output = 'sorcery:lens_rectifier_' .. name;
			recipe = {
				{shardname,casing, shardname};
				{'basic_materials:silver_wire',casing,'basic_materials:silver_wire'};
				{itemname, casing, itemname};
			};
			replacements = {
				{'basic_materials:silver_wire', 'basic_materials:empty_spool'};
				{'basic_materials:silver_wire', 'basic_materials:empty_spool'};
			};
		}
	end

	if gem.foreign then return false end
	minetest.register_craftitem(itemname, {
		description = sorcery.lib.str.capitalize(name);
		inventory_image = 'sorcery_gem_' .. name .. '.png';
		groups = { sorcery_gem = 1; };
		_proto = gem;
	})
	local tools = gem.tools
	if tools == nil then tools = {
		'group:pickaxe';
		'group:pick'; -- FUCK YOU INSTANT_ORES
		'~default:enchanted_pick_'; -- FUCK YOU XDECOR
	} end

	local ores = gem.ores
	if ores == nil then ores = {
		['default:stone_with_coal'] = 1.0;
		['default:stone_with_iron'] = 0.7;
		['default:stone_with_copper'] = 0.6;
		['default:stone_with_tin'] = 0.8;
		['default:stone_with_gold'] = 1.5;
		--
		['sorcery:stone_with_cobalt'] = 0.9;
		['sorcery:stone_with_tungsten'] = 0.3;
		['sorcery:stone_with_platinum'] = 0.6;
		['sorcery:stone_with_iridium'] = 1.6;
		['sorcery:stone_with_lithium'] = 1.4;
	} end

	for ore,fac in pairs(ores) do
		local drops = minetest.registered_nodes[ore].drop
		local newdrops
		if type(drops) == 'string' then
			newdrops = {
				max_items = 1;
				items = {
					{ rarity = 1; items = { drops }; };
				};
			}
		else newdrops = sorcery.lib.tbl.copy(drops) end
		
		newdrops.max_items = newdrops.max_items + 1
		newdrops.items[#newdrops.items + 1] = {
			rarity = gem.rarity * fac;
			items = { itemname };
		};

		minetest.override_item(ore, {drop = newdrops})
		-- might be possible to just edit it in place, since we're
		-- referring to registered_nodes anyway, but i don't want
		-- to chance it; god knows what's going on under the hood
	end
end

if minetest.get_modpath('xdecor') then
	local og = minetest.registered_tools['xdecor:hammer'].groups
	og.crafttool = 1
	minetest.override_item('xdecor:hammer', {groups = og})
end

minetest.register_on_craft(function(stack,crafter,recipe,grid)
	local ctoolclass = {
		[1] = 200;
		[2] = 100;
		[3] = 50;
		[4] = 10;
		[5] = 5;
	}
	for i=1,grid:get_size('craft') do
		local s = grid:get_stack('craft',i)
		local v = minetest.get_item_group(s:get_name(),'crafttool')
		if v ~= 0 then
			local dmg = 65536 / ctoolclass[v]
			local tool = recipe[i] 
			tool:add_wear(dmg)
			grid:set_stack('craft',i,tool)
		end
	end
	return nil
end)
for g,v in pairs(sorcery.data.gems) do sorcery.register_gem(g,v) end

sorcery.gem = {
	getdrops = function(fac)
		items = {}
		for g,v in pairs(sorcery.data.gems) do
			items[#items + 1] = {
				rarity = gem.rarity * fac;
				items = { itemname };
			};
		end
		return items
	end;
};
-- register gem cutting tools
--   - abrasive powder
--   - hand-drill

-- register gem cuts (cube, sphere, circlet, triangle, rod)
-- cube, sphere, and triangle are used in enchanting. all are used in crafting.
