export default function RefuseCollector() {
    var State = false;
    var Traegt = false;
    var HintenVoll = false;

    var BlipTonneHinten = null;
    var CheckpointTonneHinten = null;
    var ShapeTonneHinten = null;
    var BlipTonneBase = null;
    var CheckpointTonneBase = null;
    var ShapeTonneBase = null;
    var vehicle = null;

    var interval = null;

    var BlipTonne = [];
    var ShapeTonne = [];

    mp.events.add('SERVER:MuellmannStatusTrue', () => {
        State = true;
    });

    mp.events.add('SERVER:MuellmannStatusFalse', () => {
        State = false;
    });

    mp.events.add('SERVER:MuellmannBCSErstellen', (jsonPosArr, veh) => {
        let posArr = JSON.parse(jsonPosArr);
        vehicle = veh;
        for (var i = 0; i < posArr.length; i++) {
            let pos = new mp.Vector3(posArr[i].x, posArr[i].y, posArr[i].z - 1)
            BlipTonne.push(mp.blips.new(1, pos, { name: 'Müllmann', color: 45, shortRange: false }));
            BlipTonne[i].setFlashTimer(2000);
            ShapeTonne.push(mp.colshapes.newSphere(posArr[i].x, posArr[i].y, posArr[i].z + 0.5, 1.75));
        }

        BlipTonneBase = mp.blips.new(1, new mp.Vector3(-535.1912, -1713.742, 19.23861 - 1), { name: 'Müllmann', color: 5, shortRange: false });
        CheckpointTonneBase = mp.markers.new(1, new mp.Vector3(-535.1912, -1713.742, 19.23861 - 1), 10, { direction: new mp.Vector3(-235.5747, -1685.475, 32.7207), color: [255, 255, 0, 150], visible: true, dimension: 0 });
        ShapeTonneBase = mp.colshapes.newSphere(-535.1912, -1713.742, 19.23861 - 1, 10);
    });
    mp.events.add('SERVER:MuellmannBCSEntfernen', () => {
        for (var i = 0; i < BlipTonne.length; i++) {
            if (BlipTonne[i] == null) continue;
            BlipTonne[i].destroy();
            ShapeTonne[i].destroy();
        }
        BlipTonne = [];
        ShapeTonne = [];

        if (BlipTonneHinten) BlipTonneHinten.destroy();
        BlipTonneHinten = null;
        if (CheckpointTonneHinten) CheckpointTonneHinten.destroy();
        CheckpointTonneHinten = null;
        if (ShapeTonneHinten) ShapeTonneHinten.destroy();
        ShapeTonneHinten = null;
        if (BlipTonneBase) BlipTonneBase.destroy();
        BlipTonneBase = null;
        if (CheckpointTonneBase) CheckpointTonneBase.destroy();
        CheckpointTonneBase = null;
        if (ShapeTonneBase) ShapeTonneBase.destroy();
        ShapeTonneBase = null;
        Traegt = false;
    });

    mp.events.add("playerEnterColshape", (Shape, player) => {
        //mp.events.callRemote('CLIENT:ImShape', JSON.stringify(Shape));
        for (var i = 0; i < ShapeTonne.length; i++) {
            if (Shape != ShapeTonne[i]) continue;
            if (!Traegt) {
                if (!HintenVoll) {
                    mp.events.callRemote('CLIENT:MuellmannImShape', i);
                    Traegt = true;
                    BlipTonne[i].destroy();
                    BlipTonne[i] = null;
                    ShapeTonne[i].destroy();
                    ShapeTonne[i] = null;

                    interval = setInterval(function () { createMarker(); }, 2);
                }
                else {
                    mp.gui.chat.push("Der Müllwagen ist bereits voll! Fahre zur Base und lade ab!");
                    return;
                }
            }
            else {
                mp.gui.chat.push(`Du trägst bereits einen Müllsack!`);
                return;
            }
        }
        if (Shape == ShapeTonneHinten) {
            if (Traegt) {
                Traegt = false;
                mp.events.callRemote('CLIENT:MuellmannAddSack');

                clearInterval(interval);

                if (BlipTonneHinten) BlipTonneHinten.destroy();
                BlipTonneHinten = null;
                if (CheckpointTonneHinten) CheckpointTonneHinten.destroy();
                CheckpointTonneHinten = null;
                if (ShapeTonneHinten) ShapeTonneHinten.destroy();
                ShapeTonneHinten = null;
            }
        }
        else if (Shape == ShapeTonneBase) {
            BlipTonneBase.setRoute(false);
            mp.events.callRemote('CLIENT:MuellmannBaseSack');
            HintenVoll = false;
        }
    });
    mp.events.add("MuellmannUpdateColshape", (index) => {
        if (BlipTonne[index] != null)
            BlipTonne[index].destroy();
        BlipTonne[index] = null;
        if (ShapeTonne[index] != null)
            ShapeTonne[index].destroy();
        ShapeTonne[index] = null;
    });

    mp.events.add("SERVER:MuellmannZuBase", () => {
        BlipTonneBase.setRoute(true);
        BlipTonneBase.setRouteColour(5);
        HintenVoll = true;
    });

    function createMarker() {
        if (vehicle != null) {
            var boneIndex2 = vehicle.getBoneIndexByName("platelight");
            var boneIndex1 = vehicle.getBoneIndexByName("chassis_dummy");

            let posPlate = vehicle.getWorldPositionOfBone(boneIndex2);
            let posCentre = vehicle.getWorldPositionOfBone(boneIndex1);

            let plateVec = new mp.Vector3(posPlate.x, posPlate.y, posPlate.z);
            let lightVec = new mp.Vector3(posCentre.x, posCentre.y, posCentre.z);

            let posPL = lightVec.subtract(plateVec);

            let temp = new mp.Vector3(posPL.x * -1, posPL.y * -1, posPL.z * -1)

            let length = Math.sqrt((temp.x * temp.x) + (temp.y * temp.y) + (temp.z * temp.z));

            let x = temp.x / length;
            let y = temp.y / length;
            let z = temp.z / length;

            temp = new mp.Vector3(x, y, z - 1);

            let pos = plateVec.add(temp);

            if (BlipTonneHinten) BlipTonneHinten.destroy();
            BlipTonneHinten = null;
            if (CheckpointTonneHinten) CheckpointTonneHinten.destroy();
            CheckpointTonneHinten = null;
            if (ShapeTonneHinten) ShapeTonneHinten.destroy();
            ShapeTonneHinten = null;
            BlipTonneHinten = mp.blips.new(318, pos, { name: 'Müllmann', color: 24, shortRange: false });
            CheckpointTonneHinten = mp.markers.new(1, pos, 1, { direction: new mp.Vector3(-235.5747, -1685.475, 32.7207), color: [0, 255, 0, 255], visible: true, dimension: 0 });
            ShapeTonneHinten = mp.colshapes.newSphere(pos.x, pos.y, pos.z, 2);
        }
    };
}