package net.unicroak.verdirect

import org.bukkit.ChatColor
import org.bukkit.Material
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.PlayerInventory

class PickupTool(val material: Material,
                 val data: Short,
                 var name: String,
                 var lore: MutableList<String>) {

    init {
        this.name = ChatColor.translateAlternateColorCodes('&', name)
        lore.forEachIndexed { i, str -> this.lore[i] = ChatColor.translateAlternateColorCodes('&', lore[i]) }
    }

    override fun equals(other: Any?): Boolean {
        return other is ItemStack
                && equalsItemBase(other)
                && equalsName(other)
                && equalsLore(other)
    }

    fun isContained(inv: PlayerInventory): Boolean = inv.any { equals(it) }

    fun equalsItemBase(item: ItemStack): Boolean = material == item.type && (data.toInt() == -1 || item.durability == data)

    fun equalsName(item: ItemStack): Boolean = name == "ignore" || name == (item.itemMeta.displayName ?: "")

    fun equalsLore(item: ItemStack): Boolean = lore[0] == "ignore" || lore == (item.itemMeta.lore ?: emptyList())
}