let player = mp.players.local;

export default function speedometer(globalData: GlobalData) {
    var dictLoaded = false;

    var lockStatus = false;

    mp.events.add('render', () => {
        if (player.vehicle && player.vehicle.getPedInSeat(-1) === player.handle && !globalData.HideGui) // Check if player is in vehicle and is driver
        {
            if (!dictLoaded) {
                mp.game.graphics.requestStreamedTextureDict("vehicleimages", true);
                dictLoaded = true;
            }

            mp.game.graphics.drawRect(0.5, 0.995, 0.35, 0.05, 0, 0, 0, 170);

            var { x, y } = mp.game.graphics.getScreenActiveResolution(0, 0);

            var engineR = 255;
            var engineG = 255;
            var engineB = 255;

            if (player.vehicle.getIsEngineRunning()) {
                engineR = 104;
                engineG = 212;
                engineB = 42;
            } else {
                engineR = 188;
                engineG = 25;
                engineB = 25;
            }

            let speed = (player.vehicle.getSpeed() * 3.6).toFixed(0);

            var lockSprite = "";
            var lockR = 255;
            var lockG = 255;
            var lockB = 255;

            if (lockStatus) {
                lockR = 104;
                lockG = 212;
                lockB = 42;
                lockSprite = "lockclosed";
            } else {
                lockSprite = "lockopen";
                lockR = 188;
                lockG = 25;
                lockB = 25;
            }

            mp.game.graphics.drawSprite("vehicleimages", "engine", 0.343, 0.985, 0.015, 0.015 * (x / y), 0, engineR, engineG, engineB, 255);

            mp.game.graphics.drawSprite("vehicleimages", lockSprite, 0.37, 0.985, 0.015, 0.015 * (x / y), 0, lockR, lockG, lockB, 255);

            mp.game.graphics.drawText(speed.toString() + " KM/H", [0.41, 0.973], {
                font: 0,
                color: [255, 255, 255, 255],
                scale: [0.35, 0.35],
                outline: true,
                centre: false
            });

            var drivenDistance = player.vehicle.getVariable("drivenDistance");
            var drivenDistanceStr = "0";
            if (drivenDistance) drivenDistanceStr = parseFloat(drivenDistance).toFixed(3).toString().replace(".", ",");
            mp.game.graphics.drawText(drivenDistanceStr + " KM", [0.48, 0.973], {
                font: 0,
                color: [255, 255, 255, 255],
                scale: [0.35, 0.35],
                outline: true,
                centre: false
            });
        }
        else {
            if (dictLoaded) {
                mp.game.graphics.setStreamedTextureDictAsNoLongerNeeded("vehicleimages");
                dictLoaded = false;
            }
        }
    });

    mp.events.add("Vehicle_setLockStatus", (status) => {
        lockStatus = status;
    });
}