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 currentUser = null;

    public void start() {
        while (true) {
            if (currentUser == null) {
                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: ");

                if (!scanner.hasNextInt()) {
                    scanner.next();
                    System.out.println("Invalid choice");
                    continue;
                }
                int choice = scanner.nextInt();
                scanner.nextLine();

                switch (choice) {
                    case 1:
                        handleLogin();
                        break;
                    case 2:
                        handleRegister();
                        break;
                    case 3:
                        return;
                    default:
                        System.out.println("Invalid choice");
                }
            } else {
                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");
                System.out.print("Choose an option: ");

                if (!scanner.hasNextInt()) {
                    scanner.next();
                    System.out.println("Invalid choice");
                    continue;
                }
                int choice = scanner.nextInt();
                scanner.nextLine();

                switch (choice) {
                    case 1:
                        menuService.showMenu();
                        break;
                    case 2:
                        orderService.placeOrder(currentUser.getId());
                        break;
                    case 3:
                        orderService.showOrderHistory(currentUser.getId());
                        break;
                    case 4:
                        currentUser = null;
                        System.out.println("Successfully logged out.");
                        break;
                    default:
                        System.out.println("Invalid choice");
                }
            }
        }
    }

    private void handleLogin() {
        System.out.println("\n[Login]");
        System.out.print("Enter username: ");
        String username = scanner.nextLine();
        System.out.print("Enter password: ");
        String password = scanner.nextLine();

        User user = authService.login(username, password);
        if (user != null) {
            currentUser = user;
            System.out.println("Login successful. Welcome, " + currentUser.getUsername());
        } else {
            System.out.println("Invalid username or password.");
        }
    }

    private void handleRegister() {
        System.out.println("\n[Register New Account]");
        System.out.print("Enter username: ");
        String username = scanner.nextLine();
        System.out.print("Enter password: ");
        String password = scanner.nextLine();
        System.out.print("Enter email (optional, press Enter to skip): ");
        String email = scanner.nextLine();
        if (email.trim().isEmpty()) {
            email = null;
        }

        boolean success = authService.register(username, password, email);
        if (success) {
            System.out.println("Registration successful. You can now log in.");
        } else {
            System.out.println("Registration failed. The username may already exist or input was invalid.");
        }
    }
}