/**
 * VariedBot - Chatbot with randomized responses
 * This example demonstrates how to add variety to responses
 * using lists of possible replies and random selection.
 *
 * Part III of the book "Introduction to Programming in Java"
 */

import java.util.*;

public class VariedBot {
    private static final String BOT_TEMPLATE = "BOT: %s";
    private static final String USER_TEMPLATE = "USER: %s";

    private static final String NAME = "JavaTutorBot";
    private static final String WEATHER = "sunny";

    // Map of questions to lists of possible responses
    private static final Map<String, List<String>> RESPONSES = new HashMap<>();
    private static final Random RANDOM = new Random();

    static {
        // Varied responses for each question
        RESPONSES.put("what is your name?", Arrays.asList(
            "my name is " + NAME,
            "they call me " + NAME,
            "I am " + NAME
        ));
        RESPONSES.put("what's the weather like today?", Arrays.asList(
            "the weather is " + WEATHER,
            "it's " + WEATHER + " today"
        ));
        RESPONSES.put("default", Arrays.asList(
            "I didn't understand, could you repeat that?",
            "sorry, I don't know how to answer that",
            "interesting question! but I don't know the answer"
        ));
    }

    /**
     * Method that responds to a user message.
     * @param message The user's message
     * @return The bot's response
     */
    public static String respond(String message) {
        String lowerMessage = message.toLowerCase().trim();

        List<String> possibleResponses = RESPONSES.getOrDefault(lowerMessage, RESPONSES.get("default"));

        // Selects a random response from the list
        return possibleResponses.get(RANDOM.nextInt(possibleResponses.size()));
    }

    /**
     * Method that sends a message to the bot and displays the dialogue.
     * @param message The user's message
     */
    public static void sendMessage(String message) {
        System.out.println(String.format(USER_TEMPLATE, message));
        String response = respond(message);
        System.out.println(String.format(BOT_TEMPLATE, response));
    }

    public static void main(String[] args) {
        // Test the same question several times to see the variety
        for (int i = 0; i < 5; i++) {
            sendMessage("what is your name?");
        }
    }
}
