-- Boilerplate to support localized strings if intllib mod is installed.
local S
if minetest.get_modpath("intllib") then
	S = intllib.Getter(minetest.get_current_modname())
else
	S = function(s,a,...)a={a,...}return s:gsub("@(%d+)",function(n)return a[tonumber(n)]end)end
end

local groupdefs = {
	["book"] = S("Books"),
	["vessel"] = S("Vessels"),
	["dye"] = S("Dyes"),
	["stick"] = S("Sticks"),
	["wool"] = S("Wool"),
	["sand"] = S("Sand"),
	["wood"] = S("Wood"),
	["stone"] = S("Stones"),
	["metal"] = S("Metal"),
	["tree"] = S("Tree Trunks"),
	["fence"] = S("Fences"),
	["wall"] = S("Walls"),
	["leaves"] = S("Leaves and Needles"),
	["flower"] = S("Flowers"),
	["sapling"] = S("Saplings"),
	["water"] = S("Water"),
	["lava"] = S("Lava"),
	["coal"] = S("Coal"),
	["water_bucket"] = S("Water buckets"),
	["flora"] = S("Flora"),

	["cracky"] = S("Cracky"),
	["crumbly"] = S("Crumbly"),
	["choppy"] = S("Choppy"),
	["snappy"] = S("Snappy"),
	["bendy"] = S("Bendy"),
	["oddly_breakable_by_hand"] = S("Hand-breakable"),

	["fleshy"] = S("Flesh"),
}

local miscgroups = { "book", "vessel", "dye", "stick", "wool", "sand", "wood", "stone", "metal", "tree", "fence", "wall", "leaves", "flower", "sapling", "water", "lava", "coal", "water_bucket" }

local forced_items = {
	"fire:basic_flame",
	"farming:wheat_8",
	"farming:cotton_8",
	"doors:door_wood_a",
	"doors:door_steel_a",
	"doors:door_glass_a",
	"doors:door_obsidian_glass_a",
	"xpanes:pane_1",
	"xpanes:bar_1",
}

local suppressed_items = {
	"default:water_flowing",
	"default:river_water_flowing",
	"default:lava_flowing",
	"default:dry_grass_2",
	"default:dry_grass_3",
	"default:dry_grass_4",
	"default:dry_grass_5",
	"default:grass_2",
	"default:grass_3",
	"default:grass_4",
	"default:grass_5",
	"default:furnace_active",
	"doors:door_wood",
	"doors:door_steel",
	"doors:door_glass",
	"doors:door_obsidian_glass",
	"doors:door_wood_b",
	"doors:door_steel_b",
	"doors:door_glass_b",
	"doors:door_obsidian_glass_b",
	"doors:gate_wood_open",
	"doors:gate_junglewood_open",
	"doors:gate_acacia_wood_open",
	"doors:gate_aspen_wood_open",
	"doors:gate_pine_wood_open",
	"doors:trapdoor_steel_open",
	"doors:trapdoor_open",
	"doors:hidden",
	"xpanes:pane",
	"xpanes:bar",
}

local hidden_items = {
	"default:cloud",
	"default:dirt_with_grass_footsteps",
}

local item_name_overrides = {
	["screwdriver:screwdriver"] = S("Screwdriver"),
	["carts:cart"] = S("Cart"),
	["fire:basic_flame"] = S("Basic Flame"),
	["farming:wheat_8"] = S("Wheat Plant"),
	["farming:cotton_8"] = S("Cotton Plant"),
	["default:lava_source"] = S("Lava"),
	["default:water_source"] = S("Water"),
	["default:river_water_source"] = S("River Water"),
	["doors:door_wood_a"] = minetest.registered_items["doors:door_wood"].description,
	["doors:door_steel_a"] = minetest.registered_items["doors:door_steel"].description,
	["doors:door_glass_a"] = minetest.registered_items["doors:door_glass"].description,
	["doors:door_obsidian_glass_a"] = minetest.registered_items["doors:door_obsidian_glass"].description,
	["xpanes:pane_1"] = minetest.registered_items["xpanes:pane"].description,
	["xpanes:bar_1"] = minetest.registered_items["xpanes:bar"].description,
}

