local esc = minetest.formspec_escape

local page_1 = "Welcome! Thanks for trying out this Among Us clone/rip-off.\n"..
   "This short tutorial should help you get on your feet, and prepare you for what lies ahead. "..
   "If you've never played Among Us, or know nothing about the game you'll want to read this page, "..
   "otherwise feel free to skip ahead to the next page.\n\n"..
   "In this game you play with a team of others, one member of the team is a traitor/imposter. "..
   "The traitor tries to kill all the other players without being outted as the traitor, "..
   "the other team members complete tasks and try to raise XP to defeat the traitor.\n"..
   "The team wins by voting out the traitor, or earning enough XP. Different levels have different player requirements, and XP goals."

local page_2 = "Tasks\n"..
   "There are a wide variety of tasks, different tasks can give different amounts of XP. "..
   "Map makers are able to set their own XP requirements, but don't worry, every time you earn XP "..
   "you'll get a helpful chat message telling you how much more XP you need to defeat the traitor.\n"..
   "Some tasks require you to get a replacement part, click a button, enter a code, or even just punch the node. "

local page_3 = "Voting\n"..
   "Voting is one way to try and defeat the traitor. When a corpse is reported all players on a level will teleport "..
   "to that level's spawn location, and have a chance to discuss who they think the traitor is, and then vote. "..
   "A vote can also by triggered by using the 'Emergency Meeting' (usually located near the level's spawn point)\n"..
   "You vote using the /kick command. You can only vote for players on the level."..
   "If you kick yourself you'll be immediately put back into the main lobby."

local page_4 = "Clothing\n"..
   "You can set a custom skin by running the /skin command. The first time is free, and future changes will cost you XP."..
   "You'll need to buy clothing as well, as we can't have naked players running around the server."..
   "Visit the shopping center to buy clothes, and the barber's to change your hairstyle."

lobby_help_1 =
   'formspec_version[3]'..
   'size[11,7]'..
   'bgcolor[#00000000;false]'..
   'background9[0,0;11,7;lobby_help_bg.png;true;16]'..
   'textarea[0.5,0.5;10.5,5;;;'..esc(page_1)..']'..
   'button[7.5,5;2.5,1;page_2;Next]'..
   'button_exit[4,5;3,1;exit;Exit]'

lobby_help_2 =
   'formspec_version[3]'..
   'size[11,7]'..
   'bgcolor[#00000000;false]'..
   'background9[0,0;11,7;lobby_help_bg.png;false;16]'..
   'textarea[0.5,0.5;10.5,5;;;'..esc(page_2)..']'..
   'button[7.5,5;2.5,1;page_3;Next]'..
   'button[1,5;2.5,1;page_1;Previous]'..
   'button_exit[4,5;3,1;exit;Exit]'

lobby_help_3 =
   'formspec_version[3]'..
   'size[11,7]'..
   'bgcolor[#00000000;false]'..
   'background9[0,0;11,7;lobby_help_bg.png;false;16]'..
   'textarea[0.5,0.5;10.5,5;;;'..esc(page_3)..']'..
   'button[1,5;2.5,1;page_2;Previous]'..
   'button[7.5,5;3,1;page_4;Next]'..
   'button_exit[4,5;3,1;exit;Exit]'

lobby_help_4 =
   'formspec_version[3]'..
   'size[11,7]'..
   'bgcolor[#00000000;false]'..
   'background9[0,0;11,7;lobby_help_bg.png;false;16]'..
   'textarea[0.5,0.5;10.5,5;;;'..esc(page_4)..']'..
   'button[1,5;2.5,1;page_3;Previous]'..
   'button_exit[4,5;3,1;proceed;Let\'s Go!]'

minetest.register_on_player_receive_fields(function(player, formname, fields)
   local name = player:get_player_name()
   if formname == 'lobby:help_1' then
      if fields.page_2 then
         minetest.show_formspec(name, 'lobby:help_2', lobby_help_2)
      end
   elseif formname == 'lobby:help_2' then
      if fields.page_1 then
         minetest.show_formspec(name, 'lobby:help_1', lobby_help_1)
      elseif fields.page_3 then
         minetest.show_formspec(name, 'lobby:help_3', lobby_help_3)
      end
   elseif formname == 'lobby:help_3' then
      if fields.page_2 then
         minetest.show_formspec(name, 'lobby:help_2', lobby_help_2)
      elseif fields.page_4 then
         minetest.show_formspec(name, 'lobby:help_4', lobby_help_4)
      end
   elseif formname == 'lobby:help_4' then
      if fields.page_3 then
         minetest.show_formspec(name, 'lobby:help_3', lobby_help_3)
      elseif fields.proceed then
         local privs = minetest.get_player_privs(name)
         privs.pro_player = true
         minetest.set_player_privs(name, privs)
         minetest.chat_send_player(name, 'Swell, enjoy the game, and please report any bugs to minetest@nathansalapat.com')
      end
   end
end)

