local Battler = require("scenes.battlesystem.actors.battler")
local Hero = Battler:extend()

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

local ZGRAVITY = 0.2

-- INIT FUNCTIONS
-- Initialize the hero

function Hero:new(world, x, y, owner, charnumber)
  Hero.super.new(self, world, x, y, 0)
  self.isHero     = true
  self.owner = owner

  self:initMovementSystem()

  self:initSprite()

  self.side = "heroes"
end

-- UPDATE FUNCTION
-- Update the hero

function Hero:update(dt)
  Hero.super.update(self, dt)
  self:updateMovement(dt)

  self:updateAnimation(dt)
end

-- MOVE FUNCTIONS
-- All functions handling the moving

local MOVEMENT_DURATION = 0.20

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

  self.movementType = MOVEMENT_NONE

  self:initJump()
end

function Hero: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 Hero:updatePreviousPosition()
  self.xprevious = self.x
  self.yprevious = self.y
  self.zprevious = self.z
end

-- Tweener movement functions

function Hero: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 Hero: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 Hero:updateTweenerSpeed(dt)
  self.xspeed = (self.x - self.xprevious) / dt
  self.yspeed = (self.y - self.yprevious) / dt
end

-- MOTION HANDLING

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

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

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

-- Direction handling

function Hero: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 Hero:initJump()
  self.jump = {}
  self.jump.useDefaultAnimation  = true
  self.jump.isJumping = false
  self.jump.bounceNumber = 0
  self.jump.isJumpingBack = false
end

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

function Hero:jumpBack()
  self:setJump(3, 0, true)
  self:setMotion(-10, 0)
  self.jump.isJumpingBack = true
end

function Hero: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 Hero:blockChoregraphy(isBlocking, currentlyBlocking, blockedBy)
  if (isBlocking) then
    self.currentlyBlocking = currentlyBlocking
    self.blockedBy = blockedBy
  end
end

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

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

  if (signal == "resetMovement") then
    self.movementType = MOVEMENT_NONE
  end
end

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

-- ASSETS FUNCTIONS
-- Load and play assets needed by the character

function Hero:initSprite()
  self.assets:addSprite(self.owner.name, "datas/gamedata/characters/" .. self.owner.name .. "/sprites")
  self.assets.sprites[self.owner.name]:setCustomSpeed(16)
  self:setSprite(self.owner.name, 32, 48, true)
  self:cloneSprite()
  self:changeAnimation("idle")
end

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

function Hero:updateAnimation(dt)
  if (self.z > 0 and self.jump.useDefaultAnimation) then
    if self.zspeed > 0 then
      self:changeAnimation("jump")
    else
      self:changeAnimation("fall")
    end
  end

  self:setCustomSpeed(self.gspeed * 160 * dt)
end

-- DRAW FUNCTIONS
-- Draw everything related to the hero

function Hero:draw()
  self:drawSprite(0, -self.z)
end

return Hero