local image_overrides = {
	["doors:door_wood_a"] = minetest.registered_items["doors:door_wood"].inventory_image,
	["doors:door_steel_a"] = minetest.registered_items["doors:door_steel"].inventory_image,
	["doors:door_glass_a"] = minetest.registered_items["doors:door_glass"].inventory_image,
	["doors:door_obsidian_glass_a"] = minetest.registered_items["doors:door_obsidian_glass"].inventory_image,
	["xpanes:pane_1"] = minetest.registered_items["xpanes:pane"].inventory_image,
	["xpanes:bar_1"] = minetest.registered_items["xpanes:bar"].inventory_image,
}

doc.sub.items.add_friendly_group_names(groupdefs)
doc.sub.items.add_notable_groups(miscgroups)

for i=1, #hidden_items do
	local item = minetest.registered_items[hidden_items[i]]
	if item then
		minetest.override_item(hidden_items[i], { _doc_items_hidden = true } )
	end
end
for i=1, #forced_items do
	local item = minetest.registered_items[forced_items[i]]
	if item then
		minetest.override_item(forced_items[i], { _doc_items_create_entry = true} )
	end
end
for i=1, #suppressed_items do
	local item = minetest.registered_items[suppressed_items[i]]
	if item then
		minetest.override_item(suppressed_items[i], { _doc_items_create_entry = false} )
	end
end
for itemstring, data in pairs(item_name_overrides) do
	if minetest.registered_items[itemstring] ~= nil then
		minetest.override_item(itemstring, { _doc_items_entry_name = data} )
	end
end
for itemstring, data in pairs(image_overrides) do
	if minetest.registered_items[itemstring] ~= nil then
		minetest.override_item(itemstring, { _doc_items_image = data} )
	end
end

-- Minetest Game Factoids

-- Groups flammable, puts_out_fire
local function f_fire(itemstring, def)
	local s = ""
	-- Fire
	if def.groups.flammable ~= nil then
		-- TODO: When the fire mod supports different flammable levels, add flavor texts
		s = s .. S("This block is flammable.")
	end

	if def.groups.puts_out_fire ~= nil then
		if def.groups.flammable ~= nil then
			s = s .. "\n"
		end
		s = s .. S("This block will extinguish nearby fire.")
	end

	if def.groups.igniter ~= nil then
		if def.groups.flammable ~= nil or def.groups.puts_out_fire ~= nil then
			s = s .. "\n"
		end
		s = s .. S("This block will set flammable blocks within a radius of @1 on fire.", def.groups.igniter)
	end
	return s
end

-- flora group
local function f_flora(itemstring, def)
	if def.groups.flora == 1 then
		local groupname = doc.sub.items.get_group_name("flora")
		return S("This block belongs to the @1 group. It a living organism which likes to grow and spread on dirt with grass or dirt with dry grass when it is in light. Spreading will stop when the surrounding area is too crammed with @2 blocks. On desert sand, it will wither and die and turn into a dry shrub.", groupname, groupname)
	else
		return ""
	end
end

-- soil group
local function f_soil(itemstring, def)
	if def.groups.soil == 1 then
		return S("This block serves as a soil for saplings. Blocks in the Saplings group will grow into trees on this block.")
	elseif def.groups.soil == 2 or def.groups.soil == 3 then
		return S("This block serves as a soil for saplings as well as plants grown from seeds. It supports their growth.")
	else
		return ""
	end
end

local function f_leafdecay(itemstring, def)
	local formstring = ""
	if def.groups.leafdecay ~= nil then
		local groupname = doc.sub.items.get_group_name("tree")
		if def.groups.leafdecay_drop ~= nil then
			formstring = S("This block may drop as an item when no trunk is nearby. This is prevented if any block of the group “@1” is nearby (up to a distance of @2), the block is completely surrounded by blocks which are neither air or a liquid or it has been manually placed by a player.", groupname, def.groups.leafdecay)
		else
			if def.drop ~= "" and def.drop ~= nil and def.drop ~= itemstring then
				formstring = S("This block may decay when no trunk is nearby. When decaying, it disappears and may drop one of its mining drops (but never itself). Decay is prevented if any block of the group “@1” is nearby (up to a distance of @2), the block is completely surrounded by blocks which are neither air or a liquid or it has been placed by a player.", groupname, def.groups.leafdecay)
			else
				formstring = S("This block may decay and disappear when no trunk is nearby. Decay is prevented if any block of the group “@1” is nearby (up to a distance of @2), the block is completely surrounded by blocks which are neither air or a liquid or it has been placed by a player.", groupname, def.groups.leafdecay)
			end
		end
	end
	return formstring
