local box = {
   type = 'fixed',
   fixed = {-.4, -.5, -.4, .4, -.4, .4}, -- Right, Bottom, Back, Left, Top, Front
}

local function code_formspec_answer(pos)
   local meta = minetest.get_meta(pos)
   local num = math.random(800000,900000)
   meta:set_string('num', num)
   local formspec =
      'formspec_version[3]'..
      'size[8,8]'..
      'background[0,0;8,8;tasks_code_entry_bg.png]'..
      'hypertext[.75,.75;6.5,2.25;nil;<global halign=center valign=middle><style color=black size=50>'..num..'<style>]'
   return formspec
end

local code_formspec_question =
   'formspec_version[3]'..
   'size[8,8]'..
   'background[0,0;8,8;tasks_code_entry_bg.png]'..
   'hypertext[.75,.75;6.5,1.25;nil;<global halign=center valign=middle><style color=black size=50>Enter Code:<style>]'..
   'style_type[field;border=false;font=mono;font_size=50;textcolor=black]'..
   'field[.75,1.8;6.5,1;number;;]'..
   'style_type[button;border=false;bgimg=blank.png]'..
   'style_type[button:hovered;bgimg=tasks_code_btn_sel.png]'..
   'button_exit[2.58,6.44;1,1;save;]'..
   'button_exit[.58,6.44;1,1;quit;]'

minetest.register_node('tasks:code_0', {
   description = 'code node',
   drawtype = 'mesh',
   param = 'light',
   paramtype2 = 'wallmounted',
   mesh = 'tasks_wall_panel.obj',
   tiles = {'task_screen.png'},
   groups = {breakable=1, tasks=1},
   light_source = 14,
   selection_box = box,
   collision_box = box,
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      meta:set_int('time_min', 240)
      meta:set_int('time_max', 600)
      meta:set_int('xp', 1)
      meta:set_int('level', 0)
      meta:set_string('infotext', '')
      meta:set_string('info_working', '')
      meta:set_string('info_repair', 'Awaiting code entry')
   end,
   on_rightclick = function(pos, node, clicker)
      local timer = minetest.get_node_timer(pos)
      local meta = minetest.get_meta(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)
      timer:start(random_number)
      minetest.chat_send_player(clicker:get_player_name(), 'No code required now.')
   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:code_1', param2 = node.param2})
      meta:set_string('infotext', infotext)
   end,
})

minetest.register_node('tasks:code_1', {
   description = 'code node',
   drawtype = 'mesh',
   param = 'light',
   paramtype2 = 'wallmounted',
   mesh = 'tasks_wall_panel.obj',
   tiles = {'task_screen_1.png'},
   groups = {breakable=1, not_in_creative_inventory=1, tasks=1},
   light_source = 2,
   selection_box = box,
   collision_box = box,
   drop = 'tasks:code_0',
   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 level = meta:get_int('level') or 0
      if level < sabotage_level then
         tasks.player_config[name] = pos
         minetest.show_formspec(name, 'tasks:code', code_formspec_answer(pos))
         minetest.after(2, function()
            minetest.show_formspec(name, 'tasks:code', code_formspec_question)
         end)
      end
   end
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
   local name = player:get_player_name()
   if formname == 'tasks:code' then
      if fields.save or fields.number then
         local pos = tasks.player_config[name]
         local meta = minetest.get_meta(pos)
         local answer = meta:get_string('num')
         if answer == fields.number then
            local node = minetest.get_node(pos)
            local infotext = meta:get_string('info_working')
            tasks.add_xp(pos, node, player, 'tasks:code_0')
            meta:set_string('infotext', infotext)
         else
            minetest.chat_send_player(name, 'Try again!')
         end
      end
   end
end)
