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

local esc = minetest.formspec_escape

local lesson = "The lumberjack mod should automatically give you the lumberjack privilege after "..
   "you harvest enough trees and plant saplings, but it seems to not always work, so if you don't have "..
   "the privilege there is this course you can take to get it. :)\n"..
   "The lumberjack privilege lets you chop and entire tree by just breaking one node at the bottom.\n"..
   "There are no hard rules reguarding chopping down trees, but you should replant when possible so we "..
   "don't run out of wood.\n"..
   "When cutting down a full tree your axe will take the combined damage of all the nodes, so a very large tree "..
   "may add a lot of wear. This is not a bug!"

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

local priv_lumberjack_formspec_1 =
   priv_lumberjack_formspec_base..
   "textarea[1,1.5;11,3;;;What does the lumberjack priv let you do?]" ..
   'button[1,4;3,1;wrong;A) Grow an epic beard.]'..
   'button[4.5,4;3,1;wrong;B) Do tricks with axes.]'..
   'button[8,4;3,1;wrong;C) Ride log rafts.]'..
   'button[1,5.5;3,1;wrong;D) Juggle chainsaws.]'..
   'button[4.5,5.5;3,1;right;E) Chop down entire trees.]'..
   'button[8,5.5;3,1;wrong;F) Tame blue oxen.]'

local priv_lumberjack_formspec_2 =
   priv_lumberjack_formspec_base..
   "textarea[1,1.5;11,3;;;Are you REQUIRED to replant every tree you cut down?]" ..
   'button[1,4;3,1;right;A) No]'..
   'button[4.5,4;3,1;right;B) No]'..
   'button[8,4;3,1;right;C) No]'..
   'button[1,5.5;3,1;wrong;D) Yes]'..
   'button[4.5,5.5;3,1;wrong;E) Yes]'..
   'button[8,5.5;3,1;wrong;F) Yes]'

local priv_lumberjack_formspec_3 =
   priv_lumberjack_formspec_base..
   "textarea[1,1.5;11,3;;;You've passed the quiz! You now have the lumberjack 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_lumberjack_lesson' then
      if fields.go then
         minetest.show_formspec(name, 'hall:priv_lumberjack_1', priv_lumberjack_formspec_1)
      end
   elseif formname == 'hall:priv_lumberjack_1' then
      if fields.right then
         minetest.show_formspec(name, 'hall:priv_lumberjack_2', priv_lumberjack_formspec_2)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:priv_lumberjack_lesson', priv_lumberjack_formspec_lesson)
      end
   elseif formname == 'hall:priv_lumberjack_2' then
      if fields.right then
         minetest.show_formspec(name, 'hall:priv_lumberjack_3', priv_lumberjack_formspec_3)
         local privs = minetest.get_player_privs(name)
         privs.lumberjack = true
         minetest.set_player_privs(name, privs)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:priv_lumberjack_lesson', priv_lumberjack_formspec_lesson)
      end
   end
end)

minetest.register_node('hall:priv_lumberjack', {
   description = 'Lumberjack Course',
   drawtype = 'mesh',
   mesh = 'hall_chalkboard.obj',
   tiles = {'hall_chalkboard_lumberjack.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', 'Lumberjack Privs Course')
      local name = clicker:get_player_name()
      minetest.show_formspec(name, 'hall:priv_lumberjack_lesson', priv_lumberjack_formspec_lesson)
   end,
})
