// Handles commands

ServerEvents.commandRegistry(event => {
    const { commands: Commands, arguments: Arguments } = event

    event.register(Commands.literal('book')
        .executes(c => book(c.source.player)))
    event.register(Commands.literal('wiki')
        .executes(c => wiki(c.source.player)))
})


// Gives player book
// Checks if the player has the advancement for finding the stronghold
let book = (player) => {
    if (player.isAdvancementDone('minecraft:story/follow_ender_eye') == true){
        console.info('Player has been to stronghold')
        player.give(Item.of('minecraft:written_book', '{author:".̴̡̀͗̀̂̀͐͌͐́̽̏̾̑̋̌",pages:[\'{"text":"Beneath the sunlit pixel sky,\\\\nWhere ancient echoes softly lie.\\\\nA city old, in earth entwined,\\\\nHolds the key, secrets bind."}\',\'{"text":"In caverns deep, where shadows play,\\\\nUnearth the city, come what may.\\\\nFor ancient shards, the earth bestows,\\\\nTo forge a disc, where mystery grows."}\',\'{"text":"Dig below where ores gleam bright,\\\\nA city\\\'s heart, out of sight.\\\\nIn cryptic crafting, shards align,\\\\nA disc emerges, power entwined."}\',\'{"text":"To unlock realms, where dragons soar,\\\\nForge the disc, the portal\\\'s door."}\'],title:"Read Me"}'))
    }
    else {
        console.info('Player has not been to stronghold')
        player.tell('§4ERROR: Advancement: "Eye Spy" is required to execute this command')
    }
    return 1
}

// Give the player the wiki link
let wiki = (player) => {
    let url = "https://betterbeta.wiki.gg/"

    player.tell([Text.aqua('Click here to access the wiki!').hover('Click to open').click(url)])

    return 1
}


// Send a message when the player first joins, tell them about the wiki
PlayerEvents.loggedIn(event => {
    if (!event.hasGameStage('JoinMessage')) {
        event.addGameStage('JoinMessage')

        setTimeout(() => {
            event.player.tell([Text.red("Welcome to Better Beta! If you need help do /wiki for a link to the wiki!")])
        }, 1000)
    }
})