package pw.hamzantal.vagueenchants.commands

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

class AddTokens : Cmd {
	override val firstArg = "addtokens"
	override val usage = "/ve addtokens [player] [amount]"
	override val minArgs = 3
	override val description = "&7Add tokens to a player's account"

	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) ?: run {
			sender.err("target_not_found", "player" to targetName)
			return
		}

		val amount = (amt.toIntOrNull() ?: 0)
		if (amount < 0) {
			sender.err("amount_below_zero")
			return
		}
		val tokens = getTokens(target.uniqueId.toString())
		setTokens(target.uuid, tokens + amount)
		if (sender is Player) sender.success("${target.name} now has ${tokens + amount} tokens.")
	}
}