import './ls_commands/lifesteal';
import { world, Player, EntityFloatsInLiquidComponent } from 'mojang-minecraft'
import { ActionFormData, ModalFormData } from 'mojang-minecraft-ui'
import { config } from './config'

/**
 * getScore
 * @param {Player} player
 */
 function getScore(player: Player) {
    return world.scoreboard.getObjective('ls-startup').getScore(player.scoreboard)
}

world.events.tick.subscribe(data => {
    for (let player of world.getPlayers()) {
        if (!player.hasTag(`welcome-gui_done`)) {
            if (player.hasTag(`ls-sneak`)) {
                player.runCommand(`tag @s add welcome-gui_done`)
                const welcomeGUI = new ActionFormData()
                welcomeGUI.title(`Welcome!`)
                welcomeGUI.body(`§gWelcome! This world has the §6Lifesteal Addon§g installed!\nThe lifesteal addon was created by §1§lGamerDos§r§g on §fYou§cTube§g, check him out!\n\n§lDo ${config.prefix}help is to start.\n\n§r§lPlease Read:\n§r§7You are not allowed to repost this addon, if you found this addon on a site other than MCPEDL, join the discord (dsc.gg/gamerdos) and report it to GamerDos, if you plan on making a video on the addon, please link the MCPEDL page, you are also not allowed to make your own download link.`)
                welcomeGUI.button(`Agree and close`)
                welcomeGUI.show(player).then(result => {
                    if (result.selection == 0) {
                        player.runCommand(`tellraw @s {"rawtext":[{"text":"§gIf you want to support this addon further, go to www.patreon.com/user?u=55367276"}]}`)
                    }
                })
            }
            if (player.hasTag(`ls-jump`)) {
                player.runCommand(`tag @s add welcome-gui_done`)
                const welcomeGUI = new ActionFormData()
                welcomeGUI.title(`Welcome!`)
                welcomeGUI.body(`§gWelcome! This world has the §6Lifesteal Addon§g installed!\nThe lifesteal addon was created by §1§lGamerDos§r§g on §fYou§cTube§g, check him out!\n\n§lDo ${config.prefix}help is to start.\n\n§r§lPlease Read:\n§r§7You are not allowed to repost this addon, if you found this addon on a site other than MCPEDL, join the discord (dsc.gg/gamerdos) and report it to GamerDos, if you plan on making a video on the addon, please link the MCPEDL page, you are also not allowed to make your own download link.`)
                welcomeGUI.button(`Agree and close`)
                welcomeGUI.show(player).then(result => {
                    if (result.selection == 0) {
                        player.runCommand(`tellraw @s {"rawtext":[{"text":"§gIf you want to support this addon further, go to www.patreon.com/user?u=55367276"}]}`)
                    }
                })
            }
            else {}
        }
        if (player.hasTag(`openadmingui`)) {
            if (player.hasTag(`ls-sneak`)) {
                player.runCommand(`tag @s remove openadmingui`)
                const admingui = new ActionFormData()
                admingui.title(`Admin Panel`)
                admingui.body(`Choose a command:\n `)
                admingui.button(`Set max hearts`)
                admingui.button(`Set own hearts`)
                admingui.button(`Kill or revive`)
                admingui.button(`Reset a player`)
                admingui.button(`Gamemode Switcher`)
                admingui.button(`§7Close`)
                admingui.show(player).then(result => {
                    if (result.selection == 0) {
                        const maxheartsgui = new ModalFormData()
                        maxheartsgui.slider(`Choose the maximum amount of hearts`, 20, 100, 10)
                        maxheartsgui.show(player).then(result => {
                            player.runCommand(`function lifesteal/max-health-${result.formValues[0]}`)
                        })
                    }
                    if (result.selection == 1) {
                        const setheartsgui = new ModalFormData()
                        setheartsgui.slider(`Set your heart count`, 0, 100, 1)
                        setheartsgui.show(player).then(result => {
                            player.runCommand(`scoreboard players set @s ls-death ${result.formValues[0]}`)
                            player.runCommand(`effect @s resistance 30 255 true`)
                            player.runCommand(`gamemode s @s`)
                            player.runCommand(`tag @s[tag=ls-dead] remove ls-dead`)
                            player.runCommand(`scoreboard players set @s ls-death ${result.formValues[0]}`)
                        })
                    }
                    if (result.selection == 2) {
                        const killorrevivegui = new ModalFormData()
                        killorrevivegui.title(`Kill or Revive`)
                        killorrevivegui.toggle(`Kill`, false)
                        killorrevivegui.textField(`Type a player's name to revive or kill\n§7(No need for quotation marks around the name)`, `Type text here`)
                        killorrevivegui.show(player).then(result => {
                            if (result.formValues[0] == true) {
                                try {
                                    player.runCommand(`effect @a[name="${result.formValues[1]}"] saturation 0`)
                                    player.runCommand(`scoreboard players set ${result.formValues[1]} ls-death 0`)
                                    player.runCommand(`tellraw ${result.formValues[1]} {"rawtext":[{"text":"§cAn admin has disintegrated you and you have become a ghost."}]}`)
                                    player.runCommand(`tellraw @s {"rawtext":[{"text":"§bDisintegrated ${result.formValues[1]}"}]}`)
                                }
                                catch {
                                    player.runCommand(`tellraw @s {"rawtext":[{"text":"§cFailed to kill ${result.formValues[1]}"}]}`)
                                }
                            }
                            if (result.formValues[0] == false) {
                                try {
                                    player.runCommand(`tag @a[name="${result.formValues[1]}"] add ls-revive`)
                                    player.runCommand(`tellraw @a {"rawtext":[{"text":"§b${result.formValues[1]} was revived by ${player.nameTag}"}]}`)
                                    player.runCommand(`title ${result.formValues[1]} actionbar §bYou were revived by${player.nameTag}!`)
                                }
                                catch {
                                    player.runCommand(`tellraw @s {"rawtext":[{"text":"§cFailed to revive ${result.formValues[1]}"}]}`)
                                }
                            }
                        })
                    }
                    if (result.selection == 3) {
                        const resetgui = new ModalFormData()
                        resetgui.title(`Reset a player`)
                        resetgui.textField(`Type a player's name to reset back to 10 hearts\n§7(No need for quotation marks around the name)`, `Type text here`)
                        resetgui.show(player).then(result => {
                            try {
                                player.runCommand(`effect @a[name="${result.formValues[0]}"] saturation 0`)
                                player.runCommand(`scoreboard players set ${result.formValues[0]} ls-death 10`)
                                player.runCommand(`tellraw @s {"rawtext":[{"text":"§bResetted ${result.formValues[0]}"}]}`)
                            }
                            catch {
                                player.runCommand(`tellraw @s {"rawtext":[{"text":"§cFailed to reset ${result.formValues[0]}"}]}`)
                            }
                        })
                    }
                    if (result.selection == 4) {
                        const gamemodegui = new ModalFormData()
                        gamemodegui.title(`Gamemode Switcher`)
                        gamemodegui.dropdown(`Choose a gamemode to switch to!`, ["Survival", "Creative", "Adventure", "Spectator"])
                        gamemodegui.show(player).then(result => {
                            if (result.formValues[0] == 0) {
                                player.runCommand(`gamemode survival`)
                            }
                            if (result.formValues[0] == 1) {
                                player.runCommand(`gamemode creative`)
                            }
                            if (result.formValues[0] == 2) {
                                player.runCommand(`gamemode adventure`)
                            }
                            if (result.formValues[0] == 3) {
                                player.runCommand(`gamemode spectator`)
                            }
                        })
                    }
                })
            }
        }
    }
})

const reviveGUI = (player: Player) => {
    const revivegui = new ModalFormData()
    revivegui.title(`Revive a player!`)
    revivegui.textField(`Dont like UIs? Do the ${config.prefix}revive command\n\nType a player's name to revive\n§7(Put quotation marks around the name if the person has a space in their name)`, `Type text here`)
    revivegui.show(player).then(result => {
        try {
            player.runCommand(`tag @a[name="${result.formValues[0]}"] add ls-revive`)
            player.runCommand(`clear @s lifesteal:heart_of_life 0 1`)
            player.runCommand(`tellraw @a {"rawtext":[{"text":"§b${result.formValues[0]} was revived by ${player.nameTag}"}]}`)
            player.runCommand(`title ${result.formValues[0]} actionbar §bYou were revived by${player.nameTag}!`)
        }
        catch {
            player.runCommand(`tellraw @s {"rawtext":[{"text":"§cCouldn't find player §l${result.formValues[0]}§r§c, player must be online and a ghost"}]}`)
        }
    })
}

world.events.beforeItemUse.subscribe(data => {
    if (data.item.id === 'lifesteal:heart_of_life') {
        reviveGUI(data.source as Player)
    }
})

//GamerDos was here :D