import { world, system, BlockPermutation, Player, ItemStack } from "@minecraft/server";

export function savePlayerXpLevel(player) {
    try {
        const playerLevel = player.level;
        const playerId = player.id;

        const propertyName = `xp_level_${playerId}`;
        world.setDynamicProperty(propertyName, playerLevel);

    } catch (e) {
    }
}

export function loadPlayerXpLevel(player) {
    try {
        const playerId = player.id;
        const propertyName = `xp_level_${playerId}`;
        const savedLevel = world.getDynamicProperty(propertyName);

        if (typeof savedLevel === "number") {
            const currentLevel = player.level;
            const levelDifference = savedLevel - currentLevel;

            player.addLevels(levelDifference);

        } else {

        }
    } catch (e) {

    }
}

const dimensionNames = ["overworld", "nether", "the_end"];

function checkDummiesTags() {
    try {
        const players = world.getPlayers();
        const playerIds = players.map(p => p.id);

        for (const dimName of dimensionNames) {
            const dimension = world.getDimension(dimName);
            if (!dimension) continue;

            const allEntities = dimension.getEntities();
            const dummies = allEntities.filter(e => e.typeId === "cbcl_rm:interact_dummy");

            for (const dummy of dummies) {
                const tags = dummy.getTags();
                const hasValidTag = tags.some(tag => playerIds.includes(tag));

                if (!hasValidTag) {
                    dummy.runCommand("event entity @s cbcl_rm:despawn");
                }
            }
        }
    }
    catch (e) { }
}

system.runInterval(() => {
    checkDummiesTags();
}, 10);

export function getItemCount(player, itemId) {
    const inventory = player.getComponent("minecraft:inventory").container;
    let count = 0;

    for (let i = 0; i < inventory.size; i++) {
        const slotItem = inventory.getItem(i);
        if (slotItem && slotItem.typeId === itemId) {
            count += slotItem.amount;
        }
    }

    return count;
}

export function removeItems(player, itemId, amount) {
    const inventory = player.getComponent("minecraft:inventory").container;

    for (let i = 0; i < inventory.size && amount > 0; i++) {
        const slotItem = inventory.getItem(i);
        if (slotItem && slotItem.typeId === itemId) {
            if (slotItem.amount > amount) {
                slotItem.amount -= amount;
                inventory.setItem(i, slotItem);
                amount = 0;
            } else {
                amount -= slotItem.amount;
                inventory.setItem(i, undefined);
            }
        }
    }
}

export function playAnimation(player) {
    const interval = system.runInterval(() => {
        try{
        if (player.getDynamicProperty("cbcl_rm:dead")) {
            player.playAnimation("animation.cbcl_rm.player_dead");
        }
        else { system.clearRun(interval); }}
        catch(e){}
    }, 1);
}