import { world, system, EntityComponentTypes, EquipmentSlot, BlockPermutation, ItemStack } from "@minecraft/server";
import { ITEM_LORES, DEFAULT_ITEM_LORE } from "../data/lore.js";

import { Debugger } from "../utils/debug.js";
const debug = new Debugger('mythicus_tw:blocks.generic');
debug.enabled = false;
debug.priority = 0;


export const SPECIAL_ITEM_LORE_FUNCS = {
    //"mythicus_tw:smelter": (permutation) => {
    //    const fuel = getBlockFuel(permutation) ?? 0;
    //    return [`§r§6Fuel: §e${fuel}/4000`];
    //},
};

// spawn block items with lore for broken add-on blocks
world.afterEvents.playerBreakBlock.subscribe((data) => {

    // check if player is creative or add-on item
    if (data.player.getGameMode() === "creative" || !data.brokenBlockPermutation.type.id.startsWith("mythicus_tw:")) return;

    // create item stack
    const item = data.brokenBlockPermutation.getItemStack(1);

    // get special lore
    const specialLoreFunc = SPECIAL_ITEM_LORE_FUNCS[item.typeId];
    const specialLore = specialLoreFunc ? specialLoreFunc(data.brokenBlockPermutation) : undefined;

    // get settings
    const isAdvanced = data.player.getDynamicProperty("mythicus_tw:addonSettings.advancedItemDescs") ?? true;

    // set lore
    if (isAdvanced) {
        const lore = ITEM_LORES[data.brokenBlockPermutation.type.id] ?? [];
        if (specialLore) {
            item.setLore(["", ...specialLore, ...lore, ...DEFAULT_ITEM_LORE]);
        } else {
            item.setLore(["", ...lore, ...DEFAULT_ITEM_LORE]);
        }
    } else {
        if (specialLore) {
            item.setLore(["", ...specialLore, ...DEFAULT_ITEM_LORE]);
        } else {
            item.setLore(["", ...DEFAULT_ITEM_LORE]);
        }
    }

    // spawn item
    data.dimension.spawnItem(item, data.block.bottomCenter());

});


const BLOCK_ENTITY_NAMES = {
    "mythicus_tw:trashcan": "§m§y§t§h§i§c§u§s§_§i§m§:§t§1"
}

//world.beforeEvents.worldInitialize.subscribe(data => {

//    data.blockComponentRegistry.registerCustomComponent("mythicus_tw:entityPlacer", {
        
//        onPlace({ block, dimension }) {
//            const loc = block.bottomCenter();

//            const entity = dimension.spawnEntity(block.typeId, loc);
//            entity.setDynamicProperty("mythicus_tw:initLoc", loc);

//            const name = BLOCK_ENTITY_NAMES[block.typeId];
//            if (name) entity.nameTag = name;
//        },

//        onPlayerDestroy({ block, dimension }) {
//            const blockId = block.typeId;
//            for (const entity of dimension.getEntitiesAtBlockLocation(block.location)) {
//                if (entity.typeId === blockId) entity.remove();
//            }
//        }

//    });

//    data.blockComponentRegistry.registerCustomComponent("mythicus_tw:entityReplacer", {

//        onPlayerInteract({ block, dimension }) {

//            // check if entity is present but mispositioned
//            const blockId = block.typeId;
//            for (const entity of dimension.getEntitiesAtBlockLocation(block.location)) {
//                if (entity.typeId === blockId) {
//                    entity.teleport(block.bottomCenter());
//                    return;
//                }
//            }

//            // spawn new entity
//            const loc = block.bottomCenter();
//            const entity = dimension.spawnEntity(blockId, loc);
//            entity.setDynamicProperty("mythicus_tw:initLoc", loc);

//            const name = BLOCK_ENTITY_NAMES[blockId];
//            if (name) entity.nameTag = name;

//        }

//    });

//});
