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

const tag_espera = "cooldown_femto_skill";
const tempo_espera = 30;
const item_skill_id = "nb:femto_skill4";

world.beforeEvents.itemUse.subscribe(event => {
    const player = event.source;
    const item = event.itemStack;

    let em_cooldown = false;
    try { em_cooldown = player.hasTag(tag_espera); } catch(e){}

    if (item.typeId === item_skill_id && !em_cooldown) { 
        
        const nome_do_player = player.name;
        
        player.runCommandAsync(`tag @s add ${tag_espera}`);
        
        invocar_zodd(player);

        system.runTimeout(() => {
            const p = world.getAllPlayers().find(p => p.name === nome_do_player);
            
            if (p) {
                p.dimension.runCommandAsync(`tag "${nome_do_player}" remove ${tag_espera}`);
                p.dimension.runCommandAsync(`title "${nome_do_player}" actionbar §aSkill Recharged!`);
                
                try { p.playSound("random.orb"); } catch(e){}
            }
        }, tempo_espera * 20);
    
    } else if (item.typeId === item_skill_id && em_cooldown) {
        player.runCommandAsync(`title @s actionbar §cCooldown...`);
    }
});

function invocar_zodd(player) {
    const nome_do_player = player.name;

    player.runCommandAsync("inputpermission set @s movement disabled");
    player.runCommandAsync("inputpermission set @s camera disabled");
    player.runCommandAsync("particle nb:dash ^ ^1.4 ^");
    player.runCommandAsync("playanimation @s animation.femto.support");
    
    player.runCommandAsync("camerashake add @s 0.3 1.5 positional");

    system.runTimeout(() => {
        if (!player.isValid()) return;

        const direcao_olhar = player.getViewDirection();
        
        const pos_tras = {
            x: player.location.x - (direcao_olhar.x * 0.5),
            y: player.location.y,
            z: player.location.z - (direcao_olhar.z * 0.5)
        };
        try { player.teleport(pos_tras); } catch (e) {}
        
        player.runCommandAsync("particle nb:dash ^ ^1.4 ^");

        const local_spawn = {
            x: player.location.x + direcao_olhar.x * 1.5,
            y: player.location.y,
            z: player.location.z + direcao_olhar.z * 1.5
        };

        const zodd = player.dimension.spawnEntity("nb:zodd_support", local_spawn);
        
        if (!zodd) return; 

        zodd.setRotation(player.getRotation());
        
        zodd.runCommandAsync("tag @s add zodd_moving");
        player.runCommandAsync("tag @s add zodd_user");

        system.runTimeout(() => {
            if (zodd.isValid()) zodd.playAnimation("animation.femto.zodd_support");
        }, 2);

        const zodd_loop = system.runInterval(() => {
            if (!zodd.isValid()) {
                system.clearRun(zodd_loop);
                return;
            }

            const frente = zodd.getViewDirection();
            const velocidade = 2.0; 
            const nova_pos = {
                x: zodd.location.x + (frente.x * velocidade),
                y: zodd.location.y,
                z: zodd.location.z + (frente.z * velocidade)
            };
            try { zodd.teleport(nova_pos); } catch(e){}

            const inimigos = zodd.dimension.getEntities({
                location: zodd.location,
                maxDistance: 5,
                excludeTags: ['zodd_moving', 'zodd_user', tag_espera], 
                excludeFamilies: ['inanimate']
            });

            const forca_h = 5.0; 
            const forca_v = 2.0;   

            for (const alvo of inimigos) {
                if (alvo.id === zodd.id || alvo.id === player.id) continue;
                try {
                    alvo.applyKnockback(frente.x, frente.z, forca_h, forca_v);
                    alvo.applyDamage(14, { cause: 'entityAttack', damagingEntity: zodd });
                } catch(e){}
            }
        }, 5); 

        system.runTimeout(() => {
            system.clearRun(zodd_loop);
            if (zodd.isValid()) zodd.remove();
        }, 68);

    }, 2);

    system.runTimeout(() => {
        const p = world.getAllPlayers().find(pl => pl.name === nome_do_player);
        
        if (p) {
            p.dimension.runCommandAsync(`inputpermission set "${nome_do_player}" movement enabled`);
            p.dimension.runCommandAsync(`inputpermission set "${nome_do_player}" camera enabled`);
            p.dimension.runCommandAsync(`tag "${nome_do_player}" remove zodd_user`);
        }
    }, 25);
}