/*
 *  Copyright 2025-2026 R3944Realms
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package top.r3944realms.eroticdungeongame.content.enchantment;

import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentCategory;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.content.item.WhipItem;

public class CarrotAndStickEnchantment extends Enchantment {
    public CarrotAndStickEnchantment(Rarity rarity, EnchantmentCategory category, EquipmentSlot[] applicableSlots) {
        super(rarity, category, applicableSlots);
    }

    public static void hurt(@NotNull LivingEntity target, @NotNull Entity attacker, int level) {
        // 使用公式计算所有效果

        // 1. 直接治疗公式：基础0.5 + 每级0.5
        float baseHeal = 0.5F;
        float perLevelHeal = 0.5F;
        float totalHeal = baseHeal + (perLevelHeal * level);
        target.heal(totalHeal);

        // 2. 再生效果公式
        // 持续时间 = 基础20ticks + 每级20ticks
        int baseRegenTime = 20;
        int perLevelRegenTime = 20;
        int regenDuration = baseRegenTime + (perLevelRegenTime * level);

        // 效果等级 = (level - 1) / 2，向上取整
        int regenAmplifier = (int) Math.ceil((level - 1) / 2.0);
        regenAmplifier = Math.min(regenAmplifier, 2); // 最高II级

        target.addEffect(new MobEffectInstance(
                MobEffects.REGENERATION,
                regenDuration,
                regenAmplifier
        ));

        // 3. 额外效果（基于等级解锁）
        if (level >= 2) {
            // 2级解锁：伤害吸收
            int absorptionDuration = 20 * (level - 1); // 1-2秒
            target.addEffect(new MobEffectInstance(
                    MobEffects.ABSORPTION,
                    absorptionDuration,
                    0
            ));
        }

        if (level >= 3) {
            // 3级解锁：生命恢复和抗性
            target.addEffect(new MobEffectInstance(
                    MobEffects.HEAL,
                    1,
                    level - 3 // 3级=0，4级=1（如果有更高等级）
            ));

            target.addEffect(new MobEffectInstance(
                    MobEffects.DAMAGE_RESISTANCE,
                    40 * level, // 2-?秒
                    0
            ));
        }

        // 4. 特殊效果概率
        float specialEffectChance = 0.1F * level; // 10%-30%几率
        if (target.level().random.nextFloat() < specialEffectChance) {
            // 清除所有负面效果
            java.util.Collection<MobEffectInstance> effects = target.getActiveEffects();
            for (MobEffectInstance effect : effects) {
                if (!effect.getEffect().isBeneficial()) {
                    target.removeEffect(effect.getEffect());
                }
            }

            // 给予幸运效果
            target.addEffect(new MobEffectInstance(
                    MobEffects.LUCK,
                    100, // 5秒
                    0
            ));
        }
    }

    @Override
    public int getMaxLevel() {
        return 5;
    }

    @Override
    public boolean canEnchant(@NotNull ItemStack stack) {
        return stack.getItem() instanceof WhipItem;
    }
}
