package com.twu.biblioteca;

import com.twu.biblioteca.model.Book;
import com.twu.biblioteca.model.Movie;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class BibliotecaLibrary {

    private List<Book> books;
    private List<Movie> movies;

    public BibliotecaLibrary() {
        books = buildPreExistingBooks();
        movies = buildPreExistingMovies();
    }

    private List<Book> buildPreExistingBooks() {
        List<Book> books = new ArrayList<>();
        books.add(new Book("book1", "author1", 1998));
        books.add(new Book("book2", "author2", 2016));
        books.add(new Book("book3", "author3", 2014));
        return books;
    }

    private List<Movie> buildPreExistingMovies() {
        List<Movie> movies = new ArrayList<>();
        movies.add(new Movie("movie1", 2006, "director1", 8));
        movies.add(new Movie("movie2", 1980, "director2", 9));
        movies.add(new Movie("movie3", 2015, "director3", 2));
        return movies;
    }

    public boolean checkOutBook(String bookName) {
        Optional<Book> findBook = books.stream()
                .filter(book -> !book.isCheckOut() && book.getName().equals(bookName))
                .findFirst();
        if (findBook.isPresent()) {
            findBook.get().setCheckOut(true);
            return true;
        }
        return false;
    }

    public boolean returnBook(String bookName) {
        Optional<Book> findBook = books.stream()
                .filter(book -> book.isCheckOut() && book.getName().equals(bookName))
                .findFirst();
        if (findBook.isPresent()) {
            findBook.get().setCheckOut(false);
            return true;
        }
        return false;
    }

    public boolean checkOutMovie(String movieName) {
        Optional<Movie> findMovie = movies.stream()
                .filter(movie -> !movie.isCheckOut() && movie.getName().equals(movieName))
                .findFirst();
        findMovie.ifPresent(Movie::checkOut);
        return findMovie.isPresent();
    }

    public boolean returnMovie(String movieName) {
        Optional<Movie> findMovie = movies.stream()
                .filter(movie -> movie.isCheckOut() && movie.getName().equals(movieName))
                .findFirst();
        findMovie.ifPresent(movie -> movie.setCheckOut(false));
        return findMovie.isPresent();
    }

    public List<Book> getBooks() {
        return books;
    }

    public List<Movie> getMovies() {
        return movies;
    }

}

@@ -98,16 +98,54 @@
 l.Movie;
+%0Aimport com.twu.biblioteca.model.User;
 %0A%0Aimport
@@ -312,16 +312,74 @@
  movies;
+%0A    private List%3CUser%3E users;%0A    private User loginUser;
 %0A%0A    pu
@@ -485,24 +485,59 @@
 ngMovies();%0A
+        users = predifiendUsers();%0A
     %7D%0A%0A    p
@@ -1104,24 +1104,24 @@
 tor3%22, 2));%0A
-
         retu
@@ -1130,32 +1130,291 @@
  movies;%0A    %7D%0A%0A
+    private List%3CUser%3E predifiendUsers() %7B%0A        List%3CUser%3E users = new ArrayList%3C%3E();%0A        users.add(new User(%22000-0001%22, %22p1%22));%0A        users.add(new User(%22000-0002%22, %22p2%22));%0A        users.add(new User(%22000-0003%22, %22p3%22));%0A        return users;%0A    %7D%0A%0A
     public boole

add fileds users and loginUser in BibliotecaLibrary