package com.tiedup.remake.compat.mca.dialogue;

import com.tiedup.remake.compat.mca.personality.MCAMoodManager;
import com.tiedup.remake.compat.mca.personality.MCAPersonality;
import com.tiedup.remake.compat.mca.personality.MCAPersonalityManager;
import com.tiedup.remake.compat.mca.personality.TiedUpTrait;
import java.util.Random;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;

/**
 * Personality-aware dialogue system for MCA villagers.
 *
 * <p>Dialogue selection considers:
 * <ul>
 *   <li>Personality type (affects tone and content)</li>
 *   <li>Current mood (affects positivity/negativity)</li>
 *   <li>TiedUp trait (MASO has different reactions)</li>
 *   <li>Bondage state (tied, gagged, collared)</li>
 * </ul>
 */
public class MCADialogueManager {

    private static final Random RANDOM = new Random();

    // BEING TIED DIALOGUES

    /**
     * Get dialogue when being tied up.
     */
    public static String getBeingTiedDialogue(LivingEntity villager) {
        MCAPersonality personality =
            MCAPersonalityManager.getInstance().getPersonality(villager);
        TiedUpTrait trait = MCAPersonalityManager.getInstance().getTrait(
            villager
        );

        // MASO trait overrides personality
        if (trait == TiedUpTrait.MASO) {
            return pickRandom(
                "Oh yes... tie me up~",
                "Mmm, tighter please...",
                "I've been waiting for this~",
                "Don't stop..."
            );
        }

        // BROKEN trait
        if (trait == TiedUpTrait.BROKEN) {
            return pickRandom("...", "*doesn't resist*", "*stares blankly*");
        }

        // REBELLIOUS trait
        if (trait == TiedUpTrait.REBELLIOUS) {
            return pickRandom(
                "GET YOUR HANDS OFF ME!",
                "I'LL NEVER SUBMIT!",
                "You'll pay for this!",
                "*fights back violently*"
            );
        }

        return switch (personality) {
            case CONFIDENT -> pickRandom(
                "You'll regret this!",
                "I won't submit to you!",
                "This won't hold me!",
                "You're making a mistake!"
            );
            case ATHLETIC -> pickRandom(
                "You won't hold me for long!",
                "I'll break free from this!",
                "These bonds can't stop me!",
                "*struggles powerfully*"
            );
            case SHY -> pickRandom(
                "P-please... don't hurt me...",
                "*whimpers quietly*",
                "No... please no...",
                "*trembles*"
            );
            case SENSITIVE -> pickRandom(
                "*sobs*",
                "Why... why are you doing this?",
                "Please... I'm scared...",
                "*cries*"
            );
            case FRIENDLY -> pickRandom(
                "Why are you doing this?",
                "This isn't necessary...",
                "Can't we talk about this?",
                "I thought we were friends..."
            );
            case FLIRTY -> pickRandom(
                "Oh my, how forward of you~",
                "I didn't say stop...",
                "Mmm, tighter please~",
                "If you wanted me tied up, you could have just asked..."
            );
            case GRUMPY -> pickRandom(
                "Ugh, not this again.",
                "Of course this is happening.",
                "Just my luck...",
                "Great. Just great."
            );
            case LAZY -> pickRandom(
                "*sighs* Fine...",
                "This better not take long...",
                "Whatever...",
                "At least I don't have to work..."
            );
            case ODD -> pickRandom(
                "The ropes smell like Tuesday.",
                "My left elbow predicts rain.",
                "Interesting knot technique.",
                "Did you know butterflies can't fly in the rain?"
            );
            case GREEDY -> pickRandom(
                "I'll pay you to stop!",
                "How much to let me go?",
                "Name your price!",
                "This is bad for business..."
            );
            case PEPPY -> pickRandom(
                "Hey! That's not nice!",
                "Ow ow ow! Too tight!",
                "This is NOT fun!",
                "*squirms energetically*"
            );
            case WITTY -> pickRandom(
                "Well, this is a bind.",
                "I see you've taken things into your own hands.",
                "Tied up with work, I see.",
                "Quite the knotty situation."
            );
            case GLOOMY -> pickRandom(
                "I knew this would happen eventually...",
                "My life just gets worse...",
                "*sighs deeply*",
                "Why do I even bother..."
            );
            default -> pickRandom(
                "Help! Someone help me!",
                "No! Let me go!",
                "Please... stop...",
                "*struggles*"
            );
        };
    }

