import { ItemStack, system, world } from "@minecraft/server";
import { handleNPMenu, NPC } from "./modules/npcs";
system.afterEvents.scriptEventReceive.subscribe((event) => {
    const entity = event.sourceEntity; //will always be an entity
    switch (event.id) {
        case "wildphire:npc":
            const npc = NPC.getNpc(entity);
            if (!npc)
                return;
            const players = world.getDimension("overworld").getEntities({ type: "player", location: entity.location, maxDistance: 10 });
            const player = players.length > 0 ? players[0] : undefined;
            if (!player)
                return;
            const tasks = NPC.getTasks(entity);
            if (!tasks)
                return;
            const taskLevel = NPC.getTaskLevel(player, entity);
            const currentTask = tasks[taskLevel];
            handleNPMenu(player, npc, currentTask, entity);
            break;
    }
});
/*
tile.wildphire:bag_of_coins.name=Bag of Coins
tile.wildphire:plate_of_tomatoes.name=Plate of Tomatoes
tile.wildphire:plate_of_oranges.name=Plate of Oranges
tile.wildphire:coin_chest.name=Coin Chest
tile.wildphire:jar_of_rice.name=Jar of Rice
tile.wildphire:plate_of_grapes.name=Plate of Grapes
tile.wildphire:fish_plate.name=Fish Plate
tile.wildphire:jar_of_peanuts.name=Jar of Peanuts
tile.wildphire:jar_of_peas.name=Jar of Peas
tile.wildphire:chicken_plate.name=Chicken Plate

*/
world.beforeEvents.playerBreakBlock.subscribe((event) => {
    // console.warn(event.block.typeId);
    switch (event.block.typeId) {
        case "wildphire:bag_of_coins":
            event.cancel = true;
            //drop 1 - 2 coins
            spawnItem(new ItemStack("wildphire:coin", Math.floor(Math.random() * 2) + 1), event.block.location);
            breakBlock(event.block.location);
            break;
        case "wildphire:plate_of_tomatoes":
            event.cancel = true;
            spawnItem(new ItemStack("wildphire:tomatoes", 1), event.block.location);
            breakBlock(event.block.location);
            break;
        case "wildphire:plate_of_oranges":
            event.cancel = true;
            spawnItem(new ItemStack("wildphire:orange", 1), event.block.location);
            breakBlock(event.block.location);
            break;
        case "wildphire:coin_chest":
            event.cancel = true;
            //drop 4-5 coins
            spawnItem(new ItemStack("wildphire:coin", Math.floor(Math.random() * 2) + 4), event.block.location);
            breakBlock(event.block.location);
            break;
        case "wildphire:jar_of_rice":
            event.cancel = true;
            spawnItem(new ItemStack("wildphire:bowl_of_rice", 1), event.block.location);
            breakBlock(event.block.location);
            break;
        case "wildphire:plate_of_grapes":
            event.cancel = true;
            spawnItem(new ItemStack("wildphire:bowl_of_grapes", 1), event.block.location);
            breakBlock(event.block.location);
            break;
        case "wildphire:fish_plate":
            event.cancel = true;
            spawnItem(new ItemStack("minecraft:cooked_cod", 1), event.block.location);
            breakBlock(event.block.location);
            break;
        case "wildphire:jar_of_peanuts":
            event.cancel = true;
            spawnItem(new ItemStack("wildphire:bowl_of_peanuts", 1), event.block.location);
            breakBlock(event.block.location);
            break;
        case "wildphire:jar_of_peas":
            event.cancel = true;
            spawnItem(new ItemStack("wildphire:bowl_of_peas", 1), event.block.location);
            breakBlock(event.block.location);
            break;
        case "wildphire:chicken_plate":
            event.cancel = true;
            spawnItem(new ItemStack("minecraft:cooked_chicken", 1), event.block.location);
            breakBlock(event.block.location);
            break;
    }
});
function getDim() {
    return world.getDimension("overworld");
}
function spawnItem(item, location) {
    system.run(() => { getDim().spawnItem(item, { x: location.x + 0.5, y: location.y, z: location.z + 0.5 }); });
}
function breakBlock(location) {
    system.run(() => { getDim().runCommand(`setblock ${location.x} ${location.y} ${location.z} air`); });
}
