local Parent = Object:extend() -- On créer la classe des entitées, c'est la classe de base

local maputils = require "scenes.battlesystem.utils"

local TweenManager = require "game.modules.tweenmanager"

-- INIT FUNCTION
-- Initilize the actor

function Parent:new(world, x, y, z)
  self.depth = 0
  self.x     = x
  self.y     = y
  self.z     = z or 0
  self.direction = 1
  --self.id    = self.world.creationID

  self.world  = world
  self.assets = self.world.assets
  self.scene  = self.world.scene
  self.map    = self.world.map

  self.maputils = maputils

  self.isHero   = false
  self.isActor  = false
  self.isEnnemy = false
  self.isDestroyed = false

  self.tweens = TweenManager(self)

  self:setSprite()
  self:register()
end

function Parent:register()
  self.world:registerActor(self)
end

function Parent:destroy()
  self.world:destroyActor(self)
  self.isDestroyed = true
end

function Parent:update(dt)
  self:updateSprite(dt)
  self.tweens:update(dt)
end

-- GET FUNCTIONS
-- Get informations

function Parent:getCoordinate()
  return self.x, self.y
end

-- SPRITE FUNCTIONS
-- Handle the character sprite

function Parent:setSprite(spritename, ox, oy, active)
  self.sprite = {}
  self.sprite.name    = spritename or nil
  self.sprite.ox      = ox or 0
  self.sprite.oy      = oy or 0
  self.sprite.sx      = 1
  self.sprite.sy      = 1
  self.sprite.exist   = (spritename ~= nil)
  self.sprite.clone   = nil
  self.sprite.active  = active or false
end

function Parent:cloneSprite()
  if self.sprite.name ~= nil then
    self.sprite.clone = self.assets.sprites[self.sprite.name]:clone()
    self.sprite.clone:setCallback(self)
  end
end

function Parent:changeAnimation(animation, restart)
  if (self.sprite.clone == nil) then
    self.assets.sprites[self.sprite.name]:changeAnimation(animation, restart)
  else
    self.sprite.clone:changeAnimation(animation, restart)
  end
end

function Parent:animationEnded(animation)
  -- Empty placeholder function
end

function Parent:setCustomSpeed(customSpeed)
  if (self.sprite.clone == nil) then
    self.assets.sprites[self.sprite.name]:setCustomSpeed(customSpeed)
  else
    self.sprite.clone:setCustomSpeed(customSpeed)
  end
end

function Parent:updateSprite(dt)
  if (self.sprite.clone ~= nil) then
    self.sprite.clone:update(dt)
  end
end

function Parent:setSpriteScallingX(sx)
  local sx = sx or 1

  self.sprite.sx = sx
end

function Parent:setSpriteScallingY(sy)
  local sy = sy or 1

  self.sprite.sy = sy
end

function Parent:getCurrentAnimation()
  if (self.sprite.clone == nil) then
    return self.assets.sprites[self.sprite.name]:getCurrentAnimation()
  else
    return self.sprite.clone:getCurrentAnimation()
  end
end


function Parent:getSpriteScalling()
  return self.sprite.sx, self.sprite.sy
end

function Parent:getFrame()
  if (self.sprite.name ~= nil) then
    if (self.sprite.clone ~= nil) then
      return self.sprite.clone:getFrame()
    else
      return self.assets.sprites[self.sprite.name]:getFrame()
    end
  end
end

function Parent:getRelativeFrame()
  if (self.sprite.name ~= nil) then
    if (self.sprite.clone ~= nil) then
      return self.sprite.clone:getRelativeFrame()
    else
      return self.assets.sprites[self.sprite.name]:getRelativeFrame()
    end
  end
end

function Parent:getAnimationDuration()
  if (self.sprite.name ~= nil) then
    if (self.sprite.clone ~= nil) then
      return self.sprite.clone:getAnimationDuration()
    else
      return self.assets.sprites[self.sprite.name]:getAnimationDuration()
    end
  end
end

function Parent:drawSprite(tx, ty)
  utils.graphics.resetColor()

  local x, y = self.world.map:gridToPixel(self.x, self.y, true)

  local tx = tx or 0
  local ty = ty or 0

  if (self.sprite.active) then
    local sx = self.direction * self.sprite.sx
    local sy = self.sprite.sy
    if (self.sprite.clone ~= nil) then
      self.sprite.clone:draw(x + tx, y + ty, 0, sx, sy, self.sprite.ox, self.sprite.oy)
    else
      self.assets.sprites[self.sprite.name]:drawAnimation(x + tx, y + ty, 0, sx, sy, self.sprite.ox, self.sprite.oy)
    end
  end
end

-- DRAW FUNCTIONS
-- Handle draw functions

function Parent:draw()

end

function Parent:drawShadow()
  local x, y = self.world.map:gridToPixel(self.x, self.y, true)
  self.assets.images["actorsShadow"]:draw(x, y, 0, self.sprite.sx, self.sprite.sy, 12, 5)
  if (self.isSelected == true) then
    self.assets.sprites["cursorground"]:drawAnimation(x - 2, y - 1, 0, 1, 1, 12, 5)
  end
end

function Parent:drawHUD()

end

return Parent
