-----------------------------------
--  Area: Kuftal Tunnel
--  NM: Amemet
-----------------------------------
-----------------------------------
local ID = zones[xi.zone.KUFTAL_TUNNEL]
-----------------------------------
---@type TMobEntity
local entity = {}

entity.phList =
{
    [ID.mob.AMEMET - 84] = ID.mob.AMEMET, -- 74.662 -0.513 3.685
    [ID.mob.AMEMET - 83] = ID.mob.AMEMET, -- 109.000 -0.010 -48.000
    [ID.mob.AMEMET - 82] = ID.mob.AMEMET, -- 92.000 -0.396 14.000
    [ID.mob.AMEMET - 22] = ID.mob.AMEMET, -- 123.046 0.250 18.642
    [ID.mob.AMEMET - 16] = ID.mob.AMEMET, -- 112.135 -0.278 38.281
    [ID.mob.AMEMET - 15] = ID.mob.AMEMET, -- 112.008 -0.530 50.994
    [ID.mob.AMEMET - 14] = ID.mob.AMEMET, -- 67.998 -0.500 12.000
    [ID.mob.AMEMET - 13] = ID.mob.AMEMET, -- 89.590 -0.321 -9.390
    [ID.mob.AMEMET - 12] = ID.mob.AMEMET, -- 123.186 0.213 -24.716
    [ID.mob.AMEMET - 11] = ID.mob.AMEMET, -- 96.365 -0.269 -7.619
    [ID.mob.AMEMET - 8]  = ID.mob.AMEMET, -- 122.654 -0.491 0.840
    [ID.mob.AMEMET - 7]  = ID.mob.AMEMET, -- 68.454 -0.417 -0.413
    [ID.mob.AMEMET - 6]  = ID.mob.AMEMET, -- 118.633 -0.470 -43.282
}

entity.spawnPoints =
{
    { x = 123.046, y =  0.250, z =  18.642 },
    { x = 112.135, y = -0.278, z =  38.281 },
    { x = 112.008, y = -0.530, z =  50.994 },
    { x = 122.654, y = -0.491, z =   0.840 },
    { x = 123.186, y =  0.213, z = -24.716 },
    { x = 118.633, y = -0.470, z = -43.282 },
    { x = 109.000, y = -0.010, z = -48.000 },
    { x =  96.365, y = -0.269, z =  -7.619 },
    { x =  89.590, y = -0.321, z =  -9.390 },
    { x =  68.454, y = -0.417, z =  -0.413 },
    { x =  74.662, y = -0.513, z =   3.685 },
    { x =  67.998, y = -0.500, z =  12.000 },
    { x =  92.000, y = -0.396, z =  14.000 },
    { x =  99.475, y = -0.067, z =   9.035 },
    { x = 104.228, y =  0.000, z =   6.567 },
    { x = 109.032, y =  0.422, z =  -7.990 },
    { x = 122.583, y =  0.000, z =   0.622 },
    { x =  86.752, y =  0.000, z =  -0.573 },
    { x = 102.731, y =  0.491, z =  -5.173 },
    { x = 114.827, y =  0.408, z =   9.606 },
    { x =  96.311, y =  0.438, z =  -4.693 },
    { x =  97.652, y = -0.070, z =  -8.770 },
    { x =  90.926, y =  0.705, z =  -0.835 },
    { x = 109.931, y =  0.424, z =  -7.088 },
    { x = 112.120, y =  0.271, z =  15.939 },
    { x = 106.658, y =  0.000, z =   8.578 },
    { x = 102.354, y =  0.029, z =  -9.346 },
    { x = 104.305, y = -0.027, z =  13.815 },
    { x = 102.753, y =  0.892, z =  -2.631 },
    { x =  90.305, y =  0.580, z =  -2.025 },
    { x =  92.885, y =  0.268, z =  -4.161 },
    { x =  98.694, y =  0.244, z =   5.488 },
    { x = 100.363, y =  0.908, z =  -2.502 },
    { x = 110.300, y = -0.217, z = -19.833 },
    { x =  79.762, y =  0.267, z =  -7.177 },
    { x = 105.783, y = -0.092, z =  13.513 },
    { x = 115.739, y =  0.665, z =  16.603 },
    { x =  82.849, y = -0.025, z =   5.134 },
    { x = 102.541, y = -0.296, z =  14.131 },
    { x =  88.190, y = -0.036, z =  -5.870 },
    { x = 101.478, y = -0.230, z =   8.986 },
    { x = 112.781, y =  0.424, z =  13.979 },
    { x = 108.403, y =  0.100, z = -11.182 },
    { x = 115.181, y = -0.011, z = -10.044 },
    { x =  83.043, y =  0.000, z =   4.495 },
    { x =  92.363, y =  0.703, z =  -1.750 },
    { x =  76.068, y =  0.373, z =  -6.919 },
    { x =  98.145, y =  0.318, z =  -5.397 },
    { x = 120.448, y =  1.000, z =  15.336 },
    { x = 111.944, y = -0.081, z =   0.939 },
}

