local fragments_per_ingot = 4

minetest.register_lbm {
	label = "delete duranium ore again";
	name = "sorcery:delete_duranium_ore_again";
	nodenames = {'sorcery:stone_with_duranium'};
	action = function(pos,node)
		minetest.set_node(pos, {name = 'default:stone'})
	end
}

sorcery.data.alloys = {}
sorcery.data.kilnrecs = {}
sorcery.data.metallookup = {
	-- compat bullshit
	['moreores:silver_ingot'] = {
		id = 'silver'; data = sorcery.data.metals.silver;
		value = fragments_per_ingot;
	};
	['moreores:silver_block'] = {
		id = 'silver'; data = sorcery.data.metals.silver;
		value = fragments_per_ingot * 9;
	};

	['basic_materials:brass_ingot'] = {
		id = 'brass'; data = sorcery.data.metals.brass;
		value = fragments_per_ingot;
	};
	['basic_materials:brass_block'] = {
		id = 'brass'; data = sorcery.data.metals.brass;
		value = fragments_per_ingot * 9;
	};
	['morelights_vintage:brass_ingot'] = {
		id = 'brass'; data = sorcery.data.metals.brass;
		value = fragments_per_ingot;
	};
	['morelights_vintage:brass_block'] = {
		id = 'brass'; data = sorcery.data.metals.brass;
		value = fragments_per_ingot * 9;
	};
}

local tools, armors = sorcery.matreg.tools, sorcery.matreg.armors
for name, metal in pairs(sorcery.data.metals) do
	local ingot = metal.ingot or 'sorcery:' .. name .. '_ingot'
	local block = metal.block or 'sorcery:' .. name .. 'block'
	local screw = 'sorcery:screw_' .. name
	local fragment = 'sorcery:fragment_' .. name
	local powder = 'sorcery:powder_' .. name
	metal.parts = {
		ingot = ingot;
		block = block;
		screw = screw;
		fragment = fragment;
		powder = powder;
	}
	if not metal.no_tools then for t,c in pairs(tools) do
		sorcery.matreg.lookup[(metal.items and metal.items[t]) or ('sorcery:' .. t .. '_' .. name)] = {
			metal = true;
			id = name; data = metal;
			value = c.cost * fragments_per_ingot;
		}
	end end
	if not metal.no_armor then for a,c in pairs(armors) do
		sorcery.matreg.lookup[(metal.items and metal.items[a]) or ('sorcery:' .. a .. '_' .. name)] = {
			metal = true;
			id = name; data = metal;
			value = c.cost * fragments_per_ingot;
		}
	end end
	sorcery.data.metallookup[ingot] = {
		id = name; data = metal;
		value = fragments_per_ingot;
	}
	sorcery.data.metallookup[block] = {
		id = name; data = metal;
		value = fragments_per_ingot * 9;
	}
	sorcery.data.metallookup[fragment] = {
		id = name; data = metal;
		value = 1;
	}
	sorcery.data.metallookup[screw] = {
		id = name; data = metal;
		value = 0; -- prevent use in smelting
	}
	minetest.register_craftitem(screw, {
		description = sorcery.lib.str.capitalize(name) .. ' Screw';
		inventory_image = sorcery.lib.image('sorcery_screw.png'):multiply(sorcery.lib.color(metal.tone)):render();
	})
	minetest.register_craftitem(powder, {
		description = sorcery.lib.str.capitalize(name) .. ' Powder';
		inventory_image = 'sorcery_' .. name .. '_powder.png';
	})
	if metal.dye then
		minetest.register_craft {
			output = 'dye:' .. metal.dye .. ' 4';
			recipe = {
				{'',     powder,                   ''};
				{powder,'basic_materials:paraffin',powder};
				{'','bucket:bucket_water',''};
			};
			replacements = {
				{'bucket:bucket_water', 'bucket:bucket_empty'};
			};
		};
	end
	-- TODO: replace crafting recipe with kiln recipe
	minetest.register_craft {
		output = screw.. ' 4';
		recipe = {
			{fragment,fragment,fragment};
			{'',      fragment,''};
			{'',      fragment,''};
		};
	}
	if not sorcery.compat.defp(ingot) then
		-- TODO: remove instant_ores dependency
		instant_ores.register_metal {
			name = 'sorcery:' .. name;
			description = sorcery.lib.str.capitalize(name);
			color = sorcery.lib.color(metal.tone):hex() .. ':' .. ((metal.alpha and tostring(metal.alpha)) or '45');
			rarity = metal.rarity;
			depth = metal.depth;
			no_armor = metal.no_armor;
			no_tools = metal.no_tools;
			durability = metal.durability;
			power = metal.power;
			speed = metal.speed;
			artificial = metal.artificial;
			cooktime = metal.cooktime;
			hardness = metal.hardness;
			ingot_image = (metal.image and metal.image.ingot) or nil;
			lump_image = (metal.image and metal.image.lump) or nil;
			armor_weight = metal.armor_weight;
			armor_protection = metal.armor_protection;
		}
	end
	minetest.register_craftitem(fragment, {
		inventory_image = 'sorcery_' .. name .. '_fragment.png';
		description = sorcery.lib.str.capitalize(name) .. ' Fragment';
	})
	minetest.register_craft {
		type = 'cooking';
		recipe = powder;
		cooktime = (metal.cooktime or 4) * 1.5;
		output = fragment;
	}
	minetest.register_craft {
		type = 'cooking';
		recipe = ingot;
		cooktime = (metal.cooktime or 4) / 2;
		output = fragment .. ' ' .. tostring(fragments_per_ingot);
	}
	do local rec = {}
		for i=1,fragments_per_ingot do
			rec[#rec+1]=fragment 
		end
		minetest.register_craft {
			type = 'shapeless';
			recipe = rec;
			output = ingot;
		}
	end
	if metal.fuel then
		minetest.register_craft {
			type = 'fuel';
			recipe = powder;
			burntime = metal.fuel;
		}
	end
	if metal.mix then
		sorcery.data.register.alloy(sorcery.lib.tbl.merge(metal.mix, {
			output = name;
			cooktime = metal.cooktime or 10;
		}))
	end
	if metal.sinter then
		local powders = {}
		for _,m in pairs(metal.sinter) do
			powders[#powders+1] = 'sorcery:powder_' .. m
		end
		minetest.register_craft {
			type = 'shapeless';
			output = powder .. ' ' .. tostring(#powders);
			recipe = powders;
		};
	end
end
