package pw.hamzantal.vagueenchants.enchantments

import hazae41.minecraft.kutils.bukkit.listen
import hazae41.minecraft.kutils.bukkit.msg
import org.bukkit.Material.getMaterial
import org.bukkit.entity.Player
import org.bukkit.event.Listener
import org.bukkit.event.block.Action
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.inventory.ItemStack
import pw.hamzantal.vagueenchants.*
import pw.hamzantal.vagueenchants.commands.c

class OmniTool(id: Int) : CustomEnchant(id, Config.omni, "OMNITOOL"), Listener {

    init {
        VagueEnchants.enchants += this
        val pl = VagueEnchants.pl
        pl.listen(callback = this::onInteract)
        pl.registerEnchantment(this)
    }

    private fun onInteract(e: PlayerInteractEvent) {
        if (e.action != Action.LEFT_CLICK_BLOCK || e.item == null || this !in e.item.enchantments) return
        val item = e.item.tooltype
        val block = e.clickedBlock.type

        if (block in item.itemList) return
        when (block) {
            in ToolUtils.pickaxeItems -> transform(e.item, ToolType.PICKAXE)
            in ToolUtils.axeItems -> transform(e.item, ToolType.AXE)
            in ToolUtils.shovelItems -> transform(e.item, ToolType.SHOVEL)
            else -> return
        }
    }

    private fun transform(current: ItemStack, type: ToolType) {
        val strength = current.type.toString().split("_").first()
        val material = getMaterial("${strength}_${type.suffix}")
        current.type = material
    }

    override fun inspect(p: Player, level: Int) {
        p.msg("&7Description: ${config.description}")
        p.msg("&7Current level: $level")
        p.msg("&7Maximum level: ${config.max}")
    }

    override fun loreString(level: Int): String {
        return "&7OmniTool ${loreLevel(level)}".c
    }
}