import { system, EquipmentSlot } from "@minecraft/server";
import * as utils from "../utils";
import * as camera from "../camera";
import { damageItem } from "../inventory_utils";

const DAMAGE_AMOUNT = 9;
const EFFECT_DELAY = 0.05 * 20; 
const RANGE = 3;
const COOLDOWN = 5 * 20; 
const CAMERA_DURATION = 1.5 * 20;
const DAMAGE_DURATION = 2 * 20;

const TP_TRANSITION_DURATION = 0.5;


export async function onItemUse(event)
{
    const player = event.source;
    const item = event.itemStack;

    if (player.getItemCooldown("tcs_cwp_inferno_scythe") > 0) return; 
    player.startItemCooldown("tcs_cwp_inferno_scythe", COOLDOWN);

    utils.playSound(player, `@a[x=${player.location.x}, y=${player.location.y}, z=${player.location.z}, r=5]`, 'tcs_cwp:inferno_scythe_spin');

    if (player.getGameMode() === "survival")
        damageItem(player, EquipmentSlot.Mainhand, 1);

    utils.playAnimation(player, "animation.tcs_cwp.inferno_scythe.player.special_attack");
    
    player.runCommandAsync(`particle tcs_cwp:inferno_spin ~ ~1.7 ~`);
    player.runCommandAsync(`particle tcs_cwp:fireball_inferno ~ ~1.7 ~`);
    
    system.waitTicks(EFFECT_DELAY).then(() => applyAoeDamage(player));
    camera.switchToThirdPerson(player, TP_TRANSITION_DURATION);

    system.waitTicks(CAMERA_DURATION).then(() => camera.reset(player));
}

function applyAoeDamage(player, damage=-1) {
    const entitiesAroundPlayer = player.dimension.getEntities({
        location: player.location,
        excludeFamilies: [ "utility", "projectile", "furniture" ],
        excludeTypes: [ "minecraft:item", "minecraft:xp_orb" ],
        maxDistance: RANGE
    });

    if (entitiesAroundPlayer.length === 1) return;

    damage = damage === -1 ? DAMAGE_AMOUNT : damage;
    for (const entity of entitiesAroundPlayer) {
        if (entity.id === player.id) continue;
        entity.setOnFire(4, true);
        entity.runCommandAsync(`particle tcs_cwp:fireball ~ ~+1 ~`);
        utils.playSound(entity, `@a[x=${entity.location.x}, y=${entity.location.y}, z=${entity.location.z}, r=3]`, 'tcs_cwp:inferno_scythe_punch');
        if (typeof entity.applyDamage === "function") {
        entity.applyDamage(damage);
        }
    }
    system.waitTicks(10).then(() => {
        if (player.getItemCooldown("tcs_cwp_inferno_scythe") < COOLDOWN - DAMAGE_DURATION) return;
        system.waitTicks(10).then(() => {
        // Only Players have getItemCooldown
        if (typeof player.getItemCooldown !== "function") return;
        if (player.getItemCooldown("tcs_cwp_inferno_scythe") < COOLDOWN - DAMAGE_DURATION) return;
        applyAoeDamage(player, Math.floor(damage * 0.5));
        });
    });
}

export async function onEntityHit(event) {
	const attacker = event.damagingEntity;
    const hitEntity = event.hitEntity
	if (!attacker || attacker.typeId !== "minecraft:player") return;

	const target = event.hitEntity;
	if (!target) return;

	const heldItem = attacker
		.getComponent("minecraft:equippable")
		?.getEquipment(EquipmentSlot.Mainhand);
	if (!heldItem || heldItem.typeId !== "tcs_cwp:inferno_scythe") return;

    attacker.startItemCooldown("tcs_cwp_animation_left_click", 5.3);
    utils.playAnimation(attacker, "animation.tcs_cwp.inferno_scythe.tp_attack");
    hitEntity.runCommandAsync(`particle tcs_cwp:fireball ~ ~ ~`);
}