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

    private User loggedInUser;

    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(); // consume newline

            switch (choice) {

                case 1:
                    login();
                    break;

                case 2:
                    register();
                    break;

                case 3:
                    return;

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

    private void login() {

        System.out.print("Username: ");
        String username = scanner.nextLine();

        System.out.print("Password: ");
        String password = scanner.nextLine();

        loggedInUser = authService.login(username, password);

        if (loggedInUser == null) {
            System.out.println("Login failed!");
            return;
        }

        System.out.println("Login successful!");
        mainMenu();
    }

    private void register() {

        System.out.print("Username: ");
        String username = scanner.nextLine();

        System.out.print("Password: ");
        String password = scanner.nextLine();

        System.out.print("Email: ");
        String email = scanner.nextLine();

        boolean success = authService.register(username, password, email);

        if (success) {
            System.out.println("Registration successful!");
        } else {
            System.out.println("Registration failed!");
        }
    }

    private void mainMenu() {

        while (loggedInUser != null) {

            System.out.println();
            System.out.println("===== MAIN MENU =====");
            System.out.println("1. View Menu");
            System.out.println("2. Place 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(loggedInUser.getId());
                    break;

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

                case 4:
                    loggedInUser = null;
                    System.out.println("Logged out!");
                    break;

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