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

const cooldown_tempo = 30; 
const id_item_skill = "nb:femto_skill1";

world.beforeEvents.itemUse.subscribe(event => {
    const player = event.source;
    const item = event.itemStack;
    
    if (item.typeId === id_item_skill) { 
        const nome_player = player.name;
        const dim_player = player.dimension;

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

        if (em_cooldown) {
            player.runCommandAsync(`title @s actionbar §cCooldown.`);
            return;
        }

       tornado(player, nome_player, dim_player);
    }
});

function tornado(player, nome_player, dim_player) {
    player.runCommandAsync("tag @s add cooldown_femto_tornado");

    system.runTimeout(() => {
        const p = world.getAllPlayers().find(pl => pl.name === nome_player);
        if (p) {
            dim_player.runCommandAsync(`tag "${nome_player}" remove cooldown_femto_tornado`);
            dim_player.runCommandAsync(`title "${nome_player}" actionbar §aSkill Recharged`);
            try { p.playSound("random.orb"); } catch(e){}
        }
    }, cooldown_tempo * 20);
    
    player.runCommandAsync("inputpermission set @s movement disabled");
    player.runCommandAsync("inputpermission set @s sneak disabled");
    player.runCommandAsync("playanimation @s animation.femto.tornado");
    
    system.runTimeout(() => {
        if (!player.isValid()) return;
        
        const olhar = player.getViewDirection();
        const spawn_pos = {
            x: player.location.x + olhar.x * 1.5,
            y: player.location.y, 
            z: player.location.z + olhar.z * 1.5
        };
        
        const tornado_ent = dim_player.spawnEntity("nb:tornado", spawn_pos);
        if (!tornado_ent) return;

        tornado_ent.runCommandAsync("effect @s invisibility 5 5 true");
        
        const rot = player.getRotation();
        tornado_ent.setRotation({ x: rot.x, y: rot.y });
        tornado_ent.runCommandAsync("tag @s add tornado");
        
        comecar_loop_tornado(tornado_ent, player);
    
        system.runTimeout(() => {
            const p = world.getAllPlayers().find(pl => pl.name === nome_player);
            if (p) {
                dim_player.runCommandAsync(`inputpermission set "${nome_player}" movement enabled`);
                dim_player.runCommandAsync(`inputpermission set "${nome_player}" sneak enabled`);
            }
        }, 18); 
    
        system.runTimeout(() => {
            if (tornado_ent.isValid()) {
                tornado_ent.remove(); 
            }
        }, 120); 

    }, 2);
}

function comecar_loop_tornado(tornado_ent, player) {
    const loop_intervalo = system.runInterval(() => { 
        if (!tornado_ent.isValid()) {
            system.clearRun(loop_intervalo);
            return;
        }

        tornado_ent.runCommandAsync("particle nb:tornado_black ~ ~-0.3 ~");
        tornado_ent.runCommandAsync("particle nb:femto_tornado ~ ~-0.3 ~");
        tornado_ent.runCommandAsync("particle nb:tornado_terra ~ ~-0.2 ~");
        
        const direcao = tornado_ent.getViewDirection();
        const velocidade = 1.0; 
        
        const nova_pos = {
            x: tornado_ent.location.x + (direcao.x * velocidade),
            y: tornado_ent.location.y, 
            z: tornado_ent.location.z + (direcao.z * velocidade)
        };
        try { tornado_ent.teleport(nova_pos); } catch(e){}
        
        const raio_puxo = 7; 
        const alvos = tornado_ent.dimension.getEntities({
            location: tornado_ent.location, 
            maxDistance: raio_puxo,
            excludeTags: ['tornado', 'femto', 'zodd_user', 'cooldown_femto_tornado']
        });
      
        for (const entidade of alvos) {
            try {
                entidade.applyDamage(4, { cause: 'entityAttack', damagingEntity: tornado_ent });

                const impulso = {
                    x: (tornado_ent.location.x - entidade.location.x) * 0.2,
                    y: (tornado_ent.location.y - entidade.location.y) * 0.2,
                    z: (tornado_ent.location.z - entidade.location.z) * 0.2
                };

                entidade.applyImpulse(impulso); 
            } catch(e) {}
        }
    }, 5); 
}