import * as NativeUI from '../libs/NativeUI';
const Menu = NativeUI.Menu;
const UIMenuItem = NativeUI.UIMenuItem;
const UIMenuListItem = NativeUI.UIMenuListItem;
const UIMenuCheckboxItem = NativeUI.UIMenuCheckboxItem;
const UIMenuSliderItem = NativeUI.UIMenuSliderItem;
const BadgeStyle = NativeUI.BadgeStyle;
const Point = NativeUI.Point;
const ItemsCollection = NativeUI.ItemsCollection;
const Color = NativeUI.Color;
const ListItem = NativeUI.ListItem;

export default function drivingSchoolHandle(globalData: IGlobalData) {

    var keyBound = false;
    var mainMenu: NativeUI.Menu;
    let send = new UIMenuItem("Pruefung starten", "");
    var timer;
    var timerCheckpoint;
    var timerSet = false;
    var Player;
    var timeToWait = 0;
    var checkPoint;
    var counter = 0;
    var waitFinished = false;
    var kmh;

    mp.events.add('drivingSchoolMenu', () => {
        /*mp.game.ui.setTextComponentFormat('STRING');
        mp.game.ui.addTextComponentSubstringPlayerName('Drücke ~INPUT_CONTEXT~, um das Fahrschulmenü zu öffnen');
        mp.game.ui.displayHelpTextFromStringLabel(0, true, true, -1);
        */

        mp.events.call("renderHelpTextOnScreen", "~s~Drücke ~g~E, ~s~um das ~b~Fahrschulmenü ~s~zu öffnen")

        mp.keys.bind(0x45, false, keyPressHandler);
        keyBound = true;
    });

    mp.events.add('removeDrivingSchoolMenu', () => {
        mp.game.ui.clearHelp(true);
        if (mainMenu != null) {
            mainMenu.Close();
        }

        if (keyBound) {
            keyBound = false;
            mp.keys.unbind(0x45, false, keyPressHandler);
        }
    });

    mp.events.add('waitPlayerEntersVehicle', () => {
        mp.game.ui.setNewWaypoint(-761.9943, -1321.84);
        timerSet = true;
        timer = setInterval(timerPlayerInVehicle, 3000);        
    });

    
    mp.events.add('stopTimer', () => {
        timerSet = false;
        clearInterval(timer);
    });    
    
    mp.events.add('waitHere', (player, CP, time) => {
        let dist;
        waitFinished = false;
        timeToWait = time;
        Player = player;
        checkPoint = CP;
        let counter = 0;
        clearInterval(timerCheckpoint);
        counter = 0;
        timerCheckpoint = setInterval(timerWaitHere, 100);   
        
    });
    
    mp.events.add('checkWait', () => {
        if (waitFinished == false) {
            mp.gui.chat.push("Du hast an der letzten Kreuzung nicht gewartet");
        }
    });

   

    mp.events.add('lookDirectionTesting', (veh) => {
        let player = mp.players.local;
        let camera = mp.cameras.new("gameplay");
        var dir = camera.getDirection();
        

        mp.gui.chat.push("x: " + veh.rotation.x + " | y: " + veh.rotation.y + " |z: " + veh.rotation.z);
    });

    function timerWaitHere() {
        let dist = mp.game.gameplay.getDistanceBetweenCoords(Player.position.x, Player.position.y, 0, checkPoint.x, checkPoint.y, 0, false);

        if (dist <= 2) {
            counter += 100;
        } else {
            counter = 0;
        }

        if (counter >= timeToWait) {
            mp.gui.chat.push("Gut, fahre nun weiter.");
            waitFinished = true;
            clearInterval(timerCheckpoint);
            counter = 0;
        }

    };
   
    function timerPlayerInVehicle() {
        if (timerSet) {
            mp.events.callRemote('timerCheckVehicle');
        }
    };

    function keyPressHandler() {
        if (globalData.InChat || globalData.InMenu || globalData.InMenu) return;
        
        mp.events.call('removeDrivingSchoolMenu', false);
        mp.events.call('showDrivingSchoolSelector');
    }

    mp.events.add('kmh100', () => {
        let vehicle = mp.players.local.vehicle;
        let speed = vehicle.getSpeed();
        speed = speed * 3.6; // convert to kmh
        if (speed > 100) {
            mp.events.callRemote("CLIENT:drivingschool_failpoints");
        }
    });

    mp.events.add('kmh150', () => {
        let vehicle = mp.players.local.vehicle;
        let speed = vehicle.getSpeed();
        speed = speed * 3.6; //convert to kmh
        if (speed > 150) {
            mp.events.callRemote("CLIENT:drivingschool_failpoints");
        }
    });


}