local function button_formspec(xp, pos, desc)
   local formspec =
      'formspec_version[3]'..
      'size[8,6]'..
      'textarea[.5,.5;7,2;;;You can change the POS manually if you want, if you provide bad data everybody that punches the button will see you are an idiot, so test your stuff. :)]'..
      'field[1,4;2,.5;pos;Return pos (x,y,z);'..pos..']'..
      'field[5,4;2,.5;xp;XP;'..xp..']'..
      'field[1,2;6,.5;desc;description;'..desc..']'..
      'button_exit[5,5;2,.5;save;Submit]'
   return formspec
end

local button_box = {
   type = 'fixed',
      fixed = {
         {-.4375, -.5, -.4375, .4375, .3125, .4375},},}

minetest.register_node('platformer:button', {
   description = 'XP Button',
   drawtype = 'mesh',
   mesh = 'platformer_button.obj',
   tiles = {'platformer_button.png'},
   paramtype = 'light',
   light_source = 10,
   selection_box = button_box,
   collision_box = button_box,
   groups = {breakable = 1, not_in_creative_inventory=1},
   after_place_node = function(pos, placer)
      if minetest.check_player_privs(placer:get_player_name(), {server = true}) then
         local meta = minetest.get_meta(pos)
         local saved_data = platformer.player_config[placer:get_player_name()] or {x=0,y=0,z=0}
         local saved_pos = minetest.pos_to_string(saved_data, 1)
         meta:set_string('owner', placer:get_player_name())
         meta:set_int('xp', 1)
         meta:set_string('pos', saved_pos)
      else
         minetest.chat_send_player(placer:get_player_name(), 'How did you get this? No matter, you are not allowed to place it!')
         minetest.remove_node(pos)
      end
   end,
   on_rightclick = function(pos, node, clicker, itemstack)
      local meta = minetest.get_meta(pos)
      local owner = meta:get_string('owner')
      local item = itemstack:get_name()
      local name = clicker:get_player_name()
      if item == 'tasks:configurator' then
         if owner == name and minetest.check_player_privs(clicker, {server = true}) then
            tasks.player_config[name] = pos
            local xp = meta:get_int('xp')
            local pos = meta:get_string('pos')
            local desc = meta:get_string('infotext')
            minetest.show_formspec(name, 'platformer:button', button_formspec(xp, pos, desc))
         end
      end
   end,
   on_use = function(itemstack, user, pointed_thing)
      local pos = minetest.get_pointed_thing_position(pointed_thing)
      local name = user:get_player_name()
      local new_pos = {x=pos.x, y=pos.y+2, z=pos.z}
      platformer.player_config[name] = new_pos
      minetest.chat_send_player(name, 'Spawn point saved, use again to reset.')
   end,
   on_punch = function(pos, node, puncher)
      local wield = puncher:get_wielded_item()
      local wield_name = wield:get_name()
      local name = puncher:get_player_name()
      local meta = minetest.get_meta(pos)
      if wield_name == 'tasks:configurator' then
         local owner = meta:get_string('owner')
         if owner == name or minetest.check_player_privs(clicker, {server = true}) then
            tasks.player_config[name] = pos
            local xp = meta:get_int('xp')
            local pos = meta:get_string('pos')
            local desc = meta:get_string('infotext')
            minetest.show_formspec(name, 'platformer:button', button_formspec(xp, pos, desc))
         end
      elseif wield_name ~= 'creative:tool_breaking' then
         local owner = meta:get_string('owner')
         if owner ~= '' then
            local spawn = meta:get_string('pos')
            local spawn_pos = minetest.string_to_pos(spawn)
            if spawn_pos ~= nil then
               local xp = meta:get_int('xp')
               puncher:set_pos(spawn_pos)
               lobby.give_xp(puncher, xp)
            else
               minetest.chat_send_player(puncher:get_player_name(), 'Looks like '..owner..' misconfigured this button, sorry. Use /spawn to get back to the lobby.')
            end
         end
      end
   end,
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
   local name = player:get_player_name()
   if formname == 'platformer:button' then
      if fields.save then
         local pos = tasks.player_config[name]
         local meta = minetest.get_meta(pos)
         meta:set_int('xp', fields.xp)
         meta:set_string('pos', fields.pos)
         meta:set_string('infotext', fields.desc)
      end
   end
end)
