--[[
	Ce mod permet de bloquer les craft des quarry.
	Les quarry peuvent être débloquées en débloquant certains awards
	
	Liste des awards qui débloquent le craft de quarry (Les awards avec "*" sont à définir):
	award_mine3 (Hardened Miner - dig 1000 default:stone)--> 1 Quarry
	*award_iron_miner (Iron Miner - dig 100 default:stone_with_iron)--> 2 Quarry
	*award_copper_miner (Copper Miner - dig 100 default:stone_with_copper)--> 4 Quarry
	*award_gold_miner (Gold Digger - dig 100 default:stone_with_gold)--> 5 Quarry
	award_mine4 (MasterMiner - dig 10000 default:stone)--> 1 Super Quarry
	*award_iron_master(Iron Man - dig 1000 default:stone_with_iron)--> 1 Super Quarry
	award_youre_a_copper (You're a copper - dig 1000 default:stone_with_copper)--> 1 super Quarry
	*award_gold_master (Gold Master - dig 1000 default:stone_with_gold)--> 1 Super Quarry
]]--

if minetest.get_modpath("awards") then

	-- Redéfinition de la description d'awards existant
	local award = awards.registered_awards["award_mine3"]
	award.description = award.description.." (Unlock the craft of 1 Quarry)"

	award = awards.registered_awards["award_mine4"]
	award.description = award.description.." (Unlock the craft of 1 Super Quarry)"

	award = awards.registered_awards["award_youre_a_copper"]
	award.description = award.description.." (Unlock the craft of 1 Super Quarry)"
	
	-- Définitions de nouveaux awards --
	-- Iron Lady
	awards.register_award(
		"award_iron_miner", {
			title = "The Iron Lady",
			description = "Dig 100 iron ores. (Unlock the craft of 2 Quarry)",
			icon = "default_iron_lump.png^awards_level1.png",
			background = "awards_bg_mining.png",
			difficulty = 0.03,
			trigger = {
				type = "dig",
				node = "default:stone_with_iron",
				target = 100
			}
	})

	-- Iron Man
	awards.register_award(
		"award_iron_master", {
			title = "The Iron Man",
			description = "Dig 1,000 iron ores. (Unlock the craft of 1 Super Quarry)",
			icon = "default_iron_lump.png^awards_level2.png",
			background = "awards_bg_mining.png",
			difficulty = 0.04,
			trigger = {
				type = "dig",
				node = "default:stone_with_iron",
				target = 1000
			}
	})

	-- Copper Trooper
	awards.register_award(
		"award_copper_miner", {
			title = "Copper Trooper",
			description = "Dig 100 copper ores. (Unlock the craft of 4 Quarry)",
			icon = "default_copper_lump.png^awards_level1.png",
			background = "awards_bg_mining.png",
			difficulty = 0.19,
			trigger = {
				type = "dig",
				node = "default:stone_with_copper",
				target = 100
			}
	})

	-- Gold Digger
	awards.register_award(
		"award_gold_miner", {
			title = "Gold Digger",
			description = "Dig 100 gold ores. (Unlock the craft of 5 Quarry)",
			icon = "default_gold_lump.png^awards_level3.png",
			background = "awards_bg_mining.png",
			difficulty = 0.91,
			trigger = {
				type = "dig",
				node = "default:stone_with_gold",
				target = 100
			}
	})

	-- The Golden Age
	awards.register_award(
		"award_gold_master", {
			title = "The Golden Age",
			description = "Dig 1,000 gold ores. (Unlock the craft of 1 Super Quarry)",
			icon = "default_gold_lump.png^awards_level4.png",
			background = "awards_bg_mining.png",
			difficulty = 0.92,
			trigger = {
				type = "dig",
				node = "default:stone_with_gold",
				target = 1000
			}
	})

	-- New Waste Node
	minetest.register_node(
		":nalc:waste",
		{
			description = "Waste",
			tiles = {"waste.png"},
			is_ground_content = false,
			groups = {crumbly=2, flammable=2},
		})

	local limited_crafts = {
		["technic:quarry"] = {
			award_mine3 = 1,
			award_iron_miner = 2,
			award_copper_miner = 4,
			award_gold_digger = 5
		},
		["nalc:superquarry"] = {
			award_mine4 = 1,
			award_iron_master = 1,
			award_youre_a_copper = 1,
			award_gold_master = 1
		}
	}

	-- Ajout des limited_crafts dans le group limitcraft
	for item,_ in pairs(limited_crafts) do
		nalc.add_group(item, "limitcraft", 1)
	end

	local function authorized_craft(items_crafted, levels, u_awards)
		local nb = 0
		for award, count in pairs(levels) do
			if u_awards[award] then
				nb = nb + count
			end
		end
		
		return items_crafted < nb
	end

	-- Customize register_on_craft
	minetest.register_on_craft(
		function(itemstack, player, old_craft_grid, craft_inv)
			if not player or itemstack:is_empty() then
				return
			end

			local itemname = itemstack:get_name()
			itemname = minetest.registered_aliases[itemname] or itemname

			if player and player.is_player and player:is_player() then
				local playern = player:get_player_name()
				local data = awards.player(playern)
				local levels = limited_crafts[itemname]

				if levels then
					local items_crafted = data.craft[itemname] or 0
					if not authorized_craft(items_crafted, levels, data.unlocked) then
						minetest.chat_send_player(playern, "Vous n'avez pas assez d'expérience pour fabriquer cet article ! À la place, vous obtenez un bloc de déchets. / You don't have enough experience to make this item! Instead, you get a block of waste.")
						return ItemStack("nalc:waste")
					end
				end
			end

			awards.notify_craft(player, itemname, itemstack:get_count())
		end
	)
end

minetest.log("action", "[nalc_awards] loaded.")
