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 int userId;

    public void start()
    {
        while (true)
        {
            System.out.println("\n===== JAVA PIZZERIA =====");
            System.out.println("1. Login");
            System.out.println("2. Register");
            System.out.println("3. Exit");

            int choice = getIntInput();

            switch (choice)
            {
                case 1:
                    login();
                    break;

                case 2:
                    register();
                    break;

                case 3:
                    System.out.println("Goodbye!");
                    return;

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

    private void login()
    {
        System.out.println("\n[Login]");

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

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

        User user = authService.login(username, password);

        if (user != null)
        {
            userId = user.getId();
            System.out.println("Welcome " + user.getUsername());
            showMainMenu();
        }
        else System.out.println("Invalid credentials.");
    }

    private void register()
    {
        System.out.print("Username: ");
        String username = scanner.next();

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

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

        if (authService.register(username, password, email)) System.out.println("Registred successfully!");
        else System.out.println("Registration failed.");
    }

    private void showMainMenu()
    {
        while (true)
        {
            System.out.println("\n=======================================");
            System.out.println("          🍽️ MAIN MENU 🍽️");
            System.out.println("=======================================");
            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.println("=======================================");
            System.out.println("Choose an option: ");

            int choice = getIntInput();

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

                case 2:
                    menuService.showMenu();
                    orderService.placeOrder(userId);
                    break;

                case 3:
                    orderService.showOrderHistory(userId);
                    break;

                case 4:
                    return;

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

    private int getIntInput()
    {
        while (!scanner.hasNextInt())
        {
            System.out.println("Please enter a number:");
            scanner.next();
        }
        return scanner.nextInt();
    }
}