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 final 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();

            switch (choice) {

                case 1: {
                    System.out.println("Enter username: ");
                    String username = scanner.next();
                    System.out.println("Enter password: ");
                    String password = scanner.next();

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

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

                    System.out.println("welcome! " + user.getUsername());
                    MenuService menuService = new MenuService();
                    menuService.showMenu();
                    break;
                }

                case 2: {
                    System.out.println("Enter username: ");
                    String username = scanner.nextLine();
                    System.out.println("Enter password: ");
                    String password = scanner.nextLine();
                    System.out.println("Enter email: ");
                    String email = scanner.nextLine();

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

                    System.out.println("Registered successfully.");

                    break;
                }

                case 3:
                    return;

                default:
                    System.out.println("Invalid choice");

            }

        }

    }

}