import { Block, EntityEquippableComponent, EquipmentSlot, ItemStack, Player, world } from "@minecraft/server"
import { getEquipmentSlot } from "../../util/getEquipmentSlot"
import { namespace } from "../../namespace"
import { Vector3 } from "../../vector/vector3"

/**
 * 
 * @param {Player} player 
 * @param {Block} block 
 * @returns 
 */
export function itemDuperInit(player, block) {
    let equipEnt = player.dimension.getEntitiesAtBlockLocation(block.location).filter(ent => ent.typeId == `${namespace}:item_duper`)
    if (equipEnt.length == 0) equipEnt = player.dimension.spawnEntity(`${namespace}:item_duper`, block.above().location)
    const ent = equipEnt[0]
    /**
     * @type {EntityEquippableComponent}
     */
    const playerEquip = player.getComponent("equippable")
    const heldItem = getEquipmentSlot(playerEquip, EquipmentSlot.Mainhand)
    if (heldItem != undefined) {
        if (heldItem.typeId == ent.getDynamicProperty(`${namespace}:dupe_item`) && heldItem.getLore().join("1288") == ent.getDynamicProperty(`${namespace}:dupe_item_lore`)) {
            player.sendMessage("§cThe item is already being duplicated.")
            return
        }
        if (!heldItem.typeId.startsWith("minecraft:")) {
            player.sendMessage("§cYou can only dupe vanilla items.")
            return
        }
        if (ent.getDynamicProperty(`${namespace}:dupe_item`) != undefined) {
            ent.dimension.spawnItem(new ItemStack(ent.getDynamicProperty(`${namespace}:dupe_item`)), new Vector3().toVector(ent.location).add(new Vector3(0, 1, 0)))   
        }
        ent.runCommand(`replaceitem entity @s slot.weapon.mainhand 0 ${heldItem.typeId}`)
        ent.setDynamicProperty(`${namespace}:dupe_time`, 10)
        ent.setDynamicProperty(`${namespace}:dupe_item`, heldItem.typeId)
        ent.setDynamicProperty(`${namespace}:dupe_item_lore`, heldItem.getLore().join("1288"))
        
        player.onScreenDisplay.setActionBar("Sneak + hit to remove the duper.")
        
        if (heldItem.amount == 1) {
            playerEquip.setEquipment(EquipmentSlot.Mainhand)
        }
        else {
            const item = new ItemStack(heldItem.typeId, heldItem.amount - 1)
            item.setLore(heldItem.getLore())
            playerEquip.setEquipment(EquipmentSlot.Mainhand, item)
        }
    }
}

export function dupeItems() {
    let allDupers = world.getDimension("overworld").getEntities({ type: `${namespace}:item_duper` })
    allDupers = allDupers.concat(world.getDimension("nether").getEntities({ type: `${namespace}:item_duper` }))
    allDupers = allDupers.concat(world.getDimension("the_end").getEntities({ type: `${namespace}:item_duper` }))

    allDupers.forEach(duper => {
        /**
         * @type {number}
         */
        const dupeTime = duper.getDynamicProperty(`${namespace}:dupe_time`)

        if (dupeTime == undefined || dupeTime == 0) return
        duper.setDynamicProperty(`${namespace}:dupe_time`, dupeTime - 1)
        if (dupeTime - 1 == 0) {

                const item = new ItemStack(duper.getDynamicProperty(`${namespace}:dupe_item`), 1)
                /**
                 * @type {string[]}
                 */
                const lore = duper.getDynamicProperty(`${namespace}:dupe_item_lore`).split("1288")
                if (lore[0].length != 0) {
                    item.setLore(lore)
                }

                try {
                    duper.dimension.spawnItem(item, new Vector3().toVector(duper.location).add(new Vector3(0, 1, 0)))
                    duper.dimension.playSound(`${namespace}:duplicate`, duper.location)
                    duper.dimension.spawnParticle(`${namespace}:item_duper`, duper.location)
                }
                catch (e) {}
                
            duper.setDynamicProperty(`${namespace}:dupe_time`, 10)
        }
    })
}