/*
 * 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.invcleaner.items

import it.unimi.dsi.fastutil.objects.ObjectIntPair
import net.ccbluex.liquidbounce.features.module.modules.player.invcleaner.ItemCategory
import net.ccbluex.liquidbounce.features.module.modules.player.invcleaner.ItemFunction
import net.ccbluex.liquidbounce.features.module.modules.player.invcleaner.ItemType
import net.ccbluex.liquidbounce.utils.inventory.ItemSlot
import net.ccbluex.liquidbounce.utils.item.EnchantmentValueEstimator
import net.ccbluex.liquidbounce.utils.item.asHolderComparator
import net.ccbluex.liquidbounce.utils.item.attackDamage
import net.ccbluex.liquidbounce.utils.item.attackSpeed
import net.ccbluex.liquidbounce.utils.item.getEnchantment
import net.ccbluex.liquidbounce.utils.item.isSword
import net.ccbluex.liquidbounce.utils.sorting.ComparatorChain
import net.ccbluex.liquidbounce.utils.sorting.compareByCondition
import net.minecraft.world.item.enchantment.Enchantments
import kotlin.math.ceil
import kotlin.math.pow

open class WeaponItemFacet(itemSlot: ItemSlot) : ItemFacet(itemSlot) {
    companion object {
        /**
         * Estimates damage for different enchantments. Note that sharpness is already considered by
         * `ItemStack.attackDamage`
         */
        private val DAMAGE_ESTIMATOR =
            EnchantmentValueEstimator(
                EnchantmentValueEstimator.WeightedEnchantment(Enchantments.SMITE, 2.0f * 0.1f),
                EnchantmentValueEstimator.WeightedEnchantment(Enchantments.BANE_OF_ARTHROPODS, 2.0f * 0.1f),
                // Knockback deals no damage, but it allows us to deal more damage because we don't get hit as often.
                EnchantmentValueEstimator.WeightedEnchantment(Enchantments.KNOCKBACK, 0.2f),
            )
        internal val SECONDARY_VALUE_ESTIMATOR =
            EnchantmentValueEstimator(
                EnchantmentValueEstimator.WeightedEnchantment(Enchantments.LOOTING, 0.05f),
                EnchantmentValueEstimator.WeightedEnchantment(Enchantments.UNBREAKING, 0.05f),
                EnchantmentValueEstimator.WeightedEnchantment(Enchantments.MENDING, 0.1f),
                EnchantmentValueEstimator.WeightedEnchantment(Enchantments.VANISHING_CURSE, -0.1f),
                EnchantmentValueEstimator.WeightedEnchantment(Enchantments.SWEEPING_EDGE, 0.2f),
                EnchantmentValueEstimator.WeightedEnchantment(Enchantments.KNOCKBACK, 0.25f),
            )
        private val COMPARATOR =
            ComparatorChain<WeaponItemFacet>(
                Comparator.comparingDouble(::estimateDamage),
                SECONDARY_VALUE_ESTIMATOR.asHolderComparator(),
                compareByCondition { it.itemStack.isSword },
                PREFER_BETTER_DURABILITY,
                PREFER_ENCHANTABLE,
                PREFER_ITEMS_IN_HOTBAR,
                STABILIZE_COMPARISON,
            )

        private fun estimateDamage(o1: WeaponItemFacet): Double {
            // Already contains damage enchantments like sharpness
            val attackDamage = o1.itemStack.attackDamage
            val attackSpeed = o1.itemStack.attackSpeed

            val p = 0.85.pow(1 / 20.0)
            val bigT = 20.0 / attackSpeed

            val probabilityAdjustmentFactor = p.pow(ceil(bigT * 0.9))

            val speedAdjustedDamage = attackDamage * attackSpeed * probabilityAdjustmentFactor.toFloat()

            val damageFromFireAspect = (o1.itemStack.getEnchantment(Enchantments.FIRE_ASPECT) * 4.0f - 1)
                    .coerceAtLeast(0.0F) * 0.33F

            val additionalFactor = DAMAGE_ESTIMATOR.estimateValue(o1.itemStack)

            return speedAdjustedDamage * (1.0 + additionalFactor) + damageFromFireAspect
        }
    }

    override val category: ItemCategory
        get() = ItemType.WEAPON.defaultCategory

    override val providedItemFunctions: List<ObjectIntPair<ItemFunction>>
        get() = listOf(ObjectIntPair.of(ItemFunction.WEAPON_LIKE, 1))

    override fun compareTo(other: ItemFacet): Int {
        return COMPARATOR.compare(this, other as WeaponItemFacet)
    }
}
