import * as Minecraft from "@minecraft/server"
import * as MinecraftUi from "@minecraft/server-ui"


Minecraft.world.afterEvents.itemUse.subscribe((event) => {
    if (event.itemStack.typeId == "aria_ah:morph_gauntlet") {
        openMorphMenu(event.source)
    }
})

/**
 * @param {Minecraft.Player} player 
 */
function openMorphMenu(player) {
    const morphScoreboard = Minecraft.world.scoreboard.getObjective("aria_ah.morph")
    const cowScore = morphScoreboard.getScore("cow")
    const rabbitScore = morphScoreboard.getScore("rabbit")
    const horseScore = morphScoreboard.getScore("horse")
    const foxScore = morphScoreboard.getScore("fox")
    const wolfScore = morphScoreboard.getScore("wolf")
    const catScore = morphScoreboard.getScore("cat")
    const chickenScore = morphScoreboard.getScore("chicken")
    const pigScore = morphScoreboard.getScore("pig")
    const sheepScore = morphScoreboard.getScore("sheep")
    const cowUnlocked = cowScore >= 10
    const rabbitUnlocked = rabbitScore >= 10
    const horseUnlocked = horseScore >= 10
    const foxUnlocked = foxScore >= 10
    const wolfUnlocked = wolfScore >= 10
    const catUnlocked = catScore >= 10
    const chickenUnlocked = chickenScore >= 10
    const pigUnlocked = pigScore >= 10
    const sheepUnlocked = sheepScore >= 10
    const cowLocked = cowScore < 10
    const rabbitLocked = rabbitScore < 10
    const horseLocked = horseScore < 10
    const foxLocked = foxScore < 10
    const wolfLocked = wolfScore < 10
    const catLocked = catScore < 10
    const chickenLocked = chickenScore < 10
    const pigLocked = pigScore < 10
    const sheepLocked = sheepScore < 10

    new MinecraftUi.ActionFormData()
        .title("§cMorph Gauntlet")
        .button({ rawtext: [{ translate: "aria_ah.ui.morph_menu.button.unmorph" }] }, "textures/aria/ah/ui/reset")
        .button({ rawtext: [{ translate: "aria_ah.ui.morph_menu.button.cow"}, (() => cowUnlocked ? {} : { translate: "aria_ah.ui.morph_menu.button.remain_to_unlock", with: [cowScore.toString(), "10"] })()] }, cowUnlocked ? "textures/aria/ah/items/heads/cow" : "textures/aria/ah/ui/locked")
        .button({ rawtext: [{ translate: "aria_ah.ui.morph_menu.button.rabbit"}, (() => rabbitUnlocked ? {} : { translate: "aria_ah.ui.morph_menu.button.remain_to_unlock", with: [rabbitScore.toString(), "10"] })()] }, rabbitUnlocked ? "textures/aria/ah/items/heads/rabbit" : "textures/aria/ah/ui/locked")
        .button({ rawtext: [{ translate: "aria_ah.ui.morph_menu.button.horse"}, (() => horseUnlocked ? {} : { translate: "aria_ah.ui.morph_menu.button.remain_to_unlock", with: [horseScore.toString(), "10"] })()] }, horseUnlocked ? "textures/aria/ah/items/heads/horse" : "textures/aria/ah/ui/locked")
        .button({ rawtext: [{ translate: "aria_ah.ui.morph_menu.button.fox"}, (() => foxUnlocked ? {} : { translate: "aria_ah.ui.morph_menu.button.remain_to_unlock", with: [foxScore.toString(), "10"] })()] }, foxUnlocked ? "textures/aria/ah/items/heads/fox" : "textures/aria/ah/ui/locked")
        .button({ rawtext: [{ translate: "aria_ah.ui.morph_menu.button.wolf"}, (() => wolfUnlocked ? {} : { translate: "aria_ah.ui.morph_menu.button.remain_to_unlock", with: [wolfScore.toString(), "10"] })()] }, wolfUnlocked ? "textures/aria/ah/items/heads/wolf" : "textures/aria/ah/ui/locked")
        .button({ rawtext: [{ translate: "aria_ah.ui.morph_menu.button.cat"}, (() => catUnlocked ? {} : { translate: "aria_ah.ui.morph_menu.button.remain_to_unlock", with: [catScore.toString(), "10"] })()] }, catUnlocked ? "textures/aria/ah/items/heads/cat" : "textures/aria/ah/ui/locked")
        .button({ rawtext: [{ translate: "aria_ah.ui.morph_menu.button.chicken"}, (() => chickenUnlocked ? {} : { translate: "aria_ah.ui.morph_menu.button.remain_to_unlock", with: [chickenScore.toString(), "10"] })()] }, chickenUnlocked ? "textures/aria/ah/items/heads/chicken" : "textures/aria/ah/ui/locked")
        .button({ rawtext: [{ translate: "aria_ah.ui.morph_menu.button.pig"}, (() => pigUnlocked ? {} : { translate: "aria_ah.ui.morph_menu.button.remain_to_unlock", with: [pigScore.toString(), "10"] })()] }, pigUnlocked ? "textures/aria/ah/items/heads/pig" : "textures/aria/ah/ui/locked")
        .button({ rawtext: [{ translate: "aria_ah.ui.morph_menu.button.sheep"}, (() => sheepUnlocked ? {} : { translate: "aria_ah.ui.morph_menu.button.remain_to_unlock", with: [sheepScore.toString(), "10"] })()] }, sheepUnlocked ? "textures/aria/ah/items/heads/sheep" : "textures/aria/ah/ui/locked")
        .show(player).then((res) => {
        if (res.canceled) return

        if (res.selection == 0) {
            player.runCommand(`scriptevent aria_ah:morph unmorph`)
        } else if (player.getComponent(Minecraft.EntityComponentTypes.Inventory).container.emptySlotsCount < 2) {
            player.sendMessage({
                rawtext: [{ translate: "aria_ah.ui.morph_menu.text.more_space_needed" }]
            })
        } else if (res.selection == 1 && cowLocked) {
            player.sendMessage({
                rawtext: [{ translate: "aria_ah.ui.morph_menu.text.defeat_more", with: { rawtext: [ { text: ((cowScore - 10) * -1).toString() }, { translate: "aria_ah.ui.morph_menu.text.defeat_more.cows" }]} }]
            })
            player.playSound("aria_ah.wrong")
        } else if (res.selection == 2 && rabbitLocked) {
            player.sendMessage({
                rawtext: [{ translate: "aria_ah.ui.morph_menu.text.defeat_more", with: { rawtext: [ { text: ((rabbitScore - 10) * -1).toString() }, { translate: "aria_ah.ui.morph_menu.text.defeat_more.rabbits" }]} }]
            })
            player.playSound("aria_ah.wrong")
        } else if (res.selection == 3 && horseLocked) {
            player.sendMessage({
                rawtext: [{ translate: "aria_ah.ui.morph_menu.text.defeat_more", with: { rawtext: [ { text: ((horseScore - 10) * -1).toString() }, { translate: "aria_ah.ui.morph_menu.text.defeat_more.horses" }]} }]
            })
            player.playSound("aria_ah.wrong")
        } else if (res.selection == 4 && foxLocked) {
            player.sendMessage({
                rawtext: [{ translate: "aria_ah.ui.morph_menu.text.defeat_more", with: { rawtext: [ { text: ((foxScore - 10) * -1).toString() }, { translate: "aria_ah.ui.morph_menu.text.defeat_more.foxes" }]} }]
            })
            player.playSound("aria_ah.wrong")
        } else if (res.selection == 5 && wolfLocked) {
            player.sendMessage({
                rawtext: [{ translate: "aria_ah.ui.morph_menu.text.defeat_more", with: { rawtext: [ { text: ((wolfScore - 10) * -1).toString() }, { translate: "aria_ah.ui.morph_menu.text.defeat_more.wolves" }]} }]
            })
            player.playSound("aria_ah.wrong")
        } else if (res.selection == 5 && catLocked) {
            player.sendMessage({
                rawtext: [{ translate: "aria_ah.ui.morph_menu.text.defeat_more", with: { rawtext: [ { text: ((catScore - 10) * -1).toString() }, { translate: "aria_ah.ui.morph_menu.text.defeat_more.cats" }]} }]
            })
            player.playSound("aria_ah.wrong")
        } else if (res.selection == 5 && chickenLocked) {
            player.sendMessage({
                rawtext: [{ translate: "aria_ah.ui.morph_menu.text.defeat_more", with: { rawtext: [ { text: ((chickenScore - 10) * -1).toString() }, { translate: "aria_ah.ui.morph_menu.text.defeat_more.chickens" }]} }]
            })
            player.playSound("aria_ah.wrong")
        } else if (res.selection == 5 && pigLocked) {
            player.sendMessage({
                rawtext: [{ translate: "aria_ah.ui.morph_menu.text.defeat_more", with: { rawtext: [ { text: ((pigScore - 10) * -1).toString() }, { translate: "aria_ah.ui.morph_menu.text.defeat_more.pigs" }]} }]
            })
            player.playSound("aria_ah.wrong")
        } else if (res.selection == 5 && sheepLocked) {
            player.sendMessage({
                rawtext: [{ translate: "aria_ah.ui.morph_menu.text.defeat_more", with: { rawtext: [ { text: ((sheepScore - 10) * -1).toString() }, { translate: "aria_ah.ui.morph_menu.text.defeat_more.sheep" }]} }]
            })
            player.playSound("aria_ah.wrong")
        } else {
            if (res.selection == 1 && cowUnlocked) {
                player.runCommand(`scriptevent aria_ah:morph cow`)
                player.onScreenDisplay.setActionBar({
                    rawtext: [{ translate: "aria_ah.ui.morph_menu.text.morphed", with: { rawtext: [{ translate: "aria_ah.ui.morph_menu.text.cow" }] } }]
                })
                player.runCommand(`playanimation @s "animation.aria_ah.player_invisible" a 1`)
            } else if (res.selection == 2 && rabbitUnlocked) {
                player.runCommand(`scriptevent aria_ah:morph rabbit`)
                player.onScreenDisplay.setActionBar({
                    rawtext: [{ translate: "aria_ah.ui.morph_menu.text.morphed", with: { rawtext: [{ translate: "aria_ah.ui.morph_menu.text.rabbit" }] } }]
                })
                player.runCommand(`playanimation @s "animation.aria_ah.player_invisible" a 1`)
            } else if (res.selection == 3 && horseUnlocked) {
                player.runCommand(`scriptevent aria_ah:morph horse`)
                player.onScreenDisplay.setActionBar({
                    rawtext: [{ translate: "aria_ah.ui.morph_menu.text.morphed", with: { rawtext: [{ translate: "aria_ah.ui.morph_menu.text.horse" }] } }]
                })
                player.runCommand(`playanimation @s "animation.aria_ah.player_invisible" a 1`)
            } else if (res.selection == 4 && foxUnlocked) {
                player.runCommand(`scriptevent aria_ah:morph fox`)
                player.onScreenDisplay.setActionBar({
                    rawtext: [{ translate: "aria_ah.ui.morph_menu.text.morphed", with: { rawtext: [{ translate: "aria_ah.ui.morph_menu.text.fox" }] } }]
                })
                player.runCommand(`playanimation @s "animation.aria_ah.player_invisible" a 1`)
            } else if (res.selection == 5 && wolfUnlocked) {
                player.runCommand(`scriptevent aria_ah:morph wolf`)
                player.onScreenDisplay.setActionBar({
                    rawtext: [{ translate: "aria_ah.ui.morph_menu.text.morphed", with: { rawtext: [{ translate: "aria_ah.ui.morph_menu.text.wolf" }] } }]
                })
                player.runCommand(`playanimation @s "animation.aria_ah.player_invisible" a 1`)
            } else if (res.selection == 6 && catUnlocked) {
                player.runCommand(`scriptevent aria_ah:morph cat`)
                player.onScreenDisplay.setActionBar({
                    rawtext: [{ translate: "aria_ah.ui.morph_menu.text.morphed", with: { rawtext: [{ translate: "aria_ah.ui.morph_menu.text.cat" }] } }]
                })
                player.runCommand(`playanimation @s "animation.aria_ah.player_invisible" a 1`)
            } else if (res.selection == 7 && chickenUnlocked) {
                player.runCommand(`scriptevent aria_ah:morph chicken`)
                player.onScreenDisplay.setActionBar({
                    rawtext: [{ translate: "aria_ah.ui.morph_menu.text.morphed", with: { rawtext: [{ translate: "aria_ah.ui.morph_menu.text.chicken" }] } }]
                })
                player.runCommand(`playanimation @s "animation.aria_ah.player_invisible" a 1`)
            } else if (res.selection == 8 && pigUnlocked) {
                player.runCommand(`scriptevent aria_ah:morph pig`)
                player.onScreenDisplay.setActionBar({
                    rawtext: [{ translate: "aria_ah.ui.morph_menu.text.morphed", with: { rawtext: [{ translate: "aria_ah.ui.morph_menu.text.pig" }] } }]
                })
                player.runCommand(`playanimation @s "animation.aria_ah.player_invisible" a 1`)
            } else if (res.selection == 9 && sheepUnlocked) {
                player.runCommand(`scriptevent aria_ah:morph sheep`)
                player.onScreenDisplay.setActionBar({
                    rawtext: [{ translate: "aria_ah.ui.morph_menu.text.morphed", with: { rawtext: [{ translate: "aria_ah.ui.morph_menu.text.sheep" }] } }]
                })
                player.runCommand(`playanimation @s "animation.aria_ah.player_invisible" a 1`)
            }
        }
    })
}
