import { afkStatus } from '../Player/antiafk';
import { getWantedCount } from './wanteds';

export default function playerBlips() {
    var playerBlipMap: Map<PlayerMp, BlipMp>;
    var ready = false;
    var escapeTimer = null;

    const PD_BLIP = 38;
    const FIB_BLIP = 63;

    setInterval(() => {
        if (!ready) return;
        if (!playerBlipMap) playerBlipMap = new Map<PlayerMp, BlipMp>();

        mp.players.forEachInStreamRange(
            (player) => {
                if (mp.players.local == player)
                    return;

                if (!playerBlipMap.has(player)) {
                    let pBlip = mp.blips.new(1, player.position, {
                        alpha: 200,
                        shortRange: true,
                        dimension: mp.players.local.dimension,
                        drawDistance: 300,
                        scale: 0.7,
                    });

                    pBlip.setCategory(7);
                    pBlip.setDisplay(8);
                    playerBlipMap.set(player, pBlip);
                }
                let pBlip = playerBlipMap.get(player);
                if (player.isDead()) {
                    pBlip.setSprite(274);
                    pBlip.setScale(0.7);
                } else {
                    pBlip.setSprite(1);
                    pBlip.setScale(0.7);
                }
                let color = player.getVariable("blipColor");

                if (color == -1) {
                    pBlip.setAlpha(0);
                    player.setAlpha(0);
                } else {
                    pBlip.setAlpha(200);
                    player.setAlpha(255);

                    pBlip.setColour(isNaN(color) ? 0 : color);
                    pBlip.setPosition(player.position.x, player.position.y, player.position.z);

                    if ((color == PD_BLIP || color == FIB_BLIP || afkStatus)) {
                        if (escapeTimer) {
                            mp.events.call("SERVER:SetWantedFlash", false);
                            clearInterval(escapeTimer);
                            escapeTimer = null;
                        }
                        return;
                    }
                }
            });

        if (!escapeTimer && !afkStatus && getWantedCount() > 0) {
            mp.events.call("SERVER:SetWantedFlash", true);
            escapeTimer = setInterval(() => {
                if (getWantedCount() == 0) {
                    clearInterval(escapeTimer);
                    escapeTimer = null;
                    return;
                }

                mp.events.callRemote("CLIENT:EscapeWanted");
            }, 300000); //120000 -> 2 min , 300000 -> 5min
        }
    }, 50);

    mp.events.add("playerReady", () => {
        ready = true;
    });

    mp.events.add("entityStreamOut", (entity) => {
        if (playerBlipMap && entity.type === "player") {
            if (playerBlipMap.has(entity)) {
                var pBlip = playerBlipMap.get(entity);
                pBlip.destroy();
                playerBlipMap.delete(entity);
            }
        }
    });

    mp.events.addDataHandler("blipColor", (entity, value) => {
        if (entity.type === "player") {
            let color = parseInt(value);
            entity.setBlipColor(isNaN(color) ? 0 : color);
        }
    });
}