-- sfinv/init.lua

dofile(minetest.get_modpath('sfinv') .. '/api.lua')

-- Load support for MT game translation.
local S = minetest.get_translator('sfinv')
local esc = minetest.formspec_escape
local modstorage = minetest.get_mod_storage()

sfinv.register_page('sfinv:help', {
   title = S('Help'),
   get = function(self, player, context)
      local player_attributes = player:get_meta()
      local xp = player_attributes:get_float('xp')
      local luck = player_attributes:get_int('luck')
      local health = player_attributes:get_int('hp')
      local mode = player_attributes:get_string('mode')
      if not creative.is_enabled_for(player:get_player_name()) then
         return sfinv.make_formspec(player, context,
            'textarea[.25,0;2,2;;;Player Stats:\n XP: '..xp..'\n HP: '..health..'\n Luck: '..luck..']'..
            'textarea[2,0;6.5,5.5;;;   Traitor is a game based on Among Us. '..
            'Your goal is to vote out the traitor if you are not the traitor, or to kill all the other players if you are the traitor. '..
            'Visit the shop to buy upgrades, privileges, and at some point cosmetics!\n\n'..
            '   To create your own level(s) purchase the builder priv and claim a level button.]'
            , true)
      else
         return sfinv.make_formspec(player, context,
            'textarea[.25,0;2,2;;;Player Stats:\n XP: '..xp..'\n HP: '..health..'\n Luck: '..luck..']'..
            'textarea[2,0;6.5,10.5;;;   As a builder you have the ability to create your own levels, individually or collaboratively with other builders. '..
            'Build any size and with any theme, your imagination is the only limit. (Contact the admin if you need specific nodes/tasks.) '..
            'The spawn area on the level needs to be at minimum 5x5 to prevent players from spawning inside of objects or outside of the map.\n\n'..
            '   The first step to building a level is to select an area on the map. '..
            'You should always protect your area as soon as possible to prevent others from damaging it. '..
            'You can always make it larger and add other players to it if you want to build with a friend. '..
            'Once you have the level in a safely playable state you can claim a button in the spawn lobby and fill out the required information. '..
            'The button can be edited by right-clicking with the hammer or wrench. You can also unclaim a button this way. (Note: this won\'t delete the level)\n\n'..
            '   Tasks, once placed can be configured with the wrench tool. You can set the infotext, XP reward, and time range for the task to become available.\n\n'..
            '   Standard doors from the doors mod can be locked with the wrench. '..
            'There are several options for the type of locking, and further information is given when configuring the lock.\n\n'..
            '   Many items that would normally hold inventory can be setup with a stash of eight items. '..
            'Currently cabinets, chests, clocks, and file cabinets can hold items. '..
            'Punch them with a wrench to setup what items should be available for drops. ]'
            , false)
      end
   end
})

sfinv.register_page('sfinv:notes', {
   title = 'Notes',
   get = function(self, player, context)
      local content = modstorage:get_string(player:get_player_name())
      context.notes = content
      return sfinv.make_formspec(player, context,
         'textarea[0.25,0;8,10;content;;'..esc(content)..']'..
         'textarea[.25,8.75;5.5,.6;;;BE SURE TO HIT THE SAVE BUTTON!!!]'..
         'button[6,8.5;2,1;save_player_note;Save]'
         , false)
   end,
   on_player_receive_fields = function(self, player, context, fields)
      if fields.content then
         context.notes = fields.content
      end
   end
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
   local name = player:get_player_name()
   if fields.save_player_note then
      modstorage:set_string(name, fields.content)
   end
end)

sfinv.register_page('sfinv:xp', {
   title = 'XP',
   is_in_nav = function(self, player, context)
      return not creative.is_enabled_for(player:get_player_name())
   end,
   get = function(self, player, context)
      local player_attributes = player:get_meta()
      local xp = player_attributes:get_float('xp') or 0
      return sfinv.make_formspec(player, context,
      'textarea[.25,0;8,5;;;   You currently have '..xp..' xp available. use these points to buy perks in a store.\n\n'..
      '   This is NOT a pay to win scheme. Items are cosmetic or give buffs, but won\'t protect you from the traitor in any fashion.\n\n'..
      '   If you want to build your own levels you can purchase \'buttons\' with your points, and the required privileges to create. '..
      'When joining a game your privileges are cleared, but can be reset once you\'re back in the lobby'..
      '   You earn 25 points for every level you win, either as the traitor, or a surviving team member. You can also earn 1 point for each task you complete in the lobby.]'
      , false)
   end
})

sfinv.register_page('sfinv:debug', {
   title = 'Debug',
   is_in_nav = function(self, player, context)
      return minetest.check_player_privs(player:get_player_name(), { traitor_dev = true })
   end,
   get = function(self, player, context)
      local name = player:get_player_name()
      local player_attributes = player:get_meta()
      local xp = player_attributes:get_float('xp')
      local luck = player_attributes:get_int('luck')
      local health = player_attributes:get_int('hp')
      local mode = player_attributes:get_string('mode')
      local map_id = lobby.game[name]
      return sfinv.make_formspec(player, context,
         'textarea[.25,0;3,2;;;Player Stats:\n XP: '..xp..'\n HP: '..health..'\n Luck: '..luck..']'..
         'textarea[3.5,0;3,2;;;Playing as:\n '..mode..'\nOn map:\n '..map_id..']'..
         'textarea[.25,2.5;8,4;;;This screen will be expanded with time to include helpful debug information for people play testing.]'
      , false)
   end
})

sfinv.register_page('sfinv:clothing', {
   title = 'Clothes',
   is_in_nav = function(self, player, context)
      local player_attributes = player:get_meta()
      local mode = player_attributes:get_string('mode')
      if mode == 'player' then
         return false
      else
         return true
      end
   end,
   get = function(self, player, context)
      local name = player:get_player_name()
      return sfinv.make_formspec(player, context,
         'textarea[.5,3;7.5,3;;;Clothing is layered from right to left in each row. Lower rows are layered over higher rows. Hats/headgear should go in the top row or they may display incorrectly. Swap around clothing locations to see how it effects your looks!]'..
         'list[detached:'..name..'_clothing;slots;2,0;6,3]'..
         'list[current_player;clothing;0,6;8,3;]'..
         'listring[]'
      , false)
   end
})
