package dev.ui;

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

import java.util.Scanner;

public class ConsoleMenu
{

    private final AuthService authService;
    private final MenuService menuService;
    private final OrderService orderService;
    private final Scanner scanner;
    private User currentUser;

    public ConsoleMenu(AuthService authService, MenuService menuService, OrderService orderService)
    {
        this.authService = authService;
        this.menuService = menuService;
        this.orderService = orderService;
        this.scanner = new Scanner(System.in);
    }

    public void start()
    {
        while (true)
        {
            if (currentUser == null)
            {
                showGuestMenu();
            }
            else
            {
                showUserMenu();
            }
        }
    }

    private void showGuestMenu()
    {
        ConsoleStyle.printHeader("🍕 WELCOME TO JAVA PIZZERIA 🍕");
        System.out.println("1. Login");
        System.out.println("2. Register New Account");
        System.out.println("3. Exit");
        ConsoleStyle.printLine();
        System.out.print("Choose an option: ");

        String input = scanner.nextLine().trim();

        switch (input)
        {
            case "1":
                handleLogin();
                break;

            case "2":
                handleRegister();
                break;

            case "3":
                ConsoleStyle.printInfo("Goodbye! See you soon.");
                System.exit(0);
                break;

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

    private void showUserMenu()
    {
        ConsoleStyle.printHeader("🍽️ MAIN MENU - Welcome, " + currentUser.getUsername() + " 🍽️");
        System.out.println("1. View Menu");
        System.out.println("2. Place New Order");
        System.out.println("3. View Order History");
        System.out.println("4. Logout");
        System.out.println("5. Exit");
        ConsoleStyle.printLine();
        System.out.print("Choose an option: ");

        String input = scanner.nextLine().trim();

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

            case "2"->
                orderService.placeOrder(currentUser.getId());

            case "3" ->
                orderService.showOrderHistory(currentUser.getId());
            case "4"->{
                currentUser = null;
                ConsoleStyle.printSuccess("Logged out successfully.");
            }
            case "5"->{
                ConsoleStyle.printInfo("Goodbye! See you soon.");
                System.exit(0);
            }
            default->
                    ConsoleStyle.printError("Invalid choice.");
        }
    }

    private void handleLogin()
    {
        System.out.println("\n--- Login ---");
        System.out.print("Username: ");
        String username = scanner.nextLine().trim();

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

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

        if (user != null)
        {
            currentUser = user;
            ConsoleStyle.printSuccess("Login successful! Welcome back, " + user.getUsername() + ".");
        }
        else
        {
            ConsoleStyle.printError("Invalid username or password.");
        }
    }

    private void handleRegister()
    {
        System.out.println("\n--- Register ---");
        System.out.print("Enter username: ");
        String username = scanner.nextLine().trim();

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

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

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

        if (success)
        {
            ConsoleStyle.printSuccess("Registration successful! You can now login.");
        }
        else
        {
            ConsoleStyle.printError("Registration failed. Username might be taken.");
        }
    }
}

