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

export function setScore(entity, scoreboard, value) {
    try {
        entity.runCommand(`scoreboard objectives add ${scoreboard} dummy`);
        entity.runCommand(`scoreboard players set @s ${scoreboard} ${value}`);
    } catch (e) {
        console.warn(e);
    }
}

export function movePlayerInventory(player, inventoryEntity) {
    const playerContainer = player.getComponent('inventory')?.container;
    const toContainer = inventoryEntity.getComponent('inventory')?.container;

    for (let i = 0; i < playerContainer.size; i++) {
        playerContainer.moveItem(i, i, toContainer);
    }
}

export function movePlayerEquippable(player, equippableEntity) {
    const equipment = player.getComponent('equippable');
    const toContainer = equippableEntity.getComponent('inventory')?.container;
    const equipmentSlots = ['Head', 'Chest', 'Legs', 'Feet'];

    for (let i = 0; i < equipmentSlots.length; i++) {
        const equipmentSlot = equipmentSlots[i];
        const item = equipment.getEquipment(equipmentSlot);
        equipment.setEquipment(equipmentSlot, new ItemStack("minecraft:air"));
        toContainer.setItem(i, item);
    }
}

export function moveInventoryPlayer(player, inventoryEntity) {
    const playerContainer = player.getComponent('inventory')?.container;
    const fromContainer = inventoryEntity.getComponent('inventory')?.container;

    for (let i = 0; i < playerContainer.size; i++) {
        fromContainer.moveItem(i, i, playerContainer);
    }
}

export function moveEquippablePlayer(player, equippableEntity) {
    const equipment = player.getComponent('equippable');
    const fromContainer = equippableEntity.getComponent('inventory')?.container;
    const equipmentSlots = ['Head', 'Chest', 'Legs', 'Feet'];

    for (let i = 0; i < equipmentSlots.length; i++) {
        const equipmentSlot = equipmentSlots[i];
        equipment.setEquipment(equipmentSlot, fromContainer.getItem(i));
    }
}

export function saveInventory(sourceEntity) {
    const inventoryEntity = sourceEntity.dimension.spawnEntity(`cubed:inventory`, {x: 10000, y: 255, z: 10000});
    movePlayerInventory(sourceEntity, inventoryEntity);
    sourceEntity.setDynamicProperty("cubed:inventory_id", inventoryEntity.id);

    const equippableEntity = sourceEntity.dimension.spawnEntity(`cubed:equippable`, {x: 10000, y: 255, z: 10000});
    movePlayerEquippable(sourceEntity, equippableEntity);
    sourceEntity.setDynamicProperty("cubed:equippable_id", equippableEntity.id);
}

export function loadInventory(sourceEntity) {
    const inventoryEntity = world.getEntity(sourceEntity.getDynamicProperty("cubed:inventory_id"));
    const equippableEntity = world.getEntity(sourceEntity.getDynamicProperty("cubed:equippable_id"));

    moveInventoryPlayer(sourceEntity, inventoryEntity);
    moveEquippablePlayer(sourceEntity, equippableEntity);

    inventoryEntity.triggerEvent("cubed:despawn");
    equippableEntity.triggerEvent("cubed:despawn");
}

system.afterEvents.scriptEventReceive.subscribe((event) => {
    const {id, message, sourceEntity, sourceType} = event;
    if (id === "cubed:load_inventory") {
        loadInventory(sourceEntity);
    }
});



system.runInterval(() => {
    const players = world.getPlayers();
    players.forEach((player) => {
        if (player.hasTag('is_riding_dino') && player.hasTag('is_riding')) {
            const playerContainer = player.getComponent('inventory').container;
            for (let i = 0; i < playerContainer.size; i++) {
                try {
                    const item = playerContainer.getSlot(i);
                    const typeId = item.typeId;
                } catch (error) {
                    const barrier = new ItemStack("cubed:empty");
                    barrier.lockMode = "slot";  // Set lock mode if applicable
                    playerContainer.setItem(i, barrier);
                }
            }
        }
    });
}, 1);