package pw.hamzantal.vagueenchants

import org.bukkit.Material
import org.bukkit.Material.*
import org.bukkit.block.Block
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemStack

enum class ToolType(val itemList: List<Material>, val suffix: String) {
    PICKAXE(ToolUtils.pickaxeItems, "PICKAXE"),
    AXE(ToolUtils.axeItems, "AXE"),
    SHOVEL(ToolUtils.shovelItems, "SPADE"),
    OTHER(ToolUtils.pickaxeItems, "")
}

val ItemStack.tooltype: ToolType
    get() = when (this.type) {
        in ToolUtils.pickaxes -> ToolType.PICKAXE
        in ToolUtils.axes -> ToolType.AXE
        in ToolUtils.shovels -> ToolType.SHOVEL
        else -> ToolType.OTHER
    }

object ToolUtils {

    val pickaxes = listOf(DIAMOND_PICKAXE, IRON_PICKAXE, GOLD_PICKAXE, STONE_PICKAXE, WOOD_PICKAXE)

    val axes = listOf(DIAMOND_AXE, IRON_AXE, GOLD_AXE, STONE_AXE, WOOD_AXE)

    val shovels = listOf(DIAMOND_SPADE, IRON_SPADE, GOLD_SPADE, STONE_SPADE, WOOD_SPADE)

    //https://minecraft.gamepedia.com/Pickaxe#Speed
    val pickaxeItems = listOf(
        ANVIL,
        COAL_BLOCK,
        DIAMOND_BLOCK,
        EMERALD_BLOCK,
        GOLD_BLOCK,
        IRON_BLOCK,
        QUARTZ_BLOCK,
        REDSTONE_BLOCK,
        BONE_BLOCK,
        BREWING_STAND,
        BRICK_STAIRS,
        BRICK,
        CAULDRON,
        COAL_ORE,
        COBBLESTONE,
        COBBLESTONE_STAIRS,
        COBBLE_WALL,
        PRISMARINE,
        DIAMOND_ORE,
        DISPENSER,
        DRAGON_EGG,
        EMERALD_ORE,
        ENCHANTMENT_TABLE,
        ENDER_STONE,
        ENDER_CHEST,
        FROSTED_ICE,
        FURNACE,
        GOLD_ORE,
        HOPPER,
        ICE,
        IRON_FENCE,
        IRON_DOOR,
        IRON_ORE,
        IRON_TRAPDOOR,
        LAPIS_BLOCK,
        LAPIS_ORE,
        MAGMA,
        MOB_SPAWNER,
        MOSSY_COBBLESTONE,
        NETHER_FENCE,
        NETHER_BRICK_STAIRS,
        NETHER_BRICK,
        QUARTZ_ORE,
        NETHERRACK,
        OBSIDIAN,
        PACKED_ICE,
        PRISMARINE,
        QUARTZ_STAIRS,
        RAILS,
        RED_NETHER_BRICK,
        RED_SANDSTONE,
        RED_SANDSTONE_STAIRS,
        REDSTONE_ORE,
        SANDSTONE,
        SANDSTONE_STAIRS,
        SMOOTH_BRICK,
        STONE,
        SMOOTH_STAIRS,
        SMOOTH_BRICK,
        STONE_BUTTON,
        STONE_PLATE,
        STONE_SLAB2,
        DOUBLE_STONE_SLAB2,
        STEP,
        DOUBLE_STEP
    )

    //https://minecraft.gamepedia.com/Axe#Speed
    val axeItems = listOf(
        TRAP_DOOR,

        WOOD_DOOR,
        SPRUCE_DOOR,
        BIRCH_DOOR,
        JUNGLE_DOOR,
        ACACIA_DOOR,
        DARK_OAK_DOOR,

        CHEST,
        WORKBENCH,
        TRAPPED_CHEST,

        FENCE,
        SPRUCE_FENCE,
        BIRCH_FENCE,
        JUNGLE_FENCE,
        ACACIA_FENCE,
        DARK_OAK_FENCE,

        FENCE_GATE,
        SPRUCE_FENCE_GATE,
        BIRCH_FENCE_GATE,
        JUNGLE_FENCE_GATE,
        ACACIA_FENCE_GATE,
        DARK_OAK_FENCE_GATE,

        JUKEBOX,

        LOG,
        LOG_2,
        WOOD,
        WOOD_STEP,

        WOOD_STAIRS,
        SPRUCE_WOOD_STAIRS,
        BIRCH_WOOD_STAIRS,
        JUNGLE_WOOD_STAIRS,
        ACACIA_STAIRS,
        DARK_OAK_STAIRS,

        BOOKSHELF,
        BANNER,
        JACK_O_LANTERN,
        MELON,
        PUMPKIN,
        SIGN,
        NOTE_BLOCK,
        WOOD_PLATE,
        LADDER,
        BED,
        COCOA,
        DAYLIGHT_DETECTOR,
        DAYLIGHT_DETECTOR_INVERTED,
        BROWN_MUSHROOM,
        RED_MUSHROOM,
        HUGE_MUSHROOM_1,
        HUGE_MUSHROOM_2,
        VINE
    )

    //https://minecraft.gamepedia.com/Shovel
    val shovelItems = listOf(
        CLAY,
        GRASS,
        GRASS_PATH,
        GRAVEL,
        MYCEL,
        DIRT,
        SAND,
        SOUL_SAND,
        SNOW,
        SNOW_BLOCK
    )

    fun drops(block: Block, item: ItemStack): Iterable<ItemStack> {
        val baseDrops = block.getDrops(item)
        if(baseDrops.isEmpty()) return baseDrops
        val fortune = item.enchantments[Enchantment.LOOT_BONUS_BLOCKS]?: 0
        val silk = item.enchantments[Enchantment.SILK_TOUCH]?: 0
        val bonus = (Math.random() * (fortune + 2) - 1).toInt()

        if (silk > 0) return listOf(ItemStack(block.type))
        if (fortune == 0) return baseDrops
        if(block.type in listOf(LAPIS_ORE, REDSTONE_ORE)) return baseDrops.map {it.apply { it.amount += bonus }}

        return baseDrops.map { it.apply { it.amount *= (bonus + 1) } }
    }
}