package library.ui;

import library.model.Book;
import library.model.Loan;
import library.model.Member;
import library.service.LibraryService;
import library.service.LoanService;
import library.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: ");
        Book book = libraryService.addBook(title, author, isbn);
        System.out.println("Added: " + book);
    }

    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() {
        String keyword = InputUtils.readLine("Keyword: ");
        libraryService.searchBooks(keyword).forEach(System.out::println);
    }

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

    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() {
        String bookId = InputUtils.readLine("Book ID: ");
        String memberId = InputUtils.readLine("Member ID: ");
        System.out.println(loanService.borrowBook(bookId, memberId));
    }

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

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