local Parent = require("scenes.battlesystem.actors.parent")
local Battler = Parent:extend()

local MOVEMENT_NONE = "none"
local MOVEMENT_TWEENER = "tweener"
local MOVEMENT_MOTION = "motion"

local ZGRAVITY = 0.2

local MIDDLE_ARENA = 6

function Battler:new(world, x, y, z, owner)
  Battler.super.new(self, world, x, y, z)

  self.direction = utils.math.sign(MIDDLE_ARENA - x)

  self.start = {}
  self.start.x = x
  self.start.y = y
  self.start.direction = self.direction

  self:initMovementSystem()

  self.isBattler = true
  self.speed = 3
  self.isActive = false
  self.debugActiveTimer = 0

  self.damageNumber = {}
  self.damageNumber.num = 0
  self.damageNumber.isBad = true
  self.showDamage = false
  self.damageY = 0

  self.isSelected = false
  self.owner = owner

  self.isDefending = false
end

function Battler:destroy()
  Battler.super.destroy(self)
end

function Battler:getDamageNumberY()
  return 32
end

function Battler:setDamageNumber(number, isPP)
  self.damageNumber.isBad = number < 0
  self.damageNumber.isPP = (isPP == true)
  self.damageNumber.num = math.abs(math.floor(number))
  self.damageY = self:getDamageNumberY() - 8
  self.tweens:newTween(0, 0.4, {damageY = self:getDamageNumberY()}, "outBack")
  self.showDamage = true
  self.tweens:newTimer(0.5, "removeDamage")
end

function Battler:setActive()
  core.debug:print("cbs/actor","actor " .. self.id .. " is active")
  self.isActive = true
  self.debugActiveTimer = 0
end

function Battler:update(dt)
  Battler.super.update(self, dt)
  if (self.isActive) then
    self.debugActiveTimer = self.debugActiveTimer + dt
    if self.debugActiveTimer >= 0.5 then
      core.debug:print("cbs/battler", "counter ended, switching active battler")
      self.isActive = false
      self.world:switchActiveBattler()
    end
  end

  self:updateMovement(dt)
end

-- MOVE FUNCTIONS
-- All functions handling the moving

local MOVEMENT_DURATION = 0.20

function Battler:initMovementSystem()
  self.xprevious, self.yprevious, self.zprevious  = self.x, self.y, self.z
  self.xspeed, self.yspeed, self.zspeed = 0,0,0
  self.direction = self.start.direction
  self.directionPrevious = self.start.direction
  self.directionLocked = false

  self.movementType = MOVEMENT_NONE

  self:initJump()
end

function Battler:updateMovement(dt)
  if (self.movementType == MOVEMENT_TWEENER) then
    self:updateTweenerSpeed(dt)
  elseif (self.movementType == MOVEMENT_MOTION) then
    self:updateMotion(dt)
  end
  self.gspeed = math.sqrt(self.xspeed^2 + self.yspeed^2)

  self:updateDirection(dt)
  self:updateJump(dt)
  self:updatePreviousPosition(dt)
end

function Battler:updatePreviousPosition()
  self.xprevious = self.x
  self.yprevious = self.y
  self.zprevious = self.z
end

-- Tweener movement functions

function Battler:goTo(dx, dy, duration, easing)
  local easing = easing or 'inOutQuad'
  if duration > 0 then
    self.tweens:newTween(0, duration, {x = dx, y = dy}, easing)
  end
  self.tweens:newTimer(duration + 0.02, "goTo")
  self.tweens:newTimer(duration + 0.02, "resetMovement")

  self.movementType = MOVEMENT_TWEENER
end

function Battler:jumpTo(dx, dy, sizeFactor, duration, spinjump, easing)
  local easing = easing or 'inOutQuad'
  local dist = utils.math.pointDistance(self.x, self.y, dx, dy)
  local jumpHeight = dist * 8 * sizeFactor
  self.tweens:newTween(0, duration, {x = dx, y = dy}, easing)
  self.tweens:newTimer(duration + 0.02, "jumpTo")
  self:setJump(jumpHeight, spinjump, duration)
end

function Battler:updateTweenerSpeed(dt)
  self.xspeed = (self.x - self.xprevious) / dt
  self.yspeed = (self.y - self.yprevious) / dt
end

-- MOTION HANDLING

function Battler:setMotion(xspeed, yspeed)
  self.xspeed = xspeed
  self.yspeed = yspeed
  self.movementType = MOVEMENT_MOTION
end

function Battler:updateMotion(dt)
  self.x = self.x + (self.xspeed) * dt
  self.y = self.y + (self.yspeed) * dt
