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.registry.mk('alloys',false)
sorcery.registry.mk('kilnrecs',false)
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
sorcery.register.metals.foreach('sorcery:generate',{},function(name,metal)
	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;
	}
	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();
		groups = {
			metal = 1;
			sorcery_screw = 1;
			sorcery_tech_component = 1;
		};
		_sorcery = {
			material = {
				id = name, data = metal;
				grindcost = 2, grindvalue = 1;
				value = 0.5;
			};
		};
	})
	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.. ' 8';
		recipe = {
			{'',          'xdecor:hammer',''};
			{        fragment,fragment,fragment};
			{'',              fragment,''};
		};
		replacements = {
			{'xdecor:hammer','xdecor:hammer'};
		};
	}
	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/8) * 3; -- scaled wrt diamond
			level = math.ceil(((metal.hardness/8) * 3)) + 1;
			ingot_image = (metal.image and metal.image.ingot) or nil;
			ore_image = 'default_stone.png^sorcery_' .. name .. '_ore.png';
			lump_image = (metal.image and metal.image.lump) or nil;
			armor_weight = metal.armor_weight;
			armor_protection = metal.armor_protection;
		}
	end
	local ti = (metal.image and metal.image.tool)
	for _,t in pairs{'pick','sword'} do
		-- i really need to rip out instant-ores already
		local tid = string.format('sorcery:%s_%s',t,name)
		if minetest.registered_items[tid] then
			minetest.override_item(tid, {
				inventory_image = ti and ti.pick or string.format('sorcery_%s_%s.png',name,t);
			})
		end
	end
	minetest.register_craftitem(fragment, {
		inventory_image = 'sorcery_' .. name .. '_fragment.png';
		description = sorcery.lib.str.capitalize(name) .. ' Fragment';
		groups = {
			metal = 1; sorcery_metal_fragment = 1;
		};
		_sorcery = {
			recipe = {
				canonical = {
					cook = {{ingot}};
				};
			};
		};
	})
	minetest.register_craft {
		type = 'cooking';
		recipe = powder;
		cooktime = (metal.cooktime or 4) * 0.25;
		output = fragment;
	}
	minetest.register_craft {
		type = 'cooking';
		recipe = ingot;
		cooktime = (metal.cooktime or 4) * 0.5;
		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.register.alloys.link(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
		local repl = {}
		if metal.sinter_catalyst then
			for _,m in pairs(metal.sinter_catalyst) do
				powders[#powders+1] = m
				if sorcery.register.residue.db[m] then
					repl[#repl+1] = {m, sorcery.register.residue.db[m]}
				end
			end
		end

		minetest.register_craft {
			type = 'shapeless';
			output = powder .. ' ' .. tostring(#metal.sinter);
			recipe = powders;
			replacements = repl;
		};
	end
end)
