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

    public void start()
    {
        while (true)
        {
            System.out.println();
            System.out.println("====================================");
            System.out.println("           JAVA PIZZERIA            ");
            System.out.println("====================================");

            System.out.println("1. Login");
            System.out.println("2. Register");
            System.out.println("3. Exit");

            System.out.print("Choose option: ");

            int choice = scanner.nextInt();

            scanner.nextLine();

            switch (choice)
            {
                case 1 -> login();

                case 2 -> register();

                case 3 ->
                {
                    System.out.println("Goodbye.");
                    return;
                }

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

    private void register()
    {
        System.out.println();
        System.out.println("===== REGISTER =====");

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

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

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

        boolean success = authService.register(username, password, email);

        if (success)
            System.out.println("Registration successful.");
        else
            System.out.println("Registration failed.");
    }

    private void login()
    {
        System.out.println();
        System.out.println("===== LOGIN =====");

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

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

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

        if (user == null)
        {
            System.out.println("Login failed.");
            return;
        }

        System.out.println("Welcome " + user.getUsername());
        showUserMenu(user);
    }

    private void showUserMenu(User user)
    {
        while (true)
        {
            System.out.println();
            System.out.println("========== MAIN MENU ==========");

            System.out.println("1. View Menu");
            System.out.println("2. Place Order");
            System.out.println("3. View Order History");
            System.out.println("4. Logout");

            System.out.print("Choose option: ");

            int choice = scanner.nextInt();

            switch (choice)
            {
                case 1 -> menuService.showMenu();

                case 2 -> orderService.placeOrder(user.getId());

                case 3 -> orderService.showOrderHistory(user.getId());

                case 4 ->
                {
                    return;
                }

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