import * as ui from "@minecraft/server-ui";
import { ActionFormData,  MessageFormData,  ModalFormData} from "@minecraft/server-ui";
import { world, system, Player, MolangVariableMap, Entity} from "@minecraft/server";

function getScoreMontaria(player, montariaObjective) {
    const { scoreboardIdentity } = player;
    if (!(player instanceof Player)) return 0;
    try {
        return world.scoreboard.getObjective(montariaObjective).getScore(scoreboardIdentity);
    } catch (e) {
        return 0;
    }
}

function getScoreMana(player, manaObjective) {
    const { scoreboardIdentity } = player;
    if (!(player instanceof Player)) return 0;
    try {
        return world.scoreboard.getObjective(manaObjective).getScore(scoreboardIdentity);
    } catch (e) {
        return 0;
    }
}


world.afterEvents.itemUse.subscribe(async (event) => {
    const { itemStack, source } = event;
    if (itemStack.typeId !== 'well:cajado_mago3') return;
	
	if (source.isSneaking) return; // <<-- Adicione esta linha aqui

    // Obtém o valor do scoreboard 'mana' e 'skills_mago_poderes' usando a função getScoreMana
    const manaValue = getScoreMana(source, 'mana');
    const skillValue = getScoreMana(source, 'skill_ataque');

    // Verifica se o valor de 'mana' está abaixo de 10
    if (manaValue < 10) {
        // Executa o comando para mostrar a mensagem de mana vazia
        await source.runCommand('titleraw @s actionbar {"rawtext": [{"translate": "dialogue.mana.vazia"}]}');
        return; // Encerra a execução do código
    }

    // Checa se o valor de 'mana' está entre 10 e 100
    if (manaValue > 100) return;

    const viewDir = source.getViewDirection();
    let projectileType;

    // Definindo o projétil com base no nível de skill
    if (skillValue == 0) {
        projectileType = "well:bola_raio2"; // Nível 1
    }

    // Criação do projétil
    const fireBall = source.dimension.spawnEntity(projectileType, source.getHeadLocation());
    const projectile = fireBall.getComponent("projectile");
    projectile.owner = source;
    projectile.shoot(viewDir);

    // Adiciona a linha para o jogador executar um comando
    await source.runCommand('playanimation @s animation.humanoid.custom.classe.mago.cajado');
    await source.runCommand('scoreboard players remove @s mana 10');
    await source.runCommand('playsound magia.choque @a [r=10]');
    await source.runCommand('effect @a [r=10] darkness 2 1 true');
    await source.runCommand('titleraw @s actionbar {\"rawtext\": [{\"translate\": \"mana.10\"}]}');
	
	await source.runCommand('execute as @s [scores={mana=100}] run title @s [scores={mana=100}] title mana_bar10');
    await source.runCommand('execute as @s [scores={mana=90..99}] run title @s [scores={mana=90..99}] title mana_bar9');
    await source.runCommand('execute as @s [scores={mana=80..89}] run title @s [scores={mana=80..89}] title mana_bar8');
    await source.runCommand('execute as @s [scores={mana=70..79}] run title @s [scores={mana=70..79}] title mana_bar7');
    await source.runCommand('execute as @s [scores={mana=60..69}] run title @s [scores={mana=60..69}] title mana_bar6');
    await source.runCommand('execute as @s [scores={mana=50..59}] run title @s [scores={mana=50..59}] title mana_bar5');
    await source.runCommand('execute as @s [scores={mana=40..49}] run title @s [scores={mana=40..49}] title mana_bar4');
    await source.runCommand('execute as @s [scores={mana=30..39}] run title @s [scores={mana=30..39}] title mana_bar3');
    await source.runCommand('execute as @s [scores={mana=20..29}] run title @s [scores={mana=20..29}] title mana_bar2');
    await source.runCommand('execute as @s [scores={mana=10..19}] run title @s [scores={mana=10..19}] title mana_bar1');
    await source.runCommand('execute as @s [scores={mana=0..9}] run title @s [scores={mana=0..9}] title mana_bar0');
	
	
	
	
	
});

