package Library.ru.mai.pmoproject;

import java.util.HashMap;
import java.util.List;

/**
 * Created by stud on 16.04.2015.
 */
public class LibraryImpl implements Library {
    public LibraryImpl(String name)
    {
        this.name=name;

    }
String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void addNewBook(Book book) {
        if (book.Name==null||book.Name.trim().length() == 0) {
            throw new IllegalArgumentException("У книги должно быть название");
        }
        if (book.Author == null || book.Author.trim().length() == 0) {
            throw new IllegalArgumentException("У книги должен быть автор");
        }
        if (book.quantity<=0) {
            throw new IllegalArgumentException("Для внесения книг должно быть больше 0");
        }
        if (book.quantity>1)
        {
            for(int i=0; i<book.quantity; i++)
            {
                findAvailableBooks().add(book);
                System.out.println("Свободные книги:");
            }
        }
        else
        {
            findAvailableBooks().add(book);
        }
    }

    @Override
    public void borrowBook(Book book, String student) {
        if (student==null||student.trim().length()==0)
        {
            throw new IllegalArgumentException("У студента должно быть имя");
        }
        if (book.Name==null||book.Name.trim().length() == 0) {
            throw new IllegalArgumentException("У книги должно быть название");
        }
        if (book.Author == null || book.Author.trim().length() == 0) {
            throw new IllegalArgumentException("У книги должен быть автор");
        }
        else
        {
            book.add(book, findAvailableBooks());
            book.subtract(book, findNotavailableBooks);
            int i=book.quantity+1;
            Book book1 = new Book (book.Name, book.Author, i);
            int j=book.quantity-1;
            Book book2 = new Book (book.Name, book.Author, i);
            findNotavailableBooks.put(book1,student);
            findAvailableBooks().remove(book2.ID);
        }
    }

    @Override
    public void returnBook(Book book, String student) {
        if(!findNotavailableBooks.containsValue(student))
        {
            throw new IllegalArgumentException("Такой студент не брал книг");
        }
        else
        {
            if (book.Name==null||book.Name.trim().length() == 0) {
                throw new IllegalArgumentException("У книги должно быть название");
            }
            if (book.Author == null || book.Author.trim().length() == 0) {
                throw new IllegalArgumentException("У книги должен быть автор");
            }
            else
            {
                if (!findNotavailableBooks.containsKey(book)){
                    throw new IllegalArgumentException("Такую книгу не брали");
                }
                else
                {
                    while (findNotavailableBooks.containsValue(student)){
                        if (!findNotavailableBooks.containsKey(book)){
                            throw new IllegalArgumentException("Такую книгу этот студент не брал");
                        }
                        else
                        {
                            book.subtract(book, findAvailableBooks());
                            book.add(book, findNotavailableBooks);
                            int i=book.quantity+1;
                            Book book1 = new Book (book.Name, book.Author, i);
                            findNotavailableBooks.remove(book);
                            findAvailableBooks().add(book1);


                        }
                    }
                }
            }
        }


    }

    @Override
    public List<Book> findAvailableBooks() {
        for (int i = 0; i < findAvailableBooks().size(); i++) {
            System.out.println(findAvailableBooks().indexOf(i));
            }
        return null;
    }

    public HashMap <Book, String> findNotavailableBooks = new HashMap<Book, String>();


}
