package com.tiedup.remake.entities.kidnapper.components;

import com.tiedup.remake.dialogue.IDialogueSpeaker;
import com.tiedup.remake.dialogue.SpeakerType;
import com.tiedup.remake.entities.KidnapperTheme;
import com.tiedup.remake.entities.ai.kidnapper.KidnapperState;
import com.tiedup.remake.personality.PersonalityType;
import com.tiedup.remake.state.IRestrainable;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import org.jetbrains.annotations.Nullable;

/**
 * KidnapperDialogue - Implements IDialogueSpeaker methods for kidnapper entities.
 *
 * Handles:
 * 1. **Speaker Identity** - Name, type, personality mapping from theme
 * 2. **Mood Calculation** - State-based mood for dialogue selection
 * 3. **Target Relation** - Captor/captive relationship detection
 * 4. **Cooldown Tracking** - Dialogue spam prevention
 *
 * <p><b>Low complexity</b> - Pure state queries with no side effects.</p>
 */
public class KidnapperDialogue {

    // FIELDS

    /** Host callbacks */
    private final IDialogueHost host;

    /** Dialogue cooldown timer (ticks remaining before next dialogue) */
    private int dialogueCooldown = 0;

    // CONSTRUCTOR

    public KidnapperDialogue(IDialogueHost host) {
        this.host = host;
    }

    // IDIALOGUE SPEAKER METHODS

    public String getDialogueName() {
        return host.getNpcName();
    }

    public SpeakerType getSpeakerType() {
        return SpeakerType.KIDNAPPER;
    }

    @Nullable
    public PersonalityType getSpeakerPersonality() {
        // Map kidnapper theme to a personality-like behavior
        KidnapperTheme theme = host.getTheme();
        if (theme == null) {
            return PersonalityType.CALM;
        }

        return switch (theme) {
            case ROPE, SHIBARI -> PersonalityType.CALM; // Traditional, methodical
            case TAPE, LEATHER, CHAIN -> PersonalityType.FIERCE; // Rough, aggressive
            case MEDICAL, ASYLUM -> PersonalityType.PROUD; // Clinical, professional
            case LATEX, RIBBON -> PersonalityType.PLAYFUL; // Playful, teasing
            case BEAM, WRAP -> PersonalityType.CURIOUS; // Experimental, modern
        };
    }

    public int getSpeakerMood() {
        // Kidnappers mood is based on:
        // - Having a captive (+20)
        // - Current state (varies)
        int mood = 50;

        if (host.hasCaptives()) {
            mood += 20;
        }

        // State-based adjustment
        KidnapperState state = host.getCurrentState();
        if (state != null) {
            mood += switch (state) {
                case SELLING -> 10; // Excited about sale
                case JOB_WATCH -> 5;
                case GUARD -> 0;
                case CAPTURE -> 15; // Hunting excitement
                case PUNISH -> -10; // Stern
                case PATROL, IDLE, HUNT -> 0; // Neutral for patrolling/hunting
                case ALERT -> -5; // Concerned
                case TRANSPORT -> 5;
            };
        }

        return Math.max(0, Math.min(100, mood));
    }

    @Nullable
    public String getTargetRelation(Player player) {
        // Check if this kidnapper is holding the player captive
        IRestrainable captive = host.getCaptive();
        if (captive != null && captive.asLivingEntity() == player) {
            return "captor";
        }

        return null;
    }

    public LivingEntity asEntity() {
        return host.asEntity();
    }

    // COOLDOWN

    public int getDialogueCooldown() {
        return this.dialogueCooldown;
    }

    public void setDialogueCooldown(int ticks) {
        this.dialogueCooldown = ticks;
    }

    /**
     * Tick the dialogue cooldown.
     * Called from the main tick method.
     */
    public void tickDialogueCooldown() {
        if (this.dialogueCooldown > 0) {
            this.dialogueCooldown--;
        }
    }
}
