local Base = require "birb.modules.world.actors.actor2D"
local Player = Base:extend()

function Player:new(world, x, y, id)
  Player.super.new(self, world, "player", x, y, 16, 24, true)
  self:setSprite("player", true, 8, 12)
  self:setGravity(480)

  self.isPunching = false
  self.direction  = 1
  self.startx, self.starty = self.x, self.y
  self.isDead = false

  self.punchName = ""
  self:setHitboxFile("scenes.gameplay.plateform.actors.hitboxes.player")
end

function Player:updateStart(dt)
  self.xfrc = 480*3

  if self.keys["up"].isPressed and (self.onGround) then
    self.ysp = -280
  end
  if self.keys["down"].isDown then
    self.mainHitbox:modify(0, 8, 16, 16)
  else
    self.mainHitbox:modify(0, 0, 16, 24)
  end
  if self.keys["left"].isDown and (not self.isPunching) then
    self.xsp = -120
  end
  if self.keys["right"].isDown and (not self.isPunching) then
    self.xsp =  120
  end

  if self.keys["A"].isDown then
    self.isPunching = true
  else
    self.isPunching = false
  end

  if (self.isPunching) then
    self:checkHitboxesCollisions()
  end

  if self.keys["start"].isPressed then
    --self.world:switchActivity()
    --self.assets:switchActivity()
    self.scene.menusystem:activate()
    self.scene.menusystem:switchMenu("PauseMenu")
    self.scene:flushKeys()
  end

  self:setDirection(self.xsp)
end

function Player:updateEnd(dt)
  self:setAnimation()

  local width, height = self.world:getDimensions()

  if (self.y > height + self.h) and (self.isDead == false) then
    self:addTimer("respawn", 1)
    self.isDead = true
  end
end

function Player:setAnimation()
  self.sprite:setCustomSpeed(math.abs(self.xsp) / 12)
  if (self.isPunching) then
    self.sprite:changeAnimation("punch", false)
  else
  if (self.onGround) then
    if math.abs(self.xsp) > 0 then
      self.sprite:changeAnimation("walk", false)
    else
      self.sprite:changeAnimation("idle", true)
    end
  else
    self.sprite:changeAnimation("jump", true)
  end
  end
end

function Player:setDirection(direction)
  direction = direction or 0
  if direction ~= 0 then
    direction = utils.math.sign(direction)
    self.direction = direction
    self.sprite:setScalling(direction, nil)
  end
end

function Player:timerResponse(timer)
  if timer == "respawn" then
    self.x, self.y = self.startx, self.starty
    self.xspeed = 0
    self.yspeed = 0
    self.isDead = false
  end
end

function Player:collisionResponse(collision)
  if collision.other.type == "coin" then
    collision.other.owner:takeCoin(self)
  end
end

function Player:hitboxResponse(name, type, collision)
  if (collision.other.type == "coin") and (type == "punch") then
    collision.other.owner:takeCoin(self)
  end
end

function Player:draw()
  Player.super.draw(self)
  self:drawHitboxes()
  utils.graphics.resetColor()
end

function Player:drawHUD(id)
  love.graphics.print(id .. " test", 4, 4)
end

return Player
