package dev.ui;

import dev.model.User;
import dev.service.AuthService;
import dev.service.MenuService;
import dev.service.OrderService;

import java.util.HashMap;
import java.util.Map;
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 = null;

    public void start() {
        while (true) {
            if (loggedInUser == null) {
                showAuthMenu();
            } else {
                showMainMenu();
            }
        }
    }

    private void showAuthMenu() {
        System.out.println("\n===== 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();
        scanner.nextLine();

        switch (choice) {
            case 1:
                login();
                break;
            case 2:
                register();
                break;
            case 3:
                System.exit(0);
            default:
                System.out.println("Invalid choice");
        }
    }

    private void showMainMenu() {
        System.out.println("\n===== 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: ");

        int choice = scanner.nextInt();
        scanner.nextLine();

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

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

        loggedInUser = authService.login(username, password);

        if (loggedInUser == null) {
            System.out.println("Invalid credentials. Please try again.");
        } else {
            System.out.println("Login successful!");
        }
    }

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

        boolean success = authService.register(username, password, email);
        if (success) {
            System.out.println("Registration successful. You can now login.");
        } else {
            System.out.println("Registration failed. Username might be taken.");
        }
    }

    private void placeOrderFlow() {
        System.out.println("\n[Placing Order]");
        menuService.showMenu();

        Map<Integer, Integer> cart = new HashMap<>();

        while (true) {
            System.out.print("Enter the ID of the item to add (or 0 to finish): ");
            int itemId = scanner.nextInt();
            if (itemId == 0) {
                break;
            }

            System.out.print("Enter quantity: ");
            int quantity = scanner.nextInt();

            if (quantity > 0) {
                cart.put(itemId, cart.getOrDefault(itemId, 0) + quantity);
                System.out.println("Item added to your cart.");
            } else {
                System.out.println("Quantity must be greater than zero.");
            }
        }

        if (!cart.isEmpty()) {
            orderService.placeOrder(loggedInUser.getId(), cart);
        } else {
            System.out.println("Order cancelled. Cart is empty.");
        }
    }
}