package client;

/**
 * InputHandler class
 * 
 * @author Tinsae Ghilay
 *         for handling user inputs
 */

import java.util.Scanner;
import java.util.Arrays;

public class InputHandler {

    // list of valid commands
    private String[] commands = {
            "add",
            "subtract",
            "multiply",
            "divide",
            "mod",
            "power",
            "root",
            "exit"
    };

    /**
     * method to prompt user for a valid integer
     * 
     * @param scanner Scanner object for user input
     * @return valid integer input from user
     */
    public Integer promptForInt(Scanner scanner) {
        Integer number;
        while (true) {
            System.out.print("Enter an integer: ");
            if (scanner.hasNextInt()) {
                number = scanner.nextInt();
                scanner.nextLine(); // consume the newline
                break;
            } else {
                String command = scanner.nextLine();
                // check if user wants to exit and return null
                if (command.equalsIgnoreCase("exit")) {
                    return null;
                }
                System.out.println("Invalid input. Please enter a valid integer.");
            }
        }
        return number;
    }

    /**
     * method to prompt user for a valid command
     * 
     * @param scanner Scanner object for user input
     * @return valid command input from user
     */
    public String promptForCommand(Scanner scanner) {
        System.out.print("Enter command (add, subtract, multiply, divide, mod, power, root, exit): ");
        while (true) {
            String command = scanner.nextLine().trim().toLowerCase();
            if (isValidCommand(command)) {
                return command;
            } else {
                System.out.print("Invalid command. Please enter a valid command: ");
            }
        }
    }

    /**
     * helper method to check if command is valid
     * 
     * @param command
     * @return
     */
    private boolean isValidCommand(String command) {
        return Arrays.asList(commands).contains(command.toLowerCase());
    }

    /**
     * getter for commands
     * 
     * @return
     */
    public String[] getCommands() {
        return commands;
    }

    /**
     * getter for command at index
     * 
     * @param index
     * @return
     */
    public String getCommand(int index) {
        return commands[index];
    }

    /**
     * method to map user command to result term
     * 
     * @param command
     * @return
     */
    public String mapCommand(String command) {
        switch (command.toLowerCase()) {
            case "add":
                return "sum";
            case "subtract":
                return "difference";
            case "multiply":
                return "product";
            case "divide":
                return "quotient";
            case "mod":
                return "mod";
            case "power":
                return "power";
            case "root":
                return "root";
            default:
                return null;
        }
    }

}
