/**
 * SmalltalkBot - Chatbot with predefined responses
 * This example demonstrates how to use a Map to store responses
 * for specific questions.
 *
 * Part III of the book "Introduction to Programming in Java"
 */

import java.util.HashMap;
import java.util.Map;

public class SmalltalkBot {
    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 and responses
    private static final Map<String, String> RESPONSES = new HashMap<>();

    static {
        RESPONSES.put("what is your name?", "my name is " + NAME);
        RESPONSES.put("what's the weather like today?", "the weather is " + WEATHER);
        RESPONSES.put("default", "default message");
    }

    /**
     * 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();

        // Checks whether the message has a predefined response
        if (RESPONSES.containsKey(lowerMessage)) {
            return RESPONSES.get(lowerMessage);
        } else {
            return RESPONSES.get("default");
        }
    }

    /**
     * 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) {
        sendMessage("what is your name?");
        sendMessage("what's the weather like today?");
        sendMessage("what is your favorite color?");
    }
}
