local formspec_good =
   'formspec_version[3]'..
   'size[8,5]'..
   'bgcolor[;neither]'..
   'background[-1,-1;10,7;tasks_engines_working.png]'..
   'hypertext[.5,.5;7,4;nil;<global halign=center valign=middle><style color=green size=30>This reactor is performing at peak efficiency.</style>]'

local function formspec_bad(pos)
   local spos = pos.x ..','.. pos.y ..','.. pos.z
   local formspec =
      'formspec_version[3]'..
      'size[13,9]'..
      'bgcolor[;neither]'..
      'background[-1,-1;15,11;tasks_engine_0_broken.png]'..
      'hypertext[6.5,.5;6,4;nil;<global halign=center valign=middle><style color=#94c6f5 size=30>Looks like a gear broke. You can get a new part from a storage locker with a request form.<style>]'..
      'style_type[button;bgcolor=#97c3cd]'..
      'button_exit[1,5.25;3,1;gimme;Grab A Request Form]'..
      'button_exit[5,5.25;3,1;close;Close Window]'..
      'listcolors[#10101020;#7DA3CC20]'..
      'list[nodemeta:'..spos..';part;9,5.25;1,1;]'..
      'list[current_player;main;.25,6.5;8,2;]'..
      'listring[current_player;main]'..
      'listring[nodemeta:'..spos..';part]'
   return formspec
end

minetest.register_node('tasks:engine_0_on',{
   description = 'Arc Reactor',
   drawtype = 'mesh',
   mesh = 'tasks_engine_0.obj',
   tiles = {{name = 'tasks_pedestal.png'},
         {name = 'tasks_engine_0_on.png'},
         {name='tasks_engine_arc_0.png',
            animation = {
               type = 'vertical_frames',
               aspect_w = 64,
               aspect_h = 32,
               length = 2.0,},},
         },
   use_texture_alpha = 'blend',
   light_source = 13,
   groups = {breakable = 1, tasks=1, plays_sound=1},
   _sound = 'tasks_engine_0_run',
   on_construct = function(pos)
      tasks.on_construct(pos, 'Arc Reactor', 'Broken Arc Reactor')
   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.sound_play('tasks_engine_0_stop', {pos = pos, gain = 1, max_hear_distance = 10})
      meta:set_string('infotext', infotext)
      minetest.after(11, function()
         minetest.swap_node(pos, {name = 'tasks:engine_0_off', param2 = node.param2})
      end)
   end,
})

minetest.register_node('tasks:engine_0_off',{
   description = 'Arc Reactor',
   drawtype = 'mesh',
   mesh = 'tasks_engine_0.obj',
   tiles = {{name = 'tasks_pedestal.png'},
         {name = 'tasks_engine_0_off.png'},
         {name='tasks_blank.png'},
         },
         use_texture_alpha = 'clip',
   light_source = 2,
   groups = {breakable = 1, not_in_creative_inventory=1, tasks=1},
   drop = 'tasks:engine_0_on',
   req_form = 'tasks:gear_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:gear' 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.sound_play('tasks_engine_0_start', {pos = pos, gain = 1, max_hear_distance = 10})
      minetest.swap_node(pos, {name = 'tasks:engine_0_on', 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,
   })
