package pw.hamzantal.vagueenchants.commands

import hazae41.minecraft.kutils.bukkit.msg
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import pw.hamzantal.vagueenchants.enchantments.CustomEnchant

class Info : Cmd {

	override val firstArg = "info"
	override val usage = "/ve info <enchantment>"
	override val minArgs = 1
	override val description = "&7See enchantment info for the current item you are holding"

	override val requirePermission = false
	override val permission = ""
	override val requirePlayer = true

	override fun execute(sender: CommandSender, args: Array<String>) {
		val p = sender as Player


		val item = p.inventory.itemInMainHand ?: run {
			p.err("You are not holding any item. Please hold the item you would like to inspect.")
			return
		}

		val enchs = item.enchantments.toMutableMap()
		if (enchs.isEmpty()) {
			p.err("Your item does not have any enchantments.")
			return
		}

		if (args.size > 1) {
			val ench = args.component2().ench(p) ?: return
			val level = enchs[ench] ?: run {
				p.err("That enchantment is not currently applied on your tool.")
				return
			}
			enchs.clear()
			enchs[ench] = level
		}

		p.msg("&7&m------------------")
		enchs.forEach { (e, l) ->
			p.msg("&7Name: ${e.name}")
			if (e is CustomEnchant) e.inspect(p, l)
			else p.msg("&7Current level: $l")
			p.msg("&7&m------------------")
		}
	}
}