package ui;

import model.Book;
import model.Loan;
import model.Member;
import service.LibraryService;
import service.LoanService;
import util.InputUtils;
import java.util.List;

public class ConsoleUI {
    private final LibraryService libraryService;
    private final LoanService loanService;

    public ConsoleUI(LibraryService libraryService, LoanService loanService) {
        this.libraryService = libraryService;
        this.loanService = loanService;
    }

    public void start() {
        System.out.println("=== Library Management System ===");
        boolean running = true;
        while (running) {
            printMenu();
            int choice = InputUtils.readInt("Enter choice: ");
            switch (choice) {
                case 1 -> addBook();
                case 2 -> listBooks();
                case 3 -> searchBooks();
                case 4 -> registerMember();
                case 5 -> listMembers();
                case 6 -> borrowBook();
                case 7 -> returnBook();
                case 8 -> listLoans();
                case 0 -> { running = false; System.out.println("Goodbye!"); }
                default -> System.out.println("Invalid choice.");
            }
        }
        InputUtils.close();
    }

    private void printMenu() {
        System.out.println("""
            \n--- MENU ---
            1. Add book
            2. List all books
            3. Search books
            4. Register member
            5. List members
            6. Borrow book
            7. Return book
            8. List active loans
            0. Exit""");
    }

    private void addBook() {
        String title  = InputUtils.readLine("Title: ");
        String author = InputUtils.readLine("Author: ");
        String isbn   = InputUtils.readLine("ISBN: ");
        System.out.println("Added: " + libraryService.addBook(title, author, isbn));
    }

    private void listBooks() {
        List<Book> books = libraryService.listAllBooks();
        if (books.isEmpty()) { System.out.println("No books."); return; }
        books.forEach(System.out::println);
    }

    private void searchBooks() {
        libraryService.searchBooks(InputUtils.readLine("Keyword: ")).forEach(System.out::println);
    }

    private void registerMember() {
        String name  = InputUtils.readLine("Name: ");
        String email = InputUtils.readLine("Email: ");
        System.out.println("Registered: " + libraryService.registerMember(name, email));
    }

    private void listMembers() {
        List<Member> members = libraryService.listAllMembers();
        if (members.isEmpty()) { System.out.println("No members."); return; }
        members.forEach(System.out::println);
    }

    private void borrowBook() {
        System.out.println(loanService.borrowBook(
            InputUtils.readLine("Book ID: "),
            InputUtils.readLine("Member ID: ")
        ));
    }

    private void returnBook() {
        System.out.println(loanService.returnBook(InputUtils.readLine("Loan ID: ")));
    }

    private void listLoans() {
        List<Loan> loans = loanService.getActiveLoans();
        if (loans.isEmpty()) { System.out.println("No active loans."); return; }
        loans.forEach(System.out::println);
    }
}
