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

local formspec_good =
   'size[6,3]'..
   'textarea[1,1;5,2;;;Everything is functioning properly]'

local function formspec_bad(pos)
   local spos = pos.x ..','.. pos.y ..','.. pos.z
   local formspec =
      'size[8,6]'..
      'textarea[.5,;5,2;;;There must have been a power surge.\nGet a new part from a storage locker.]' ..
      'button_exit[2.5,1.5;3,1;gimme;Grab a part request form]'..
      'list[nodemeta:'..spos..';part;6,1;1,1;]'..
      'list[current_player;main;0,3;8,2;]'..
      'listring[current_player;main]'..
      'listring[nodemeta:'..spos..';part]'
   return formspec
end

minetest.register_node('tasks:torpedo_heater_0',{
   description = 'Torpedo Heater',
   drawtype = 'mesh',
   mesh = 'tasks_torpedo_heater.obj',
   tiles = {'tasks_torpedo_heater_0.png'},
   paramtype2 = 'facedir',
   selection_box = col_box,
   collision_box = col_box,
   groups = {breakable = 1, tasks=1},
   on_construct = function(pos)
      tasks.on_construct(pos, 'Torpedo Heater', 'Torpedo Fan')
   end,
   on_rightclick = function(pos, node, clicker)
      tasks.right_click_on(pos, node, clicker, formspec_good)
   end,
   on_timer = function(pos)
      local node = minetest.get_node(pos)
      local meta = minetest.get_meta(pos)
      local infotext = meta:get_string('info_repair')
      minetest.swap_node(pos, {name = 'tasks:torpedo_heater_1', param2 = node.param2})
      meta:set_string('infotext', infotext)
   end
})

minetest.register_node('tasks:torpedo_heater_1',{
   description = 'Torpedo Heater',
   drawtype = 'mesh',
   mesh = 'tasks_torpedo_heater.obj',
   tiles = {'tasks_torpedo_heater_1.png'},
   paramtype2 = 'facedir',
   selection_box = col_box,
   collision_box = col_box,
   groups = {breakable = 1, not_in_creative_inventory=1, tasks=1},
   drop = 'tasks:torpedo_heater_0',
   req_form = 'tasks:wick_req',
   req_count = 4,
   on_rightclick = function(pos, node, clicker)
      tasks.right_click_off(pos, node, clicker, formspec_bad(pos))
   end,
   allow_metadata_inventory_put = function(pos, listname, index, stack)
      if listname == 'part' then
         if stack:get_name() == 'tasks:wick' then
            return 1
         else
            return 0
         end
      end
   end,
   on_metadata_inventory_put = function(pos, listname, index, stack, player)
      local name = player:get_player_name()
      local meta = minetest.get_meta(pos)
      local xp = meta:get_int('xp') or 1
      local inv = meta:get_inventory()
      local node = minetest.get_node(pos)
      local timer = minetest.get_node_timer(pos)
      local min = meta:get_int('time_min') or 30
      local max = meta:get_int('time_max') or 60
      local random_number = math.random(min,max)
      local infotext = meta:get_string('info_working')
      inv:set_stack('part', 1, '')
      minetest.swap_node(pos, {name = 'tasks:torpedo_heater_0', param2 = node.param2})
      minetest.close_formspec(name, 'tasks:part_req_form')
      meta:set_string('infotext', infotext)
      tasks.only_add_xp(xp, name)
      timer:start(random_number)
   end
})