local pathStart =
{
    { x = 102.61, y = 0.92, z = 1.60 },
}

local pathA =
{
    { x = 102.61, y = 0.92, z = 1.60 },
    { x = 90.06, y = -0.28, z = 12.24 },
    { x = 67.60, y = -0.34, z = 12.42 },
    { x = 54.61, y = -4.11, z = 16.89 },
    { x = 25.03, y =  -9.28, z = 23.12 },
    { x = 19.43, y = -7.95, z = 44.37 },
    { x = 18.80, y = -6.63, z = 47.77 },
    { x = 17.72, y = -5.14, z = 51.88 },
    { x = 16.93, y = -3.95, z = 55.33 },
    { x = 16.33, y = -3.09, z = 57.46 },
    { x = 14.76, y = -1.78, z = 60.84 },
    { x = 11.86, y = -0.60, z = 65.58 },
    { x = -0.52, y = 0.00, z = 82.02 },
}

local pathAb =
{
    { x = 3.06, y = 0.15, z = 125.56 },
    { x = 40.12, y = 0.000, z = 158.89 },
    { x = 60.35, y = -4.24, z = 143.23 },
    { x = 79.86, y = -8.75, z = 139.89 },
    { x = 85.86, y = -8.82, z = 138.11 },
    { x = 91.87, y = -8.83, z = 142.52 },
    { x = 101.60, y = -8.71, z = 143.17 },
    { x = 100.62, y = -8.75, z = 78.86 },
    { x = 108.29, y = -4.60, z = 54.26 },
    { x = 118.98, y = 0.00, z = 38.12 },
    { x = 102.61, y = 0.92, z = 1.60 },
}

local pathB =
{
    { x = 102.61, y = 0.92, z = 1.60 },
    { x = 109.49, y = 0.59, z = -0.07 },
    { x = 124.47, y = 0.00, z = -42.08 },
    { x = 91.93, y = -5.15, z = -58.24 },
    { x = 66.46, y = -8.81, z = -62.91 },
    { x = 60.86, y = -9.02, z = -73.80 },
    { x = 57.09, y = -9.37, z = -96.88 },
    { x = 38.39, y = -8.62, z = -98.74 },
    { x = 15.73, y = -1.02, z = -94.88 },
    { x = -0.65, y = 0.00, z = -84.13 },
    { x = -40.24, y = 0.00, z = -38.35 },
    { x = -44.36, y = 0.68, z = 13.98 },
    { x = -31.28, y = -0.23, z = 48.64 },
    { x = -0.52, y = 0.00, z = 82.02 },
}

local pathBb =
{
    { x = 3.06, y = 0.15, z = 125.56 },
    { x = 40.12, y = 0.000, z = 158.89 },
    { x = 60.35, y = -4.24, z = 143.23 },
    { x = 79.86, y = -8.75, z = 139.89 },
    { x = 85.86, y = -8.82, z = 138.11 },
    { x = 91.87, y = -8.83, z = 142.52 },
    { x = 101.60, y = -8.71, z = 143.17 },
    { x = 100.62, y = -8.75, z = 78.86 },
    { x = 108.29, y = -4.60, z = 54.26 },
    { x = 118.98, y = 0.00, z = 38.12 },
    { x = 102.61, y = 0.92, z = 1.60 },
}