function lobby.voting_formspec(map_id, refresh)
   local mugs = ''
   if refresh then
      local survivors = {}
      for _, player in pairs(minetest.get_connected_players()) do
         local name = player:get_player_name()
         if lobby.game[name] == map_id then
            survivors[#survivors+1] = name
         end
      end
      for i = 0, (#survivors-1) do
         local name = survivors[i+1]
         local player = minetest.get_player_by_name(name)
         local player_attributes = player:get_meta()
         local tone = player_attributes:get_int('tone')
         local eyes = player_attributes:get_int('eyes')
         local mouth = player_attributes:get_int('mouth')
         local hair = player_attributes:get_int('hair')
         local skin = 'gamer_skin_'..tone..'.png^gamer_eyes_'..eyes..'.png^gamer_mouth_'..mouth..'.png^clothing_hair_'..hair..'.png'
         --we start at zero so this will put six faces in a single line.
         if i < 5 then
            local offset = (i*3)+.5
            mugs = mugs..'model['..offset..',3;3,2;player model;clothing_hair_formspec.obj;'..skin..';0,0]label['..(offset + 1)..',5;'..name..']'
         else
            local offset = ((i*3)+.5)-15
            mugs = mugs..'model['..offset..',6;3,2;player model;clothing_hair_formspec.obj;'..skin..';0,0]label['..(offset + 1)..',8;'..name..']'
         end
      end
      lobby.survivors[map_id] = mugs
   else
      mugs = lobby.survivors[map_id] or ''
   end
   local formspec =
   'formspec_version[3]'..
   'size[16,12]'..
   'bgcolor[#080808BB;false]'..
   'textarea[.5,.5;15,2.5;;;Who do you think the traitor is? Now is your time to vote and try to kick them from the level. '..
   'There is a three minute timer which is reset each time somebody votes. '..
   'Should the timer hit zero before all votes are in the player with the most votes will be kicked.'..
   '\n\nUse chat command /kick <playername> to vote for the player you think is the traitor. Use chat command /skip to skip voting.'..
   '\nYou can see the list of survivors at any time by using the /survivors chat command]'..
   'button_exit[15.5,0;.5,.5;go;X]'..
   mugs
   return formspec
end

function lobby.survivors_formspec(map_id)
   local mugs = lobby.survivors[map_id] or ''
   local formspec =
   'formspec_version[3]'..
   'size[16,12]'..
   'bgcolor[#080808BB;false]'..
   'textarea[.5,.5;15,2.5;;;This is the most recent list of survivors on this level. '..
   'It is possible that some of these players may no longer be living, the list only updates when a body is reported or somebody calls an emergency meeting.]'..
   'button_exit[15.5,0;.5,.5;go;X]'..
   mugs
   return formspec
end

local text_body = [[
   Welcome to the exciting world of level building! You are only limited by your imagination, and to some degree the nodes available. (Send a message to Admin with any requests.)

   There are a few things you should know, they'll make your building experiences better/faster.

   When you claim a button it will be given a random map location, and a platform will be placed there. If you want to enter a different location you can, but no new platform will be placed. Starting out as a new builder you won't have worldedit, so you'll need to use the /placeblock chat command. This chat command will place a single block at your feet.

   Protect your area(s). Any builders can visit your level once you have it linked to a button, and could place/remove nodes if you don't protect the area. Use  /area_pos1 and /area_pos2 in alternate corners. You can always add extra areas or make a new larger area if need be.

   After you've built a little something send Admin an letter in the mailbox located outside of the post office and request worldedit, be sure and send some coordinates for your level. You'll likely be granted the privileges within several days.

   If you claim a button but don't make it playable and then don't visit the server for over sixty days the button will be unclaimed. Everything you built will still exist however and an Admin can retrieve the level data for you at another time if you decide to come back.

   You can view this information at any time by using the /level_help chat command.
]]

lobby.getting_started =
   'formspec_version[3]'..
   'size[12,10]'..
   'bgcolor[;neither]'..
   'background[0,0;12,10;lobby_level_help.png]'..
   'textarea[.5,.75;11,8;;;'..text_body..']'..
   'button_exit[4,8.5;4,1;close;Close This Dialog]'

function lobby.report_level(map_id)
   local map_name = lobby.savedata.id_2_name[map_id]
   local formspec = ''
   if map_name then
      formspec =
         'formspec_version[3]'..
         'size[12,8]'..
         'background[-1,-1;14,10;lobby_button_bg.png]'..
         'style_type[label;textcolor=red;font=bold;font_size=20]'..
         'label[2,.5;Report Level ('..map_name..')]'..
         'style_type[label;textcolor=;font=;font_size=]'..
         'label[.75,1.5;Report Reason:]'..
         'dropdown[.5,2;5,.5;reason;Incomplete,Broken,No tasks,Broken spawn;0]'..
         'textarea[.5,2.75;5,3.75;;Filling out this form will send a message to the server operator, the creator of the level, and you. The map name and creator are automatically added. Just select the reason for reporting the level and optionally fill out additional information.;]'..
         'textarea[6,1.66;5.5,5;input;Further Details (optional);]'..
         'button_exit[2,7;3,.75;save;Submit]'..
         'button_exit[7,7;3,.75;lol;Cancel]'
   else
      formspec =
      'formspec_version[3]'..
      'size[12,8]'..
      'textarea[3,1.66;6,5;;Sorry you can\'t report this level. You can\'t even play it so how could you possibly know anything about it to report???;]'..
      'button_exit[4.5,7;3,.75;lol;Okay]'
   end
   return formspec
end
