-----------------------------------
-- Area: Castle Oztroja [S]
--   NM: Suu Xicu the Cantabile
-- TODO:
--  Summoning of pets should be tied to Soul Voice usage.
--  Gains a hidden regen from Army's Paeon V. Even if it is dispelled, it will gain several HP%.
-----------------------------------
mixins = { require('scripts/mixins/job_special') }
-----------------------------------
---@type TMobEntity
local entity = {}

entity.spawnPoints =
{
    { x = -214.770, y = -0.681, z =  96.357 }
}

entity.onMobInitialize = function(mob)
    xi.mob.updateNMSpawnPoint(mob)
    mob:setRespawnTime(math.random(900, 10800))
end

entity.onMobRoam = function(mob)
    local mobId = mob:getID()
    local hpp = mob:getHPP()

    if hpp > 50 and mob:getLocalVar('petsOne') == 1 then
        mob:setLocalVar('petsOne', 0)

        for i = mobId + 5, mobId + 6 do
            local pet = GetMobByID(i)
            if pet and pet:isSpawned() then
                DespawnMob(i)
            end
        end
    end

    if hpp > 25 and mob:getLocalVar('petsTwo') == 1 then
        mob:setLocalVar('petsTwo', 0)

        for i = mobId + 7, mobId + 8 do
            local pet = GetMobByID(i)
            if pet and pet:isSpawned() then
                DespawnMob(i)
            end
        end
    end
end

entity.onMobFight = function(mob, target)
    local mobId = mob:getID()
    local hpp = mob:getHPP()
    local x = mob:getXPos()
    local y = mob:getYPos()
    local z = mob:getZPos()
    local r = mob:getRotPos()

    if hpp < 50 and mob:getLocalVar('petsOne') == 0 then
        mob:setLocalVar('petsOne', 1)

        for i = mobId + 5, mobId + 6 do
            local pet = GetMobByID(i)
            if pet and not pet:isSpawned() then
                pet:setSpawn(x + math.random(-2, 2), y, z + math.random(-2, 2), r)
                pet:spawn()
            end
        end
    end

    if hpp < 25 and mob:getLocalVar('petsTwo') == 0 then
        mob:setLocalVar('petsTwo', 1)

        for i = mobId + 7, mobId + 8 do
            local pet = GetMobByID(i)
            if pet and not pet:isSpawned() then
                pet:setSpawn(x + math.random(-2, 2), y, z + math.random(-2, 2), r)
                pet:spawn()
            end
        end
    end
end

entity.onMobDeath = function(mob, player, optParams)
end

entity.onMobDespawn = function(mob)
    local mobId = mob:getID()

    for i = mobId + 5, mobId + 8 do
        local pet = GetMobByID(i)
        if pet and pet:isSpawned() then
            DespawnMob(i)
        end
    end

    xi.mob.updateNMSpawnPoint(mob)
    mob:setRespawnTime(math.random(14400, 18000)) -- 4 to 5 hours
end

return entity