world.afterEvents.itemUse.subscribe(async (event) => {
    const { itemStack, source } = event;
    if (itemStack.typeId !== 'well:cajado_mago5') return;
	
	if (source.isSneaking) return; // <<-- Adicione esta linha aqui

    // Obtém o valor do scoreboard 'mana' e 'skills_mago_poderes' usando a função getScoreMana
    const manaValue = getScoreMana(source, 'mana');
    const skillValue = getScoreMana(source, 'skill_ataque');

    // Verifica se o valor de 'mana' está abaixo de 10
    if (manaValue < 10) {
        // Executa o comando para mostrar a mensagem de mana vazia
        await source.runCommand('titleraw @s actionbar {"rawtext": [{"translate": "dialogue.mana.vazia"}]}');
        return; // Encerra a execução do código
    }

    // Checa se o valor de 'mana' está entre 10 e 100
    if (manaValue > 100) return;

    const viewDir = source.getViewDirection();
    let projectileType;

    // Definindo o projétil com base no nível de skill
    if (skillValue == 0) {
        projectileType = "well:bola_sol"; // Nível 2
    }

    // Criação do projétil
    const fireBall = source.dimension.spawnEntity(projectileType, source.getHeadLocation());
    const projectile = fireBall.getComponent("projectile");
    projectile.owner = source;
    projectile.shoot(viewDir);

    // Adiciona a linha para o jogador executar um comando
    await source.runCommand('playanimation @s animation.humanoid.custom.classe.mago.cajado');
    await source.runCommand('scoreboard players remove @s mana 10');
    await source.runCommand('playsound mob.ghast.fireball @a [r=10]');
    await source.runCommand('titleraw @s actionbar {"rawtext": [{"translate": "mana.10"}]}');
	
	await source.runCommand('execute as @s [scores={mana=100}] run title @s [scores={mana=100}] title mana_bar10');
    await source.runCommand('execute as @s [scores={mana=90..99}] run title @s [scores={mana=90..99}] title mana_bar9');
    await source.runCommand('execute as @s [scores={mana=80..89}] run title @s [scores={mana=80..89}] title mana_bar8');
    await source.runCommand('execute as @s [scores={mana=70..79}] run title @s [scores={mana=70..79}] title mana_bar7');
    await source.runCommand('execute as @s [scores={mana=60..69}] run title @s [scores={mana=60..69}] title mana_bar6');
    await source.runCommand('execute as @s [scores={mana=50..59}] run title @s [scores={mana=50..59}] title mana_bar5');
    await source.runCommand('execute as @s [scores={mana=40..49}] run title @s [scores={mana=40..49}] title mana_bar4');
    await source.runCommand('execute as @s [scores={mana=30..39}] run title @s [scores={mana=30..39}] title mana_bar3');
    await source.runCommand('execute as @s [scores={mana=20..29}] run title @s [scores={mana=20..29}] title mana_bar2');
    await source.runCommand('execute as @s [scores={mana=10..19}] run title @s [scores={mana=10..19}] title mana_bar1');
    await source.runCommand('execute as @s [scores={mana=0..9}] run title @s [scores={mana=0..9}] title mana_bar0');
});

