export default function RefuseCollector() {
    var State = false;
    var hasBinBag = false;
    var dumptruckIsFull = false;

    var blipDumptruck = null;
    var dumptruckMarker = null;
    var colshapeDumptruck = null;
    var blipBase = null;
    var markerBase = null;
    var colshapeBase = null;
    var vehRemoteID = null;

    var interval = null;

    var garbageToCollect = new Map<number, { blip: BlipMp, colshape: ColshapeMp }>();

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

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

    mp.events.add('SERVER:MuellmannBCSErstellen', (jsonPosArr, veh: VehicleMp) => {
        let posArr = JSON.parse(jsonPosArr);
        vehRemoteID = veh.remoteId;
        for (var i = 0; i < posArr.length; i++) {
            let pos = new mp.Vector3(posArr[i].x, posArr[i].y, posArr[i].z - 1)

            let newBlip = mp.blips.new(1, new mp.Vector3(pos.x, pos.y, pos.z), { name: 'Mülltonne', color: 0, shortRange: false, scale: 0.7 });
            let newColShape = mp.colshapes.newSphere(posArr[i].x, posArr[i].y, posArr[i].z + 0.5, 1.75);
            newBlip.setPriority(1000);

            newBlip.setFlashTimer(2000);

            garbageToCollect.set(i, { blip: newBlip, colshape: newColShape });
        }

        blipBase = mp.blips.new(515, new mp.Vector3(-442.3999, -1701.5234, 18.933002 - 1), { name: 'Recylinganlage', color: 24, shortRange: false });
        markerBase = mp.markers.new(1, new mp.Vector3(-442.3999, -1701.5234, 18.933002 - 1), 10, { direction: new mp.Vector3(-235.5747, -1685.475, 32.7207), color: [255, 255, 0, 150], visible: true, dimension: 0 });
        colshapeBase = mp.colshapes.newSphere(-442.3999, -1701.5234, 18.933002 - 1, 10);
    });
    mp.events.add('SERVER:MuellmannBCSEntfernen', () => {
        garbageToCollect.forEach((collection, index) => {
            let { blip, colshape } = collection;

            if (colshape && mp.colshapes.exists(colshape))
                colshape.destroy();
            if (blip && mp.blips.exists(blip))
                blip.destroy();
        });

        garbageToCollect.clear();

        if (blipDumptruck) blipDumptruck.destroy();
        blipDumptruck = null;
        if (dumptruckMarker) dumptruckMarker.destroy();
        dumptruckMarker = null;
        if (colshapeDumptruck) colshapeDumptruck.destroy();
        colshapeDumptruck = null;
        if (blipBase) blipBase.destroy();
        blipBase = null;
        if (markerBase) markerBase.destroy();
        markerBase = null;
        if (colshapeBase) colshapeBase.destroy();
        colshapeBase = null;
        hasBinBag = false;
    });

    mp.events.add("playerEnterColshape", (currentShape, player) => {
        //mp.events.callRemote('CLIENT:ImShape', JSON.stringify(Shape));

        let i = getIndex(currentShape);

        if (garbageToCollect.has(i)) {
            let { blip, colshape } = garbageToCollect.get(i);

            if (mp.players.local.vehicle) { mp.events.call("renderTextOnScreen", "Steige aus dem Müllwagen aus um den Müllsack zu entnehmen."); return; }
            if (hasBinBag) { mp.gui.chat.push("Du trägst bereits einen Müllsack!"); return; }

            if (!dumptruckIsFull) {
                mp.events.callRemote('CLIENT:MuellmannImShape', i);
                hasBinBag = true;

                if (colshape)
                    colshape.destroy();
                if (blip)
                    blip.destroy();

                garbageToCollect.delete(i);

                interval = setInterval(function () { createMarker(); }, 2);
            }
            else {
                mp.gui.chat.push("Der Müllwagen ist bereits voll! Fahre zur Base und lade ab!");
                return;
            }
        }

        if (currentShape == colshapeDumptruck) {
            if (hasBinBag) {
                hasBinBag = false;
                mp.events.callRemote('CLIENT:MuellmannAddSack');

                clearInterval(interval);

                if (blipDumptruck) blipDumptruck.destroy();
                blipDumptruck = null;
                if (dumptruckMarker) dumptruckMarker.destroy();
                dumptruckMarker = null;
                if (colshapeDumptruck) colshapeDumptruck.destroy();
                colshapeDumptruck = null;
            }
        }
        else if (currentShape == colshapeBase) {
            blipBase.setRoute(false);
            mp.events.callRemote('CLIENT:MuellmannBaseSack');
            dumptruckIsFull = false;
        }
    });
    mp.events.add("MuellmannUpdateColshape", (index) => {
        if (!garbageToCollect.has(index)) return;

        let { blip, colshape } = garbageToCollect.get(index);

        if (blip && mp.blips.exists(blip)) {
            blip.destroy();
        }

        if (colshape && mp.colshapes.exists(colshape)) {
            colshape.destroy();
        }

        garbageToCollect.delete(index);
    });

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

    function getIndex(shape: ColshapeMp): number {
        let i = -1;
        garbageToCollect.forEach((collection, index) => {
            let { blip, colshape } = collection;
            if (shape == colshape) i = index;
        });
        return i;
    }

    function createMarker() {
        let vehicle;
        if (vehRemoteID)
            vehicle = mp.vehicles.atRemoteId(vehRemoteID);
        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 (blipDumptruck) blipDumptruck.destroy();
            blipDumptruck = null;
            if (dumptruckMarker) dumptruckMarker.destroy();
            dumptruckMarker = null;
            if (colshapeDumptruck) colshapeDumptruck.destroy();
            colshapeDumptruck = null;
            blipDumptruck = mp.blips.new(318, pos, { name: 'Müllmann', color: 24, shortRange: false });
            dumptruckMarker = 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 });
            colshapeDumptruck = mp.colshapes.newSphere(pos.x, pos.y, pos.z, 2);
        }
    };
}