Baleba = {}
class('Baleba').extends(NobleSprite)

function randomFloat(lower, greater)
    return lower + math.random() * (greater - lower);
end

function Baleba:init(x, y, player, loop)
    Baleba.super.init(self, "assets/sprites/baleba", true)

    -- Animation properties
    self.animation:addState("run", 1, 4)
    self.animation.run.frameDuration = 2

    self.animation:setState("run")

    -- Collision properties
    self:setZIndex(ZIndex.enemy)
    self:setCollideRect(3, 25, 40, 15)
    self:setTag(154)
    self:setSize(64, 64)
    self:setGroups(CollideGroups.enemy)
    self:setCollidesWithGroups(
        {
            CollideGroups.player
        })

    -- Sprite properties
    self.standrate = randomFloat(0.3, 2)
    self.xVelocity = Baleba.standrate
    self.player = player
    self.loop = loop

    -- Danger properties
    self.danger = Danger(366, y, self)

    self:add()
    -- Move to initial position
    self:moveTo(x, y)
end

function Baleba:update()
    -- Speedup when player is moving right
    if self.player == nil or self.player.isMovingRight() == false then
        self.xVelocity = self.standrate
    else
        self.xVelocity = 1.2 + self.standrate
    end

    self:moveTo(self.x - self.xVelocity, self.y)

    -- Danger arrow management
    if self.x < 430 and self.danger ~= nil then
        self.danger:remove()
        self.danger = nil
    elseif self.danger == nil and self.x > 430 then
        self.danger = Danger(366, self.y, self)
    end

    -- Lifeciycle management
    if self.x < 0 and self.loop == true then
        self.standrate = randomFloat(0.5, 6)
        self:moveTo(math.random(450, 600), math.random(50, 200))
    elseif self.x < 0 then
        self:destroy()
    end
end

function Baleba:destroy()
    if self.danger ~= nil then
        self.danger:remove()
    end
    self:remove()
end