    // TIED IDLE DIALOGUES

    /**
     * Get dialogue for idle state while tied.
     * Affected by current mood.
     */
    public static String getTiedIdleDialogue(LivingEntity villager) {
        MCAPersonality personality =
            MCAPersonalityManager.getInstance().getPersonality(villager);
        TiedUpTrait trait = MCAPersonalityManager.getInstance().getTrait(
            villager
        );
        int mood = MCAMoodManager.getInstance().getMoodValue(villager);

        // MASO enjoying it
        if (trait == TiedUpTrait.MASO) {
            return pickRandom(
                "*content sigh*",
                "This is nice...",
                "*relaxes into the bonds*",
                "Mmm..."
            );
        }

        // BROKEN - no reaction
        if (trait == TiedUpTrait.BROKEN) {
            return pickRandom("...", "*stares*", "*empty eyes*");
        }

        // Very low mood - desperate
        if (mood < -5) {
            return pickRandom(
                "*sobs quietly*",
                "Someone... please...",
                "Why is this happening to me...",
                "*whimpers*"
            );
        }

        return switch (personality) {
            case CONFIDENT -> pickRandom(
                "*struggles against the bonds*",
                "I will get out of this.",
                "*glares defiantly*",
                "You won't break me."
            );
            case SHY -> pickRandom(
                "*trembles*",
                "*looks around nervously*",
                "*stays very quiet*",
                "*avoids eye contact*"
            );
            case LAZY -> pickRandom(
                "*yawns*",
                "At least I don't have to work...",
                "*naps*",
                "Wake me when this is over..."
            );
            case FLIRTY -> pickRandom(
                "*winks*",
                "See something you like?",
                "*poses suggestively*",
                "Like what you see?"
            );
            case ODD -> pickRandom(
                "*hums tunelessly*",
                "I wonder what clouds taste like...",
                "*counts ceiling tiles*",
                "The floor has a nice texture."
            );
            default -> pickRandom(
                "*struggles*",
                "*looks around for help*",
                "*tests the bonds*",
                "*sighs*"
            );
        };
    }

    // STRUGGLE DIALOGUES

    /**
     * Get dialogue for struggling.
     */
    public static String getStruggleDialogue(
        LivingEntity villager,
        boolean success
    ) {
        MCAPersonality personality =
            MCAPersonalityManager.getInstance().getPersonality(villager);
        TiedUpTrait trait = MCAPersonalityManager.getInstance().getTrait(
            villager
        );

        if (success) {
            // MASO disappointed to be free
            if (trait == TiedUpTrait.MASO) {
                return pickRandom(
                    "Oh... it's over already?",
                    "Aww...",
                    "*slightly disappointed*"
                );
            }

            return switch (personality) {
                case CONFIDENT -> "I knew I'd break free!";
                case ATHLETIC -> "Finally! I'm free!";
                case SHY -> "I-I did it...";
                case FLIRTY -> "That was... intense~";
                default -> "I'm free!";
            };
        } else {
            // MASO happy to fail
            if (trait == TiedUpTrait.MASO) {
                return pickRandom("Good... still tied~", "*secretly pleased*");
            }

            // REBELLIOUS trait: never give up
            if (trait == TiedUpTrait.REBELLIOUS) {
                return pickRandom(
                    "I'LL NEVER STOP TRYING!",
                    "YOU CAN'T BREAK ME!",
                    "*struggles violently*"
                );
            }

            return switch (personality) {
                case CONFIDENT -> "These bonds are tougher than I thought...";
                case ATHLETIC -> "*pants* Need to try harder...";
                case LAZY -> "Eh, not worth the effort...";
                default -> pickRandom(
                    "*struggles futilely*",
                    "It's too tight...",
                    "I can't break free..."
                );
            };
        }
    }

