package com.tiedup.remake.personality;

import java.util.Random;

/**
 * Defines the 11 base personality types for NPCs.
 * Each personality affects behavior:
 * - struggleModifier: Multiplier for struggle success chance
 * - flightModifier: Multiplier for flee behavior distance/urgency
 * - baseCompliance: Base compliance rate (kept for dialogue variety)
 * - spawnWeight: Weight for random generation
 */
public enum PersonalityType {
    TIMID(0.7f, 1.5f, 0.75f, 0.15f),
    GENTLE(0.5f, 1.2f, 0.80f, 0.12f),
    SUBMISSIVE(0.3f, 1.0f, 0.90f, 0.05f),
    CALM(1.0f, 1.0f, 0.60f, 0.20f),
    CURIOUS(0.8f, 0.9f, 0.70f, 0.10f),
    PROUD(1.3f, 0.7f, 0.30f, 0.10f),
    FIERCE(1.5f, 0.3f, 0.20f, 0.08f),
    DEFIANT(1.4f, 0.5f, 0.10f, 0.07f),
    PLAYFUL(0.9f, 1.1f, 0.60f, 0.08f),
    MASOCHIST(0.4f, 0.8f, 0.85f, 0.03f),
    SADIST(1.2f, 0.6f, 0.40f, 0.02f);

    /** Multiplier for struggle escape chance */
    public final float struggleModifier;

    /** Multiplier for flight behavior */
    public final float flightModifier;

    /** Base compliance rate (0.0 to 1.0) — for dialogue/RP variety */
    public final float baseCompliance;

    /** Spawn weight for random generation */
    public final float spawnWeight;

    PersonalityType(
        float struggleModifier,
        float flightModifier,
        float baseCompliance,
        float spawnWeight
    ) {
        this.struggleModifier = struggleModifier;
        this.flightModifier = flightModifier;
        this.baseCompliance = baseCompliance;
        this.spawnWeight = spawnWeight;
    }

    /**
     * Calculate struggle timer multiplier.
     * Lower = struggles more often.
     *
     * @return Timer multiplier (0.3 for FIERCE to 4.0 for MASOCHIST)
     */
    public float getStruggleTimerMultiplier() {
        return switch (this) {
            case FIERCE -> 0.3f;
            case DEFIANT -> 0.5f;
            case PROUD -> 0.6f;
            case CALM, CURIOUS, PLAYFUL -> 1.0f;
            case TIMID -> 1.5f;
            case GENTLE -> 2.0f;
            case SUBMISSIVE -> 3.0f;
            case MASOCHIST -> 4.0f;
            case SADIST -> 0.8f;
        };
    }

    // --- Static utility methods ---

    private static final Random RANDOM = new Random();
    private static float totalWeight = -1;

    /**
     * Generate a random personality based on spawn weights.
     *
     * @return Random PersonalityType
     */
    public static PersonalityType randomForDamsel() {
        if (totalWeight < 0) {
            totalWeight = 0;
            for (PersonalityType type : values()) {
                if (type != SADIST) {
                    totalWeight += type.spawnWeight;
                }
            }
        }

        float roll = RANDOM.nextFloat() * totalWeight;
        float cumulative = 0;

        for (PersonalityType type : values()) {
            if (type == SADIST) continue;
            cumulative += type.spawnWeight;
            if (roll < cumulative) {
                return type;
            }
        }

        return CALM; // Fallback
    }

    /**
     * Generate a random personality for kidnappers.
     * Includes SADIST and excludes SUBMISSIVE.
     *
     * @return Random PersonalityType for kidnapper
     */
    public static PersonalityType randomForKidnapper() {
        float roll = RANDOM.nextFloat();

        if (roll < 0.25f) return FIERCE;
        if (roll < 0.45f) return SADIST;
        if (roll < 0.60f) return PROUD;
        if (roll < 0.75f) return DEFIANT;
        if (roll < 0.85f) return CALM;
        if (roll < 0.92f) return PLAYFUL;
        return CURIOUS;
    }
}
