local breath_value = {}

local function has_interact(player)
   local name = player:get_player_name()
   return not minetest.check_player_privs(name, { interact = true })
end

minetest.register_chatcommand('spawn', {
   description = 'Teleport to spawn',
   privs = {home=true},
   func = function (name)
      local player = minetest.get_player_by_name(name)
      player:setpos({x=1473, y=2486, z=2970})
      default.player_attached[player] = false
      default.player_set_animation(player, "stand", 30)
      armor:set_player_armor(player)
   end,
})

minetest.register_chatcommand('interact', {
   description = 'Gives player interact',
   privs = {home=true},
   func = function(name, params)
      if params == 'strawhat' then
         local privs = minetest.get_player_privs(name)
         privs.interact = true
         minetest.set_player_privs(name, privs)
         minetest.chat_send_player(name, 'Enjoy your stay on the Epic server!')
      else
         minetest.chat_send_player(name, 'Check your spelling and try again.')
      end
   end,
})

minetest.register_chatcommand('rules', {
   description = 'Shows player rules',
   func = function(name, other_player)
      if other_player ~= '' then
         if not minetest.get_player_by_name(other_player) then
            minetest.chat_send_player(name, 'No player with that name.')
         else
            local n00b = minetest.get_player_by_name(other_player)
            if has_interact(n00b) then
               minetest.show_formspec(other_player, 'rules', spawn_rules_formspec)
            end
         end
      else
         minetest.show_formspec(name, 'rules', spawn_rules_formspec)
      end
   end,
})

minetest.register_chatcommand('news', {
   description = 'Shows player news',
   func = function(name)
      minetest.show_formspec(name, 'news', spawn_news_formspec)
   end,
})

minetest.register_on_joinplayer(function(player)
   player:set_properties({breath_max = 40})
   player:set_breath(40)
   if has_interact(player) then
      local player_name = player:get_player_name()
      minetest.chat_send_player(player_name, 'Read the rules for information on how to get interact.')
      minetest.show_formspec(player_name, 'rules', spawn_rules_formspec)
      sounds.random_player()
   else
      local player_name = player:get_player_name()
      local info_count = tonumber(spawn.storage:get_string(player_name..'_info_count')) or 15
      if info_count < 15  then
         local new_count = (info_count + 1)
         spawn.storage:set_string(player_name..'_info_count', new_count)
         minetest.show_formspec(player_name, '_info_message', spawn_info_formspec)
      end
   end
end)

local function drown(player, air)
   local new_breath = (air-4)
   breath_value[player] = new_breath
   player:set_breath(new_breath)
end

minetest.register_playerevent(function(player, event)
   local air = player:get_breath()
   local old_air = breath_value[player] or 40
   if event == "breath_changed" then
      if old_air > air then --Drowning
         drown(player, air)
      else --Filling lungs with air
         breath_value[player] = air
      end
   end
end)
