import * as NativeUI from '../libs/NativeUI';
import InputHelper from '../inputhelper';

export default function checkpointHandle(globalData: GlobalData) {
    var count = 0;
    var myVar;
    var posCp;
    var Player;
    var activeCheckpoint;
    var inCheckpoint = false;
    var delay;
    var delayCounter;
    var markerID;
    var markerSize;
    var event;
    var checkPointsDone = 0;
    var activeBlip = null;

    mp.events.add('setCheckPoint', (position, player, checkPointID, nDelay, nMarkerID, nMarkerSize, nEvent) => {
        inCheckpoint = false;
        posCp = position;
        Player = player;
        delay = nDelay;
        markerID = nMarkerID;
        markerSize = nMarkerSize;
        delayCounter = 0;
        checkPointsDone = checkPointID;
        event = nEvent;
        activeCheckpoint = mp.markers.new(markerID, posCp, markerSize, {
            color: [255, 0, 0, 150],
            visible: true,
            dimension: 0
        });

        activeBlip = mp.blips.new(1, posCp,
            {
                color: 33,
                alpha: 255,
                shortRange: false,
            });
        activeBlip.setRoute(true);
        activeBlip.setRouteColour(33);
        clearInterval(myVar);
        //mp.events.call("SERVER:Util_setWaypoint", posCp.X, posCp.Z);
        //mp.game.ui.setNewWaypoint(posCp.x, posCp.y);
        myVar = setInterval(myTimer, 100);
    });    

    mp.events.add('destroyCP', () => {
        if (activeCheckpoint != null) {
            activeCheckpoint.destroy();
            activeBlip.destroy();
            clearInterval(myVar);
            activeBlip = null;
            activeCheckpoint = null;
        }       
    });

    mp.events.add('getCP', () => {
        var pos = posCp;
        mp.events.callRemote("sendCP", posCp.x, posCp.y, posCp.z);
    });


    function myTimer() {
        let dist = mp.game.gameplay.getDistanceBetweenCoords(Player.position.x, Player.position.y, 0, posCp.x, posCp.y, 0, false);
        //mp.gui.chat.push("delay: " + delay + " | delayCounter: " + delayCounter);
        if (dist <= 3) {

            if (!inCheckpoint) {
                if (delay != 0) {
                    massage = "Warte nun " + delay / 1000 +" Sekunden.";
                    render = true;
                    textTime = setInterval(ClearText, delay)
                }
                mp.game.audio.playSoundFrontend(1, "Beep_Red", "DLC_HEIST_HACKING_SNAKE_SOUNDS", true);
            }
            inCheckpoint = true;

            if (delayCounter >= delay) {
                activeCheckpoint.destroy();
                activeBlip.destroy();
                activeBlip = null;
                activeCheckpoint = null;
                clearInterval(myVar);
                mp.events.callRemote("playerInCheckpoint");
                if (event != "") {
                    while (checkPointsDone > 10000) {

                    }
                    mp.events.callRemote(event, checkPointsDone);
                }
            } else {
                delayCounter += 100;
            }
        } else if (dist > 2) {
            inCheckpoint = false;
            delayCounter = 0;
        }
    }


    var renderedText = null;
    var textTime = null;
    var render = false;
    var massage;

    function ClearText() {
        render = false;
        clearInterval(textTime);
        textTime = null;
    }

    mp.events.add('render', () => {
        if (render) {
            renderedText = mp.game.graphics.drawText(massage, [0.5, 0.85], {
                font: 4,
                centre: true,
                color: [255, 255, 145, 185],
                scale: [0.5, 0.5],
                outline: true
            });
        }

    });

    mp.events.add('renderTextOnScreen', (msg) => {
        if (textTime != null) {
            clearInterval(textTime);
            textTime = null;
        }
        massage = msg;
        render = true;
        textTime = setInterval(ClearText, 7000)
    });

}