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 in =
            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 = in.nextInt();
            in.nextLine();

            switch (choice) {

                case 1:
                    login();
                    break;

                case 2:
                    register();
                    break;

                case 3:
                    return;

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

            }

        }

    }


    public void login() {

        System.out.println("Enter your username: ");
        String username = in.nextLine();

        System.out.println("Enter your password: ");
        String password = in.nextLine();

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

        if(user != null) {
            System.out.println("Welcome back, " + username);
            showOptions(user);
        }
        else System.out.println("Wrong username or password.");

    }


    public void register() {

        System.out.println("Enter your username: ");
        String username = in.nextLine();

        System.out.println("Enter password: ");
        String password = in.nextLine();

        System.out.println("Enter your email: ");
        String email = in.nextLine();

        if (authService.register(username, password, email)) {
            System.out.println("Welcome!");
        }
        else System.out.println("Registration failed");
    }

    public void showOptions(User user) {

        System.out.println("""
                =======================================
                          \uD83C\uDF7D️ MAIN MENU \uD83C\uDF7D️
                =======================================""");



        while (true) {

            System.out.println("1. View Menu\n" +
                    "2. Place a New Order\n" +
                    "3. View Order History \n" +
                    "4. Logout");

            int choice = in.nextInt();

            switch (choice) {

                case 1:
                    menuService.showMenu();
                    break;

                case 2:
                    orderService.placeOrder(user.getId());
                    break;

                case 3:
                    orderService.showOrderHistory(user.getId());
                    break;

                case 4:
                    return;
            }
        }
    }
}