package dev.ui;

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

import java.util.Scanner;

public class ConsoleMenu {

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

    private final AuthService authService = new AuthService();
    private final MenuService menuService = new MenuService();
    private final OrderService orderService = new OrderService(scanner);

    public void start() {

        System.out.println("=======================================");
        System.out.println("      🍕 WELCOME TO JAVA PIZZERIA 🍕");
        System.out.println("=======================================");

        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");
            System.out.print("Choose an option: ");

            int choice = readInt();

            switch (choice) {

                case 1:
                    handleLogin();
                    break;

                case 2:
                    handleRegister();
                    break;

                case 3:
                    System.out.println("Goodbye!");
                    return;

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

            }

        }

    }

    private void handleLogin() {

        System.out.print("Enter username: ");
        String username = scanner.next();
        System.out.print("Enter password: ");
        String password = scanner.next();

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

        if (user == null) {
            System.out.println("Login failed: wrong username or password.");
            return;
        }

        System.out.println("Welcome back, " + user.getUsername() + "!");
        showMainMenu(user);
    }

    private void handleRegister() {

        System.out.print("Choose a username: ");
        String username = scanner.next();
        System.out.print("Choose a password: ");
        String password = scanner.next();
        System.out.print("Email (optional, or '-' to skip): ");
        String email = scanner.next();
        if (email.equals("-")) {
            email = null;
        }

        boolean ok = authService.register(username, password, email);
        if (ok) {
            System.out.println("Account created! You can log in now.");
        } else {
            System.out.println("Registration failed. Please try again.");
        }
    }

    /** The menu shown after a successful login. */
    private void showMainMenu(User user) {

        while (true) {

            System.out.println();
            System.out.println("=======================================");
            System.out.println("          🍽️ MAIN MENU 🍽️");
            System.out.println("=======================================");
            System.out.println("1. View Menu");
            System.out.println("2. Place a New Order");
            System.out.println("3. View Order History");
            System.out.println("4. Logout");
            System.out.print("Choose an option: ");

            int choice = readInt();

            switch (choice) {

                case 1:
                    menuService.showMenu();
                    break;

                case 2:
                    orderService.placeOrder(user.getId());
                    break;

                case 3:
                    orderService.showOrderHistory(user.getId());
                    break;

                case 4:
                    System.out.println("Logged out.");
                    return;

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

            }

        }

    }

    /** Reads an int without crashing on bad input. */
    private int readInt() {
        while (!scanner.hasNextInt()) {
            System.out.print("Please enter a number: ");
            scanner.next();
        }
        return scanner.nextInt();
    }

}
