import { Player } from "@minecraft/server";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui";
import { colours } from "./util/colour";
import { namespace } from "./namespace";
import { inventoryItemCount } from "./util/inventory";
import { hackerTable } from "./items/hackerTable";
const c = colours

/**
 * 
 * @param {Player} player 
 */
export function mainForm(player) {
    const form = new ActionFormData()
    form.title("Hacker Items")
    form.body("Gather Glitch Shards and use them at the Hacker Table. You can find these shards by defeating various entities. The more shards you collect, the higher your chances of obtaining a valuable item!")
    form.button("Guide Book", craftImg("guide_book"))
    form.button("Hacker Table", craftImg("hacker_table"))
    form.button("View Hacker Items")

    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:
                descriptionForm(player, 26)
                break
            case 1:
                descriptionForm(player, 27)
                break
            case 2:
                hackerItemsForm(player)
                break
        }
    })
}

/**
 * 
 * @param {Player} player 
 */
function hackerItemsForm(player) {
    const form = new ActionFormData()
    form.title("Hacker Items - All Items")
    form.body("Select an item to learn about it.")
    // Legendary
    form.button(`${c.dark_green}Glitch Shard`, itemImg("glitch_shard"))
    form.button(`${c.gold}Hacker Helmet`, itemImg("hacker_helmet"))
    form.button(`${c.gold}Hacker Chestplate`, itemImg("hacker_chestplate"))
    form.button(`${c.gold}Hacker Leggings`, itemImg("hacker_leggings"))
    form.button(`${c.gold}Hacker Boots`, itemImg("hacker_boots"))
    form.button(`${c.gold}Hacker Sword`, itemImg("hacker_sword"))
    form.button(`${c.gold}Item Duplicator`, "textures/block/ha/items/item_duper_block")
    form.button(`${c.gold}Super Fortune Pickaxe`, "textures/items/netherite_pickaxe")
    form.button(`${c.gold}Hacker Apple`, itemImg("hacker_apple"))

    // Rare
    form.button(`${c.blue}Guardian Chestplate`, itemImg("ka_chestplate"))
    form.button(`${c.blue}Fly Boots`, itemImg("fly_boots"))
    form.button(`${c.blue}Treecapitator`, itemImg("treecapitator"))
    form.button(`${c.blue}Knockback Sword`, itemImg("knockback_sword"))
    form.button(`${c.blue}Hacker TNT`, itemImg("hacker_tnt"))
    form.button(`${c.blue}Replenish Hoe`, itemImg("replenish_hoe"))
    form.button(`${c.blue}Hacker Command Block`, itemImg("hacker_cmd_block_item"))

    // Common
    form.button(`${c.dark_gray}Speed Boots`, itemImg("speed_boots"))
    form.button(`${c.dark_gray}Infinite Lava Bucket`, itemImg("infinite_lava"))
    form.button(`${c.dark_gray}Infinite Water Bucket`, itemImg("infinite_water"))
    form.button(`${c.dark_gray}Teleport Compass`, itemImg("teleport_compass"))
    form.button(`${c.dark_gray}Frog Sword`, itemImg("frog_sword"))
    form.button(`${c.dark_gray}Teleporter`, itemImg("teleporter"))
    form.button(`${c.dark_gray}Mega Pickaxe`, itemImg("mega_pickaxe"))
    form.button(`${c.dark_gray}Random Ore Pickaxe`, itemImg("random_ore_pickaxe"))
    form.button(`${c.dark_gray}Mega Shovel`, itemImg("mega_shovel"))
    form.button(`${c.dark_gray}Hacker Clock`, itemImg("hacker_clock"))

    form.button("Back")

    form.show(player).then(data => {
        player.dimension.playSound(`${namespace}:ui_select`, player.location)

        if (data.canceled) return
        
        if (data.selection == 26) {
            mainForm(player)
            return
        }

        descriptionForm(player, data.selection)
    })
}

/**
 * 
 * @param {Player} player 
 * @param {number} index 
 */
