import * as ui from "@minecraft/server-ui";
import { ActionFormData,  MessageFormData,  ModalFormData} from "@minecraft/server-ui";
import { world, system, Player, MolangVariableMap, Entity} from "@minecraft/server";
//const overworld = world.getDimension('minecraft:overworld');

import {world as World} from "@minecraft/server";


world.beforeEvents.playerInteractWithBlock.subscribe((data) => {
    const player = data.player;
    const block = data.block;

    if (!player.isSneaking && block.typeId === "well:place_alma_da_arvore") {
        data.cancel = true;
        const { x, y, z } = block.location;

        system.run(() => {
            player.runCommand(`execute as @a [r=30, c=10, tag=novice3, hasitem={item=well:alma_da_arvore_item}] run structure load dungeon_selva1 ${x - 3} ${y - 28} ${z - 3}`);
            player.runCommand(`execute as @a [r=30, c=10, tag=novice3, hasitem={item=well:alma_da_arvore_item}] run structure load dungeon_selva2 ${x + 13} ${y - 28} ${z - 3}`);
            player.runCommand(`execute as @a [r=30, c=10, tag=novice3, hasitem={item=well:alma_da_arvore_item}] run fill ${x - 1} ${y} ${z} ${x + 1} ${y} ${z} well:alma_da_arvore [] replace well:place_alma_da_arvore`);
            player.runCommand(`tag @a [r=30, c=10, tag=novice3, hasitem={item=well:alma_da_arvore_item}] add boss1`);
            player.runCommand(`execute as @a [r=30, c=10, tag=novice3, hasitem={item=well:alma_da_arvore_item}] run fill ${x +1} ${y - 12} ${z} ${x+1} ${y} ${z} air`);
            player.runCommand(`playsound Secret @a [tag=novice3, c=10, hasitem={item=well:alma_da_arvore_item}] ~ ~ ~`);
            player.runCommand(`tag @a [r=30, tag=novice3, c=10, hasitem={item=well:alma_da_arvore_item}] remove novice3`);
            player.runCommand(`clear @a [r=30, tag=boss1, c=10, hasitem={item=well:alma_da_arvore_item}] well:alma_da_arvore_item`);
            player.runCommand(`spawnpoint @a [r=30,c=10] ${x} ${y} ${z}`);
            player.runCommand(`music play dungeon_music 1 1 loop`);
            player.runCommand(`function dungeon1`);
            player.runCommand(`titleraw @a [tag=boss1, c=10, r=30] title {"rawtext":[{ "translate":"entrada.dungeon.floresta.titulo" }]}`);
            player.runCommand(`titleraw @a [tag=boss1, c=10, r=30] subtitle {"rawtext":[{ "translate":"entrada.dungeon.floresta.subtitulo" }]}`);
            player.runCommand(`titleraw @a [tag=boss1, c=10, r=30] actionbar {"rawtext":[{ "translate":"entrada.dungeon.floresta.barradeacao" }]}`);
        });
    }
});

world.beforeEvents.playerInteractWithBlock.subscribe((data) => {
    const player = data.player;
    const block = data.block;

    if (!player.isSneaking && block.typeId === "well:place_gelo_arcaico") {
        data.cancel = true;
        const { x, y, z } = block.location;

        system.run(() => {
            player.runCommand(`execute as @a [r=30, c=10, tag=adventure4, hasitem={item=well:gelo_arcaico_item}] run structure load dungeon_gelo1 ${x - 3} ${y - 26} ${z - 5}`);
            player.runCommand(`execute as @a [r=30, c=10, tag=adventure4, hasitem={item=well:gelo_arcaico_item}] run structure load dungeon_gelo2 ${x + 13} ${y - 26} ${z - 5}`);
            player.runCommand(`execute as @a [r=30, c=10, tag=adventure4, hasitem={item=well:gelo_arcaico_item}] run fill ${x - 2} ${y} ${z} ${x - 2} ${y - 7} ${z} air`);
            player.runCommand(`execute as @a [r=30, c=10, tag=adventure4, hasitem={item=well:gelo_arcaico_item}] run structure load boss2_parte1 ${x} ${y - 46} ${z}`);
            player.runCommand(`execute as @a [r=30, c=10, tag=adventure4, hasitem={item=well:gelo_arcaico_item}] run structure load boss2_parte2 ${x} ${y - 46} ${z + 21}`);
            player.runCommand(`execute as @a [r=30, c=10, tag=adventure4, hasitem={item=well:gelo_arcaico_item}] run structure load boss2_parte3 ${x + 16} ${y - 46} ${z}`);
            player.runCommand(`execute as @a [r=30, c=10, tag=adventure4, hasitem={item=well:gelo_arcaico_item}] run structure load boss2_parte4 ${x + 16} ${y - 46} ${z + 21}`);
            player.runCommand(`tag @a [r=30, c=10, tag=adventure4, hasitem={item=well:gelo_arcaico_item}] add boss2`);
            player.runCommand(`scoreboard players add @a [r=30, c=10, tag=adventure4, hasitem={item=well:gelo_arcaico_item}] arte 0`);
            player.runCommand(`playsound Secret @a [tag=adventure4, c=10, hasitem={item=well:gelo_arcaico_item}] ~ ~ ~`);
            player.runCommand(`tag @a [r=30, tag=adventure4, c=10, hasitem={item=well:gelo_arcaico_item}] remove adventure4`);
            player.runCommand(`execute as @a [r=30, c=10, tag=boss2, hasitem={item=well:gelo_arcaico_item}] run setblock ${x - 1} ${y} ${z} well:gelo_arcaico`);
            player.runCommand(`clear @a [r=30, tag=boss2, c=10, hasitem={item=well:gelo_arcaico_item}] well:gelo_arcaico_item`);
            player.runCommand(`spawnpoint @a [c=10, r=30] ${x} ${y} ${z}`);
            player.runCommand(`music play dungeon_music 1 1 loop`);
            player.runCommand(`function dungeon2`);
            player.runCommand(`titleraw @a [tag=boss2, c=10, r=30] title {"rawtext":[{ "translate":"entrada.dungeon.gelo.titulo" }]}`);
            player.runCommand(`titleraw @a [tag=boss2, c=10, r=30] subtitle {"rawtext":[{ "translate":"entrada.dungeon.gelo.subtitulo" }]}`);
            player.runCommand(`titleraw @a [tag=boss2, c=10, r=30] actionbar {"rawtext":[{ "translate":"entrada.dungeon.gelo.barradeacao" }]}`);    
        });
    }
});

