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

    public void mainMenu(User user) {
        OrderService orderService = new OrderService();

        while (true) {
            System.out.println("=======================================\n" +
                    "               MAIN MENU               \n" +
                    "=======================================\n" +
                    "1. Place a New Order\n" +
                    "2. View Order History\n" +
                    "3. Logout\n" +
                    "=======================================");
            System.out.print("Choose an option: ");

            int choice = scanner.nextInt();
            System.out.println();
            switch (choice) {
                case 1: {
                    MenuService menuService = new MenuService();
                    menuService.showMenu();
                    orderService.placeOrder(user.getId());
                    System.out.println("Press _c_ to continue...");
                    scanner.next();
                    break;
                }
                case 2: {
                    orderService.showOrderHistory(user.getId());
                    System.out.println("Press _c_ to continue...");
                    scanner.next();
                    break;
                }
                case 3: {
                    return;
                }
                default:
                    System.out.println("Invalid choice");
            }
        }
    }

    public void start() {

        AuthService authService = new AuthService();

        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 = scanner.nextInt();

            switch (choice) {

                case 1: {
                    // TODO
                    System.out.print("\nEnter your username: ");
                    String username = scanner.next();
                    System.out.print("\nEnter your password: ");
                    String password = scanner.next();
                    System.out.println();
                    User user = authService.login(username, password);
                    if (user != null) {
                        System.out.println("Login success.");
                        mainMenu(user);
                    }
                    else {
                        System.out.println("Login failure.");
                    }
                    break;
                }

                case 2: {
                    // TODO
                    System.out.print("\nEnter your username: ");
                    String new_username = scanner.next();
                    System.out.print("\nEnter your password: ");
                    String new_password = scanner.next();
                    System.out.print("\nEnter your email (optional): ");
                    String new_email = scanner.next();
                    System.out.println();
                    boolean b = authService.register(new_username, new_password, new_email);
                    if (b) {
                        System.out.println("Registered successfully.");
                    }
                    else {
                        System.out.println("Registration failed.");
                    }
                    System.out.println("Press _c_ to continue...");
                    scanner.next();
                    break;
                }

                case 3:
                    return;

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

            }

        }

    }

}