local priv_spill_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>~~Spill Privilege~~</style></center>]'

local esc = minetest.formspec_escape

local lesson = "The spill privilege will let you empty liquids from buckets. "..
   "Due to the griefing potential of liquids there is this quiz. "..
   "There are a few things you should know about liquids and spilling them. Besides the obvious that spills can get messy, "..
   "lava will start flammable nodes on fire. If you're going to place lava make sure there isn't anything around that can burn. "..
   "Keep in mind that Grass is flammable on this server. Water is less dangerous, but due to it's infinite nature "..
   "it can easily make a huge mess that will take a while to clean up. \n"..
   "Bulk dumping of water and lava underground, away from other players areas is fine. Be it for farming mobs, or making elevators."..
   "You must never empty liquids in such a way that they flow into another players protected area(s)."..
   "You may face jail time, or banning for intentionally ignoring these rules."

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

local priv_spill_formspec_1 =
   priv_spill_formspec_base..
   "textarea[1,1.5;11,3;;;What privilege do you need to empty buckets?]" ..
   'button[1,4;3,1;wrong;A) Bucket]'..
   'button[4.5,4;3,1;wrong;B) Liquid]'..
   'button[8,4;3,1;wrong;C)  Splash]'..
   'button[1,5.5;3,1;right;D) Spill]'..
   'button[4.5,5.5;3,1;wrong;E) Water]'..
   'button[8,5.5;3,1;wrong;F) Lava]'

local priv_spill_formspec_2 =
   priv_spill_formspec_base..
   "textarea[1,1.5;11,3;;;Are water elevators allowed?]" ..
   'button[1,4;3,1;wrong;A) No]'..
   'button[4.5,4;3,1;wrong;B) No]'..
   'button[8,4;3,1;wrong;C) No]'..
   'button[1,5.5;3,1;right;D) Yes]'..
   'button[4.5,5.5;3,1;right;E) Yes]'..
   'button[8,5.5;3,1;right;F) Yes]'

local priv_spill_formspec_3 =
   priv_spill_formspec_base..
   "textarea[1,1.5;11,3;;;Do you need the spill privilege to pick up liquids with a bucket?]" ..
   'button[1,4;3,1;right;A) No]'..
   'button[4.5,4;3,1;wrong;B) Yes]'..
   "button[8,4;3,1;right;C) The lesson didn't say]"..
   "button[1,5.5;3,1;right;D) I don't know]"..
   'button[4.5,5.5;3,1;wrong;E) This quiz is junk!]'..
   "button[8,5.5;3,1;wrong;F) Don't pick me]"

local priv_spill_formspec_4 =
   priv_spill_formspec_base..
   "textarea[1,1.5;11,3;;;Are you allowed to grief with liquids?]" ..
   'button[1,4;3,1;wrong;A) Yes]'..
   'button[4.5,4;3,1;wrong;B) Sure]'..
   'button[8,4;3,1;wrong;C) Why not?]'..
   'button[1,5.5;3,1;right;D) No]'..
   'button[4.5,5.5;3,1;right;E) Nope]'..
   'button[8,5.5;3,1;right;F) Absolutely not]'

local priv_spill_formspec_5 =
   priv_spill_formspec_base..
   "textarea[1,1.5;11,3;;;You've passed the quiz! You now have the spill privilege]"..
   '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_spill_lesson' then
      if fields.go then
         minetest.show_formspec(name, 'hall:priv_spill_1', priv_spill_formspec_1)
      end
   elseif formname == 'hall:priv_spill_1' then
      if fields.right then
         minetest.show_formspec(name, 'hall:priv_spill_2', priv_spill_formspec_2)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:priv_spill_lesson', priv_spill_formspec_lesson)
      end
   elseif formname == 'hall:priv_spill_2' then
      if fields.right then
         minetest.show_formspec(name, 'hall:priv_spill_3', priv_spill_formspec_3)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:priv_spill_lesson', priv_spill_formspec_lesson)
      end
   elseif formname == 'hall:priv_spill_3' then
      if fields.right then
         minetest.show_formspec(name, 'hall:priv_spill_4', priv_spill_formspec_4)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:priv_spill_lesson', priv_spill_formspec_lesson)
      end
   elseif formname == 'hall:priv_spill_4' then
      if fields.right then
         minetest.show_formspec(name, 'hall:priv_spill_5', priv_spill_formspec_5)
         local privs = minetest.get_player_privs(name)
         privs.spill = true
         minetest.set_player_privs(name, privs)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:priv_spill_lesson', priv_spill_formspec_lesson)
      end
   end
end)

minetest.register_node('hall:priv_spill', {
   description = 'Spill Course',
   drawtype = 'mesh',
   mesh = 'hall_chalkboard.obj',
   tiles = {'hall_chalkboard_spill.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', 'Spill Privs Course')
      minetest.show_formspec(name, 'hall:priv_spill_lesson', priv_spill_formspec_lesson)
   end,
})
