/**
 * FullEliza - ELIZA-style chatbot with phrase extraction and pronoun transformation
 * This example integrates phrase extraction with pronoun replacement
 * to build a smarter conversational bot.
 *
 * Part III of the book "Introduction to Programming in Java"
 */

import java.util.*;
import java.util.regex.*;

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

    // Map of regex patterns and possible responses
    private static final Map<Pattern, List<String>> RULES = new LinkedHashMap<>();
    private static final Random RANDOM = new Random();

    private static final Map<String, String> PRONOUN_MAP = Map.of(
        "i", "you",
        "me", "you",
        "my", "your",
        "am", "are",
        "you", "I",
        "your", "my",
        "are", "am"
    );

    private static final Pattern PRONOUN_PATTERN =
        Pattern.compile("\\b(i|me|my|am|you|your|are)\\b");

    static {
        // Rule for sentences like "I want X"
        RULES.put(Pattern.compile("I want (.*)", Pattern.CASE_INSENSITIVE), Arrays.asList(
            "What would it mean to you if you got {0}?",
            "Why do you want {0}?",
            "What is stopping you from getting {0}?"
        ));

        // Rule for sentences like "I [verb] you"
        RULES.put(Pattern.compile("I (.*) you", Pattern.CASE_INSENSITIVE), Arrays.asList(
            "Why do you {0} me?",
            "What makes you feel that you {0} me?"
        ));

        // Rule for sentences like "Do you remember X"
        RULES.put(Pattern.compile("Do you remember (.*?)\\??$", Pattern.CASE_INSENSITIVE), Arrays.asList(
            "How could I forget {0}?",
            "Why wouldn't you be able to forget {0}?"
        ));

        // Rule for sentences like "Do you [verb] me?" or "You [verb] me"
        RULES.put(Pattern.compile("(?:Do you|You) (.*) me\\??$", Pattern.CASE_INSENSITIVE), Arrays.asList(
            "Why do you think I {0} you?",
            "What makes you think I {0} you?"
        ));

        // Rule for sentences starting with "If X"
        RULES.put(Pattern.compile("If (.*)", Pattern.CASE_INSENSITIVE), Arrays.asList(
            "Do you think it's likely that {0}?",
            "Do you wish that {0}?",
            "What do you think about {0}?",
            "Really... if {0}"
        ));
    }

    /**
     * Transforms first-person pronouns into second-person pronouns and vice versa.
     * @param phrase The phrase to transform
     * @return The phrase with transformed pronouns
     */
    public static String replacePronouns(String phrase) {
        String lowerPhrase = phrase.toLowerCase();
        return PRONOUN_PATTERN.matcher(lowerPhrase)
            .replaceAll(match -> PRONOUN_MAP.get(match.group(1)));
    }

    /**
     * Processes the user message and returns a matching response.
     * @param message The user's message
     * @return The bot's response
     */
    public static String matchRule(String message) {
        for (Map.Entry<Pattern, List<String>> entry : RULES.entrySet()) {
            Pattern pattern = entry.getKey();
            Matcher matcher = pattern.matcher(message);

            if (matcher.find()) {
                String phrase = matcher.group(1);
                List<String> responses = entry.getValue();
                String response = responses.get(RANDOM.nextInt(responses.size()));

                // Apply pronoun transformation to the extracted phrase
                String transformedPhrase = replacePronouns(phrase);

                return response.replace("{0}", transformedPhrase);
            }
        }

        return "That's interesting. Tell me more.";
    }

    /**
     * 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 = matchRule(message);
        System.out.println(String.format(BOT_TEMPLATE, response));
    }

    public static void main(String[] args) {
        sendMessage("I want to learn Java");
        sendMessage("I hate you");
        sendMessage("Do you hate me?");
        sendMessage("Do you remember our last meeting?");
        sendMessage("If I study hard, I will pass");
    }
}
