package pw.hamzantal.vagueenchants.commands

import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.command.CommandSender
import pw.hamzantal.vagueenchants.enchantments.CustomEnchant

class DelEnch : Cmd {

	override val firstArg = "delench"
	override val usage = "/ve delench [player] [enchantment]"
	override val minArgs = 3
	override val description = "&7Remove an enchantment to the tool you are holding for target player"

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

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

		val target = Bukkit.getPlayer(targetName) ?: run {
			sender.err("target_not_found", "player" to targetName)
			return
		}
		val ench = enchName.ench(sender) ?: return

		val item = target.inventory.itemInMainHand
		if (item == null || item.type == Material.AIR) {
			sender.err("no_item_held")
			return
		}

		val success = if (ench is CustomEnchant) !ench.customEnchant(item, -1)
		else item.enchantments[ench]?: 0 == item.removeEnchantment(ench)

		if (success) {
			sender.success("&aEnchantment Successfully removed")
			target.success("delench_receiver", "ench" to ench.name.toLowerCase())
		} else sender.err("general_error")
	}
}