import { system, world, ItemStack } from "@minecraft/server";
import {
    applyEntityColor,
    spawnColoredItem,
    emptyInventoryItems
} from "./common";

// Constants
export const MAX_BREAK_PUNCHES = 3; // Number of consecutive punches to trigger the event
export const BREAK_PUNCH_WINDOW_TICKS = 20; // Maximum time window between punches in ticks
export const EXCLUDED_INTERACTION_BLOCKS = [
    'minecraft:frame',
    'minecraft:glow_frame',
    'minecraft:chiseled_bookshelf',
    // 'minecraft:chest',
    // 'minecraft:trapped_chest',
    // 'minecraft:ender_chest',
    // 'minecraft:brewing_stand',
];

// Define item spawn mapping
export const colorCodeIds = {
    // Color codes
    "black": 0,
    "blue": 1,
    "copper": 2,
    "green": 3,
    "purple": 4,
    "red": 5,
    "silver": 6,
    "yellow": 7,
    "police": 8
};
export const spawnItems = {
    "oct_spcrs:car_manual": {
        entityId: "oct_spcrs:car_manual",
        spawnSound: {
            sound: "item.book.page_turn"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            const pageId = itemStack.getDynamicProperty('oct_spcrs:stored_page_id') || 0;
            spawnedEntity.setProperty('oct_spcrs:page_id', pageId);
        }
    },
    "oct_spcrs:car_1_COLORCODE": {
        entityId: "oct_spcrs:car_1",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_1");
        }
    },
    "oct_spcrs:car_2_COLORCODE": {
        entityId: "oct_spcrs:car_2",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_2");
        }
    },
    "oct_spcrs:car_3_COLORCODE": {
        entityId: "oct_spcrs:car_3",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_3");
        }
    },
    "oct_spcrs:car_4_COLORCODE": {
        entityId: "oct_spcrs:car_4",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_4");
        }
    },
    "oct_spcrs:car_5_COLORCODE": {
        entityId: "oct_spcrs:car_5",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_5");
        }
    },
    "oct_spcrs:car_6_COLORCODE": {
        entityId: "oct_spcrs:car_6",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_6");
        }
    },
    "oct_spcrs:car_7_COLORCODE": {
        entityId: "oct_spcrs:car_7",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_7");
        }
    },
    "oct_spcrs:car_8_COLORCODE": {
        entityId: "oct_spcrs:car_8",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_8");
        }
    },
    "oct_spcrs:car_9_COLORCODE": {
        entityId: "oct_spcrs:car_9",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_9");
        }
    },
    "oct_spcrs:car_10_COLORCODE": {
        entityId: "oct_spcrs:car_10",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_10");
        }
    },
    "oct_spcrs:car_11_COLORCODE": {
        entityId: "oct_spcrs:car_11",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_11");
        }
    },
    "oct_spcrs:car_12_COLORCODE": {
        entityId: "oct_spcrs:car_12",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_12");
        }
    },
    "oct_spcrs:car_13_COLORCODE": {
        entityId: "oct_spcrs:car_13",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_13");
        }
    },
    "oct_spcrs:car_14_COLORCODE": {
        entityId: "oct_spcrs:car_14",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_14");
        }
    },
    "oct_spcrs:car_15_COLORCODE": {
        entityId: "oct_spcrs:car_15",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_15");
        }
    },
    "oct_spcrs:car_16_COLORCODE": {
        entityId: "oct_spcrs:car_16",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_16");
        }
    },
    "oct_spcrs:car_17_COLORCODE": {
        entityId: "oct_spcrs:car_17",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_17");
        }
    },
    "oct_spcrs:car_18_COLORCODE": {
        entityId: "oct_spcrs:car_18",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_18");
        }
    },
    "oct_spcrs:car_19_COLORCODE": {
        entityId: "oct_spcrs:car_19",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_19");
        }
    },
    "oct_spcrs:car_20_COLORCODE": {
        entityId: "oct_spcrs:car_20",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_20");
        }
    },
    "oct_spcrs:car_21_COLORCODE": {
        entityId: "oct_spcrs:car_21",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_21");
        }
    },
    "oct_spcrs:car_22_COLORCODE": {
        entityId: "oct_spcrs:car_22",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_22");
        }
    },
    "oct_spcrs:car_23_COLORCODE": {
        entityId: "oct_spcrs:car_23",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_23");
        }
    },
    "oct_spcrs:car_24_COLORCODE": {
        entityId: "oct_spcrs:car_24",
        facingDirections: 8,
        spawnSound: {
            sound: "mob.irongolem.crack"
        },
        onSpawnFunction: (source, spawnedEntity, spawnLocation, itemStack) => {
            carSpawnFunction(spawnedEntity, itemStack, "car_24");
        }
    }
};
// Define break behaviors for entities
export const entityBreak = {
    "oct_spcrs:car_manual": {
        spawnItem: "oct_spcrs:car_manual",
        onBreakFunction: (entity) => {
            // Retrieve the numeric color code from the mailbox entity
            const pageId = entity.getProperty('oct_spcrs:page_id') || 0;

            // Create the item
            const itemStack = new ItemStack(entityBreak[entity.typeId].spawnItem, 1);

            // Encode the page
            if (pageId) {
                itemStack.setDynamicProperty('oct_spcrs:stored_page_id', pageId);
            }

            // Spawn the item
            entity.dimension.spawnItem(itemStack, entity.location);

            // Despawn the entity
            system.runTimeout(() => {
                if (entity.isValid()) {
                    entity.remove();
                }
            }, 20);
        }
    },
    "oct_spcrs:car_1": {
        fallbackSpawnItem: "oct_spcrs:car_1_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_1_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_2": {
        fallbackSpawnItem: "oct_spcrs:car_2_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_2_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_3": {
        fallbackSpawnItem: "oct_spcrs:car_3_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_3_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_4": {
        fallbackSpawnItem: "oct_spcrs:car_4_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_4_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_5": {
        fallbackSpawnItem: "oct_spcrs:car_5_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_5_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_6": {
        fallbackSpawnItem: "oct_spcrs:car_6_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_6_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_7": {
        fallbackSpawnItem: "oct_spcrs:car_7_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_7_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_8": {
        fallbackSpawnItem: "oct_spcrs:car_8_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_8_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_9": {
        fallbackSpawnItem: "oct_spcrs:car_9_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_9_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_10": {
        fallbackSpawnItem: "oct_spcrs:car_10_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_10_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_11": {
        fallbackSpawnItem: "oct_spcrs:car_11_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_11_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_12": {
        fallbackSpawnItem: "oct_spcrs:car_12_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_12_", true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_13": {
        fallbackSpawnItem: "oct_spcrs:car_13_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_13_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_14": {
        fallbackSpawnItem: "oct_spcrs:car_14_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_14_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_15": {
        fallbackSpawnItem: "oct_spcrs:car_15_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_15_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_16": {
        fallbackSpawnItem: "oct_spcrs:car_16_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_16_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_17": {
        fallbackSpawnItem: "oct_spcrs:car_17_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_17_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_18": {
        fallbackSpawnItem: "oct_spcrs:car_18_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_18_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_19": {
        fallbackSpawnItem: "oct_spcrs:car_19_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_19_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_20": {
        fallbackSpawnItem: "oct_spcrs:car_20_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_20_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_21": {
        fallbackSpawnItem: "oct_spcrs:car_21_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_21_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_22": {
        fallbackSpawnItem: "oct_spcrs:car_22_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_22_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_23": {
        fallbackSpawnItem: "oct_spcrs:car_23_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_23_", true, true, true);
            emptyInventoryItems(entity);
        }
    },
    "oct_spcrs:car_24": {
        fallbackSpawnItem: "oct_spcrs:car_24_silver",
        breakSound: {
            sound: "mob.irongolem.repair"
        },
        hitSound: {
            sound: "mob.irongolem.hit",
            pitchMin: 0.9,
            pitchMax: 1.1
        },
        onBreakFunction: (entity) => {
            spawnColoredItem(entity, "oct_spcrs:car_24_", true, true, true);
            emptyInventoryItems(entity);
        }
    }
};

export function getVehicleStats(EntityTypeId) {
    const gasSoundKeys = {
        engineIdle: "oct_spcrs.car.engine_idle",
        driveFast: "oct_spcrs.car.engine_drive_fast",
        driveSlow: "oct_spcrs.car.engine_drive_slow",
        engineStart: "oct_spcrs.car.engine_start",
        engineStop: "oct_spcrs.car.engine_stop",
        doorClose: "oct_spcrs.car.door_close",
        horn: "oct_spcrs.car.car_horn",
        siren: "oct_spcrs.car.police_siren",
    }
    const electricSoundKeys = {
        engineIdle: "",
        driveFast: "oct_spcrs.car.electric_engine_drive_fast",
        driveSlow: "oct_spcrs.car.electric_engine_drive_slow",
        engineStart: "oct_spcrs.car.electric_engine_start",
        engineStop: "oct_spcrs.car.electric_engine_stop",
        doorClose: "oct_spcrs.car.door_close",
        horn: "oct_spcrs.car.car_horn",
        siren: "oct_spcrs.car.police_siren",
    }
    const defaultStats = {
        inputThreshold: 0.0005,
        lateralMovementThreshold: 0.4,
        //driftCorrectionFactor: 0.01,
        //maxDriftAngle: 30,
        driftAngle: 5,
        driftThreshold: 0.5,
        nitroFillRate: 2,
        baseAcceleration: 0.05,
        baseBackwardsAcceleration: 0.1,
        backwardSpeedMultiplier: 0.8,
        accelerationDampeningPower: 1.4,
        topSpeed: 150,
        isElectric: false,
        fuelType: 'gas',
        soundKeys: {
            ...gasSoundKeys
        }
    };
    const vehicleStatsMap = {
        "oct_spcrs:car_1": { // Astor Speedster
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.4,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 5,
            driftThreshold: 0.5,
            nitroFillRate: 3,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 145,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_2": { // Audion Electra
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.5,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 4,
            driftThreshold: 0.5,
            nitroFillRate: 2,
            baseAcceleration: 0.04,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 160,
            isElectric: true,
            fuelType: 'electric',
            soundKeys: {
                ...electricSoundKeys
            }
        },
        "oct_spcrs:car_3": { // Bugato Hyperion
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.4,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 5,
            driftThreshold: 0.5,
            nitroFillRate: 3,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 165,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_4": { // Velocity Alpha 5
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.45,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 4.5,
            driftThreshold: 0.5,
            nitroFillRate: 2,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 145,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_5": { // Ferrix Blaze
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.4,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 5,
            driftThreshold: 0.5,
            nitroFillRate: 3,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 165,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_6": { // Ferrix Thunder
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.45,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 4.5,
            driftThreshold: 0.5,
            nitroFillRate: 2,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 175,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_7": { // Lambor Storm
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.35,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 6,
            driftThreshold: 0.5,
            nitroFillRate: 3,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 160,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_8": { // Vortex Supercoupe
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.45,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 4.5,
            driftThreshold: 0.5,
            nitroFillRate: 2,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 160,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_9": { // Porcha Nova
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.45,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 4.5,
            driftThreshold: 0.5,
            nitroFillRate: 2,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 145,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_10": { // Porcha Venom
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.45,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 4.5,
            driftThreshold: 0.5,
            nitroFillRate: 2,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 165,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_11": { // Teslar Supertruck
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.5,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 4,
            driftThreshold: 0.5,
            nitroFillRate: 2,
            baseAcceleration: 0.04,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 135,
            isElectric: true,
            fuelType: 'electric',
            soundKeys: {
                ...electricSoundKeys
            }
        },
        "oct_spcrs:car_12": { // Teslar Roadster
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.5,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 4,
            driftThreshold: 0.5,
            nitroFillRate: 2,
            baseAcceleration: 0.06,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 190,
            isElectric: true,
            fuelType: 'electric',
            soundKeys: {
                ...electricSoundKeys
            }
        },
        "oct_spcrs:car_13": { // Astor Regal
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.4,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 5,
            driftThreshold: 0.5,
            nitroFillRate: 3,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 160,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_14": { // Bugato Mariner
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.5,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 4,
            driftThreshold: 0.5,
            nitroFillRate: 2,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 135,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_15": { // Ferrix V40
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.35,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 6,
            driftThreshold: 0.5,
            nitroFillRate: 3,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 165,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_16": { // Vortex F1
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.5,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 4,
            driftThreshold: 0.5,
            nitroFillRate: 2,
            baseAcceleration: 0.04,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 190,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_17": { // Imperion Specter
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.5,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 4,
            driftThreshold: 0.5,
            nitroFillRate: 2,
            baseAcceleration: 0.01,
            baseBackwardsAcceleration: 0.05,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 135,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_18": { // Astraeus Horizon
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.45,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 4.5,
            driftThreshold: 0.5,
            nitroFillRate: 2,
            baseAcceleration: 0.06,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 190,
            isElectric: true,
            fuelType: 'electric',
            soundKeys: {
                ...electricSoundKeys
            }
        },
        "oct_spcrs:car_19": { // Vortex Sunburst
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.45,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 4.5,
            driftThreshold: 0.5,
            nitroFillRate: 2,
            baseAcceleration: 0.04,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 190,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_20": { // Lambor Hurricane
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.45,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 4.5,
            driftThreshold: 0.5,
            nitroFillRate: 2,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 175,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_21": { // Lambor Aurochs
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.35,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 6,
            driftThreshold: 0.5,
            nitroFillRate: 3,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 165,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_22": { // Pontor Phoenix
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.4,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 5,
            driftThreshold: 0.5,
            nitroFillRate: 3,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 160,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_23": { // Pengar Paradise
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.5,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 4,
            driftThreshold: 0.5,
            nitroFillRate: 2,
            baseAcceleration: 0.04,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 190,
            soundKeys: {
                ...gasSoundKeys
            }
        },
        "oct_spcrs:car_24": { // Chevaron Stallion
            inputThreshold: 0.0005,
            lateralMovementThreshold: 0.4,
            //driftCorrectionFactor: 0.01,
            //maxDriftAngle: 30,
            driftAngle: 5,
            driftThreshold: 0.5,
            nitroFillRate: 3,
            baseAcceleration: 0.02,
            baseBackwardsAcceleration: 0.1,
            backwardSpeedMultiplier: 0.8,
            accelerationDampeningPower: 1.4,
            topSpeed: 165,
            soundKeys: {
                ...gasSoundKeys
            }
        },
    };

    return vehicleStatsMap[EntityTypeId] || defaultStats;
}

function carSpawnFunction(spawnedEntity, itemStack, idKey) {
    // Extract the color code by replacing the prefix`
    const colorCode = itemStack.typeId.replace(`oct_spcrs:${idKey}_`, "");
    applyEntityColor(spawnedEntity, colorCode);

    // Check for encoded hp
    const hp = itemStack.getDynamicProperty("oct_spcrs:hp");
    if (hp) {
        spawnedEntity.getComponent("health").setCurrentValue(hp);
    }

    // Check for encoded name
    const name = itemStack.getDynamicProperty("oct_spcrs:name");
    if (name) {
        spawnedEntity.nameTag = name;
    }

    // Check for encoded fuel
    const fuel = itemStack.getDynamicProperty("oct_spcrs:fuel_level");
    if (fuel) {
        spawnedEntity.setDynamicProperty("oct_spcrs:fuel_level", fuel);
    }

    system.runTimeout(() => {
        if (!spawnedEntity.isValid()) return;
        if (spawnedEntity.getProperty("oct_spcrs:anim.spawn_anim")) {
            spawnedEntity.setProperty("oct_spcrs:anim.spawn_anim", false);
        }
    }, 10);

}