end

function Battler:endMotion()
  self.movementType = MOVEMENT_NONE
  self.xspeed = 0
  self.yspeed = 0
end

-- Direction handling

function Battler:updateDirection()
  -- Handle direction
  if math.abs(self.xspeed) >= 0.01 then
    if (self.directionLocked == false) then
      self.direction = utils.math.sign(self.xspeed)
    end
  end
end

-- Jump system

function Battler:initJump()
  self.jump = {}
  self.jump.useDefaultAnimation  = true
  self.jump.isJumping = false
  self.jump.bounceNumber = 0
  self.jump.isJumpingBack = false
end

function Battler:setJump(power, bounceNumber, useDefaultAnimation)
  self.zspeed = power
  self.jump.spin = (useDefaultAnimation == false)
  self.jump.bounceNumber = bounceNumber
  self.jump.isJumping = true
end

function Battler:jumpBack()
  self:setJump(4, 0, true)
  local dir = utils.math.pointDirection(self.x, self.y, self.start.x, self.start.y)
  local hspeed, vspeed = utils.math.lengthdir(8, dir)
  self:setMotion(hspeed, vspeed)
  self.jump.isJumpingBack = true
end

function Battler:updateJump(dt)
  if (self.jump.isJumping) then
    self.zspeed = self.zspeed - ZGRAVITY
    self.z = self.z + self.zspeed
    if (self.z <= 0) then
      if (self.jump.bounceNumber > 0) then
        self.zspeed = self.zspeed * -0.5
        self.jump.bounceNumber = self.jump.bounceNumber - 1
      else
        self.z = 0
        self.jump.isJumping = false
        self.jump.spin = false
        self:timerResponse("jump")
        if (self.jump.isJumpingBack) then
          self:endMotion()
        end
        self:changeAnimation("idle")
      end
    end
  end
end

-- CHOREGRAPHY FUNCTIONS
-- All functions related to the choregraphy system

function Battler:blockChoregraphy(isBlocking, currentlyBlocking, blockedBy)
  if (isBlocking) then
    self.currentlyBlocking = currentlyBlocking
    self.blockedBy = blockedBy
  end
end

function Battler:unblockChoregraphy()
  self.currentlyBlocking:finish()
  self.currentlyBlocking = nil
end

function Battler:timerResponse(signal)
  if ((self.currentlyBlocking ~= nil) and (signal == self.blockedBy)) then
    self:unblockChoregraphy()
  end

  if (signal == "resetMovement") then
    self.movementType = MOVEMENT_NONE
  elseif (signal == "removeDamage") then
    self.showDamage = false
  end
end

function Battler:choregraphyEnded()
  self.direction = self.start.direction
  self.x = self.start.x
  self.y = self.start.y
  self.xspeed = 0
  self.yspeed = 0
  self.movementType = MOVEMENT_NONE
end

function Battler:getHurt()

end

function Battler:die()
  self:destroy()
end

-- DRAW FUNCTIONS
function Battler:drawDamageNumber()
  if (self.showDamage) then
    local x, y = self.world.map:gridToPixel(self.x, self.y, true)
    if (self.damageNumber.isBad) then
      love.graphics.setColor(1, 0, 0, 1)
    else
      if (self.damageNumber.isPP) then
        love.graphics.setColor(0.3, 0.8, 1, 1)
      else
        love.graphics.setColor(0, 1, 0, 1)
      end
    end

    self.assets.fonts["hudnbrs_small"]:print(self.damageNumber.num, x, y - self.damageY, "center")
    utils.graphics.resetColor()
  end
end

function Battler:draw()
  local x, y = self.world.map:gridToPixel(self.x, self.y, true)
  love.graphics.setColor(1, 0, 0, 1)
  love.graphics.rectangle("fill", x - 8, y - 32, 16, 32)
  love.graphics.setColor(1, 1, 1, 1)
end

function Battler:initSprite()
  if (self.assets.sprites[self.owner.name] == nil) then
    self.assets:addSprite(self.owner.name, self:getSpritePath())
  end
  self.assets.sprites[self.owner.name]:setCustomSpeed(16)
  self:setSprite(self.owner.name, 32, 48, true)
  self:cloneSprite()
  self:changeAnimation("idle")
end

function Battler:animationEnded(animation)
  if (self.currentlyBlocking ~= nil and self.blockedBy=="animation") then
    self:unblockChoregraphy()
  end
  self:getNewAnimation(animation)
end

function Battler:getNewAnimation(animation)

end

function Battler:validateAction()

end

return Battler
