/**
 * StateManager - Conversation state manager for the final project
 *
 * Part of Part III of the book "Introduction to Programming in Java"
 */

package chatbot;

import java.util.*;

/**
 * Manages conversation state and slot collection.
 */
public class StateManager {

    /**
     * Possible conversation states.
     */
    public enum State {
        IDLE,             // Waiting for a command
        COLLECTING_SLOTS, // Collecting required information
        CONFIRMING,       // Confirming the request
        EXECUTING         // Executing the action
    }

    /**
     * Represents a slot (a field to be filled during the conversation).
     */
    public static class Slot {
        public final String name;
        public final String question;
        public String value;

        public Slot(String name, String question) {
            this.name = name;
            this.question = question;
            this.value = null;
        }

        public boolean isFilled() {
            return value != null && !value.isEmpty();
        }
    }

    private State currentState;
    private IntentClassifier.Intent currentIntent;
    private List<Slot> slots;
    private int currentSlotIndex;
    private Map<String, String> context;

    /**
     * Constructor.
     */
    public StateManager() {
        currentState = State.IDLE;
        slots = new ArrayList<>();
        context = new HashMap<>();
    }

    /**
     * Starts a new multi-turn conversation flow.
     * @param intent The intent of the conversation
     * @param requiredSlots The slots that need to be filled
     */
    public void startConversation(IntentClassifier.Intent intent, List<Slot> requiredSlots) {
        currentIntent = intent;
        currentState = State.COLLECTING_SLOTS;
        slots = new ArrayList<>(requiredSlots);
        currentSlotIndex = 0;
        context.clear();
    }

    /**
     * Ends the current conversation flow and resets state.
     */
    public void endConversation() {
        currentState = State.IDLE;
        currentIntent = null;
        slots.clear();
        currentSlotIndex = 0;
        context.clear();
    }

    /**
     * Returns the current state.
     * @return The current state
     */
    public State getCurrentState() {
        return currentState;
    }

    /**
     * Returns the current intent.
     * @return The current intent
     */
    public IntentClassifier.Intent getCurrentIntent() {
        return currentIntent;
    }

    /**
     * Returns the current slot to be filled.
     * @return The current slot, or null if none remain
     */
    public Slot getCurrentSlot() {
        if (currentSlotIndex < slots.size()) {
            return slots.get(currentSlotIndex);
        }
        return null;
    }

    /**
     * Fills the current slot with a value.
     * @param value The value to assign
     */
    public void fillCurrentSlot(String value) {
        if (currentSlotIndex < slots.size()) {
            slots.get(currentSlotIndex).value = value;
            context.put(slots.get(currentSlotIndex).name, value);
            currentSlotIndex++;
        }
    }

    /**
     * Checks whether all slots have been filled.
     * @return true if every slot is filled
     */
    public boolean allSlotsFilled() {
        for (Slot slot : slots) {
            if (!slot.isFilled()) {
                return false;
            }
        }
        return true;
    }

    /**
     * Returns a copy of the current context (all filled slots).
     * @return Map containing the conversation context
     */
    public Map<String, String> getContext() {
        return new HashMap<>(context);
    }

    /**
     * Advances to the next state.
     */
    public void advanceState() {
        switch (currentState) {
            case COLLECTING_SLOTS:
                if (allSlotsFilled()) {
                    currentState = State.CONFIRMING;
                }
                break;
            case CONFIRMING:
                currentState = State.EXECUTING;
                break;
            case EXECUTING:
                currentState = State.IDLE;
                break;
            default:
                break;
        }
    }
}
