local S = minetest.get_translator(minetest.get_current_modname())
local has_doc = minetest.get_modpath("doc")

mcl_flowerpots = {}
mcl_flowerpots.registered_pots = {}

minetest.register_node("mcl_flowerpots:flower_pot", {
	description = S("Flower Pot"),
	_tt_help = S("Can hold a small flower or plant"),
	_doc_items_longdesc = S("Flower pots are decorative blocks in which flowers and other small plants can be placed."),
	_doc_items_usagehelp = S("Just place a plant on the flower pot. Flower pots can hold small flowers (not higher than 1 block), saplings, ferns, dead bushes, mushrooms and cacti. Rightclick a potted plant to retrieve the plant."),
	drawtype = "mesh",
	mesh = "flowerpot.obj",
	tiles = {
		"mcl_flowerpots_flowerpot.png",
	},
	use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "clip" or true,
	visual_scale = 0.5,
	wield_image = "mcl_flowerpots_flowerpot_inventory.png",
	wield_scale = {x=1.0, y=1.0, z=1.0},
	paramtype = "light",
	sunlight_propagates = true,
	selection_box = {
		type = "fixed",
		fixed = {-0.2, -0.5, -0.2, 0.2, -0.1, 0.2}
	},
	collision_box = {
		type = "fixed",
		fixed = {-0.2, -0.5, -0.2, 0.2, -0.1, 0.2}
	},
	is_ground_content = false,
	inventory_image = "mcl_flowerpots_flowerpot_inventory.png",
	groups = {dig_immediate=3, deco_block=1, attached_node=1, dig_by_piston=1, flower_pot=1},
	sounds = mcl_sounds.node_sound_stone_defaults(),
	on_rightclick = function(pos, node, clicker, itemstack)
		local name = clicker:get_player_name()
		if minetest.is_protected(pos, name) then
			minetest.record_protection_violation(pos, name)
			return
		end
		local item = clicker:get_wielded_item():get_name()
		if mcl_flowerpots.registered_pots[item] then
			minetest.swap_node(pos, {name="mcl_flowerpots:flower_pot_"..mcl_flowerpots.registered_pots[item]})
			if not minetest.is_creative_enabled(clicker:get_player_name()) then
				itemstack:take_item()
			end
		end
	end,
})

minetest.register_craft({
	output = "mcl_flowerpots:flower_pot",
	recipe = {
		{"mcl_core:brick", "", "mcl_core:brick"},
		{"", "mcl_core:brick", ""},
		{"", "", ""},
	}
})

function mcl_flowerpots.register_potted_flower(name, def)
	mcl_flowerpots.registered_pots[name] = def.name
	minetest.register_node(":mcl_flowerpots:flower_pot_"..def.name, {
		description = def.desc.." "..S("Flower Pot"),
		_doc_items_create_entry = false,
		drawtype = "mesh",
		mesh = "flowerpot.obj",
		tiles = {
			"[combine:32x32:0,0=mcl_flowerpots_flowerpot.png:0,0="..def.image,
		},
		use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "clip" or true,
		visual_scale = 0.5,
		wield_scale = {x=1.0, y=1.0, z=1.0},
		paramtype = "light",
		sunlight_propagates = true,
		selection_box = {
			type = "fixed",
			fixed = {-0.2, -0.5, -0.2, 0.2, -0.1, 0.2}
		},
		collision_box = {
			type = "fixed",
			fixed = {-0.2, -0.5, -0.2, 0.2, -0.1, 0.2}
		},
		is_ground_content = false,
		groups = {dig_immediate=3, attached_node=1, dig_by_piston=1, not_in_creative_inventory=1, flower_pot=2},
		sounds = mcl_sounds.node_sound_stone_defaults(),
		on_rightclick = function(pos, item, clicker)
			local player_name = clicker:get_player_name()
			if minetest.is_protected(pos, player_name) then
				minetest.record_protection_violation(pos, player_name)
				return
			end
			minetest.add_item({x=pos.x, y=pos.y+0.5, z=pos.z}, name)
			minetest.set_node(pos, {name="mcl_flowerpots:flower_pot"})
		end,
		drop = {
			items = {
				{ items = { "mcl_flowerpots:flower_pot", name } }
			}
		},
	})
	-- Add entry alias for the Help
	if has_doc then
		doc.add_entry_alias("nodes", "mcl_flowerpots:flower_pot", "nodes", "mcl_flowerpots:flower_pot_"..name)
	end
end

