import { world, system } from "@minecraft/server";
import * as utils from "../utils";
import * as Vector3 from "../vector3";

const COOLDOWN_CATEGORY = "spider_blade";
const COOLDOWN_DURATION = 20; // 20 ticks (1s)

const PROJECTILE_SPEED = 2;

export async function onItemUse(event) {
    const player = event.source;
    const cooldown = player.getItemCooldown(COOLDOWN_CATEGORY);
    if (cooldown > 0) return;

    // Disable player movement and camera
    //player.inputPermissions.movementEnabled = false;
    //player.inputPermissions.cameraEnabled = false;

    // Play third person animation
    utils.runPlayerCommand(player, `playanimation @s animation.goe_bos.player.throw_sword animation.goe_bos.player.fp_attack_v2 0 "v.is_first_person"`);
    utils.runPlayerCommand(player, `playanimation @s animation.goe_bos.player.fp_attack_v2 animation.goe_bos.player.throw_sword 0 "!v.is_first_person"`);
    utils.runPlayerCommand(player, `playanimation @s animation.goe_bos.player.throw_sword animation.player.empty 0 "q.all_animations_finished"`);
    utils.runPlayerCommand(player, `playanimation @s animation.goe_bos.player.fp_attack_v2 animation.player.empty 0 "q.all_animations_finished"`);

    player.dimension.spawnParticle("goe_bos:weapon_mist_gold", player.location);

    player.playSound("goe_bos:spider_blade_slash");

    // Delay projectiles
    system.runTimeout(() => {
        shoot(player);
        player.playSound("goe_bos:shoot_web");
    }, 5);

    // Enable camera and movement
    // system.runTimeout(() => {
    //     player.inputPermissions.movementEnabled = true;
    //     player.inputPermissions.cameraEnabled = true;
    // }, 10);

    // Start cooldown
    player.startItemCooldown(COOLDOWN_CATEGORY, COOLDOWN_DURATION);

    damageHeldItem(player);
}

async function shoot(player) {
    const spawnPosition = player.location;
    spawnPosition.x += player.getViewDirection().x;
    spawnPosition.y += 1.5;
    spawnPosition.z += player.getViewDirection().z;

    const projectile = player.dimension.spawnEntity("goe_bos:purple_web_projectile", spawnPosition);
    let projectileComponent = projectile.getComponent("minecraft:projectile");
    projectileComponent.owner = player;
    projectileComponent.shoot({ 
        x: player.getViewDirection().x * PROJECTILE_SPEED, 
        y: player.getViewDirection().y * PROJECTILE_SPEED, 
        z: player.getViewDirection().z * PROJECTILE_SPEED 
    });
}

function damageHeldItem(player, amount = 1) {
    if (player.getGameMode() === "creative") return;
    
    const inventory = player.getComponent("minecraft:inventory").container;
    const selectedSlot = inventory.getSlot(player.selectedSlotIndex);
    const heldItem = selectedSlot.getItem();
    if (heldItem === undefined) return;

    const durabilityComponent = heldItem.getComponent("minecraft:durability");
    if (durabilityComponent === undefined) return;

    const itemDurability = durabilityComponent.maxDurability;
    const itemDamage = ++durabilityComponent.damage;

    if (itemDamage + 1 === itemDurability) {
        selectedSlot.setItem(undefined);
        return;
    }

    selectedSlot.setItem(heldItem);
}