package pw.hamzantal.vagueenchants.commands

import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import pw.hamzantal.vagueenchants.getTokens
import pw.hamzantal.vagueenchants.setTokens
import pw.hamzantal.vagueenchants.uuid

class DelTokens : Cmd {
	override val firstArg = "deltokens"
	override val usage = "/ve deltokens [player] [amount]"
	override val minArgs = 3
	override val description = "&7Remove tokens from a player"

	override val requirePermission = true
	override val permission = "ve.admin"
	override val requirePlayer = false

	override fun execute(sender: CommandSender, args: Array<String>) {
		val (_, targetName, amt) = args

		val target = Bukkit.getOfflinePlayer(targetName)

		val amount = (amt.toLongOrNull() ?: 0)
		if (amount < 0) {
			sender.err("amount_below_zero")
			return
		}
		val tokens = getTokens(target.uuid)
		if (tokens - amount < 0) setTokens(target.uuid, 0)
		else setTokens(target.uuid, tokens - amount)

		val newTokens = getTokens(target.uuid)
		sender.success("&a${target.name} now has $newTokens tokens.")
		if (target.isOnline)
			target.player.success("deltoken_receiver")
	}
}