import { Block, GameMode, ItemStack, Player, system } from "@minecraft/server"
import { CustomBlockTags } from "../utils/blockTags"
import { EntityTypeIds } from "../../entities/utils/entityTypesIds"
import { BlockHelper } from "../utils/blockHelpers"
import { BlockValuedTagsKeys } from "../utils/blockConstants"
import { ItemHelper } from "../../items/utils/itemHelper"

export const CUSTOM_ORES = {
    _customOreXpDrops: {
        "cobaltium_ore": 3,
        "duskite_ore": 5,
        "venomite_ore": 5
    },

    /**
     * 
     * @param {Block} block 
     * @param {Player} player
     * @param {ItemStack} itemStack
     * @returns 
     */
    shouldDropXp(block, player, itemStack) {
        if (!CustomBlockTags.isCustom(block)) return false
        if (!Object.keys(this._customOreXpDrops).some(o => block.typeId.endsWith(o))) return false
        if (player.getGameMode == GameMode.creative) return false
        
        const toolRequired = BlockHelper.getBlockTagValue(block, BlockValuedTagsKeys.correctTool)
        const toolUsed = ItemHelper.getItemToolTier(itemStack)  
        
        if (toolRequired != toolUsed) return false;

        return true
    },

    /**
     * 
     * @param {Block} block 
     */
    dropXpOrb(block) {
        for (const oreType in this._customOreXpDrops) {
            if (!block.typeId.endsWith(oreType)) continue
            const xpNumber = this._customOreXpDrops[oreType]
            for (let x = 0; x < xpNumber; x++) {
                system.run(() => block.dimension.spawnEntity(EntityTypeIds.xpOrb, block.location) )
            }
            break
        }
    }
}