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

    private User loggedInUser;

    public ConsoleMenu(AuthService authService,
                       MenuService menuService,
                       OrderService orderService) {
        this.authService = authService;
        this.menuService = menuService;
        this.orderService = orderService;
    }

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

            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 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)
        {
            loggedInUser = user;
            System.out.println("Login successful!");
            userMenu();
        }
        else {System.out.println("Login failed!");}
    }

    private void 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 userMenu()
    {
        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. Order History");
            System.out.println("4. Logout");

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

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

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

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

                case 4 ->
                {
                    loggedInUser = null;
                    return;
                }

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