package dev.ui;

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

import java.util.Scanner;

public class ConsoleMenu {

    private AuthService authService = new AuthService();
    private MenuService menuService = new MenuService();
    private orderService orderService = new orderService();
    private Scanner scanner = new Scanner(System.in);
    private User loggedInUser = null; // برای حفظ وضعیت کاربر لاگین شده

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

    // ۱. منوی اولیه: لاگین و ثبت‌نام
    private void showAuthMenu() {
        System.out.println("=======================================");
        System.out.println("      🍕 WELCOME TO JAVA PIZZERIA 🍕");
        System.out.println("=======================================");
        System.out.println("1. Login");
        System.out.println("2. Register New Account");
        System.out.println("3. Exit");
        System.out.println("=======================================");
        System.out.print("Choose an option: ");

        int option = scanner.nextInt();
        scanner.nextLine(); // خالی کردن بافر اسکنر

        switch (option) {
            case 1 -> handleLogin();
            case 2 -> handleRegister();
            case 3 -> {
                System.out.println("Goodbye!");
                System.exit(0);
            }
            default -> System.out.println("Invalid option! Try again.");
        }
    }

    // ۲. منوی اصلی بعد از لاگین موفق
    private void showRestaurantMenu() {
        System.out.println("=======================================");
        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.print("Choose an option: ");

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

        switch (option) {
            case 1 -> menuService.showMenu(); // فقط متد نمایش رو از سرویس صدا می‌زنیم
            case 2 -> orderService.placeOrder(loggedInUser.getId()); // هدایت به بخش ثبت سفارش
            case 3 -> orderService.showOrderHistory(loggedInUser.getId());
            case 4 -> {
                loggedInUser = null; // خروج از حساب کاربری
                System.out.println("Logged out successfully.");
            }
            default -> System.out.println("Invalid option!");
        }
    }

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

        // سپردن کار اصلی تایید به لایه سرویس
        loggedInUser = authService.login(username, password);
    }

    private void handleRegister() {
        System.out.println("[Register]");
        System.out.print("Enter username: ");
        String username = scanner.nextLine();
        System.out.print("Enter password: ");
        String password = scanner.nextLine();
        System.out.print("Enter email (optional): ");
        String email = scanner.nextLine();
        boolean success = authService.register(username, password, email);
        if (success) {
            System.out.println("Account registered successfully! Please login.");
        }
    }
}