function mcl_flowerpots.register_potted_cube(name, def)
	mcl_flowerpots.registered_pots[name] = def.name
	minetest.register_node(":mcl_flowerpots:flower_pot_"..def.name, {
		description = def.desc.." "..S("Flower Pot"),
		_doc_items_create_entry = false,
		drawtype = "mesh",
		mesh = "flowerpot_with_long_cube.obj",
		tiles = {
			def.image,
		},
		use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "clip" or true,
		visual_scale = 0.5,
		wield_scale = {x=1.0, y=1.0, z=1.0},
		paramtype = "light",
		sunlight_propagates = true,
		selection_box = {
			type = "fixed",
			fixed = {-0.2, -0.5, -0.2, 0.2, -0.1, 0.2}
		},
		collision_box = {
			type = "fixed",
			fixed = {-0.2, -0.5, -0.2, 0.2, -0.1, 0.2}
		},
		is_ground_content = false,
		groups = {dig_immediate=3, attached_node=1, dig_by_piston=1, not_in_creative_inventory=1, flower_pot=2},
		sounds = mcl_sounds.node_sound_stone_defaults(),
		on_rightclick = function(pos, item, clicker)
			local player_name = ""
			if clicker:is_player() then
				player_name = clicker:get_player_name()
			end
			if minetest.is_protected(pos, player_name) then
				minetest.record_protection_violation(pos, player_name)
				return
			end
			minetest.add_item({x=pos.x, y=pos.y+0.5, z=pos.z}, name)
			minetest.set_node(pos, {name="mcl_flowerpots:flower_pot"})
		end,
		drop = {
			items = {
				{ items = { "mcl_flowerpots:flower_pot", name } }
			}
		},
	})
	-- Add entry alias for the Help
	if has_doc then
		doc.add_entry_alias("nodes", "mcl_flowerpots:flower_pot", "nodes", "mcl_flowerpots:flower_pot_"..def.name)
	end
end

--forced because hard dependency to mcl_core
mcl_flowerpots.register_potted_cube("mcl_core:cactus", {
	name = "cactus",
	desc = S("Cactus"),
	image = "mcl_flowerpots_cactus.png",
})

mcl_flowerpots.register_potted_flower("mcl_mushrooms:mushroom_brown", {
	name = "mushroom_brown",
	desc = S("Brown Mushroom"),
	image = "farming_mushroom_brown.png",
})

mcl_flowerpots.register_potted_flower("mcl_mushrooms:mushroom_red", {
	name = "mushroom_red",
	desc = S("Red Mushroom"),
	image = "farming_mushroom_red.png",
})

mcl_flowerpots.register_potted_flower("mcl_core:sapling", {
	name = "sapling",
	desc = S("Oak Sapling"),
	image = "default_sapling.png",
})

mcl_flowerpots.register_potted_flower("mcl_core:acaciasapling", {
	name = "acaciasapling",
	desc = S("Acacia Sapling"),
	image = "default_acacia_sapling.png",
})

mcl_flowerpots.register_potted_flower("mcl_core:junglesapling", {
	name = "junglesapling",
	desc = S("Jungle Sapling"),
	image = "default_junglesapling.png",
})

mcl_flowerpots.register_potted_flower("mcl_core:darksapling", {
	name = "darksapling",
	desc = S("Dark Oak Sapling"),
	image = "mcl_core_sapling_big_oak.png",
})

mcl_flowerpots.register_potted_flower("mcl_core:sprucesapling", {
	name = "sprucesapling",
	desc = S("Spruce Sapling"),
	image = "mcl_core_sapling_spruce.png",
})

mcl_flowerpots.register_potted_flower("mcl_core:birchsapling", {
	name = "birchsapling",
	desc = S("Birch Sapling"),
	image = "mcl_core_sapling_birch.png",
})

mcl_flowerpots.register_potted_flower("mcl_core:deadbush", {
	name = "deadbush",
	desc = S("Dead Bush"),
	image = "default_dry_shrub.png",
})

-- Add the possibility to pot nether fungi and roots, depend to mcl_mushroom

mcl_flowerpots.register_potted_flower("mcl_mushroom:warped_fungus", {
	name = "warped_fungus",
	desc = S("Warped Fungus Mushroom"),
	image = "farming_warped_fungus.png",
})

mcl_flowerpots.register_potted_flower("mcl_mushroom:crimson_fungus", {
	name = "crimson_fungus",
	desc = S("Crimson Fungus Mushroom"),
	image = "farming_crimson_fungus.png",
})

mcl_flowerpots.register_potted_flower("mcl_mushroom:warped_roots", {
	name = "warped_roots",
	desc = S("Warped Roots"),
	image = "warped_roots.png",
})

mcl_flowerpots.register_potted_flower("mcl_mushroom:crimson_roots", {
	name = "crimson_roots",
	desc = S("Crimson Roots"),
	image = "crimson_roots.png",
})