import { world, system } from "@minecraft/server";

const helmetParticle = {
    //BELOW IS HEAD TRAILING
    "just:baby_turtle": { id: "minecraft:water_splash_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:bee_hat": { id: "just:honey_drip", offset: { x: 0, y: 1, z: 0 } },
    "just:bee_hat_trans": { id: "minecraft:honey_drip_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:bee_hat_pride": { id: "minecraft:honey_drip_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:camera": { id: "minecraft:camera_shoot_explosion", offset: { x: 0, y: 2, z: 0 } },
    "just:camera_beard": { id: "minecraft:camera_shoot_explosion", offset: { x: 0, y: 2, z: 0 } },
    "just:candle_on_head": { id: "just:red_flame", offset: { x: 0, y: 2, z: 0 } },
    "just:demon_eyes": { id: "just:music_note", offset: { x: 0, y: 2, z: 0 } },
    "just:demon_horns": { id: "just:music_note", offset: { x: 0, y: 2, z: 0 } },
    "just:dragon_skull": { id: "just:music_note", offset: { x: 0, y: 2, z: 0 } },
    "just:fake_blight": { id: "minecraft:mobspell_ambient", offset: { x: 0, y: 2, z: 0 } },
    "just:fake_fire": { id: "just:red_flame", offset: { x: 0, y: 2, z: 0 } },
    "just:firework": { id: "minecraft:critical_hit_emitter", offset: { x: 0, y: 2, z: 0 } },
    "just:fish_on_head": { id: "minecraft:water_splash_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:floating_hearts": { id: "minecraft:heart_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:floating_star": { id: "minecraft:electric_spark_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:flower_crown": { id: "minecraft:azalea_leaves_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:flower_on_head": { id: "minecraft:spore_blossom_ambient_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:ghostmask": { id: "minecraft:soul_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:head_bolts": { id: "minecraft:electric_spark_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:headphones_blue": { id: "just:music_note", offset: { x: 0, y: 2, z: 0 } },
    "just:headphones_red": { id: "just:music_note", offset: { x: 0, y: 2, z: 0 } },
    "just:headphones_pink": { id: "just:music_note", offset: { x: 0, y: 2, z: 0 } },
    "just:headphones_reggae": { id: "just:music_note", offset: { x: 0, y: 2, z: 0 } },
    "just:holy_head": { id: "minecraft:glow_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:ice_dragon_skull": { id: "just:snowflake", offset: { x: 0, y: 2, z: 0 } },
    "just:smoking_pipe": { id: "minecraft:basic_smoke_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:snowman_baby": { id: "just:snowflake", offset: { x: 0, y: 2, z: 0 } },
    "just:stress": { id: "just:music_note", offset: { x: 0, y: 2, z: 0 } },
    "just:unicorn_horn": { id: "minecraft:glow_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:wing_hat": { id: "minecraft:cauldron_explosion_emitter", offset: { x: 0, y: 2, z: 0 } },
    "just:bucket_cap": { id: "just:bucket_drip", offset: { x: 0, y: 2, z: 0 } },
    "just:sleepy_eye_mask": { id: "minecraft:heart_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:axolotl_on_head": { id: "minecraft:dolphin_move_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:bee_on_head": { id: "minecraft:honey_drip_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:crystal_horns": { id: "minecraft:glow_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:druid_antlers_rare": { id: "minecraft:spore_blossom_ambient_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:eye_of_the_bee_holder": { id: "minecraft:honey_drip_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:dark_eye_of_the_holder": { id: "minecraft:small_soul_fire_flame", offset: { x: 0, y: 2, z: 0 } },
    "just:evil_eye_of_the_holder": { id: "minecraft:end_chest", offset: { x: 0, y: 2, z: 0 } },
    "just:warm_eye_of_the_holder": { id: "minecraft:lava_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:xanath_eye_of_the_holder": { id: "minecraft:enchanting_table_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:mimic_head_dark": { id: "minecraft:soul_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:mimic_head_gold": { id: "minecraft:electric_spark_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:slime_cube_dnd": { id: "minecraft:soul_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:slime_head": { id: "minecraft:spore_blossom_ambient_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:stinky_cheese_man": { id: "minecraft:mycelium_dust_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:toilet_hat": { id: "minecraft:water_splash_particle", offset: { x: 0, y: 2, z: 0 } },
    "just:just_ducke": { id: "minecraft:water_splash_particle", offset: { x: 0, y: 2, z: 0 } },
    //BELOW IS FULL BODY TRAILING...............................................................................................................................
    "just:christmas_tree": { id: "just:snowflake", offset: { x: 0, y: 1, z: 0 } },
    "just:dragon_skull_ender": { id: "minecraft:dragon_breath_trail", offset: { x: 0, y: 1, z: 0 } },
    "just:frozen_head": { id: "just:snowflake", offset: { x: 0, y: 1, z: 0 } },
    "just:host_hat": { id: "just:music_note", offset: { x: 0, y: 1, z: 0 } },
    "just:potions_head": { id: "minecraft:mobspell_ambient", offset: { x: 0, y: 1, z: 0 } },
    "just:shroom_cap": { id: "minecraft:mycelium_dust_particle", offset: { x: 0, y: 1, z: 0 } },
    "just:red_shroom_cap": { id: "minecraft:mycelium_dust_particle", offset: { x: 0, y: 1, z: 0 } },
    "just:super_sand_hat": { id: "minecraft:electric_spark_particle", offset: { x: 0, y: 1, z: 0 } },
    "just:zigzag_witch_hat": { id: "minecraft:witchspell_emitter", offset: { x: 0, y: 1, z: 0 } },
    "just:amalgalich_hat": { id: "minecraft:end_chest", offset: { x: 0, y: 1, z: 0 } },
    "just:baby_dolphin": { id: "minecraft:dolphin_move_particle", offset: { x: 0, y: 1, z: 0 } },
    "just:artsy": { id: "minecraft:glow_particle", offset: { x: 0, y: 1, z: 0 } },
    "just:nyan_doll": { id: "minecraft:mobspell_ambient", offset: { x: 0, y: 1, z: 0 } },
    "just:mindflayer": { id: "minecraft:glow_particle", offset: { x: 0, y: 1, z: 0 } },
    "just:mindflayer_alhoon": { id: "minecraft:enchanting_table_particle", offset: { x: 0, y: 1, z: 0 } },
    "just:octodad": { id: "minecraft:water_splash_particle", offset: { x: 0, y: 1, z: 0 } },
    "just:pika_ears": { id: "minecraft:electric_spark_particle", offset: { x: 0, y: 1, z: 0 } },
    //BELOW IS FEET TRAILING...................................................................................................................................
    "just:blue_fire_eye": { id: "just:soul_flame", offset: { x: 0, y: 0.2, z: 0 } },
    "just:dragon_hat": { id: "just:red_flame", offset: { x: 0, y: 0.2, z: 0 } },
    "just:demon_eyes": { id: "minecraft:lava", offset: { x: 0, y: 0.2, z: 0 } },
    "just:sonic_hood": { id: "minecraft:sonic_explosion", offset: { x: 0, y: 0.2, z: 0 } },
};

const toggleKey = "particleToggle";

world.beforeEvents.itemUse.subscribe((event) => {
    const player = event.source;
    if (!player.isSneaking) return;

    const item = event.itemStack;
    if (!item || !(item.typeId in helmetParticle)) return;

    const current = item.getDynamicProperty(toggleKey);
    const newState = !(current === true);

    item.setDynamicProperty(toggleKey, newState);
    player.sendMessage(`Helmet particle ${newState ? "enabled" : "disabled"}.`);

    const equipment = player.getComponent("minecraft:equippable");
    if (!equipment) return;

    const equippedHelmet = equipment.getEquipment("Head");
    if (equippedHelmet && equippedHelmet.typeId === item.typeId) {
        equipment.setEquipment("Head", item);
    }
});

system.runInterval(() => {
    for (const player of world.getPlayers()) {
        const equipment = player.getComponent("minecraft:equippable");
        if (!equipment) continue;

        const helmet = equipment.getEquipment("Head");
        if (!helmet) continue;

        const particleInfo = helmetParticle[helmet.typeId];
        if (!particleInfo) continue;

        const enabled = helmet.getDynamicProperty(toggleKey);
        if (!enabled) continue;

        const id = particleInfo.id;
        const offset = particleInfo.offset;
        const pos = player.location;

        const spawnPos = {
            x: pos.x + offset.x,
            y: pos.y + offset.y,
            z: pos.z + offset.z,
        };

        player.dimension.spawnParticle(id, spawnPos);
    }
}, 10);
