local col_box = {
   type = 'fixed',
   fixed = {{-.45, -.5, -.45, 1.5, .4, .5},
            {.7, .4, -.35, 1.4, 1, .35}}
         }

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

local formspec_bad =
   '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[current_name;part;6,1;1,1;]'..
   'list[current_player;main;0,3;8,3;]'..
   'listring[current_player;main]'..
   'listring[current_name;part]'

minetest.register_node('tasks:transformer_0_on',{
   description = 'Power Transformer',
   drawtype = 'mesh',
   mesh = 'tasks_transformer.obj',
   tiles = {'tasks_transformer.png'},
   paramtype2 = 'facedir',
   selection_box = col_box,
   collision_box = col_box,
   groups = {breakable = 1, tasks=1},
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      meta:set_string('formspec', formspec_good)
      meta:set_string('infotext', 'Power transformer')
      tasks.on_construct(pos)
   end,
   on_rightclick = tasks.right_click,
   on_timer = function(pos)
      local node = minetest.get_node(pos)
      local meta = minetest.get_meta(pos)
      local infotext = meta:get_string('infotext')
      minetest.swap_node(pos, {name = 'tasks:transformer_0_off', param2 = node.param2})
      meta:set_string('formspec', formspec_bad)
      meta:set_string('infotext', infotext..' (Needs repair!)')
   end,
})

minetest.register_node('tasks:transformer_0_off',{
   description = 'Power Transformer',
   drawtype = 'mesh',
   mesh = 'tasks_transformer.obj',
   tiles = {'tasks_transformer.png'},
   paramtype2 = 'facedir',
   selection_box = col_box,
   collision_box = col_box,
   groups = {breakable = 1, not_in_creative_inventory=1, tasks=1},
   drop = 'tasks:transformer_0_on',
   on_receive_fields = function(pos, formname, fields, sender)
      if fields ['gimme'] then
         local player_inv = sender:get_inventory()
         if not player_inv:contains_item('main', {name='tasks:fuse_req', count = 4}) then
            player_inv:add_item('main', 'tasks:fuse_req')
         else
            minetest.chat_send_player(sender:get_player_name(), 'You have too many of these already.')
         end
      end
   end,
   allow_metadata_inventory_put = function(pos, listname, index, stack)
      if listname == 'part' then
         if stack:get_name() == 'tasks:fuse' 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 = string.sub(meta:get_string('infotext'), 1, -17)
      inv:set_stack('part', 1, '')
      minetest.swap_node(pos, {name = 'tasks:transformer_0_on', param2 = node.param2})
      meta:set_string('formspec', formspec_good)
      meta:set_string('infotext', infotext)
      tasks.only_add_xp(xp, name)
      timer:start(random_number)
   end,
   })
