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

    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:
                    // TODO
                    login();
                    break;

                case 2:
                    // TODO
                    register();
                    break;

                case 3:
                    return;

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

            }

        }

    }

    private void register() {

        System.out.println();
        System.out.println("[Register]");

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

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

        System.out.print("Enter email (optional, press enter to skip): ");
        scanner.nextLine();
        String email = scanner.nextLine();

        if (email.isBlank()) {
            email = null;
        }

        if (authService.register(username, password, email)) {
            System.out.println("Registration successful! You can now login.");
        }

    }

    private void login() {

        System.out.println();
        System.out.println("[Login]");

        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 successful! Welcome, " + user.getUsername() + "!");
            showMainMenu(user);
        }

    }

    private void showMainMenu(User user) {

        while (true) {

            System.out.println();
            System.out.println("===== MAIN MENU =====");
            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");

            int choice = scanner.nextInt();

            switch (choice) {

                case 1:
                    menuService.showMenu();
                    break;

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

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

                case 4:
                    return;

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

            }

        }

    }

}