/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2026 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */
package net.ccbluex.liquidbounce.features.module.modules.player.autoshop

import net.ccbluex.liquidbounce.utils.kotlin.contains
import net.minecraft.client.gui.screens.inventory.ContainerScreen
import net.minecraft.core.registries.BuiltInRegistries
import net.minecraft.world.item.BlockItem
import net.minecraft.world.item.Item
import net.minecraft.world.item.Items

const val WOOL_ID           = "wool"
const val TERRACOTTA_ID     = "terracotta"
const val STAINED_GLASS_ID  = "stained_glass"
const val CONCRETE_ID       = "concrete"
const val POTION_PREFIX     = "potion:"     //usage example: potion:speed
const val EXPERIENCE_ID     = "experience"
const val TIER_ID           = ":tier:"      //usage example: sword:tier:2

/**
 * The items usually used to buy other items in BedWars.
 *
 * A server will take them from the player if the latter wants to buy something.
 */
@JvmField
val LIMITED_ITEMS: Set<String> = hashSetOf(
    "brick", "copper_ingot", "iron_ingot", "gold_ingot", "diamond", "emerald", EXPERIENCE_ID
)

fun Item.isStainedGlass() : Boolean {
    return this is BlockItem && this in Items.STAINED_GLASS
}

fun Item.isConcrete() : Boolean {
    return this is BlockItem && this in Items.CONCRETE
}

fun String.isArmorItem() : Boolean {
    // example: armor:tier:3 -> diamond_boots:protection:2 -> diamond_boots
    if (this.isItemWithTiers()) {
        val actualTierItem = actualTierItem(this)
        return actualTierItem.substringBefore(':') in ARMOR_ITEMS
    }

    return this.substringBefore(':') in ARMOR_ITEMS
}

fun ContainerScreen.stacks(): List<String> {
    return this.menu.slots
        .filter {
            !it.item.isEmpty &&
            it.container === this.menu.container
        }
        .mapNotNull { BuiltInRegistries.ITEM.getKey(it.item.item).path }
}

/**
 * Some BedWars implementations don't give players armor straight after a purchase.
 * The players receive it after a shop gets closed.
 */
private val ARMOR_ITEMS: Set<String> = arrayOf(
    Items.LEATHER_HELMET,
    Items.CHAINMAIL_HELMET,
    Items.COPPER_HELMET,
    Items.IRON_HELMET,
    Items.DIAMOND_HELMET,
    Items.NETHERITE_HELMET,

    Items.LEATHER_CHESTPLATE,
    Items.CHAINMAIL_CHESTPLATE,
    Items.COPPER_CHESTPLATE,
    Items.IRON_CHESTPLATE,
    Items.DIAMOND_CHESTPLATE,
    Items.NETHERITE_CHESTPLATE,

    Items.LEATHER_LEGGINGS,
    Items.CHAINMAIL_LEGGINGS,
    Items.COPPER_LEGGINGS,
    Items.IRON_LEGGINGS,
    Items.DIAMOND_LEGGINGS,
    Items.NETHERITE_LEGGINGS,

    Items.LEATHER_BOOTS,
    Items.CHAINMAIL_BOOTS,
    Items.COPPER_BOOTS,
    Items.IRON_BOOTS,
    Items.DIAMOND_BOOTS,
    Items.NETHERITE_BOOTS
).mapTo(hashSetOf()) { BuiltInRegistries.ITEM.getKey(it).path }
