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

const COOLDOWN_CATEGORY = "unholy_dagger";
const COOLDOWN_DURATION = 60; // 60 ticks (3s)

const PROJECTILE_SPEED = 1;

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.unholy_crown.attack.special animation.player.empty 0 "v.is_first_person || q.all_animations_finished"`);
    utils.runPlayerCommand(player, `playanimation @s animation.player.empty animation.goe_bos.player.unholy_crown.attack.special 0 "!v.is_first_person"`);
    
    system.runTimeout(() => {
        player.playSound("goe_bos:woosh");
    }, 4);

    // Delay projectiles
    system.runTimeout(() => {
        shoot(player);
        player.dimension.spawnParticle("goe_bos:large_magic_circle_blue", player.location);
    }, 10);

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

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

    damageHeldItem(player);
}

async function shoot(player) {
    const forwardVector = player.getViewDirection();

    let spawnLocation = player.getHeadLocation();
    spawnLocation.x += forwardVector.x * 2;
    spawnLocation.y -= 0.5;
    spawnLocation.z += forwardVector.z * 2;
    wither_projectile.shootProjectile(player, "follow_target", spawnLocation, forwardVector, PROJECTILE_SPEED);

    let direction = Vector3.rotateYAxis(forwardVector, 20);
    spawnLocation = player.getHeadLocation();
    spawnLocation.x += direction.x;
    spawnLocation.y -= 0.5;
    spawnLocation.z += direction.z;
    wither_projectile.shootProjectile(player, "follow_target", spawnLocation, direction, PROJECTILE_SPEED);


    direction = Vector3.rotateYAxis(forwardVector, -20);
    spawnLocation = player.getHeadLocation();
    spawnLocation.x += direction.x;
    spawnLocation.y -= 0.5;
    spawnLocation.z += direction.z;
    wither_projectile.shootProjectile(player, "follow_target", spawnLocation, direction, 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);
}