local markets_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>~~Commodity Markets~~</style></center>]'

local esc = minetest.formspec_escape

local lesson = "The commodity markets create a network of 'shops' that you can sell and buy from. "..
   "  Items can be placed for sale at any location, and be purchased from any other location. "..
   "This means you can place a Trading Post at your base to summon a caravan when you want to sell or buy without "..
   "needing to travel to a central location to access the market.\n"..
   "  Markets use coins, which can be gathered by killing mobs, or by converting gold ingots. "..
   "One gold ingot is equivalent to one thousand coins. You can add coins or gold to your account by clicking on the "..
   "'Your Inventory' tab in the market and placing the coins or gold in the input slot on the left. You can withdraw gold or coins "..
   "by typing in the value you want to withdraw and clicking 'Withdraw' on the right. A value of more than one thousand will "..
   "be automatically converted to gold, with any remainder being cashed out in coins.\n"..
   "  The markets create a free market, you are able to sell items for any price you want, and buyers can decide if you are "..
   "offering a fair deal or not. A savvy player can watch the markets buying items low and selling them high.\n"..
   "  To sell items add them to the market inventory on the 'Your Inventory' tab, and then click on the 'Market Orders' tab. "..
   "Use the search box to easily find the items in your inventory. To put the item up for sale click on the listing, enter the "..
   "quantity you want to sell and how many coins you want, and then click the sell button.\n"..
   "  If you want to buy an item that isn't listed for sale you can create a Buy order. Click on the listing for the item you want to buy, "..
   "enter the quantity you'd like to purchase, and how much you're willing to pay, and click on the Buy button. To buy an item that is listed "..
   "you need only click on the item, the quantity you want to purchase and a price equal or greater than the listed price.\n"..
   "  You can cancel your orders by double clicking on them in the list. If it was a sell order the items will be added back to your available "..
   "inventory in the 'Your Inventory' tab.\n"..
   "  You are limited to one thousand items in the market inventory at any one time."


local markets_formspec_lesson =
   markets_formspec_base..
   "textarea[.75,1.5;11.25,4;;;"..esc(lesson).."]" ..
   'button[4.5,5;3,1;go;Test your knowledge]'

local markets_formspec_1 =
   markets_formspec_base..
   "textarea[1,1.5;11,3;;;A gold ingot can be converted into how many coins?]" ..
   'button[1,4;3,1;wrong;A) 1]'..
   'button[4.5,4;3,1;wrong;B) 10]'..
   'button[8,4;3,1;wrong;C) 100]'..
   'button[1,5.5;3,1;right;D) 1,000]'..
   'button[4.5,5.5;3,1;wrong;E) 10,000]'..
   'button[8,5.5;3,1;wrong;F) 100,000]'

local markets_formspec_2 =
   markets_formspec_base..
   "textarea[1,1.5;11,3;;;What item will summon a caravan?]" ..
   'button[1,4;3,1;wrong;A) A Trader Mob]'..
   'button[4.5,4;3,1;wrong;B) A Secret Keyword]'..
   'button[8,4;3,1;wrong;C) A Caravan Scroll]'..
   'button[1,5.5;3,1;wrong;D) A Pile of Zombie Teeth]'..
   'button[4.5,5.5;3,1;wrong;E) A Diamond Block]'..
   'button[8,5.5;3,1;right;F) A Trading Post]'

local markets_formspec_3 =
   markets_formspec_base..
   "textarea[1,1.5;11,3;;;Can you pull coins/gold out of the market?]" ..
   'button[1,4;3,1;right;A) Yes]'..
   'button[4.5,4;3,1;right;B) Yes]'..
   "button[8,4;3,1;right;C) Yes]"..
   "button[1,5.5;3,1;wrong;D) No]"..
   'button[4.5,5.5;3,1;wrong;E) No]'..
   "button[8,5.5;3,1;wrong;F) No]"

local markets_formspec_4 =
   markets_formspec_base..
   "textarea[1,1.5;11,3;;;How many items can you have in the market at once?]" ..
   'button[1,4;3,1;wrong;A) 1]'..
   'button[4.5,4;3,1;wrong;B) 10]'..
   'button[8,4;3,1;wrong;C) 100]'..
   'button[1,5.5;3,1;right;D) 1,000]'..
   'button[4.5,5.5;3,1;wrong;E) 10,000]'..
   'button[8,5.5;3,1;wrong;F) 100,000]'

local markets_formspec_5 =
   markets_formspec_base..
   "textarea[1,1.5;11,3;;;You've passed the quiz! Here's a free trading post.]"..
   '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:markets_lesson' then
      if fields.go then
         minetest.show_formspec(name, 'hall:markets_1', markets_formspec_1)
      end
   elseif formname == 'hall:markets_1' then
      if fields.right then
         minetest.show_formspec(name, 'hall:markets_2', markets_formspec_2)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:markets_lesson', markets_formspec_lesson)
      end
   elseif formname == 'hall:markets_2' then
      if fields.right then
         minetest.show_formspec(name, 'hall:markets_3', markets_formspec_3)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:markets_lesson', markets_formspec_lesson)
      end
   elseif formname == 'hall:markets_3' then
      if fields.right then
         minetest.show_formspec(name, 'hall:markets_4', markets_formspec_4)
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:markets_lesson', markets_formspec_lesson)
      end
   elseif formname == 'hall:markets_4' then
      if fields.right then
         minetest.show_formspec(name, 'hall:markets_5', markets_formspec_5)
         local give = hall.storage:get_string(name..'_market')
         if give ~= 'earned' then
            local player_inv = player:get_inventory()
            if player_inv:room_for_item('main', 'commoditymarket:caravan_post') then
               player_inv:add_item('main', 'commoditymarket:caravan_post')
            else
               local drop_pos = player:get_pos()
               minetest.add_item(drop_pos, 'commoditymarket:caravan_post')
            end
            hall.storage:set_string(name..'_market', 'earned')
         end
      elseif fields.wrong then
         minetest.show_formspec(name, 'hall:markets_lesson', markets_formspec_lesson)
      end
   end
end)

minetest.register_node('hall:markets', {
   description = 'Markets Course',
   drawtype = 'mesh',
   mesh = 'hall_chalkboard.obj',
   tiles = {'hall_chalkboard_markets.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', 'Market Course')
      minetest.show_formspec(name, 'hall:markets_lesson', markets_formspec_lesson)
   end,
})
