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

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

    mp.events.add('setCheckPoint', (position, player, checkPointID, nDelay, nMarkerID, nMarkerSize, nMarkerDist, nUseVehicle, nEvent) => {
        inCheckpoint = false;
        posCp = position;
        Player = player;
        delay = nDelay;
        markerID = nMarkerID;
        markerSize = nMarkerSize;
        useVehicle = nUseVehicle;
        markerDist = nMarkerDist;
        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, 50);
    });    

    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);
    });
    mp.events.add("playerStartExitVehicle", (vehicle, seat) => {
        Leaving = true;
    });
    mp.events.add("playerExitVehicle", (vehicle, seat) => {
        Leaving = true;
    });
    mp.events.add("playerStartLeaveVehicle", (vehicle, seat) => {
        Leaving = true;
    });
    mp.events.add("playerLeaveVehicle", (vehicle, seat) => {
        Leaving = true;
    });
    mp.events.add("playerEnterVehicle", (vehicle, seat) => {
        Leaving = false;
    });

    function myTimer() {
        if (useVehicle == true) {
            if (Player.vehicle) {

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

                    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);
                        mp.events.callRemote("playerWaitsInCheckpoint");
                        //mp.players.local.freezePosition(true);
                    }
                    inCheckpoint = true;

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

                            }
                            mp.events.callRemote(event, checkPointsDone);
                        }
                    } else {
                        delayCounter += 50;
                    }
                } else if (dist > 2) {
                    inCheckpoint = false;
                    delayCounter = 0;
                    //mp.gui.chat.push("Läuft.");
                }
            }
            else {

            }
        }
        if(useVehicle == false) {

            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 <= markerDist) {

                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);
                    mp.events.callRemote("playerWaitsInCheckpoint");
                    //mp.players.local.freezePosition(true);
                }
                inCheckpoint = true;

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

                        }
                        mp.events.callRemote(event, checkPointsDone);
                    }
                } else {
                    delayCounter += 50;
                }
            } 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)
    });

}