export let afkStatus: boolean = false;

export default function antiAfk(globalData: IGlobalData) {
    let lastPosition: Vector3Mp = mp.players.local.position;
    let afkCounter: number = 0;

    setInterval(checkAfkPosition, 1000 * 10);

    function checkAfkPosition() {
        if (!globalData.LoggedIn) {
            return;
        }

        let lp = lastPosition;
        let np = mp.players.local.position;

        let distance = mp.game.gameplay.getDistanceBetweenCoords(lp.x, lp.y, lp.z, np.x, np.y, np.z, false);

        if (distance <= 1.5) {
            if (!afkStatus) {
                afkCounter++;
            }
        } else {
            afkCounter = 0;
            if (afkStatus) {
                afkStatus = false;
                globalData.IsAfk = afkStatus;
                setServerAfkStatus(afkStatus);
            }
        }

        if (afkCounter >= 60) {
            afkStatus = true;
            globalData.IsAfk = afkStatus;
            setServerAfkStatus(afkStatus);
        }

        lastPosition = np;
    }

    function setServerAfkStatus(status: boolean) {
        mp.events.callRemote("CLIENT:SetAfkStatus", status);
    }
}