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 scanner =
            new Scanner(System.in);



    private final AuthService authService =
            new AuthService();


    private final MenuService menuService =
            new MenuService();


    private final OrderService orderService =
            new OrderService();



    private User currentUser;





    public void start(){



        while(true){



            System.out.println(
                    """
                    =======================================
                        🍕 WELCOME TO JAVA PIZZERIA 🍕
                    =======================================
                            
                      1. Login
                      2. Register New Account
                      3. Exit

                      Choose an option:
                    """
            );



            int choice =
                    scanner.nextInt();



            scanner.nextLine();



            switch(choice){


                case 1:

                    login();

                    break;



                case 2:

                    register();

                    break;



                case 3:

                    System.out.println(
                            "Goodbye!"
                    );

                    return;



                default:

                    System.out.println(
                            "Invalid option"
                    );

            }


        }


    }








    private void register() {

        System.out.println("Username:");
        String username = scanner.nextLine();

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

        System.out.println("Email:");
        String email = scanner.nextLine();

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

        if (success) {
            System.out.println("Registration successful!");
        } else {
            System.out.println("Username already exists!");
        }
    }









    private void login(){


        System.out.println(
                "Username:"
        );

        String username =
                scanner.nextLine();



        System.out.println(
                "Password:"
        );

        String password =
                scanner.nextLine();




        currentUser =
                authService.login(
                        username,
                        password
                );



        if(currentUser != null){


            System.out.println(
                    "Login successful"
            );


            mainMenu();


        }

        else{


            System.out.println(
                    "Wrong username or password"
            );


        }


    }







    private void mainMenu(){



        while(currentUser != null){



            System.out.println(
                    """
                    =======================================
                              🍽️ MAIN MENU 🍽️
                    =======================================
                    1. View Menu
                    2. Place Order
                    3. Order History
                    4. Logout
                    =========================
                    """
            );



            int choice =
                    scanner.nextInt();



            switch(choice){


                case 1:

                    menuService.showMenu();

                    break;



                case 2:

                    orderService.placeOrder(
                            currentUser.getId()
                    );

                    break;



                case 3:

                    orderService.showOrderHistory(
                            currentUser.getId()
                    );

                    break;



                case 4:

                    currentUser=null;

                    System.out.println(
                            "Logged out"
                    );

                    break;



                default:

                    System.out.println(
                            "Invalid option"
                    );


            }


        }


    }


}