plants.saplings = {}

local function plants_sapling_formspec(name)
   local data = plants.saplings[name]
   local trunk_node = 'levels:tree_1'
   local leaf_node = 'levels:leaves_1'
   local trunk_height = 3
   local height = 7
   if data then
      trunk_node = data['trunk_node']
      leaf_node = data['leaf_node']
      trunk_height = data['trunk_height']
      height = data['height']
   end
   local formspec =
   'formspec_version[3]'..
   'size[8,8]'..
   'textarea[.5,.25;7,1;;;Set your tree options below and click save. Place the sapling to generate your tree.]'..
   'field[.5,1.5;7,.5;trunk_n;Trunk Node;'..trunk_node..']'..
   'field[.5,2.5;7,.5;leaf_n;Leaf Node;'..leaf_node..']'..
   'field[.5,3.5;3,.5;height_overall;Overall Height;'..height..']'..
   'field[4.5,3.5;3,.5;height_trunk;Trunk Height;'..trunk_height..']'..
   'button_exit[3,6.5;2,1;save;Save]'
   return formspec
end


minetest.register_craftitem('plants:sapling', {
   description = 'Tree Tool',
   inventory_image = 'plants_sapling.png',
   stack_max = 1,
   on_drop = lobby.no_drop,
   on_place = function(itemstack, placer, pointed_thing)
      local pos = minetest.get_pointed_thing_position(pointed_thing, true)
      if minetest.is_protected(pos, placer:get_player_name()) then
         minetest.chat_send_player(placer:get_player_name(), 'Sorry, can\'t do that here.')
      else
         local name = placer:get_player_name()
         local data = plants.saplings[name]
         if data then
            local trunk_node = data['trunk_node'] or 'levels:tree_1'
            local leaf_node = data['leaf_node'] or 'levels:leaves_1'
            local trunk_height = tonumber(data['trunk_height']) or 3
            local height = math.max((tonumber(data['height']) or 7), 21)
            local leaf_height = height - trunk_height
            local leaf_width = math.max(3, math.ceil(leaf_height/2))
            if leaf_width % 2 == 0 then
               leaf_width = leaf_width + 1
            end
            local crown_width = math.floor(leaf_width/2)
            local leaf_base = {x = pos.x, y = pos.y+trunk_height, z = pos.z}
            local leaf_top = {x = pos.x, y = pos.y+(height-1), z = pos.z}
            local crown1 = {x = pos.x-crown_width, y = pos.y+(height-1), z = pos.z-crown_width}
            local crown2 = {x = pos.x+crown_width, y = pos.y+(height-1), z = pos.z+crown_width}
            worldedit.cube(leaf_base, leaf_width, (leaf_height-1), leaf_width, leaf_node, false)
            worldedit.set(crown1, crown2, {leaf_node, 'air'})
            local i = 1
            minetest.set_node(pos, {name = trunk_node})
            while i <= trunk_height do --Setting trunk
               local new_pos = {x=pos.x, y=pos.y+i, z=pos.z}
               minetest.set_node(new_pos, {name = trunk_node})
               i = i + 1
            end
         else
            minetest.chat_send_player(name, 'Set sapling parameters first.')
         end
      end
   end,
   on_use = function(itemstack, user)
      local name = user:get_player_name()
      minetest.show_formspec(name, 'plants:sapling', plants_sapling_formspec(name))
   end
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
   local name = player:get_player_name()
   if formname == 'plants:sapling' then
      if fields.save then
         local passing = 0
         local data = {}
         local trunk_node = fields.trunk_n
         local leaf_node = fields.leaf_n
         if minetest.registered_nodes[trunk_node] then
            data.trunk_node = trunk_node
            passing = passing + 1
         end
         if minetest.registered_nodes[leaf_node] then
            data.leaf_node = leaf_node
            passing = passing + 1
         end
         if passing == 2 then
            data.height = fields.height_overall
            data.trunk_height = fields.height_trunk
            plants.saplings[name] = data
            minetest.chat_send_player(name, 'Right-click to place your tree. Punch again to change parameters.')
         else
            minetest.chat_send_player(name, 'Looks like some of your data is bad, try again.')
         end
      end
   end
end)

--[[
Saving data:
local data = {}
data.level_pos = {x = pos_x, y = pos_y, z = pos_z}
data.xp = tonumber(fields.xp)
lobby.savedata.IDs[map_id] = true
lobby.savedata.data[map_id] = data

----
Retriving data:
local game_data = lobby.savedata.data[map_id]
local game_pos = game_data['level_pos']
]]