function descriptionForm(player, index) {
    const form = new ActionFormData()
    form.title(`Hacker Items - ${names[index]}`)
    form.body(descriptions[index])
    form.button("Back")
    form.show(player).then((data) => {
        player.dimension.playSound(`${namespace}:ui_select`, player.location)
        if (data.canceled) return

        if (index == 26 || index == 27) {
            mainForm(player)
            return
        }

        hackerItemsForm(player)
    })
}

/**
 * 
 * @param {Player} player 
 */
export function hackerTableForm(player) {
    const shardCount = inventoryItemCount(player, `${namespace}:glitch_shard`)
    if (shardCount <= 0) {
        const form = new ActionFormData()
        form.title("Hacker Table")
        form.body(`${c.dark_red}You need Glitch Shards to use the Hacker Table.`)
        form.button("Close")

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

        form.show(player).then((data) => {
            player.dimension.playSound(`${namespace}:ui_select`, player.location)
        })
    }
    else {
        const form = new ModalFormData()
        form.title("Hacker Table")
        form.slider("Amount of Glitch Shards to use", 1, shardCount, 1, 1)

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

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

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

            if (data.canceled) return
            const amount = data.formValues[0]
            hackerTable(player, amount)
            player.runCommandAsync(`clear @s ${namespace}:glitch_shard 0 ${amount}`)
        })
    }
}

const names = [
    "Glitch Shard",
    "Hacker Helmet",
    "Hacker Chestplate",
    "Hacker Leggings",
    "Hacker Boots",
    "Hacker Sword",
    "Item Duplicator",
    "Super Fortune Pickaxe",
    "Hacker Apple",
    "Guardian Chestplate",
    "Fly Boots",
    "Treecapitator",
    "Knockback Sword",
    "Hacker TNT",
    "Replenish Hoe",
    "Hacker Command Block",
    "Speed Boots",
    "Infinite Lava Bucket",
    "Infinite Water Bucket",
    "Teleport Compass",
    "Frog Sword",
    "Teleporter",
    "Mega Pickaxe",
    "Random Ore Pickaxe",
    "Mega Shovel",
    "Hacker Clock",
    "Guide Book",
    "Hacker Table"
]

const descriptions = [
    `Defeat entities to collect these and use them at the Hacker Table.`,
    `Gives the wearer lots of protection\nWear the full set for OP effects`,
    `Gives the wearer lots of protection\nWear the full set for OP effects`,
    `Gives the wearer lots of protection\nWear the full set for OP effects`,
    `Gives the wearer lots of protection\nWear the full set for OP effects`,
    `Eliminates all entities in a single hit.`,
    `Duplicates the item that is used on it every 10 seconds. Hacker Items such as Glitch Shards, armour, etc. can not be duplicated.`,
    `Greatly increases the amount of ores dropped.`,
    `Makes you invincible for 1 minute.`,
    `Damages all mobs around you while wearing.`,
    `Allows you to jump on air.`,
    `Breaks trees in one hit.`,
    `Knocks entities far away when hit.`,
    `Explodes a massive area around it, you will die if you are too close.`,
    `Replaces crops when broken, must have seeds in your inventory.`,
    `Gives access to 3 useful commands.`,
    `Gives you speed 2 while wearing.`,
    `Infinitely place lava.`,
    `Infinitely place water.`,
    `Teleports you to your spawn point.`,
    `Turn mobs that you hit into frogs.`,
    `Teleport to where you are looking. Has a maximum distance of 100 blocks.`,
    `Breaks 5 blocks at once if they can be mined using a pickaxe.`,
    `Changes stone into a random ore after right-clicking`,
    `Breaks 5 blocks at once if they can be mined using a shovel.`,
    `Change the time, weather, and their respective gamerules`,
    `Learn how to use the Hacker Items`,
    `Interact with the Hacker Table to get items. Use your Glitch Shards at the Hacker Table to redeem items. The more shards you use, the better the item you will get!`,
]

/**
 * 
 * @param {string} name 
 * @returns {string}
 */
function craftImg(name) {
    return `textures/block/ha/crafting/${name}`
}

/**
 * 
 * @param {string} name 
 * @returns {string}
 */
function itemImg(name) {
    return `textures/block/ha/items/${name}`
}
