local modname = minetest.get_current_modname()

local S = minetest.get_translator(modname)

local plant_sounds =
{
	dig = "plant_dig",
	dug = "plant_dug",
	footstep = "plant_footstep",
	place = "plant_place",
}

minetest.register_node(modname .. ":plant",
	{
		tiles = {modname .. "_base.png"},
		description = S("Bromelia"),
		drawtype = "plantlike",
		groups = {dig_immediate = 2, attached_node = 1},
		walkable = false,
		paramtype = "light",
		floodable = true,
		sounds = plant_sounds,
		is_ground_content = false,
	})

minetest.register_node(modname .. ":plant_stalked",
	{
		tiles = {modname .. "_base.png^" .. modname .. "_bloom_stalk.png"},
		description = S("Bromelia"),
		drawtype = "plantlike",
		groups = {dig_immediate = 2, attached_node = 1},
		walkable = false,
		paramtype = "light",
		floodable = true,
		sounds = plant_sounds,
		is_ground_content = false,
		after_destruct = function(pos)
			pos.y = pos.y + 1
			local node = minetest.get_node(pos)
			if node.name == modname .. ":fruit" or
				node.name == modname .. ":bloom"
			then
				minetest.dig_node(pos)
			end
		end,
		on_flood = function(pos)
			pos.y = pos.y + 1
			local above = minetest.get_node(pos)
			if above.name == modname .. ":fruit" or
				above.name == modname .. ":bloom"
			then
				minetest.dig_node(pos)
			end
		end,
		drop = modname .. ":plant",
	})

local function bloom_fruit_destructor(pos)
	pos = pos - 1
	local node = minetest.get_node(pos)
		if node.name == modname .. ":plant_stalked"
		then
			minetest.set_node(pos, {name = modname .. ":plant"})
		end
end

minetest.register_node(modname .. ":fruit",
	{
		tiles = {modname .. "_fruit.png"},
		description = S("Bromelia Fruit"),
		drawtype = "plantlike",
		groups = {dig_immediate = 3},
		walkable = false,
		paramtype = "light",
		floodable = true,
		sounds = plant_sounds,
		is_ground_content = false,
		drop =
		{
			max_items = 1,
			items =
			{
				{rarity = 15, items = {modname .. ":appleapple"}},
				{rarity = 10, items = {modname .. ":coconutapple"}},
				{rarity = 5, items = {modname .. ":spruceapple"}},
				{rarity = 1.3, items = {modname .. ":pineapple"}},
			}
		},
	})

minetest.register_node(modname .. ":bloom",
	{
		tiles = {modname .. "_bloom.png"},
		description = S("Bromelia Fruit"),
		drawtype = "plantlike",
		groups = {dig_immediate = 3, flower = 1},
		drop = "",
		walkable = false,
		paramtype = "light",
		floodable = true,
		sounds = plant_sounds,
		is_ground_content = false,
	})


--Growth ABM
minetest.register_abm(
	{
		label = "Bromelia growing",
		nodenames = {modname .. ":plant", modname .. ":plant_stalked"},
		interval = 15,
		chance = 60,
		catch_up = true,
		action = function(pos, node)
			local above = vector.new(pos.x, pos.y + 1, pos.z)
			local above_node = minetest.get_node(above)
			local name = above_node.name
			if name == "air"
			then
				minetest.set_node(pos, {name = modname .. ":plant_stalked"})
				minetest.set_node(above, {name = modname .. ":bloom"})
			
			elseif name == modname .. ":bloom"
			then
				minetest.set_node(above, {name = modname .. ":fruit"})
				
			elseif name == modname .. ":fruit"
			then
				minetest.set_node(pos, {name = modname .. ":plant"})
				minetest.set_node(above, {name = "air"})
			end
		end
	})

if minetest.get_modpath("eg_mapgen")
then
	--Spawning as decoration
	minetest.register_decoration(
		{
			deco_type = "simple",
			place_on = "eg_mapgen:grass",
			sidelen = 8,
			noise_params =
			{
				offset = -0.29,
				scale = 0.2,
				spread = {x = 500, y = 500, z = 500},
				seed = 5024,
				octaves = 3,
				persist = 0.7,
				lacunarity = 16.0,
			},
			decoration = modname .. ":plant",
			height = 1,
			
		})
end

--FRUIT ITEMS
minetest.register_tool(modname .. ":pineapple",
	{
		on_use = minetest.item_eat(4), --TODO: use own eat stuff with hunger
		description = S("Pineapple"),
		inventory_image = modname .. "_pineapple.png",
	})

minetest.register_tool(modname .. ":spruceapple",
	{
		on_use = minetest.item_eat(4), --TODO: use own eat stuff with hunger
		description = S("Spruceapple"),
		inventory_image = modname .. "_spruceapple.png",
	})

minetest.register_tool(modname .. ":coconutapple",
	{
		on_use = minetest.item_eat(4), --TODO: use own eat stuff with hunger
		description = S("Coconutapple"),
		inventory_image = modname .. "_coconutapple.png",
	})

minetest.register_tool(modname .. ":appleapple",
	{
		on_use = minetest.item_eat(4), --TODO: use own eat stuff with hunger
		description = S("Appleapple"),
		inventory_image = modname .. "_appleapple.png",
	})