    // FREED DIALOGUES

    /**
     * Get dialogue for being freed.
     */
    public static String getFreedDialogue(LivingEntity villager) {
        MCAPersonality personality =
            MCAPersonalityManager.getInstance().getPersonality(villager);
        TiedUpTrait trait = MCAPersonalityManager.getInstance().getTrait(
            villager
        );

        // MASO disappointed
        if (trait == TiedUpTrait.MASO) {
            return pickRandom(
                "Aww, already?",
                "That was... fun~",
                "Maybe again sometime?",
                "*reluctantly stands up*"
            );
        }

        // BROKEN - minimal reaction
        if (trait == TiedUpTrait.BROKEN) {
            return pickRandom("...thank you.", "*nods slowly*", "...");
        }

        return switch (personality) {
            case FRIENDLY -> "Thank you so much! You saved me!";
            case SHY -> "T-thank you... *sniffles*";
            case CONFIDENT -> "About time. I was about to break free anyway.";
            case GRUMPY -> "Hmph. Took you long enough.";
            case FLIRTY -> "My hero~ How can I ever repay you?";
            case LAZY -> "Finally... that was exhausting.";
            case GREEDY -> "I'll remember this! Here's something for your trouble...";
            case SENSITIVE -> "*hugs you* Thank you so much!";
            default -> "Thank you for freeing me!";
        };
    }

    // COLLAR DIALOGUES

    /**
     * Get dialogue for collar being put on.
     */
    public static String getCollarPutOnDialogue(LivingEntity villager) {
        MCAPersonality personality =
            MCAPersonalityManager.getInstance().getPersonality(villager);
        TiedUpTrait trait = MCAPersonalityManager.getInstance().getTrait(
            villager
        );

        // MASO loves it
        if (trait == TiedUpTrait.MASO) {
            return pickRandom(
                "Yes... I'm yours now~",
                "*happy shiver*",
                "Finally... a collar~",
                "Mark me as yours..."
            );
        }

        // BROKEN accepts it
        if (trait == TiedUpTrait.BROKEN) {
            return pickRandom("*accepts silently*", "...", "*nods*");
        }

        return switch (personality) {
            case CONFIDENT -> "You think a collar makes me yours? Think again.";
            case SHY -> "*whimpers* N-no... please not that...";
            case GREEDY -> "Is there something in it for me at least?";
            case FLIRTY -> "A collar? How... possessive of you~";
            case GRUMPY -> "A collar? Really? How degrading.";
            case LAZY -> "Ugh, this is uncomfortable...";
            default -> "No... not a collar...";
        };
    }

    // BROADCAST HELPER

    /**
     * Broadcast a dialogue message from a villager to nearby players.
     *
     * @param villager The villager speaking
     * @param message The dialogue text
     * @param radius Radius in blocks
     */
    public static void broadcastDialogue(
        LivingEntity villager,
        String message,
        double radius
    ) {
        if (villager.level().isClientSide()) return;
        if (message == null || message.isEmpty()) return;

        String villagerName = villager.getName().getString();
        Component chatMessage = Component.literal(
            "<" + villagerName + "> " + message
        );

        villager
            .level()
            .getEntitiesOfClass(
                Player.class,
                villager.getBoundingBox().inflate(radius),
                player -> true
            )
            .forEach(player -> {
                player.sendSystemMessage(chatMessage);
            });
    }

    // UTILITY

    private static String pickRandom(String... options) {
        return options[RANDOM.nextInt(options.length)];
    }
}
