import { Block, BlockPermutation, ItemStack, Player } from "@minecraft/server"
import { addDamageItem, setLoreItem } from "../../library/itemManager";


const blockEvents = {
    "minecraft:deepslate": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_stone"),
    "minecraft:cobbled_deepslate": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_stone"),
    "minecraft:blackstone": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_stone"),
    "minecraft:tuff": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_stone"),
    "minecraft:netherrack": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_stone"),
    "minecraft:copper_ore": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_ore"),
    "minecraft:deepslate_copper_ore": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_ore"),
    "minecraft:deepslate_lapis_ore": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_ore"),
    "minecraft:ancient_debris": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_ore"),
    "minecraft:quartz_ore": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_ore"),
    "minecraft:nether_gold_ore": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_ore"),
    "minecraft:raw_copper_block": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_ore"),
    "minecraft:magma": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:amethyst_block": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:calcite": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:basalt": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:polished_basalt": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:smooth_basalt": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:copper_block": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:coal_block": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:diamond_block": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:emerald_block": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:lapis_block": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:waxed_copper": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:cut_copper": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:waxed_cut_copper": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:oxidized_copper": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:oxidized_cut_copper": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:waxed_oxidized_copper": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:waxed_oxidized_cut_copper": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:exposed_copper": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:waxed_exposed_copper": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:exposed_cut_copper": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:waxed_exposed_cut_copper": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:weathered_copper": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:waxed_weathered_copper": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:weathered_cut_copper": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "minecraft:waxed_weathered_cut_copper": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block"),
    "nps_ec:charged_amethyst_block": (player, block) => triggerEvent(player, block, "nps_ec:drill_digger_special_block")
};



/**
 * Evento del componente plasmaDrillMineBlockComponent
 * @param {{itemStack: ItemStack, source: Player, block: Block, minedBlockPermutation: BlockPermutation}} event 
 */
export const plasmaDrillMineBlockEvent = ({itemStack, source, block, minedBlockPermutation}) => {
    const state = source.getDynamicProperty('nps_ec:drill');
    const currentDamage = itemStack.getComponent('minecraft:durability').damage;
    const maxDurability = itemStack.getComponent('minecraft:durability').maxDurability;
    const durability = maxDurability - currentDamage;
    if(state == undefined) {
        source.setDynamicProperty('nps_ec:drill', 0);
        setLoreItem(itemStack, source, "§r§9+18", 'Drill in single block mode');
    }


    if(state) {
        setLoreItem(itemStack, source, "§r§9Damage area", 'Drill in multiblock mode');
        if(durability > 30) {
            addDamageItem(itemStack, source, 12);
            source.runCommand("playsound drill.digger @a [r=5] ~ ~ ~ 2.0");

            const minedBlock = block;
            minedBlock.setPermutation(minedBlockPermutation)

            if(minedBlock.hasTag('stone') || minedBlock.typeId.includes("sandstone") || minedBlock.typeId.includes('terracotta')) return triggerEvent(source, block, "nps_ec:drill_digger_stone");
            if (minedBlock.hasTag('diamond_pick_diggable') || minedBlock.hasTag('minecraft:diamond_tier_destructible') || minedBlock.hasTag('minecraft:iron_tier_destructible')) return triggerEvent(source, block, "nps_ec:drill_digger_ore");
            if(minedBlock.hasTag('metal')) return triggerEvent(source, block, "nps_ec:drill_digger_special_block");

            source.runCommand(`setblock ${block.x} ${block.y} ${block.z} air replace`)
            try {
                blockEvents[minedBlockPermutation.type.id](source, block);
            } catch { }
        } else {
            source.runCommand("playsound shoot.locked @a [r=5] ~ ~ ~ 0.7");
            source.runCommand("replaceitem entity @s slot.weapon.mainhand 0 nps_ec:plasma_drill_unloaded 1 1239")
        }
    } else {
        setLoreItem(itemStack, source, "§r§9+18", 'Drill in single block mode');
        if(durability > 10) {
            addDamageItem(itemStack, source, 1);
            
            source.runCommand("playsound drill.digger @a [r=5] ~ ~ ~ 3.0");
        } else {
            source.runCommand("playsound shoot.locked @a [r=5] ~ ~ ~ 0.7");
            source.runCommand("replaceitem entity @s slot.weapon.mainhand 0 nps_ec:plasma_drill_unloaded 1 1239")
        }
    }
}

/**
 * 
 * @param {Player} player 
 * @param {Block} block 
 * @param {string} event 
 */
const triggerEvent = (player, block, event) => {
    player.runCommand(`setblock ${block.x} ${block.y} ${block.z} air replace`)
    switch(event) {
        case "nps_ec:drill_digger_stone": 
            player.runCommand(`summon nps_ec:plasma_drill_entity ${block.x} ${block.y} ${block.z} ~ ~ nps_ec:stone_blocks`)
            break;
        case "nps_ec:drill_digger_ore":
            player.runCommand(`summon nps_ec:plasma_drill_entity ${block.x} ${block.y} ${block.z} ~ ~ nps_ec:ore_blocks`)
            break;
        case "nps_ec:drill_digger_special_block":
            player.runCommand(`summon nps_ec:plasma_drill_entity ${block.x} ${block.y} ${block.z}  ~ ~ nps_ec:special_blocks`)
            break;
    }
}