local formspec_idle =
   'size[6,3]'..
   'textarea[1,1;5,2;;;No new specimens identify at this time.]'

local specimen_table = {
   'Onion', 'Algae', 'Pumpkin', 'Fungus', 'Snow', 'Yeast', 'Tree Rings' , 'Orange', 'Oak Flower'
}

local function microscope_identify(pos, ran, count)
   local formspec =
      'formspec_version[3]'..
      'size[11,7]'..
      'bgcolor[#171717]'..
      'textarea[7,.5;3.5,3;;;There are currently '..count..' specimens waiting to be identified!]'..
      'image[.5,.5;6,6;tasks_microscope_specimen_'..ran..'.png]'..--specimen images should be cropped to a circle, 192px wide & tall
      'dropdown[7,4;3.5,.5;specimen;'..table.concat(specimen_table, ',')..';0]'..
      'button_exit[8,5.5;2,1;save;Save]'

   return formspec
end

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

minetest.register_node('tasks:microscope',{
   description = 'Microscope',
   drawtype = 'mesh',
   mesh = 'tasks_microscope.obj',
   tiles = {name = 'tasks_microscope.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = box,
   collision_box = box,
   groups = {breakable = 1, tasks=1},
   on_construct = function(pos)
      tasks.on_construct(pos, 'Microscope', 'Microscope with Specimens')
   end,
   on_rightclick = function(pos, node, clicker)
      local name = clicker:get_player_name()
      local map_id = lobby.game[name]
      local sabotage_level = lobby.sabotage_level[map_id] or 5
      local meta = minetest.get_meta(pos)
      local count = meta:get_int('count') or 0
      local level = meta:get_int('level') or 0
      if level < sabotage_level then
         tasks.player_config[name] = pos
         if count == 0 then
            tasks.right_click_on(pos, node, clicker, formspec_idle)
         else
            local name = clicker:get_player_name()
            local ran = math.random(2,9)
            meta:set_int('answer', ran)
            minetest.show_formspec(name, 'tasks:microscope', microscope_identify(pos, ran, count))
         end
      else
         minetest.chat_send_player(name, 'level is currently sabotaged, and you can\'t do this now.')
      end
   end,
   on_timer = function(pos)
      local node = minetest.get_node(pos)
      local meta = minetest.get_meta(pos)
      local info_repair = meta:get_string('info_repair')
      meta:set_string('infotext', info_repair)
      local min = meta:get_int('time_min') or 240
      local max = meta:get_int('time_max') or 600
      local count = meta:get_int('count') or 0
      local random_number = math.random(min,max)
      local timer = minetest.get_node_timer(pos)
      if count < 20 then
         meta:set_int('count', count+1)
      end
      timer:start(random_number)
   end,
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
   local name = player:get_player_name()
   if formname == 'tasks:microscope' then
      if fields.save then
         local pos = tasks.player_config[name]
         local meta = minetest.get_meta(pos)
         local count = meta:get_int('count')
         local answer = meta:get_int('answer')
         for i, index in ipairs(specimen_table) do
            if index == fields.specimen then
               if i == answer then
                  local xp = meta:get_int('xp')
                  tasks.only_add_xp(xp, name)
                  minetest.chat_send_player(name, 'You got it.')
               else
                  minetest.chat_send_player(name, 'That ain\'t it chief.') --This shouldn't be sent if the player closes the formspec
               end
            end
         end
         local ran = math.random(2,9)
         meta:set_int('count', count - 1)
         meta:set_int('answer', ran)
         if count > 1 then
            minetest.show_formspec(name, 'tasks:microscope', microscope_identify(pos, ran, count-1))
         else
            minetest.close_formspec(name, 'tasks:microscope')
            local info_working = meta:get_string('info_working')
            meta:set_string('infotext', info_working)
         end
      end
   end
end)
