minetest.register_chatcommand('survivors', {
   description = 'See what players are still on the level',
   func = function(name)
      local map_id = lobby.game[name]
      local survivors = lobby.players_on_level(map_id)
      minetest.chat_send_player(name, survivors..' are left on this map.')
   end
})

minetest.register_chatcommand('spawn', {
   description = 'Teleport to spawn. (Builders only)',
   privs = {creative=true},
   func = function (name)
      local player = minetest.get_player_by_name(name)
      player:set_pos(lobby.spawn_pos)
      lobby.game[name] = 'lobby'
   end
})

minetest.register_chatcommand('msg', {
   description = 'prevents people from sending private messages.',
   func = function(name)
      minetest.chat_send_player(name, 'Private messages are disabled here.')
   end
})

minetest.register_chatcommand('placeblock', {
   description = 'Places a block where player stands.',
   privs = {creative=true},
   func = function(name)
      local user = minetest.get_player_by_name(name)
      local pos = user:get_pos()
      if minetest.is_protected(pos, name) then
         minetest.chat_send_player(name, 'Try again. You can\'t run this in a protected area.')
      else
         minetest.set_node(pos, {name='color:white'})
         minetest.chat_send_player(name, 'Placed starter block for you. Have fun building.')
      end
   end
})

minetest.register_chatcommand('lobby', {
   description = 'Travel back to the lobby when a ghost or playing solo.',
   func = function(name)
      local player = minetest.get_player_by_name(name)
      local player_attributes = player:get_meta()
      local mode = player_attributes:get_string('mode')
      if mode == 'ghost' or mode == 'solo' then
         player:set_nametag_attributes({
            color = {a = 255, r = 255, g = 255, b = 255}
         })
         player:set_properties({visual_size = {x = 10, y = 10}, collisionbox = {-0.3, 0.0, -0.3, 0.3, 1.7, 0.3}})
         player:set_pos(lobby.spawn_pos)
         lobby.game[name] = 'lobby'
         minetest.chat_send_player(name, 'Here you are.')
         -- if player is a builder, restore builder mode
         if lobby.is_builder(player) then
            lobby.player_to_builder(player)
         else
            -- restore normal player mode
            player_attributes:set_string('mode', 'solo')
            local player_inv = player:get_inventory()
            player_inv:set_list('main', {})
         end
      else
         minetest.chat_send_player(name, 'You can only run this command when you are a ghost or playing solo.')
      end
   end
})

minetest.register_chatcommand('kick', {
   description = 'Kick somebody, hopefully the traitor, from the level.',
   func = function(name, params)
      local map_id = lobby.game[name]
      local player = minetest.get_player_by_name(name)
      local player_attributes = player:get_meta()
      local mode = player_attributes:get_string('mode')
      if map_id ~= 'lobby' and mode == 'player' then
         if name == params then
            local traitor = lobby.traitors[map_id]
            local player = minetest.get_player_by_name(name)
            player:set_pos(lobby.spawn_pos)
            player:set_physics_override({speed=1})
            local player_inv = player:get_inventory()
            player_inv:set_list('main', {})
            lobby.game[name] = 'lobby'
            lobby.update_maps(map_id)
            if name == traitor then
               lobby.team_win(map_id)
            end
         end
         local survivors = lobby.players_on_level(map_id)
         local actual_votes = lobby.votes[map_id] or 0
         if not lobby.voted[name] then
            lobby.vote_timer[map_id] = 1800
            minetest.register_globalstep(function(dtime)
               if lobby.vote_timer[map_id] == 1 then
                  lobby.message_to_level(map_id, 'Voting time is over.')
                  lobby.vote(map_id, true)
               elseif lobby.vote_timer[map_id] == 300 then
                  lobby.message_to_level(map_id, 'Thirty seconds left to vote.')
               elseif lobby.vote_timer[map_id] == 600 then
                  lobby.message_to_level(map_id, 'One minute left to vote.')
               elseif lobby.vote_timer[map_id] == 1200 then
                  lobby.message_to_level(map_id, 'Two minutes left to vote')
               end
               if lobby.vote_timer[map_id] >= 0 then
                  lobby.vote_timer[map_id] = lobby.vote_timer[map_id] - 1
               end
            end)
            if params ~= '' then
               if string.find(survivors, params) then --player inputs a player name
                  minetest.chat_send_player(name, 'Thanks for your vote.')
                  lobby.voted[name] = true
                  lobby.votes[map_id] = actual_votes + 1
                  lobby.suspect[params] = lobby.suspect[params] + 1
                  lobby.vote(map_id)
               else --player votes for an invalid username
                  minetest.chat_send_player(name, 'Check your spelling and try again.')
               end
            end
         else
            minetest.chat_send_player(name, 'You can\'t vote right now.')
         end
      end
   end
})

minetest.register_chatcommand('skip', {
   description = 'Skip voting in a kick vote.',
   func = function(name)
      local map_id = lobby.game[name]
      if map_id ~= 'lobby' then
         local actual_votes = lobby.votes[map_id] or 0
         if not lobby.voted[name] then
            minetest.chat_send_player(name, 'You skipped voting this round.')
            lobby.voted[name] = true
            lobby.votes[map_id] = actual_votes + 1
            lobby.vote(map_id)
         else
            minetest.chat_send_player(name, 'You can\'t vote right now.')
         end
      end
   end
})

minetest.register_chatcommand('givexp', {--
   description = 'Gives a player XP',
   params = '[player, xp]',
   func = function(name, params)
      local recipient, xp_input = params:match('^(%S+)%s(.+)$')
      local xp = tonumber(xp_input)
      if recipient and xp then
         if minetest.player_exists(recipient) then
            local player = minetest.get_player_by_name(name)
            if minetest.check_player_privs(player, {server = true}) then
               if not minetest.get_player_by_name(recipient) then
                  minetest.chat_send_player(name, recipient..' is not online right now. They will be given XP when they join next.')
                  lobby.queue_xp(recipient, xp)
               else
                  local player = minetest.get_player_by_name(recipient)
                  lobby.give_xp(player, xp)
                  minetest.chat_send_player(recipient, 'You just were awarded '..xp..' XP.')
                  minetest.chat_send_player(name, 'You just sent '..recipient..' '..xp..' XP.')
               end
            else
               if lobby.take_xp(player, xp) then
                  if not minetest.get_player_by_name(recipient) then
                     minetest.chat_send_player(name, recipient..' is not online right now. They will be given XP when they join next.')
                     lobby.queue_xp(recipient, xp)
                  else
                     local player = minetest.get_player_by_name(recipient)
                     lobby.give_xp(player, xp)
                     minetest.chat_send_player(recipient, 'You just were awarded '..xp..' XP.')
                     minetest.chat_send_player(name, 'You just sent '..recipient..' '..xp..' XP.')
                  end
               else
                  minetest.chat_send_player(name, 'You can\'t give more XP than you have.')
               end
            end
         end
      end
   end
})

minetest.register_chatcommand('checkxp', {
   description = 'Check the current XP on a level.',
   func = function(name)
      local map_id = lobby.game[name]
      if map_id ~= 'lobby' then
         local xp = lobby.xp[map_id]
         if xp then
            minetest.chat_send_player(name, 'Currently have '..xp..' xp on this level.')
         else
            minetest.chat_send_player(name, 'Welp, no XP here?')
         end
      else
         minetest.chat_send_player(name, 'Can\'t do that here.')
      end
   end
})

minetest.register_chatcommand('level_help', {
   description = 'Show level building help screen.',
   func = function(name)
      minetest.show_formspec(name, 'lobby:get_started', lobby.getting_started)
   end
})
