package dev.ui;

import dev.model.User;
import dev.service.AuthService;
import dev.service.MenuService;

import java.util.Scanner;

public class ConsoleMenu {

    private final Scanner scanner =
            new Scanner(System.in);

    private AuthService authService = new AuthService();

    public void start() {

        while (true) {

            System.out.println();
            System.out.println("===== JAVA PIZZERIA =====");
            System.out.println("1. Login");
            System.out.println("2. Register");
            System.out.println("3. Exit");

            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {

                case 1: {
                    System.out.print("Enter your username: ");
                    String username = scanner.nextLine();
                    System.out.print("Enter your password: ");
                    String password = scanner.nextLine();

                    User user = authService.login(username, password);

                    if (user == null) {
                        System.err.println("Unable to login.");
                        continue;
                    }

                    MenuService menuService = new MenuService();
                    menuService.ShowPanel(user);
                    break;
                }
                case 2: {
                    System.out.print("Enter your username: ");
                    String username = scanner.nextLine();
                    System.out.print("Enter your password: ");
                    String password = scanner.nextLine();
                    System.out.print("Enter your email (optional): ");
                    String tmp = scanner.nextLine();
                    String email = tmp.isEmpty() ? null : tmp;

                    if (!authService.register(username, password, email)) {
                        System.err.println("Registration failed.");
                        continue;
                    }

                    System.out.println("Registration successful!");
                    break;
                }
                case 3: {
                    return;
                }
                default: {
                    System.out.println("Invalid choice enter a number between [1, 3]");
                }
            }

        }

    }

}