
local add_tree = function (pos, ofx, ofy, ofz, schem, replace)
   -- check for schematic
   if not schem then
      print ("Schematic not found")
      return
   end
   -- remove sapling and place schematic
   minetest.swap_node(pos, {name = "air"})
   minetest.place_schematic(
      {x = pos.x - ofx, y = pos.y - ofy, z = pos.z - ofz},
      schem, 0, replace, false)
end

-- Register Saplings
local register_sapling = function(name, desc, texture, height)

   minetest.register_node(name .. "_sapling", {
      description = desc .. " Tree Sapling",
      drawtype = "plantlike",
      tiles = {texture .. ".png"},
      inventory_image = texture .. ".png",
      wield_image = texture .. ".png",
      paramtype = "light",
      sunlight_propagates = true,
      is_ground_content = false,
      walkable = false,
      selection_box = {
         type = "fixed",
         fixed = {-4 / 16, -0.5, -4 / 16, 4 / 16, 7 / 16, 4 / 16}
      },
      groups = {
         snappy = 2, dig_immediate = 3, flammable = 2,
         epic_trees_sapling = 1, attached_node = 1, sapling = 1
      },
      sounds = default.node_sound_leaves_defaults(),
      grown_height = height,
   })
end

register_sapling("epic_trees:banana_tree", "Banana", "banana_tree_sapling", 8)
register_sapling("epic_trees:orange_tree", "Orange", "orange_tree_sapling", 6)

function epic_trees.grow_banana_tree(pos)
   add_tree(pos, 3, 0, 3, epic_trees.bananatree)
end


function epic_trees.grow_orange_tree(pos)
   add_tree(pos, 1, 0, 1, epic_trees.orangetree)
end


local enough_height = function(pos, height)

   local nod = minetest.line_of_sight(
      {x = pos.x, y = pos.y + 1, z = pos.z},
      {x = pos.x, y = pos.y + height, z = pos.z})

   if not nod then
      return false -- obstructed
   else
      return true -- can grow
   end
end

local function contains(table, val)
   for i=1,#table do
      if table[i] == val then
         return true
      end
   end
   return false
 end

local grow_sapling = function(pos, node)
   local nodeu = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name
   local under = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name

   if not minetest.registered_nodes[node.name] then
      return
   end

   local height = minetest.registered_nodes[node.name].grown_height

   -- do we have enough height to grow sapling into tree?
   if not height or not enough_height(pos, height) then
      return
   end

   -- Check if Ethereal Sapling is growing on correct substrate
   if node.name == "epic_trees:banana_tree_sapling" and contains({"default:dirt", "default:sand", 'default:dirt_with_grass'}, under) then
      epic_trees.grow_banana_tree(pos)
   elseif node.name == "epic_trees:orange_tree_sapling" then
      if minetest.get_item_group(under, 'soil') > 0 then
         epic_trees.grow_orange_tree(pos)
      end
   end
end

minetest.register_abm({
   label = "Epic Trees grow sapling",
   nodenames = {"group:epic_trees_sapling"},
   interval = 10,
   chance = 50,
   catch_up = false,
   action = function(pos, node)

      local light_level = minetest.get_node_light(pos) or 0

      if light_level < 13 then
         return
      end

      grow_sapling(pos, node)
   end,
})
