package dev.ui;

import java.util.Scanner;
import dev.model.User;
import dev.service.AuthService;
import dev.service.MenuService;
import dev.service.OrderService;
import java.lang.reflect.Field;

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("===== 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:
                    // TODO
                    handleLogin();
                    break;

                case 2:
                    // TODO
                    handleRegister();
                    break;

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

                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 loggedInUser = authService.login(username, password);
        if (loggedInUser != null){
            System.out.println("Login successful! Welcome, " + username + ".");
            showUserMenu(loggedInUser);
        }
        else{
            System.out.println("Error: 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;
        }
        if (authService.register(username, password, email)){
            System.out.println("Registration successful! You can now log in.");
        }
        else{
            System.out.println("Registration failed.");
        }
    }

    private void showUserMenu(User user){
        try {
            Field idField = User.class.getDeclaredField("id");
            idField.setAccessible(true);
            int userId = (int) idField.get(user);

            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.print("Choose an option: ");

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

                switch (choice){
                    case 1:
                        menuService.showMenu();
                        break;
                    case 2:
                        menuService.showMenu();
                        orderService.placeOrder(userId);
                        break;
                    case 3:
                        orderService.showOrderHistory(userId);
                        break;
                    case 4:
                        System.out.println("Logged out successfully.");
                        return;
                    default:
                        System.out.println("Invalid choice.");
                }
            }
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
    }

}