local priv_fire_formspec_base =
   'size[12,7]'..
   'no_prepend[]'..
   'bgcolor[#080808BB;true]'..
   'background[0,0;12,7;hall_chalkboard_bg.png]'..
   'hypertext[0,.2;12,1;;<center><style color=white size=40>~~Fire Privilege~~</style></center>]'

local esc = minetest.formspec_escape

local lesson = "The Fire privilege will let you start things on fire, and use explosives. "..
   "Due to the potentially dangerous nature of these things it's important that you understand required safety precautions. "..
   "On this server grass can burn, so it is all the more important to construct fire lanes before starting fires. "..
   "A good firelane will be two nodes wide, built of a nonflammable material, and have some anti-fire powder placed in it as well. "..
   "If you don't have anti-fire powder be sure and watch the fire lane and stop any fire that may pass over. "..
   "Starting fires that get out of hand can be grounds for jailing, and if repeated potential banning. "..
   "You can start fires with Flint & Steel and Lava."

local priv_fire_formspec_lesson =
   priv_fire_formspec_base..
   "textarea[.75,1.5;11.25,6;;;"..esc(lesson).."]" ..
   'button[4.5,5;3,1;go;Take Quiz]'

local priv_fire_formspec_1 =
   priv_fire_formspec_base..
   "textarea[1,1.5;11,3;;;Is fire enabled on this server?]" ..
   'button[1,4;3,1;wrong;A) Do pigs fly?]'..
   'button[4.5,4;3,1;wrong;B) Certainly Not]'..
   'button[8,4;3,1;wrong;C) A Nope]'..
   'button[1,5.5;3,1;right;D) Yes]'..
   'button[4.5,5.5;3,1;wrong;E) No]'..
   'button[8,5.5;3,1;right;F) Yep]'

local priv_fire_formspec_2 =
   priv_fire_formspec_base..
   "textarea[1,1.5;11,3;;;What makes a good fire lane?]" ..
   'button[1,4;3,1;wrong;A) Lava]'..
   'button[4.5,4;3,1;right;B) Water]'..
   'button[8,4;3,1;wrong;C) Dirt with Grass]'..
   'button[1,5.5;3,1;right;D) Stone]'..
   'button[4.5,5.5;3,1;right;E) Anti-fire Powder]'..
   'button[8,5.5;3,1;right;F) Nonflamable Nodes]'

local priv_fire_formspec_3 =
   priv_fire_formspec_base..
   "textarea[1,1.5;11,3;;;How wide should a firelane be?]" ..
   'button[1,4;3,1;wrong;A) 1 Node or More]'..
   'button[4.5,4;3,1;right;B) 2 Nodes or More]'..
   'button[8,4;3,1;wrong;C) 3 Nodes or More]'..
   'button[1,5.5;3,1;wrong;D) 4 Nodes or More]'..
   'button[4.5,5.5;3,1;wrong;E) 5 Nodes or More]'..
   'button[8,5.5;3,1;wrong;F) 6 Nodes or More]'

local priv_fire_formspec_4 =
   priv_fire_formspec_base..
   "textarea[1,1.5;11,3;;;Which of the follow can start fires.?]" ..
      'button[1,4;3,1;wrong;A) Flint & Steel]'..
      'button[4.5,4;3,1;wrong;B) Lava]'..
      'button[8,4;3,1;wrong;C) Torches]'..
      'button[1,5.5;3,1;wrong;D) Brasiers]'..
      'button[4.5,5.5;3,1;wrong;E) Hot Cobble]'..
      'button[8,5.5;3,1;right;F) Both A and B]'

local priv_fire_formspec_5 =
   priv_fire_formspec_base..
   "textarea[1,1.5;11,3;;;You've passed the quiz! You now have the fire privilege. Remember only YOU can prevent forest fires.]"..
   'button_exit[4.5,5;3,1;;Exit]'


minetest.register_on_player_receive_fields(function(player, formname, fields)
   local name = player:get_player_name()
   if formname == 'hall:priv_fire_lesson' then
      if fields.go then
         minetest.show_formspec(name, 'hall:priv_fire_1', priv_fire_formspec_1)
      end
   elseif formname == 'hall:priv_fire_1' then
      if fields.right then
         minetest.show_formspec(name, 'hall:priv_fire_2', priv_fire_formspec_2)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:priv_fire_lesson', priv_fire_formspec_lesson)
      end
   elseif formname == 'hall:priv_fire_2' then
      if fields.right then
         minetest.show_formspec(name, 'hall:priv_fire_3', priv_fire_formspec_3)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:priv_fire_lesson', priv_fire_formspec_lesson)
      end
   elseif formname == 'hall:priv_fire_3' then
      if fields.right then
         minetest.show_formspec(name, 'hall:priv_fire_4', priv_fire_formspec_4)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:priv_fire_lesson', priv_fire_formspec_lesson)
      end
   elseif formname == 'hall:priv_fire_4' then
      if fields.right then
         minetest.show_formspec(name, 'hall:priv_fire_5', priv_fire_formspec_5)
         local privs = minetest.get_player_privs(name)
         privs.fire = true
         minetest.set_player_privs(name, privs)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:priv_fire_lesson', priv_fire_formspec_lesson)
      end
   end
end)

minetest.register_node('hall:priv_fire', {
   description = 'Fire Course',
   drawtype = 'mesh',
   mesh = 'hall_chalkboard.obj',
   tiles = {'hall_chalkboard_fire.png'},
   paramtype2 = 'facedir',
   paramtype = 'light',
   selection_box = {
      type = 'fixed',
      fixed = {-1, -.75, .4375, 1, .5, .5},
      },
   collision_box = {
      type = 'fixed',
      fixed = {-1, -.75, .4375, 1, .5, .5},
      },
   groups = {oddly_breakable_by_hand = 2, choppy=3, not_in_creative_inventory=1},
   on_rightclick = function(pos, node, clicker, itemstack)
      local meta = minetest.get_meta(pos)
      local name = clicker:get_player_name()
      meta:set_string('infotext', 'Fire Privs Course')
      minetest.show_formspec(name, 'hall:priv_fire_lesson', priv_fire_formspec_lesson)
   end,
})