local pathFind =
{
    ['pathFind1'] = function(mob, reversePath)
        local pathNodes = {}
        if reversePath == 0 or reversePath == 1 then
            local pathRnd = math.random(0, 1)
            local reverseCheck = math.random(0, 2)
            if pathRnd == 1 then
                mob:setLocalVar('mobPath', 2)
                if reverseCheck ~= 2 then
                    mob:setLocalVar('reversePath', 0)
                    pathNodes = pathA
                else
                    mob:setLocalVar('reversePath', 1)
                    pathNodes = pathAb
                end
            else
                mob:setLocalVar('mobPath', 4)
                if reverseCheck ~= 2 then
                    mob:setLocalVar('reversePath', 0)
                    pathNodes = pathB
                else
                    mob:setLocalVar('reversePath', 1)
                    pathNodes = pathBb
                end
            end
        end

        return pathNodes
    end,

    ['pathFind2'] = function(mob, reversePath)
        local pathNodes = pathA
        mob:setLocalVar('mobPath', 3)
        if reversePath == 0 then
            pathNodes = pathAb
        end

        return pathNodes
    end,

    ['pathFind3'] = function(mob, reversePath)
        local pathNodes = {}
        if reversePath == 0 or reversePath == 1 then
            mob:setLocalVar('mobPath', 1)
            pathNodes = pathStart
        end

        return pathNodes
    end,

    ['pathFind4'] = function(mob, reversePath)
        local pathNodes = pathB
        mob:setLocalVar('mobPath', 3)
        if reversePath == 0 then
            pathNodes = pathBb
        end

        return pathNodes
    end,
}

entity.onMobInitialize = function(mob)
    mob:setMobMod(xi.mobMod.GIL_MIN, 6000)
    mob:setMobMod(xi.mobMod.GIL_MAX, 6000)
end

entity.onMobSpawn = function(mob)
    mob:setLocalVar('isPaused', 0)
    mob:setLocalVar('mobPath', 1)
    mob:pathThrough(pathStart, xi.path.flag.COORDS)
end

entity.onPath = function(mob)
    if not mob:isFollowingPath() then
        if mob:getLocalVar('isPaused') ~= 0 then
            local currentPath = 'pathFind' .. mob:getLocalVar('mobPath')
            local reversePath = mob:getLocalVar('reversePath')

            mob:setLocalVar('isPaused', 0)
            mob:clearPath()

            local pathNodes = pathFind[currentPath](mob, reversePath)

            local newReverse = mob:getLocalVar('reversePath')
            if newReverse == 0 then
                mob:pathThrough(pathNodes, xi.path.flag.COORDS)
            else
                mob:pathThrough(pathNodes, bit.bor(xi.path.flag.REVERSE, xi.path.flag.COORDS))
            end
        else
            -- Amemet has a chance to pause, if successful he will wait
            -- a random amount of time before resuming his path
            mob:clearPath()
            local x = mob:getXPos()
            local y = mob:getYPos()
            local z = mob:getZPos()
            local pauses = {}
            local pauseRnd = math.random(0, 2)
            if pauseRnd == 2 then
                local count = math.random(0, 6)
                for i = 0, count do
                    local wait = math.random(4000, 6000)
                    pauses[i + 1] =
                    {
                        x = x, y = y, z = z, wait = wait
                    }
                end
            else
                local count = 1
                for i = 0, count do
                    pauses[i + 1] =
                    {
                        x = x, y = y, z = z
                    }
                end
            end

            mob:pathThrough(pauses, xi.path.flag.COORDS)
            mob:setLocalVar('isPaused', 1)
        end
    end
end

entity.onMobFight = function(mob)
    -- At 25% HP or less, Amemet receives regain.
    if mob:getHPP() <= 25 then
        mob:setMod(xi.mod.REGAIN, 10)
    end
end

entity.onMobDisengage = function(mob)
    mob:setMod(xi.mod.REGAIN, 0)
end

entity.onMobDeath = function(mob, player, optParams)
    xi.hunts.checkHunt(mob, player, 418)
end

return entity
