package com.tiedup.remake.items;

import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.tiedup.remake.core.SettingsAccessor;
import com.tiedup.remake.core.ModSounds;
import java.util.UUID;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;

/**
 * Taser - Kidnapper's defensive weapon
 *
 * Used by kidnappers when attacked while holding a captive.
 * On hit:
 * - Plays electric shock sound
 * - Applies Slowness I + Weakness I for 5 seconds
 * - Deals 3 hearts of damage
 */
public class ItemTaser extends Item {

    /** UUID for the attack damage modifier */
    private static final UUID ATTACK_DAMAGE_UUID = UUID.fromString(
        "CB3F55D3-645C-4F38-A497-9C13A33DB5CF"
    );

    private final Multimap<Attribute, AttributeModifier> defaultModifiers;

    public ItemTaser() {
        super(new Item.Properties().stacksTo(1).durability(64));
        // Build attribute modifiers for attack damage
        // NOTE: Using default value 5.0 (ModConfig not loaded yet during item registration)
        ImmutableMultimap.Builder<Attribute, AttributeModifier> builder =
            ImmutableMultimap.builder();
        builder.put(
            Attributes.ATTACK_DAMAGE,
            new AttributeModifier(
                ATTACK_DAMAGE_UUID,
                "Weapon modifier",
                5.0, // Default damage (matches ModConfig default)
                AttributeModifier.Operation.ADDITION
            )
        );
        this.defaultModifiers = builder.build();
    }

    /**
     * Called when this item is used to attack an entity.
     * Applies shock effects on successful hit.
     */
    @Override
    public boolean hurtEnemy(
        ItemStack stack,
        LivingEntity target,
        LivingEntity attacker
    ) {
        // Play electric shock sound
        target
            .level()
            .playSound(
                null,
                target.blockPosition(),
                ModSounds.ELECTRIC_SHOCK.get(),
                SoundSource.HOSTILE,
                1.0f,
                1.0f
            );

        int duration = SettingsAccessor.getTaserStunDuration();

        // Apply Slowness I
        target.addEffect(
            new MobEffectInstance(
                MobEffects.MOVEMENT_SLOWDOWN,
                duration,
                0 // Amplifier 0 = level I
            )
        );

        // Apply Weakness I
        target.addEffect(
            new MobEffectInstance(
                MobEffects.WEAKNESS,
                duration,
                0 // Amplifier 0 = level I
            )
        );

        // Consume durability
        stack.hurtAndBreak(1, attacker, e ->
            e.broadcastBreakEvent(EquipmentSlot.MAINHAND)
        );

        return true;
    }

    /**
     * Get attribute modifiers for this item when equipped.
     */
    @Override
    public Multimap<Attribute, AttributeModifier> getDefaultAttributeModifiers(
        EquipmentSlot slot
    ) {
        return slot == EquipmentSlot.MAINHAND
            ? this.defaultModifiers
            : super.getDefaultAttributeModifiers(slot);
    }
}