world.beforeEvents.playerInteractWithBlock.subscribe((data) => {
    const player = data.player;
    const block = data.block;

    if (!player.isSneaking && block.typeId === "well:bloco_porta") {
        data.cancel = true;
        const { x, y, z } = block.location;

        system.run(() => {
            player.runCommand("execute as @s [scores={alavancas=1..}, tag=boss1, r=5, c=1] run fill ^ ^2 ^ ^ ^ ^2 air [] replace well:bloco_porta")
			player.runCommand("execute as @s [scores={alavancas=1..}, tag=boss1, r=5, c=1] run fill ^ ^2 ^-2 ^-2 ^2 ^2 air [] replace barrier")
			player.runCommand("execute as @s [scores={alavancas=1..}, tag=boss1, r=5, c=1] run summon well:porta ~ ~ ~ ~180")
			player.runCommand("execute as @s [scores={alavancas=1..}, tag=boss1, r=5, c=1] run playsound Secret @p")
			
			player.runCommand("execute as @s [scores={fantasma_morto=1..}, r=5, c=1] run fill ^ ^2 ^-2 ^-2 ^ ^2 air [] replace well:bloco_porta")
			player.runCommand("execute as @s [scores={fantasma_morto=1..}, r=5, c=1] run fill ^ ^2 ^-2 ^-2 ^ ^2 air [] replace barrier")
			player.runCommand("execute as @s [scores={fantasma_morto=1..}, r=5, c=1] run summon well:porta ~ ~ ~")
			player.runCommand("playsound random.door_open @p")
			player.runCommand("execute as @s [scores={fantasma_morto=1}, r=5, c=1] run playsound Secret @p")
			player.runCommand("execute as @s [scores={fantasma_morto=1}, r=5, c=1] run scoreboard players set @a [scores={fantasma_morto=1..}] fantasma_morto 2")
			player.runCommand("execute as @s [scores={fantasma_morto=0}, r=5, c=1] run titleraw @s title {\"rawtext\": [{\"translate\": \"title.fantasma.derrotar.missao\"}]}")
			player.runCommand("execute as @s [scores={fantasma_morto=0}, r=5, c=1] run titleraw @s subtitle {\"rawtext\": [{\"translate\": \"subtitle.fantasma.derrotar.missao\"}]}")
            
        });
    }
});

//////

world.beforeEvents.playerInteractWithBlock.subscribe((data) => {
    const player = data.player;
    const block = data.block;
    if (!player.isSneaking && block.typeId == "well:planta_mistica") {
        data.cancel = true
        system.run(() => {
            player.runCommand("playsound Secret @p [r=10, tag=adventure1, hasitem={item=well:fertilizante_mistico, location = slot.weapon.mainhand}] ~ ~ ~")
			
			player.runCommand("scoreboard players set @p [r=10, tag=adventure1, hasitem={item=well:fertilizante_mistico, location = slot.weapon.mainhand}] missaowell6 3")
			
			
			player.runCommand("scoreboard players set @p [r=10, tag=adventure1, hasitem={item=well:fertilizante_mistico, location = slot.weapon.mainhand}] fertilizante 1")
			player.runCommand("execute as @p [r=10, tag=adventure1, hasitem={item=well:fertilizante_mistico, location = slot.weapon.mainhand}] run fill ~-2~~ ~2~~ well:planta_mistica2 [] replace well:planta_mistica")
			player.runCommand("clear @p [r=10, tag=adventure1, hasitem={item=well:fertilizante_mistico, location = slot.weapon.mainhand}] well:fertilizante_mistico")
			player.runCommand("dialogue open @e [type=npc:karlos, r=10] @p [r=10, tag=adventure1, scores={fertilizante=1}] karlos2")
			
			
			
			
			//debug
			//player.runCommand("scoreboard objectives setdisplay sidebar fertilizante ascending")
			
        })
    }
})