world.afterEvents.itemUse.subscribe(async (event) => {
    const { itemStack, source } = event;
    if (itemStack.typeId !== 'well:cajado_mago') return;
	
	if (source.isSneaking) return; // <<-- Adicione esta linha aqui

    // Obtém o valor do scoreboard 'mana' e 'skills_mago_poderes' usando a função getScoreMana
    const manaValue = getScoreMana(source, 'mana');
    const skillValue = getScoreMana(source, 'skill_ataque');

    // Verifica se o valor de 'mana' está abaixo de 10
    if (manaValue < 10) {
        // Executa o comando para mostrar a mensagem de mana vazia
        await source.runCommand('titleraw @s actionbar {"rawtext": [{"translate": "dialogue.mana.vazia"}]}');
        return; // Encerra a execução do código
    }

    // Checa se o valor de 'mana' está entre 10 e 100
    if (manaValue > 100) return;

    const viewDir = source.getViewDirection();
    let projectileType;

    // Definindo o projétil com base no nível de skill
    if (skillValue == 1) {
        projectileType = "well:esfera_da_vida"; // Nível 2
    }

    // Criação do projétil
    const fireBall = source.dimension.spawnEntity(projectileType, source.getHeadLocation());
    const projectile = fireBall.getComponent("projectile");
    projectile.owner = source;
    projectile.shoot(viewDir);

    // Adiciona a linha para o jogador executar um comando
    await source.runCommand('playanimation @s animation.humanoid.custom.classe.mago.cajado');
    await source.runCommand('scoreboard players remove @s mana 10');
    await source.runCommand('playsound mob.ghast.fireball @a [r=10]');
    await source.runCommand('titleraw @s actionbar {"rawtext": [{"translate": "mana.10"}]}');
	
	await source.runCommand('execute as @s [scores={mana=100}] run title @s [scores={mana=100}] title mana_bar10');
    await source.runCommand('execute as @s [scores={mana=90..99}] run title @s [scores={mana=90..99}] title mana_bar9');
    await source.runCommand('execute as @s [scores={mana=80..89}] run title @s [scores={mana=80..89}] title mana_bar8');
    await source.runCommand('execute as @s [scores={mana=70..79}] run title @s [scores={mana=70..79}] title mana_bar7');
    await source.runCommand('execute as @s [scores={mana=60..69}] run title @s [scores={mana=60..69}] title mana_bar6');
    await source.runCommand('execute as @s [scores={mana=50..59}] run title @s [scores={mana=50..59}] title mana_bar5');
    await source.runCommand('execute as @s [scores={mana=40..49}] run title @s [scores={mana=40..49}] title mana_bar4');
    await source.runCommand('execute as @s [scores={mana=30..39}] run title @s [scores={mana=30..39}] title mana_bar3');
    await source.runCommand('execute as @s [scores={mana=20..29}] run title @s [scores={mana=20..29}] title mana_bar2');
    await source.runCommand('execute as @s [scores={mana=10..19}] run title @s [scores={mana=10..19}] title mana_bar1');
    await source.runCommand('execute as @s [scores={mana=0..9}] run title @s [scores={mana=0..9}] title mana_bar0');
	
});



//world.afterEvents.itemUse.subscribe(async (event) => {
//    const { itemStack, source } = event;
//    if (itemStack.typeId !== 'well:cajado_mago2') return;
//
//    // Obtém o valor do scoreboard 'mana' e 'skills_mago_poderes' usando a função getScoreMana
//    const manaValue = getScoreMana(source, 'mana');
//    const skillValue = getScoreMana(source, 'carregar');
//
//    // Verifica se o valor de 'mana' está abaixo de 10
//    if (manaValue < 10) {
//        // Executa o comando para mostrar a mensagem de mana vazia
//        await source.runCommand('titleraw @s actionbar {"rawtext": [{"translate": "dialogue.mana.vazia"}]}');
//        return; // Encerra a execução do código
//    }
//
//    // Checa se o valor de 'mana' está entre 10 e 100
//    if (manaValue > 100) return;
//
//    const viewDir = source.getViewDirection();
//    let projectileType;
//
//    // Definindo o projétil com base no nível de skill
//    if (skillValue >= 5) {
//        projectileType = "well:bola_fogo"; // Nível 5 ou mais
//    } else if (skillValue >= 4) {
//        projectileType = "well:bola_veneno"; // Nível 4
//    } else if (skillValue >= 3) {
//        projectileType = "well:estacas_lancador"; // Nível 3
//    } else if (skillValue >= 2) {
//        projectileType = "well:estaca_gelo"; // Nível 2
//    }
//
//    // Criação do projétil
//    const fireBall = source.dimension.spawnEntity(projectileType, source.getHeadLocation());
//    const projectile = fireBall.getComponent("projectile");
//    projectile.owner = source;
//    projectile.shoot(viewDir);
//
//    // Adiciona a linha para o jogador executar um comando
//    await source.runCommand('playanimation @s animation.humanoid.custom.classe.mago.cajado');
//    await source.runCommand('scoreboard players remove @s mana 10');
//    await source.runCommand('playsound mob.ghast.fireball');
//    await source.runCommand('titleraw @s actionbar {"rawtext": [{"translate": "mana.10"}]}');
//});
//


