package org.example.demo.praktikum1.aufgabe3;
import javax.naming.InitialContext;
import javax.naming.Context;
import java.util.Properties;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
        props.setProperty(Context.PROVIDER_URL, "http-remoting://localhost:8080");

        InitialContext ctx = new InitialContext(props);
        String jndiName = "ejb:/ejb-server-1.0-SNAPSHOT/GuessingGame!org.example.demo.praktikum1.aufgabe3.GuessingGameRemote";
        var game = (GuessingGameRemote) ctx.lookup(jndiName);
        var scanner = new Scanner(System.in);
        var another = true;
        while (another) {
            playRound(game, scanner);

            // todo: input exception handling
            System.out.print("Do you want to play another? ");
            another = scanner.nextBoolean();
        }
        scanner.close();
    }

    private static void playRound(GuessingGameRemote game, Scanner scanner) {
        game.init();

        GuessResult result;
        do {
            // todo: input exception handling
            System.out.print("Guess a number from 1-100: ");
            var guess = scanner.nextInt();
            result = game.testGuess(guess);

            switch (result) {
            case Success:
                System.out.println("Correct! The number was "+ guess);
                break;
            case TooBig:
                System.out.println("Your guess was too big!");
                break;
            case TooSmall:
                System.out.println("Your guess was too small!");
                break;
            }
        }
        while (result != GuessResult.Success);
    }
}