end

doc.sub.items.register_factoid("nodes", "groups", f_fire)
doc.sub.items.register_factoid("nodes", "groups", f_flora)
doc.sub.items.register_factoid("nodes", "groups", f_leafdecay)
doc.sub.items.register_factoid("nodes", "groups", f_soil)

-- Add node aliases
for i=2,5 do
	doc.add_entry_alias("nodes", "default:grass_1", "default:grass_"..i)
	doc.add_entry_alias("nodes", "default:dry_grass_1", "default:dry_grass_"..i)
end
for i=1,7 do
	doc.add_entry_alias("nodes", "farming:wheat_8", "farming:wheat_"..i)
	doc.add_entry_alias("nodes", "farming:cotton_8", "farming:cotton_"..i)
end
doc.add_entry_alias("nodes", "default:lava_source", "default:lava_flowing")
doc.add_entry_alias("nodes", "default:water_source", "default:water_flowing")
doc.add_entry_alias("nodes", "default:river_water_source", "default:river_water_flowing")
doc.add_entry_alias("nodes", "default:furnace", "default:furnace_active")
doc.add_entry_aliases("nodes", "doors:door_wood_a", {"doors:door_wood_b", "doors:doors_wood"})
doc.add_entry_aliases("nodes", "doors:door_steel_a", {"doors:door_steel_b", "doors:door_steel"})
doc.add_entry_aliases("nodes", "doors:door_glass_a", {"doors:door_glass_b", "doors:door_glass"})
doc.add_entry_aliases("nodes", "doors:door_obsidian_glass_a", {"doors:door_obsidian_glass_b", "doors:door_obsidian_glass"})
doc.add_entry_alias("nodes", "doors:gate_wood_closed", "doors:gate_wood_open")
doc.add_entry_alias("nodes", "doors:gate_junglewood_closed", "doors:gate_junglewood_open")
doc.add_entry_alias("nodes", "doors:gate_acacia_wood_closed", "doors:gate_acacia_wood_open")
doc.add_entry_alias("nodes", "doors:gate_aspen_wood_closed", "doors:gate_aspen_wood_open")
doc.add_entry_alias("nodes", "doors:gate_pine_wood_closed", "doors:gate_pine_wood_open")
doc.add_entry_alias("nodes", "doors:trapdoor", "doors:trapdoor_open")
doc.add_entry_alias("nodes", "doors:trapdoor_steel", "doors:trapdoor_steel_open")
doc.add_entry_alias("nodes", "tnt:tnt", "tnt:tnt_burning")
doc.add_entry_alias("nodes", "tnt:gunpowder", "tnt:gunpowder_burning")
local pane_aliases = {}
local bar_aliases = {}
for i=2,15 do
	table.insert(pane_aliases, "xpanes:pane_"..i)
	table.insert(bar_aliases, "xpanes:bar_"..i)
end
table.insert(pane_aliases, "xpanes:pane")
table.insert(bar_aliases, "xpanes:bar")
doc.add_entry_aliases("nodes", "xpanes:pane_1", pane_aliases)
doc.add_entry_aliases("nodes", "xpanes:bar_1", bar_aliases)

-- Gather help texts
dofile(minetest.get_modpath("doc_minetest_game") .. "/helptexts.lua")

-- Register boat object for doc_identifier
if minetest.get_modpath("doc_identifier") ~= nil then
	doc.sub.identifier.register_object("boats:boat", "craftitems", "boats:boat")
	doc.sub.identifier.register_object("carts:cart", "craftitems", "carts:cart")
end

-- Add another craft for encyclopedia
if minetest.get_modpath("doc_encyclopedia") ~= nil then
	minetest.register_craft({
		output = "doc_encyclopedia:encyclopedia",
		recipe = {
			{ "default:book" },
			{ "default:book" },
			{ "default:book" },
		}
	})
end



-- Remove the “helper” comment from screwdriver description since the help is
-- provided by this mod
minetest.override_item("screwdriver:screwdriver", {description = item_name_overrides["screwdriver:screwdriver"]})


-- TODO: Maybe add achievement for discovering all Minetest Game node entries