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

const GunShootComponent = {
    onUse({ itemStack, source }) {
        if (!source || !itemStack) return;

        const typeId = itemStack.typeId;

        // Check for the first gun (10mm Pistol)
        if (typeId === "fw:pistol_10mm") {
            source.runCommandAsync("scoreboard players remove @s pistol_10mm 1").catch(() => {});
            source.runCommandAsync("execute at @s run playsound guns.10mm_pistol_shoot @a ~ ~ ~").catch(() => {});
            source.runCommandAsync("camerashake add @s 0.05 0.25").catch(() => {});
            system.run(() => {
                try {
                    const viewDir = source.getViewDirection();
                    const headLoc = source.getHeadLocation();
                    const dimension = source.dimension;
                    if (!viewDir || !headLoc || !dimension) return;

                    const spawnPos = {
                        x: headLoc.x + viewDir.x * 1.5, // Further out of hitbox
                        y: headLoc.y + viewDir.y * 1.5,
                        z: headLoc.z + viewDir.z * 1.5,
                    };

                    const velocity = {
                        x: viewDir.x * 8,
                        y: viewDir.y * 8,
                        z: viewDir.z * 8,
                    };

                    const bullet = dimension.spawnEntity("fw:b10mm_bullet", spawnPos);
                    if (bullet) {
                        bullet.applyImpulse(velocity);
                        bullet.addTag("fw:b10mm_cooldown");

                        system.runTimeout(() => {
                            bullet.removeTag("fw:b10mm_cooldown");
                        }, 2);
                    }
                } catch (e) {
                    console.warn("Error firing 10mm bullet:", e);
                }
            });
        }

        // Check for the second gun (Pipe Pistol)
        else if (typeId === "fw:pipe_pistol") {
            source.runCommandAsync("scoreboard players remove @s pipe_pistol 1").catch(() => {});
            source.runCommandAsync("execute at @s run playsound guns.pipe_pistol_shoot @a ~ ~ ~").catch(() => {});
            source.runCommandAsync("camerashake add @s 0.05 0.25").catch(() => {});
            system.run(() => {
                try {
                    const viewDir = source.getViewDirection();
                    const headLoc = source.getHeadLocation();
                    const dimension = source.dimension;
                    if (!viewDir || !headLoc || !dimension) return;

                    const spawnPos = {
                        x: headLoc.x + viewDir.x * 1.5, // Further out of hitbox
                        y: headLoc.y + viewDir.y * 1.5,
                        z: headLoc.z + viewDir.z * 1.5,
                    };

                    const velocity = {
                        x: viewDir.x * 8,
                        y: viewDir.y * 8,
                        z: viewDir.z * 8,
                    };
                    const bullet = dimension.spawnEntity("fw:b38_bullet", spawnPos);
                    if (bullet) bullet.applyImpulse(velocity);
                } catch (e) {
                    console.warn("Error firing b38 bullet:", e);
                }
            });
        }

        // Check for the next gun (gauss rifle)
        else if (typeId === "fw:gauss_rifle") {
            source.runCommandAsync("scoreboard players remove @s gauss_rifle 1").catch(() => {});
            source.runCommandAsync("execute at @s run playsound guns.gauss_rifle_shoot @a ~ ~ ~").catch(() => {});
            source.runCommandAsync("camerashake add @s 0.05 0.25").catch(() => {});
            system.run(() => {
                try {
                    const viewDir = source.getViewDirection();
                    const headLoc = source.getHeadLocation();
                    const dimension = source.dimension;
                    if (!viewDir || !headLoc || !dimension) return;

                    const spawnPos = {
                        x: headLoc.x + viewDir.x * 1.5, // Further out of hitbox
                        y: headLoc.y + viewDir.y * 1.5,
                        z: headLoc.z + viewDir.z * 1.5,
                    };

                    const velocity = {
                        x: viewDir.x * 8,
                        y: viewDir.y * 8,
                        z: viewDir.z * 8,
                    };
                    const bullet = dimension.spawnEntity("fw:b2mm_ec", spawnPos);
                    if (bullet) bullet.applyImpulse(velocity);
                } catch (e) {
                    console.warn("Error firing 2mm EC bullet:", e);
                }
            });
        }

        else if (typeId === "fw:classic_10mm_pistol") {
            source.runCommandAsync("scoreboard players remove @s classic_10mm_pistol 1").catch(() => {});
            source.runCommandAsync("execute at @s run playsound guns.10mm_pistol_shoot @a ~ ~ ~").catch(() => {});
            source.runCommandAsync("camerashake add @s 0.05 0.25").catch(() => {});
            system.run(() => {
                try {
                    const viewDir = source.getViewDirection();
                    const headLoc = source.getHeadLocation();
                    const dimension = source.dimension;
                    if (!viewDir || !headLoc || !dimension) return;

                    const spawnPos = {
                        x: headLoc.x + viewDir.x * 1.5, // Further out of hitbox
                        y: headLoc.y + viewDir.y * 1.5,
                        z: headLoc.z + viewDir.z * 1.5,
                    };

                    const velocity = {
                        x: viewDir.x * 8,
                        y: viewDir.y * 8,
                        z: viewDir.z * 8,
                    };
                    const bullet = dimension.spawnEntity("fw:b10mm_bullet", spawnPos);
                    if (bullet) bullet.applyImpulse(velocity);
                } catch (e) {
                    console.warn("Error firing b38 bullet:", e);
                }
            });
        }

        // Check for the next gun (gauss rifle)
        else if (typeId === "fw:sniper_rifle") {
            source.runCommandAsync("scoreboard players remove @s sniper_rifle 1").catch(() => {});
            source.runCommandAsync("execute at @s run playsound guns.hunting_rifle_shoot @a ~ ~ ~").catch(() => {});
            source.runCommandAsync("camerashake add @s 0.05 0.25").catch(() => {});
            system.run(() => {
                try {
                    const viewDir = source.getViewDirection();
                    const headLoc = source.getHeadLocation();
                    const dimension = source.dimension;
                    if (!viewDir || !headLoc || !dimension) return;

                    const spawnPos = {
                        x: headLoc.x + viewDir.x * 1.5, // Further out of hitbox
                        y: headLoc.y + viewDir.y * 1.5,
                        z: headLoc.z + viewDir.z * 1.5,
                    };

                    const velocity = {
                        x: viewDir.x * 8,
                        y: viewDir.y * 8,
                        z: viewDir.z * 8,
                    };
                    const bullet = dimension.spawnEntity("fw:b308_bullet", spawnPos);
                    if (bullet) bullet.applyImpulse(velocity);
                } catch (e) {
                    console.warn("Error firing 308 bullet:", e);
                }
            });
        }

        // Check for the third gun (Hunting Rifle)
        else if (typeId === "fw:hunting_rifle") {
            source.runCommandAsync("scoreboard players remove @s hunting_rifle 1").catch(() => {});
            source.runCommandAsync("execute at @s run playsound guns.hunting_rifle_shoot @a ~ ~ ~").catch(() => {});
            source.runCommandAsync("camerashake add @s 0.05 0.25").catch(() => {});
            system.run(() => {
                try {
                    const viewDir = source.getViewDirection();
                    const headLoc = source.getHeadLocation();
                    const dimension = source.dimension;
                    if (!viewDir || !headLoc || !dimension) return;

                    const spawnPos = {
                        x: headLoc.x + viewDir.x * 1.5, // Further out of hitbox
                        y: headLoc.y + viewDir.y * 1.5,
                        z: headLoc.z + viewDir.z * 1.5,
                    };

                    const velocity = {
                        x: viewDir.x * 8,
                        y: viewDir.y * 8,
                        z: viewDir.z * 8,
                    };
                    const bullet = dimension.spawnEntity("fw:b308_bullet", spawnPos);
                    if (bullet) bullet.applyImpulse(velocity);
                } catch (e) {
                    console.warn("Error firing b308 bullet:", e);
                }
            });
        }

        // New guns below

        else if (typeId === "fw:pipe_rifle") {
            source.runCommandAsync("scoreboard players remove @s pipe_rifle 1").catch(() => {});
            source.runCommandAsync("execute at @s run playsound guns.pipe_rifle_shoot @a ~ ~ ~").catch(() => {});
            source.runCommandAsync("camerashake add @s 0.05 0.25").catch(() => {});
            system.run(() => {
                try {
                    const viewDir = source.getViewDirection();
                    const headLoc = source.getHeadLocation();
                    const dimension = source.dimension;
                    if (!viewDir || !headLoc || !dimension) return;

                    const spawnPos = {
                        x: headLoc.x + viewDir.x * 1.5, // Further out of hitbox
                        y: headLoc.y + viewDir.y * 1.5,
                        z: headLoc.z + viewDir.z * 1.5,
                    };

                    const velocity = {
                        x: viewDir.x * 8,
                        y: viewDir.y * 8,
                        z: viewDir.z * 8,
                    };
                    const bullet = dimension.spawnEntity("fw:b38_bullet", spawnPos);
                    if (bullet) bullet.applyImpulse(velocity);
                } catch (e) {
                    console.warn("Error firing b38 bullet (pipe_rifle):", e);
                }
            });
        }

        else if (typeId === "fw:combat_rifle") {
            source.runCommandAsync("scoreboard players remove @s combat_rifle 1").catch(() => {});
            source.runCommandAsync("execute at @s run playsound guns.combat_rifle_shoot @a ~ ~ ~").catch(() => {});
            source.runCommandAsync("camerashake add @s 0.05 0.25").catch(() => {});
            system.run(() => {
                try {
                    const viewDir = source.getViewDirection();
                    const headLoc = source.getHeadLocation();
                    const dimension = source.dimension;
                    if (!viewDir || !headLoc || !dimension) return;

                    const spawnPos = {
                        x: headLoc.x + viewDir.x * 1.5, // Further out of hitbox
                        y: headLoc.y + viewDir.y * 1.5,
                        z: headLoc.z + viewDir.z * 1.5,
                    };

                    const velocity = {
                        x: viewDir.x * 8,
                        y: viewDir.y * 8,
                        z: viewDir.z * 8,
                    };

                    const bullet = dimension.spawnEntity("fw:b45_bullet", spawnPos);
                    if (bullet) bullet.applyImpulse(velocity);
                } catch (e) {
                    console.warn("Error firing b45 bullet (combat_rifle):", e);
                }
            });
        }


        else if (typeId === "fw:institute_rifle") {
            source.runCommandAsync("scoreboard players remove @s institute_rifle 1").catch(() => {});
            source.runCommandAsync("execute at @s run playsound guns.institute_rifle_shoot @a ~ ~ ~").catch(() => {});
            source.runCommandAsync("camerashake add @s 0.05 0.25").catch(() => {});
            system.run(() => {
                try {
                    const viewDir = source.getViewDirection();
                    const headLoc = source.getHeadLocation();
                    const dimension = source.dimension;
                    if (!viewDir || !headLoc || !dimension) return;

                    const spawnPos = {
                        x: headLoc.x + viewDir.x * 1.5, // Further out of hitbox
                        y: headLoc.y + viewDir.y * 1.5,
                        z: headLoc.z + viewDir.z * 1.5,
                    };

                    const velocity = {
                        x: viewDir.x * 8,
                        y: viewDir.y * 8,
                        z: viewDir.z * 8,
                    };

                    const bullet = dimension.spawnEntity("fw:laser", spawnPos);
                    if (bullet) bullet.applyImpulse(velocity);
                } catch (e) {
                    console.warn("Error firing laser (institute_rifle):", e);
                }
            });
        }

        else if (typeId === "fw:institute_pistol") {
            source.runCommandAsync("scoreboard players remove @s institute_pistol 1").catch(() => {});
            source.runCommandAsync("execute at @s run playsound guns.institute_rifle_shoot @a ~ ~ ~").catch(() => {});
            source.runCommandAsync("camerashake add @s 0.05 0.25").catch(() => {});
            system.run(() => {
                try {
                    const viewDir = source.getViewDirection();
                    const headLoc = source.getHeadLocation();
                    const dimension = source.dimension;
                    if (!viewDir || !headLoc || !dimension) return;

                    const spawnPos = {
                        x: headLoc.x + viewDir.x * 1.5, // Further out of hitbox
                        y: headLoc.y + viewDir.y * 1.5,
                        z: headLoc.z + viewDir.z * 1.5,
                    };

                    const velocity = {
                        x: viewDir.x * 8,
                        y: viewDir.y * 8,
                        z: viewDir.z * 8,
                    };

                    const bullet = dimension.spawnEntity("fw:laser", spawnPos);
                    if (bullet) bullet.applyImpulse(velocity);
                } catch (e) {
                    console.warn("Error firing laser (institute_pistol):", e);
                }
            });
        }

        else if (typeId === "fw:laser_rifle") {
            source.runCommandAsync("scoreboard players remove @s laser_rifle 1").catch(() => {});
            source.runCommandAsync("execute at @s run playsound guns.institute_rifle_shoot @a ~ ~ ~").catch(() => {});
            source.runCommandAsync("camerashake add @s 0.05 0.25").catch(() => {});
            system.run(() => {
                try {
                    const viewDir = source.getViewDirection();
                    const headLoc = source.getHeadLocation();
                    const dimension = source.dimension;
                    if (!viewDir || !headLoc || !dimension) return;

                    const spawnPos = {
                        x: headLoc.x + viewDir.x * 1.5, // Further out of hitbox
                        y: headLoc.y + viewDir.y * 1.5,
                        z: headLoc.z + viewDir.z * 1.5,
                    };

                    const velocity = {
                        x: viewDir.x * 8,
                        y: viewDir.y * 8,
                        z: viewDir.z * 8,
                    };

                    const bullet = dimension.spawnEntity("fw:laser", spawnPos);
                    if (bullet) bullet.applyImpulse(velocity);
                } catch (e) {
                    console.warn("Error firing laser (laser_rifle):", e);
                }
            });
        }

        else if (typeId === "fw:laser_pistol") {
            source.runCommandAsync("scoreboard players remove @s laser_pistol 1").catch(() => {});
            source.runCommandAsync("execute at @s run playsound guns.institute_rifle_shoot @a ~ ~ ~").catch(() => {});
            source.runCommandAsync("camerashake add @s 0.05 0.25").catch(() => {});
            system.run(() => {
                try {
                    const viewDir = source.getViewDirection();
                    const headLoc = source.getHeadLocation();
                    const dimension = source.dimension;
                    if (!viewDir || !headLoc || !dimension) return;

                    const spawnPos = {
                        x: headLoc.x + viewDir.x * 1.5, // Further out of hitbox
                        y: headLoc.y + viewDir.y * 1.5,
                        z: headLoc.z + viewDir.z * 1.5,
                    };

                    const velocity = {
                        x: viewDir.x * 8,
                        y: viewDir.y * 8,
                        z: viewDir.z * 8,
                    };

                    const bullet = dimension.spawnEntity("fw:laser", spawnPos);
                    if (bullet) bullet.applyImpulse(velocity);
                } catch (e) {
                    console.warn("Error firing laser (laser_pistol):", e);
                }
            });
        }

        else if (typeId === "fw:assault_rifle") {
            source.runCommandAsync("scoreboard players remove @s assault_rifle 1").catch(() => {});
            source.runCommandAsync("execute at @s run playsound guns.assault_rifle_shoot @a ~ ~ ~").catch(() => {});
            source.runCommandAsync("camerashake add @s 0.05 0.25").catch(() => {});
            system.run(() => {
                try {
                    const viewDir = source.getViewDirection();
                    const headLoc = source.getHeadLocation();
                    const dimension = source.dimension;
                    if (!viewDir || !headLoc || !dimension) return;

                    const spawnPos = {
                        x: headLoc.x + viewDir.x * 1.5, // Further out of hitbox
                        y: headLoc.y + viewDir.y * 1.5,
                        z: headLoc.z + viewDir.z * 1.5,
                    };

                    const velocity = {
                        x: viewDir.x * 8,
                        y: viewDir.y * 8,
                        z: viewDir.z * 8,
                    };

                    const bullet = dimension.spawnEntity("fw:b5_56_bullet", spawnPos);
                    if (bullet) bullet.applyImpulse(velocity);
                } catch (e) {
                    console.warn("Error firing b5_56 bullet (assault_rifle):", e);
                }
            });
        }

        else if (typeId === "fw:machine_gun") {
            source.runCommandAsync("scoreboard players remove @s machine_gun 1").catch(() => {});
            source.runCommandAsync("execute at @s run playsound guns.assault_rifle_shoot @a ~ ~ ~").catch(() => {});
            source.runCommandAsync("camerashake add @s 0.05 0.25").catch(() => {});
            system.run(() => {
                try {
                    const viewDir = source.getViewDirection();
                    const headLoc = source.getHeadLocation();
                    const dimension = source.dimension;
                    if (!viewDir || !headLoc || !dimension) return;

                    const spawnPos = {
                        x: headLoc.x + viewDir.x * 1.5, // Further out of hitbox
                        y: headLoc.y + viewDir.y * 1.5,
                        z: headLoc.z + viewDir.z * 1.5,
                    };

                    const velocity = {
                        x: viewDir.x * 8,
                        y: viewDir.y * 8,
                        z: viewDir.z * 8,
                    };

                    const bullet = dimension.spawnEntity("fw:b5_56_bullet", spawnPos);
                    if (bullet) bullet.applyImpulse(velocity);
                } catch (e) {
                    console.warn("Error firing b5_56 bullet (machine_gun):", e);
                }
            });
        }

        else if (typeId === "fw:minigun") {
            source.runCommandAsync("scoreboard players remove @s minigun 1").catch(() => {});
            source.runCommandAsync("execute at @s run playsound guns.minigun_shoot @a ~ ~ ~").catch(() => {});
            source.runCommandAsync("camerashake add @s 0.05 0.25").catch(() => {});
            system.run(() => {
                try {
                    const viewDir = source.getViewDirection();
                    const headLoc = source.getHeadLocation();
                    const dimension = source.dimension;
                    if (!viewDir || !headLoc || !dimension) return;

                    const spawnPos = {
                        x: headLoc.x + viewDir.x * 1.5, // Further out of hitbox
                        y: headLoc.y + viewDir.y * 1.5,
                        z: headLoc.z + viewDir.z * 1.5,
                    };

                    const velocity = {
                        x: viewDir.x * 8,
                        y: viewDir.y * 8,
                        z: viewDir.z * 8,
                    };

                    const bullet = dimension.spawnEntity("fw:b5mm_bullet", spawnPos);
                    if (bullet) bullet.applyImpulse(velocity);
                } catch (e) {
                    console.warn("Error firing b5mm bullet (minigun):", e);
                }
            });
        }
    },
};

world.beforeEvents.worldInitialize.subscribe(({ itemComponentRegistry }) => {
    itemComponentRegistry.registerCustomComponent("fw:gun_shoot", GunShootComponent);
});
