package com.tiedup.remake.util;

import com.tiedup.remake.core.ModSounds;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;

/**
 * Sound utility functions for TiedUp mod.
 *
 * Provides convenient methods for playing mod-specific sounds.
 * Works for both Players and NPCs (EntityDamsel, etc.)
 *
 */
public class TiedUpSounds {

    /**
     * Play a lock closing sound at entity's position.
     * Used when locking collars, gags, blindfolds, etc.
     *
     * @param entity The entity at whose position to play the sound
     */
    public static void playLockSound(Entity entity) {
        playSound(entity, ModSounds.COLLAR_KEY_CLOSE.get(), 1.0f);
    }

    /**
     * Play an unlock sound at entity's position.
     * Used when unlocking collars, gags, blindfolds, etc.
     *
     * @param entity The entity at whose position to play the sound
     */
    public static void playUnlockSound(Entity entity) {
        playSound(entity, ModSounds.COLLAR_KEY_OPEN.get(), 1.0f);
    }

    /**
     * Play a shock sound at entity's position.
     * Used when shocking with collar or shocker controller.
     *
     * @param entity The entity being shocked
     */
    public static void playShockSound(Entity entity) {
        playSound(entity, ModSounds.ELECTRIC_SHOCK.get(), 1.0f);
    }

    /**
     * Play a binding sound at entity's position.
     * Used when applying binds/ropes.
     *
     * @param entity The entity being bound
     */
    public static void playBindSound(Entity entity) {
        playSound(entity, ModSounds.CHAIN.get(), 0.8f);
    }

    /**
     * Play a struggle sound at entity's position.
     * Used when struggling against restraints.
     *
     * @param entity The entity struggling
     */
    public static void playStruggleSound(Entity entity) {
        // Use chain sound with slightly higher pitch for struggle effect
        playSound(entity, ModSounds.CHAIN.get(), 0.6f, 1.2f);
    }

    /**
     * Play a collar equip sound at entity's position.
     *
     * @param entity The entity receiving the collar
     */
    public static void playCollarSound(Entity entity) {
        playSound(entity, ModSounds.COLLAR_PUT.get(), 1.0f);
    }

    /**
     * Play a slap sound at entity's position.
     * Used for paddle/discipline items.
     *
     * @param entity The entity being slapped
     */
    public static void playSlapSound(Entity entity) {
        playSound(entity, ModSounds.SLAP.get(), 1.0f);
    }

    /**
     * Play a whip sound at entity's position.
     *
     * @param entity The entity being whipped
     */
    public static void playWhipSound(Entity entity) {
        playSound(entity, ModSounds.WHIP.get(), 1.0f);
    }

    /**
     * Play shocker activation sound.
     *
     * @param entity The entity activating the shocker
     */
    public static void playShockerActivatedSound(Entity entity) {
        playSound(entity, ModSounds.SHOCKER_ACTIVATED.get(), 1.0f);
    }

    /**
     * Play earplugs equip sound at entity's position.
     * Uses a soft variation of the collar sound.
     *
     * @param entity The entity receiving the earplugs
     */
    public static void playEarplugsEquipSound(Entity entity) {
        // Use collar_put with softer volume and higher pitch for earplugs
        playSound(entity, ModSounds.COLLAR_PUT.get(), 0.5f, 1.3f);
    }

    /**
     * Play earplugs remove sound at entity's position.
     *
     * @param entity The entity having earplugs removed
     */
    public static void playEarplugsRemoveSound(Entity entity) {
        // Use collar_put with softer volume and lower pitch for removal
        playSound(entity, ModSounds.COLLAR_PUT.get(), 0.4f, 0.9f);
    }

    /**
     * Play a generic sound at an entity's position.
     *
     * Based on original Utils.playSound()
     *
     * @param entity The entity at whose position to play the sound
     * @param sound The sound event to play
     * @param volume The volume (1.0f = 100%)
     */
    public static void playSound(
        Entity entity,
        SoundEvent sound,
        float volume
    ) {
        if (entity == null || entity.level() == null || sound == null) {
            return;
        }

        Level level = entity.level();

        // Server-side only (will be synced to clients automatically)
        if (!level.isClientSide) {
            level.playSound(
                null, // Player (null = everyone hears it)
                entity.blockPosition(), // Position
                sound, // Sound event
                SoundSource.NEUTRAL, // Sound category (NEUTRAL for mod sounds)
                volume, // Volume
                1.0f // Pitch
            );
        }
    }

    /**
     * Play a sound with custom pitch.
     *
     * @param entity The entity at whose position to play the sound
     * @param sound The sound event to play
     * @param volume The volume (1.0f = 100%)
     * @param pitch The pitch (1.0f = normal, 0.5f = lower, 2.0f = higher)
     */
    public static void playSound(
        Entity entity,
        SoundEvent sound,
        float volume,
        float pitch
    ) {
        if (entity == null || entity.level() == null || sound == null) {
            return;
        }

        Level level = entity.level();

        if (!level.isClientSide) {
            level.playSound(
                null,
                entity.blockPosition(),
                sound,
                SoundSource.NEUTRAL,
                volume,
                pitch
            );
        }
    }
}
