import { Player, world } from "@minecraft/server";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui";
import { Vector3 } from "../../vector/vector3";
import { namespace } from "../../namespace";

/**
 * 
 * @param {Player} player 
 */
export function hackerCommandBlock(player) {
    const form = new ActionFormData()
    form.title("Hacker Command Block")
    form.button("Teleport")
    form.button("Add An Effect")
    form.button("Spawn Entity")

    player.dimension.playSound(`${namespace}:ui`, player.location)

    form.show(player).then((data) => {

        player.dimension.playSound(`${namespace}:ui_select`, player.location)

        if (data.canceled) return
        switch (data.selection) {
            case 0:
                openTeleportMenu(player)
                break
            case 1:
                openEffectMenu(player)
                break
            case 2:
                openSpawnEntityMenu(player)
                break
        }
    })
}

/**
 * 
 * @param {Player} player 
 */
function openTeleportMenu(player) {
    const roundedLoc = new Vector3().toVector(player.location).rounded(1)
    const form = new ModalFormData()
    form.title("Teleport")
    form.textField("X Coordinate", roundedLoc.x.toString())
    form.textField("Y Coordinate", roundedLoc.y.toString())
    form.textField("Z Coordinate", roundedLoc.z.toString())
    form.show(player).then((data) => {

        player.dimension.playSound(`${namespace}:ui_select`, player.location)

        if (data.canceled) return

        const x = parseFloat(data.formValues[0])
        const y = parseFloat(data.formValues[1])
        const z = parseFloat(data.formValues[2])
        if (isNaN(x) || isNaN(y) || isNaN(z)) {
            player.onScreenDisplay.setActionBar("Invalid coordinates")
            return
        }

        player.teleport(new Vector3(x, y, z))
    })
}

const effects = {
    "Absorption": "absorption",
    "Bad Omen": "bad_omen",
    "Blindness": "blindness",
    "Conduit Power": "conduit_power",
    "Darkness": "darkness",
    "Fire Resistance": "fire_resistance",
    "Haste": "haste",
    "Health Boost": "health_boost",
    "Hero of the Village": "village_hero",
    "Hunger": "hunger",
    "Instant Damage": "instant_damage",
    "Instant Health": "instant_health",
    "Invisibility": "invisibility",
    "Jump Boost": "jump_boost",
    "Levitation": "levitation",
    "Mining Fatigue": "mining_fatigue",
    "Nausea": "nausea",
    "Night Vision": "night_vision",
    "Poison": "poison",
    "Regeneration": "regeneration",
    "Resistance": "resistance",
    "Saturation": "saturation",
    "Slow Falling": "slow_falling",
    "Slowness": "slowness",
    "Speed": "speed",
    "Strength": "strength",
    "Water Breathing": "water_breathing",
    "Weakness": "weakness",
    "Wither": "wither"
}

/**
 * 
 * @param {Player} player 
 */
function openEffectMenu(player) {
    const targets = ["@a", "@s", "@e", "@r"].concat(world.getPlayers().map((p) => p.name))
    const form = new ModalFormData()
    form.title("Effect")
    form.dropdown("Target", targets)
    form.dropdown("Effects", Object.keys(effects))
    form.slider("Amplifier", 1, 6, 1, 1)
    form.slider("Duration (seconds)", 1, 180, 1, 60)
    form.show(player).then((data) => {

        player.dimension.playSound(`${namespace}:ui_select`, player.location)

        if (data.canceled) return
        const target = targets[data.formValues[0]]
        const effect = Object.values(effects)[data.formValues[1]]
        const amplifier = parseInt(data.formValues[2])
        const duration = parseInt(data.formValues[3])
        if (isNaN(amplifier) || isNaN(duration)) {
            player.onScreenDisplay.setActionBar("Invalid amplifier or duration")
            return
        }
        player.runCommand(`effect ${target} ${effect} ${duration} ${amplifier - 1}`)
    })
}

/**
 * 
 * @param {Player} player 
 */
function openSpawnEntityMenu(player) {
    const form  = new ActionFormData()
    Object.keys(mobs).forEach((mob) => form.button(mob))

    form.show(player).then((data) => {

        player.dimension.playSound(`${namespace}:ui_select`, player.location)

        if (data.canceled) return

        const mob = Object.keys(mobs)[data.selection]
        player.dimension.spawnEntity(mobs[mob], player.location)
    })
}

const mobs = {
    "Chicken": "chicken",
    "Cow": "cow",
    "Pig": "pig",
    "Sheep": "sheep",
    "Wolf": "wolf",
    "Villager": "villager",
    "Mooshroom": "mooshroom",
    "Squid": "squid",
    "Rabbit": "rabbit",
    "Bat": "bat",
    "Iron Golem": "iron_golem",
    "Snow Golem": "snow_golem",
    "Ocelot": "ocelot",
    "Horse": "horse",
    "Donkey": "donkey",
    "Mule": "mule",
    "Skeleton Horse": "skeleton_horse",
    "Zombie Horse": "zombie_horse",
    "Polar Bear": "polar_bear",
    "Llama": "llama",
    "Parrot": "parrot",
    "Dolphin": "dolphin",
    "Turtle": "turtle",
    "Cat": "cat",
    "Pufferfish": "pufferfish",
    "Salmon": "salmon",
    "Tropical Fish": "tropicalfish",
    "Cod": "cod",
    "Panda": "panda",
    "Wandering Trader": "wandering_trader",
    "Fox": "fox",
    "Bee": "bee",
    "Strider": "strider",
    "Goat": "goat",
    "Frog": "frog",
    "Tadpole": "tadpole",
    "Axolotl": "axolotl",
    "Glow Squid": "glow_squid",
    "Allay": "allay",
    "Zombie": "zombie",
    "Creeper": "creeper",
    "Skeleton": "skeleton",
    "Spider": "spider",
    "Zombie Pigman": "zombie_pigman",
    "Slime": "slime",
    "Enderman": "enderman",
    "Silverfish": "silverfish",
    "Cave Spider": "cave_spider",
    "Ghast": "ghast",
    "Magma Cube": "magma_cube",
    "Blaze": "blaze",
    "Zombie Villager": "zombie_villager",
    "Witch": "witch",
    "Stray": "stray",
    "Husk": "husk",
    "Wither Skeleton": "wither_skeleton",
    "Guardian": "guardian",
    "Elder Guardian": "elder_guardian",
    "Wither": "wither",
    "Ender Dragon": "ender_dragon",
    "Shulker": "shulker",
    "Endermite": "endermite",
    "Vindicator": "vindicator",
    "Phantom": "phantom",
    "Ravager": "ravager",
    "Evocation Illager": "evocation_illager",
    "Vex": "vex",
    "Drowned": "drowned",
    "Piglin": "piglin",
    "Hoglin": "hoglin",
    "Zoglin": "zoglin",
    "Piglin Brute": "piglin_brute",
    "Warden": "warden"
}