import { world, system, EasingType, EntityDamageCause, ItemStack } from "@minecraft/server";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui";

import { pokemonsData } from "./data.js"

const randomItems = [
    "pr:pokeball",
    "pr:great_ball",
    "pr:beast_ball",
    "pr:ultra_ball",
    "pr:master_ball",
    "pr:safari_ball",
    "pr:level_ball",
    "pr:lure_ball",
    "pr:moon_ball",
    "pr:friend_ball",
    "pr:love_ball",
    "pr:heavy_ball",
    "pr:net_ball",
    "pr:timer_ball",
    "pr:repeat_ball",
    "pr:premier_ball",
    "pr:nest_ball",
    "pr:dive_ball",
    "pr:luxury_ball",
    "pr:heal_ball",
    "pr:quick_ball",
    "pr:dusk_ball",
    "pr:cherish_ball",
    "pr:dream_ball"
]

const randomItemsPokeball = [
    "pr:antidote",
    "pr:awakening",
    "pr:berry_juice",
    "pr:burn_heal",
    "pr:elixir",
    "pr:energy_powder",
    "pr:energy_root",
    "pr:ether",
    "pr:fresh_water",
    "pr:full_heal",
    "pr:full_restore",
    "pr:heal_powder",
    "pr:hyper_potion",
    "pr:ice_heal",
    "pr:lemonade",
    "pr:max_elixir",
    "pr:max_ether",
    "pr:max_potion",
    "pr:max_revive",
    "pr:moomoo_milk",
    "pr:paralyze_heal",
    "pr:potion",
    "pr:revival_herb",
    "pr:revive",
    "pr:soda_pop",
    "pr:super_potion",
    "pr:ball_of_mud",
    "pr:scattering_bang",
    "pr:spoiled_apricon",
    "pr:sticky_glob",
    "pr:pokeball",
    "pr:great_ball",
    "pr:beast_ball",
    "pr:ultra_ball",
    "pr:master_ball",
    "pr:safari_ball",
    "pr:level_ball",
    "pr:lure_ball",
    "pr:moon_ball",
    "pr:friend_ball",
    "pr:love_ball",
    "pr:heavy_ball",
    "pr:net_ball",
    "pr:timer_ball",
    "pr:repeat_ball",
    "pr:premier_ball",
    "pr:nest_ball",
    "pr:dive_ball",
    "pr:luxury_ball",
    "pr:heal_ball",
    "pr:quick_ball",
    "pr:dusk_ball",
    "pr:cherish_ball",
    "pr:dream_ball",
    "pr:chicken_key",
    "pr:pokeball_key",
    "pr:pumpkin_key",
    "pr:snow_key",
    "pr:ultra_key",
    "pr:upgrade_key",
    "pr:rare_candy",
    "pr:poke_coin"
]


world.afterEvents.playerInteractWithEntity.subscribe((event) => {
    const { player, target, itemStack } = event
    const itemsLenght = randomItems.length
    const randomNumber = Math.floor(Math.random() * itemsLenght)
    const itemsLenght2 = randomItems.length
    const randomNumber2 = Math.floor(Math.random() * itemsLenght)
    const randomQuantity = Math.random() < 0.95 ? 1 : 1;
    const randomQuantity2 = Math.random() < 0.5 ? 1 : 3;

    // Guard: only certain entities have mark_variant
    let mark_v = 0;
    const markComp = target?.getComponent?.("mark_variant");
    if (markComp && typeof markComp.value !== "undefined") {
      mark_v = markComp.value;
    }
    if (target.typeId.startsWith("pr:") && target.typeId.includes("crate")) {
        if ((itemStack?.typeId == "pr:pokeball_key" && target.typeId == "pr:pokeball_crate") || (itemStack?.typeId == "pr:pumpkin_key" && target.typeId == "pr:pokeball_pumpkin_crate") || (itemStack?.typeId == "pr:snow_key" && target.typeId == "pr:pokeball_snow_crate") || (itemStack?.typeId == "pr:chicken_key" && target.typeId == "pr:pokeball_chicken_crate") || (itemStack?.typeId == "pr:ultra_key" && target.typeId == "pr:pokeball_ultra_crate") || (itemStack?.typeId == "pr:upgrade_key" && target.typeId == "pr:pokeball_upgrade_crate")) {
            if (mark_v == 0) {
                const inv = player.getComponent("equippable")
                if (inv.getEquipment("Mainhand")) {
                    inv.setEquipment("Mainhand", undefined)
                }
                target.triggerEvent("oppening")
                system.runTimeout(() => {
                    const item = new ItemStack(randomItems[randomNumber], randomQuantity);
                    const spawnedItem = player.dimension.spawnItem(item, target.location)
                    spawnedItem.applyImpulse({ x: 0, y: 0.5, z: 0 });
                    player.playSound("random.orb")
                }, 1.5 * 20)
            }
        }
    }

    if (target.typeId == "pr:poke_stop") {
        if (mark_v == 0) {
            target.triggerEvent("used")
            const item = new ItemStack(randomItemsPokeball[randomNumber2], randomQuantity2);
            const spawnedItem = player.dimension.spawnItem(item, target.location)
            spawnedItem.applyImpulse({ x: 0, y: 0.5, z: 0 });
            player.playSound("random.orb")
        }
    }
})
