package dev.ui;

import dev.dao.UserDao;
import dev.model.User;
import dev.service.AuthService;
import dev.service.OrderService;
import java.util.Scanner;

public class ConsoleMenu {

    private final Scanner scanner = new Scanner(System.in);
    private final UserDao userDao = new UserDao();
    private final AuthService authService = new AuthService();
    private final OrderService orderService = new OrderService();
    private boolean printMainMenu = true;
    private boolean printMenu = true;

    public void start() {
        while (true) {
            if (printMainMenu) {
                System.out.println("\n=======================================");
                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: ");
            }

            String choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    handleLogin();
                    printMainMenu = true;
                    break;
                case "2":
                    handleRegister();
                    printMainMenu = true;
                    break;
                case "3":
                    System.out.println("Thank you for visiting Java Pizzeria!");
                    return;
                default:
                    System.out.println("Invalid choice.");
                    printMainMenu = false;
            }
        }
    }

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

        User user = authService.login(username, password);
        if (user != null) {
            showMainMenu(user);
        } else {
            System.out.println("Invalid username or password.");
        }
    }

    private void handleRegister() {
        System.out.println("\n[Registering 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: ");
        String email = scanner.nextLine();

        boolean success = authService.register(username, password, email);
        if (success) {
            System.out.println("Account registered successfully! You can now log in.");
        } else {
            System.out.println("Registration failed. Data may be invalid or username taken.");
        }
    }

    private void showMainMenu(User user) {
        while (true) {
            if (printMenu) {
                System.out.println();
                System.out.println("=======================================");
                System.out.println("             MAIN MENU                 ");
                System.out.println("=======================================");
                System.out.println("1. View Menu");
                System.out.println("2. View Order History ");
                System.out.println("3. Logout");
                System.out.println("4. Delete Account");
                System.out.println("=======================================");
                System.out.print("Choose an option: ");
            }

            String choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    orderService.placeOrder(user.getId());
                    printMenu = true;
                    break;
                case "2":
                    orderService.showOrderHistory(user.getId());
                    printMenu = true;
                    break;
                case "3":
                    System.out.println("Logging out...");
                    return;
                case "4":
                    while (true) {
                        System.out.print("Enter your password to confirm (or 0 to cancel): ");
                        String confirmation = scanner.nextLine();

                        if (confirmation.equals("0")) {
                            System.out.println("\nDeletion canceled. Returning to safety.");
                            break;
                        }

                        if (confirmation.equals(user.getPassword())) {
                            userDao.deleteUser(user.getId());
                            return;

                        } else {
                            System.out.println("Password not correct.");
                        }
                    }
                    break;
                default:
                    System.out.println("Invalid choice.");
                    printMenu = false;
            }
        }
    }
}