local PlayerManager = Object:extend()
local Obj = require "scenes.levels.entities"

function PlayerManager:new(scene)
  self.scene        = scene
  self.players      = {}
  self.deathTimer   = -100
  self.activePlayer = -1
  self.startx, self.starty = self.scene.world:getStartPosition()

  self.itemList = {}
  self.score = 0
  self.gold  = 0
  local mediumfont, smallfont
  mediumfont  = self.scene.assets.fonts["medium"]
  smallfont   = self.scene.assets.fonts["small"]
  self.hpbar = game.gui.newProgressBar("greenbar",  mediumfont, smallfont, "HP", 96)
  self.mpbar = game.gui.newProgressBar("bluebar",   mediumfont, smallfont, "MP", 96)

  self.itembox = game.gui.newTextBox("assets/sprites/gui/textbox/yellowbox.png", 24,24)
end

-- PLAYER FUNCTIONS
-- Handle virtual players

-- TODO: Gérer la manière dont le joueur va avoir une équipe de cochons

function PlayerManager:addPlayer(pigID)
  -- Enregistrer le joueur n'est pas le rajouter à une liste des objets qui existe,
  -- mais juste insérer ses informations les plus importantes afin d'aider le jeu
  -- à pouvoir le reconstruire.
  local play = {}
  play.pigID = pigID
  play.isDead = 0

  table.insert(self.players, play)
end

function PlayerManager:spawnPlayer(playerID)
  local play = self.players[playerID]
  Obj.Player(self.scene, self.startx, self.starty, playerID)
  self.activePlayer = playerID
end

function PlayerManager:getPlayers()
  local itemList = self.scene.world:getEntities()
  local playerList = {}

  for i,v in ipairs(itemList) do
    if (v.playerID > 0) then
      table.insert(playerList, v)
    end
  end

  return playerList
end

function PlayerManager:getPlayerByID(id)
  local itemList = self.scene.world:getEntities()
  local player

  if (id == nil) then
    error("You must have an ID to search")
  end

  for i,v in ipairs(itemList) do
    if (v.playerID == id) then
      player = v
    end
  end

  return player
end

function PlayerManager:playerExist(id)
  return (self.players[id] ~= nil)
end

function PlayerManager:setDeathTimer(timer)
  self.deathTimer = timer
end

function PlayerManager:playerHaveObject(id)
  player = self:getPlayerByID(id)

  if (player == nil) then
    return false
  else
    return true
  end
end

-- UPDATE FUNCTIONS
-- Update the death timer and respawn the player when it's done

function PlayerManager:update(dt)
  if (self.deathTimer > 0) then
    self.deathTimer = self.deathTimer - dt
  elseif (self.deathTimer > -100) then
    self:spawnPlayer(self.activePlayer)
    self.deathTimer = -100
  end
end

-- DRAW FUNCTIONS
-- Functions made to draw the HUD

function PlayerManager:drawHUD(dt)
  utils.graphics.resetColor()
  local hp = 0
  local mp = 0
  local weapon = 0
  love.graphics.draw(self.itembox, 16, 16)

  if self:playerHaveObject(1) then
    local player = self:getPlayerByID(1)
    hp = player.hp / player.stats.maxHP
    mp = player.mp / player.stats.maxMP
    weapon = player.weapon
  end

  if (weapon ~= 0) and (weapon ~= nil) then
    self.scene.assets.tileset["weapon"]:drawTile(weapon,28,28,0,1,1,8,8)
  end
  self.hpbar:draw(68, 14, hp)
  self.mpbar:draw(68, 30, mp)

  self.scene.assets.fonts["medium"]:set()

  love.graphics.printf( utils.math.numberToString(self.score, 6), 373, 10, 96, "right")
  love.graphics.printf( utils.math.numberToString(self.gold, 4), 373, 25, 96-18, "right")
  love.graphics.setColor(1, 1, 85/256)
  love.graphics.printf( "G", 373, 25, 96, "right")
  utils.graphics.resetColor()
end

return PlayerManager
