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

local esc = minetest.formspec_escape

local lesson = "The Areas privilege will let you create, rename, transfer, and delete areas that you have created. "..
   "To create an area you will need a land deed, one of which will be given to you upon the completion of this course. "..
   "Set the corners of the area you want to protect with /area_pos1 and /area_pos2 and protect the area with /protect <areaname>\n"..
   "You can add other players to your area using /add_owner <ID> <playername> <areaname>\n"..
   "You can list all your areas with /list_areas and can remove an area with /remove_area <ID>\n"..
   "If you want to rename an area use /rename_area <ID> <newareaname>\n"..
   "The ID of an area can be found by looking in the lower left corner of your screen while you are in the area. It will be a numerical value."..
   "You can only protect five areas max size of 128*128*128, and must keep twenty-five nodes space between another players area and your own. "..
   "Land deeds are not returned when deleting an area, due to potential exploits. If you want your deed back have an admin delete your area."..
   "Be careful when creating an area to make sure there are not other players protection blocks within the area you are trying to protect."

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

local priv_areas_formspec_1 =
   priv_areas_formspec_base..
   "textarea[1,1.5;11,3;;;What do you need to create an area?]" ..
   'button[1,4;3,1;wrong;A) Land Deed]'..
   'button[4.5,4;3,1;wrong;B) A Diamond Block]'..
   'button[8,4;3,1;wrong;C) A Secret Potion]'..
   'button[1,5.5;3,1;wrong;D) Twenty-five Zombie Teeth]'..
   'button[4.5,5.5;3,1;wrong;E) The Areas Priv]'..
   'button[8,5.5;3,1;right;F) Both A and E]'

local priv_areas_formspec_2 =
   priv_areas_formspec_base..
   "textarea[1,1.5;11,3;;;How many areas is a player allowed to have?]" ..
   'button[1,4;3,1;wrong;A) One]'..
   'button[4.5,4;3,1;wrong;B) Three]'..
   'button[8,4;3,1;right;C) Five]'..
   'button[1,5.5;3,1;wrong;D) Seven]'..
   'button[4.5,5.5;3,1;wrong;E) Nine]'..
   'button[8,5.5;3,1;wrong;F) Eleven]'

local priv_areas_formspec_3 =
   priv_areas_formspec_base..
   "textarea[1,1.5;11,3;;;What is the maximum size of an area?]" ..
   'button[1,4;3,1;wrong;A) 32*32*32]'..
   'button[4.5,4;3,1;wrong;B) 40*40*40]'..
   'button[8,4;3,1;wrong;C) 64*64*64]'..
   'button[1,5.5;3,1;wrong;D) 256*256*256]'..
   'button[4.5,5.5;3,1;right;E) 128*128*128]'..
   'button[8,5.5;3,1;wrong;F) 100*100*100]'

local priv_areas_formspec_4 =
   priv_areas_formspec_base..
   "textarea[1,1.5;11,3;;;How much space do you need to leave between another players area and your own?]" ..
      'button[1,4;3,1;wrong;A) None]'..
      'button[4.5,4;3,1;wrong;B) One Node]'..
      'button[8,4;3,1;wrong;C) Ten Nodes]'..
      'button[1,5.5;3,1;right;D) Twenty-five Nodes]'..
      'button[4.5,5.5;3,1;wrong;E) Fifty Nodes]'..
      'button[8,5.5;3,1;wrong;F) One Hundred Nodes]'

local priv_areas_formspec_5 =
   priv_areas_formspec_base..
   "textarea[1,1.5;11,3;;;You've passed the quiz! If you haven't taken the quiz before a land deed was added to your inventory, or dropped at your feet.]"..
   '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_areas_lesson' then
      if fields.go then
         minetest.show_formspec(name, 'hall:priv_areas_1', priv_areas_formspec_1)
      end
   elseif formname == 'hall:priv_areas_1' then
      if fields.right then
         minetest.show_formspec(name, 'hall:priv_areas_2', priv_areas_formspec_2)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:priv_areas_lesson', priv_areas_formspec_lesson)
      end
   elseif formname == 'hall:priv_areas_2' then
      if fields.right then
         minetest.show_formspec(name, 'hall:priv_areas_3', priv_areas_formspec_3)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:priv_areas_lesson', priv_areas_formspec_lesson)
      end
   elseif formname == 'hall:priv_areas_3' then
      if fields.right then
         minetest.show_formspec(name, 'hall:priv_areas_4', priv_areas_formspec_4)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:priv_areas_lesson', priv_areas_formspec_lesson)
      end
   elseif formname == 'hall:priv_areas_4' then
      if fields.right then
         minetest.show_formspec(name, 'hall:priv_areas_5', priv_areas_formspec_5)
         local give = hall.storage:get_string(name..'_areas')
         if give ~= 'earned' then
            local player_inv = player:get_inventory()
            if player_inv:room_for_item('main', 'epic:deed') then
               player_inv:add_item('main', 'epic:deed')
            else
               local drop_pos = player:get_pos()
               minetest.add_item(drop_pos, 'epic:deed')
            end
         end
         local privs = minetest.get_player_privs(name)
         privs.areas = true
         minetest.set_player_privs(name, privs)
         hall.storage:set_string(name..'_areas', 'earned')
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:priv_areas_lesson', priv_areas_formspec_lesson)
      end
   end
end)

minetest.register_node('hall:priv_areas', {
   description = 'Areas Course',
   drawtype = 'mesh',
   mesh = 'hall_chalkboard.obj',
   tiles = {'hall_chalkboard_areas.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)
      meta:set_string('infotext', 'Areas Privs Course')
      local name = clicker:get_player_name()
      minetest.show_formspec(name, 'hall:priv_areas_lesson', priv_areas_formspec_lesson)
   end,
})