world.beforeEvents.playerInteractWithBlock.subscribe((data) => {
    const player = data.player;
    const block = data.block;
    if (!player.isSneaking && block.typeId == "well:planta_mistica2") {
        data.cancel = true
        system.run(() => {
            player.runCommand("execute as @p [hasitem={item=well:fertilizante_mistico, location = slot.weapon.mainhand}, r=10, c=1] run fill ~~~ ~~~ well:planta_mistica3 [] replace well:planta_mistica2")
			player.runCommand("clear @p [r=10, hasitem={item=well:fertilizante_mistico, location = slot.weapon.mainhand}] well:fertilizante_mistico")
            //debug
			//player.runCommand("scoreboard objectives setdisplay sidebar fertilizante ascending")
		
		
		})
    }
})

world.beforeEvents.playerInteractWithBlock.subscribe((data) => {
    const player = data.player;
    const block = data.block;
    if (!player.isSneaking && block.typeId == "well:planta_mistica3") {
        data.cancel = true
        system.run(() => {
            player.runCommand("fill ~~~ ~~~ well:planta_mistica [] replace well:planta_mistica3")
        })
    }
})

world.beforeEvents.playerInteractWithBlock.subscribe((data) => {
    const player = data.player;
    const block = data.block;
    if (!player.isSneaking && block.typeId == "well:bloco_planta_mistica") {
        data.cancel = true
        system.run(() => {
            player.runCommand("execute as @p [r=10, hasitem={item=well:po_negro, location = slot.weapon.mainhand}] run function Blocks/ticking/plantacoes/planta_mistica")
			player.runCommand("clear @p [r=10, hasitem={item=well:po_negro, location = slot.weapon.mainhand}] well:po_negro 0 1")
			//player.runCommand("playsound Secret @p [r=10, tag=adventure1, hasitem={item=well:fertilizante_mistico, location = slot.weapon.mainhand}] ~ ~ ~")
			//player.runCommand("scoreboard players set @p [r=10, tag=adventure1, hasitem={item=well:fertilizante_mistico, location = slot.weapon.mainhand}] fertilizante 1")
			//player.runCommand("execute as @p [r=10, tag=adventure1, hasitem={item=well:fertilizante_mistico, location = slot.weapon.mainhand}] run fill ~-2~~ ~2~~ well:planta_mistica2 [] replace well:bloco_planta_mistica")
			//player.runCommand("clear @p [r=10, tag=adventure1, hasitem={item=well:fertilizante_mistico, location = slot.weapon.mainhand}] well:fertilizante_mistico")
			//player.runCommand("dialogue open @e [type=npc:karlos, r=10] @p [r=10, tag=adventure1, scores={fertilizante=1}] karlos2")
        })
    }
})

world.beforeEvents.playerInteractWithBlock.subscribe((data) => {
    const player = data.player;
    const block = data.block;
    if (!player.isSneaking && block.typeId == "well:bloco_planta_mistica2") {
        data.cancel = true
        system.run(() => {
            player.runCommand("execute as @p [r=10, hasitem={item=well:po_negro, location = slot.weapon.mainhand}] run function Blocks/ticking/plantacoes/planta_mistica2")
			player.runCommand("clear @p [r=10, hasitem={item=well:po_negro, location = slot.weapon.mainhand}] well:po_negro 0 1")
			player.runCommand("execute as @p [r=10, hasitem={item=well:po_negro, location = slot.weapon.mainhand}] run playsound hit.slime")
        })
    }
})

/////

const cooldown = new Set();

const blockTransitions = {
    "well:azul_claro": "well:azul_escuro",
    "well:azul_escuro": "well:branco",
    "well:branco": "well:preto",
    "well:preto": "well:roxo",
    "well:roxo": "well:verde_claro",
    "well:verde_claro": "well:verde_escuro",
    "well:verde_escuro": "well:azul_claro"
};

world.beforeEvents.playerInteractWithBlock.subscribe((data) => {
    const player = data.player;
    const block = data.block;
    const { x, y, z } = block.location;
    const currentBlock = block.typeId;

    const posKey = `${x},${y},${z}`;

    if (!player.isSneaking && blockTransitions[currentBlock] && !cooldown.has(posKey)) {
        data.cancel = true;
        cooldown.add(posKey);

        const nextBlock = blockTransitions[currentBlock];

        system.runTimeout(() => {
            // Troca o bloco
            player.runCommand(`setblock ${x} ${y} ${z} ${nextBlock}`);
            // Toca o som na posição
            player.runCommand(`playsound minecraft:note.pling @a ${x} ${y} ${z}`);

            cooldown.delete(posKey);
        }, 2); // Delay de 6 ticks (aproximadamente 300 ms)
    }
});


///////////////////////////////////////////////////
