--[[
| This file was obtained through the combined efforts
| of Madbluntz & Plymouth Antiquarian Society.
|
| Credits: lifestorm, Gregory Wayne Rossel JR.,
| 	Maloy, DrPepper10 @ RIP, Atle!
|
| Visit for more: https://plymouth.thetwilightzone.ru/
--]]


local PLUGIN = PLUGIN
local INTERVAL = 30

util.AddNetworkString("ixPrintAfkPlayers")

ix.log.AddType("afkStart", function(client)
    return Format("%s est maintenant AFK.", client:Name())
end)
ix.log.AddType("afkEnd", function(client, ...)
	local arg = {...}
    return Format("%s n'est plus AFK. Temps total passé AFK : %s", client:Name(), arg[1])
end)

do
    local PLAYER = FindMetaTable("Player")

    function PLAYER:CheckAFKTick(interval)
        local aimVector = self:GetAimVector()
        local posVector = self:GetPos()

        if (self.ixLastAimVector == aimVector
            and self.ixLastPosition == posVector
            and !self.autowalk)
            then

            return self:AddAFKTime(interval or INTERVAL)
        else
            self.ixLastAimVector = aimVector
            self.ixLastPosition = posVector
            if (self:GetNetVar("afkTime", 0) > 0) then
                return self:ResetAFKTimer()
            end
        end
    end

    function PLAYER:AddAFKTime(time)
        local oldValue, oldTime = self:IsAFK(), self:GetNetVar("afkTime", 0)
        self:SetNetVar("afkTime", oldTime + time)

        if (self:IsAFK() and !oldValue) then
            ix.log.Add(self, "afkStart")
            return 5
        elseif (oldTime + time >= ix.config.Get("afkTime") - 60 and oldTime < ix.config.Get("afkTime") - 60) then
            self:Notify("Vous serez considéré comme AFK dans 60 secondes si vous ne bougez pas.")
            return 1
        end
    end

    function PLAYER:ResetAFKTimer()
        if (self:IsAFK()) then
            ix.log.Add(self, "afkEnd", self:GetNetVar("afkTime", 0))
        elseif (self:GetNetVar("afkTime", 0) > (ix.config.Get("afkTime") - 60)) then
            self:Notify("Timer d'AFK réinitialiser.")
        end

        self:SetNetVar("afkTime", 0)

        return INTERVAL
    end
end

function PLUGIN:KickAFK(target, byAdmin)
	if (target and target:IsAFK()) then
		target:Kick("Vous avez été expulsé pour être AFK")
		if (byAdmin) then
			ix.util.NotifyLocalized("kickedAdminAFK", nil, byAdmin:Name(), target:Name())
		else
			ix.util.NotifyLocalized("kickedAFK", nil, target:Name())
		end
    elseif (byAdmin) then
        byAdmin:NotifyLocalized("targetNotAFK", target:Name())
	end
end
