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;
    private final AuthService authService;
    private final MenuService menuService;
    private final OrderService orderService;

    public ConsoleMenu(AuthService authService, MenuService menuService, OrderService orderService) {
        this.scanner = new Scanner(System.in);
        this.authService = authService;
        this.menuService = menuService;
        this.orderService = orderService;
    }

    public void start() {

        while (true) {

            System.out.println();
            System.out.println("===== \uD83C\uDF55 PIZZA RESTAURANT SYSTEM =====");
            System.out.println("1. Login");
            System.out.println("2. Register");
            System.out.println("3. Exit");
            System.out.print("Select an option: ");

            int choice = Integer.parseInt(scanner.nextLine());

            switch (choice) {

                case 1:
                    System.out.println("Username: ");
                    String user = scanner.nextLine();
                    System.out.println("Password: ");
                    String pass = scanner.nextLine();

                    User user1 = authService.login(user,pass);
                    if (user1 != null){
                        System.out.println("Welcome back");
                        menuService.setCurrentUsers(user1);
                        showMainMenu(user1);
                    }else {
                        System.out.println("Login failed");
                    }
                    break;

                case 2:
                    System.out.println("New username: ");
                    String user2 = scanner.nextLine();
                    System.out.println("New password: ");
                    String pass2 = scanner.nextLine();
                    System.out.println("Email: ");
                    String email = scanner.nextLine();

                    authService.register(user2, pass2, email);
                    System.out.println("Registration successful");


                    break;

                case 3:
                    return;

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

            }

        }

    }
    private void showMainMenu(User authenticatedUser) {
        while (true) {
            System.out.println("\n===== MAIN MENU =====");
            System.out.println("1. Show Menu");
            System.out.println("2. Place Order");
            System.out.println("3. Order History");
            System.out.println("4. Logout");
            System.out.print("Select an option: ");

            int choice = Integer.parseInt(scanner.nextLine());

            switch (choice) {
                case 1:
                    menuService.showMenuItems();
                    break;

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

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

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

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