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

import com.tiedup.remake.core.TiedUpMod;
import com.tiedup.remake.entities.ai.kidnapper.KidnapperState;

/**
 * KidnapperStateManager - Manages the behavioral state of a kidnapper.
 *
 * Handles the current KidnapperState enum (IDLE, HUNT, CAPTURE, etc.)
 * with debug logging for state transitions.
 *
 * <p><b>No dependencies</b> - Foundation component.</p>
 */
public class KidnapperStateManager {

    // FIELDS

    /** Host callbacks for logging */
    private final IStateHost host;

    /** Current behavioral state */
    private KidnapperState currentState = KidnapperState.IDLE;

    // CONSTRUCTOR

    public KidnapperStateManager(IStateHost host) {
        this.host = host;
    }

    // STATE MANAGEMENT

    /**
     * Get the current behavioral state.
     *
     * @return The current state
     */
    public KidnapperState getCurrentState() {
        return this.currentState;
    }

    /**
     * Set the current behavioral state with debug logging.
     *
     * @param state The new state
     */
    public void setCurrentState(KidnapperState state) {
        if (this.currentState != state) {
            TiedUpMod.LOGGER.debug(
                "[KidnapperStateManager] {} state change: {} -> {}",
                host.getNpcName(),
                this.currentState,
                state
            );
            this.currentState = state;
        }
    }

    // NBT SERIALIZATION

    /**
     * Get the NBT key for this component.
     *
     * @return The NBT key
     */
    public String getNBTKey() {
        return "CurrentState";
    }

    /**
     * Serialize state to NBT.
     *
     * @return The state name as a string
     */
    public String serializeState() {
        return currentState.name();
    }

    /**
     * Deserialize state from NBT.
     *
     * @param stateName The state name string
     */
    public void deserializeState(String stateName) {
        try {
            this.currentState = KidnapperState.valueOf(stateName);
        } catch (IllegalArgumentException e) {
            TiedUpMod.LOGGER.warn(
                "[KidnapperStateManager] Invalid state name: {}, defaulting to IDLE",
                stateName
            );
            this.currentState = KidnapperState.IDLE;
        }
    }
}
