import { EnchantmentType, EnchantmentTypes, EntityInventoryComponent, ItemComponentTypes, ItemEnchantableComponent, ItemStack, Player, world } from "@minecraft/server";
import { namespace } from "../namespace";
import { randomBetween } from "../util/random";
import { colours } from "../util/colour"
import { Vector3 } from "../vector/vector3"

/**
 * 
 * @param {Player} player 
 * @param {number} shardAmount 
 */
export function hackerTable(player, shardAmount) {
    // weighted random system that increases the chance of getting a higher tier item based on the amount of shards used
    const legendaryChance = shardAmount / 5
    const rareChance = shardAmount / 2.5
    const commonChance = shardAmount / 1

    const random = randomBetween(0, 1)
    let item = null;

    if (random < legendaryChance) {
        item = items.legendary[randomBetween(0, items.legendary.length - 1, true)]
    } else if (random < rareChance) {
        item = items.rare[randomBetween(0, items.rare.length - 1, true)]
    } else if (random < commonChance) {
        item = items.common[randomBetween(0, items.common.length - 1, true)]
    }

    if (item) {
        let itemStack = new ItemStack(item.id)
        itemStack.setLore(item.lore)
        if (item.id == `${namespace}:fly_boots`) {
            /**
             * @type {ItemEnchantableComponent}
             */
            const ench = itemStack.getComponent(ItemComponentTypes.Enchantable)
            ench.addEnchantment({ type: new EnchantmentType("feather_falling"), level: 4 })
        }

        player.dimension.spawnItem(itemStack, player.location)
        player.dimension.playSound(`${namespace}:hacker_table_item_equip`, player.location)
    }
}

const r = colours.reset + colours.dark_gray
const g = colours.reset + colours.gray

export const items = {
    "legendary": [
        //legendary armour
        { id: `${namespace}:hacker_boots`, lore: [`${r}Gives the wearer lots of protection`, `${r}Wear the full set to gain many helpful effects`] },
        { id: `${namespace}:hacker_chestplate`, lore: [`${r}Gives the wearer lots of protection`, `${r}Wear the full set to gain many helpful effects`] },
        { id: `${namespace}:hacker_helmet`, lore: [`${r}Gives the wearer lots of protection`, `${r}Wear the full set to gain many helpful effects`] },
        { id: `${namespace}:hacker_leggings`, lore: [`${r}Gives the wearer lots of protection`, `${r}Wear the full set to gain many helpful effects`] },

        //legendary items
        { id: `${namespace}:hacker_sword`, lore: [`${g}Sharpness 32767`, `${r}Instantly eliminates most entities`] },
        { id: `${namespace}:item_duper`, lore: [`${r}Duplicates the item that is used on it`] },
        { id: `${namespace}:super_fortune_pickaxe`, lore: [`${g}Fortune 32767`, `${r}Greatly increases the amount of ores dropped`] },
        { id: `${namespace}:hacker_apple`, lore: [`${r}Makes you invincible for 1 minute`] },

    ],
    "rare": [
        //epic armour
        { id: `${namespace}:infinity_chestplate`, lore: [`${r}Damages all mobs around you while wearing`] },
        { id: `${namespace}:fly_boots`, lore: [`${r}Allows you to jump on air`] },

        //epic tools

        { id: `${namespace}:treecapitator`, lore: [`${r}Breaks trees in one hit`] },
        { id: `${namespace}:knockback_sword`, lore: [`${r}Knocks entities away when hit`] },
        { id: `${namespace}:hacker_tnt_item`, lore: [`${r}Explodes a massive area around it`] },
        { id: `${namespace}:replenish_hoe`, lore: [`${r}Replaces crops when broken`, `${r}Must have seeds in inventory`] },
        { id: `${namespace}:hacker_command_block`, lore: [`${r}Gives access to 3 useful commands`] }
    ],
    "common": [
        //common armour
        { id: `${namespace}:speed_boots`, lore: [`${r}Gives you speed 2.`] },

        //common tools
        { id: `${namespace}:infinite_lava_bucket`, lore: [`${r}Infinitely place lava`] },
        { id: `${namespace}:infinite_water_bucket`, lore: [`${r}Infinitely place water`] },
        { id: `${namespace}:teleport_compass`, lore: [`${r}Teleport to your spawn point`] },
        { id: `${namespace}:frog_sword`, lore: [`${r}Turn mobs that you hit into frogs`] },
        { id: `${namespace}:teleporter`, lore: [`${r}Teleport to where you are looking`] },
        { id: `${namespace}:mega_pickaxe`, lore: [`${r}Breaks 5 blocks at once`] },
        { id: `${namespace}:random_ore_pickaxe`, lore: [`${r}Right-click to change stone to ore`] },
        { id: `${namespace}:mega_shovel`, lore: [`${r}Breaks 5 blocks at once`] },
        { id: `${namespace}:hacker_clock`, lore: [`${r}Change the time, weather, and their gamerules`] }
    ]
}