package book;

import java.util.*;


public class Bookshelf {
	private ArrayList<Book> bookList;
	
	public Bookshelf() {
		this.bookList = new ArrayList<Book>();
	}
	
	public void addNewBook(Book item) {
		if(item == null)
			return;
		
		this.bookList.add(item);
	}
	
	public void removeBook(Book item) {
		if(item == null || !this.bookList.contains(item))
			return;
		
		this.bookList.remove(item);
	}
	
	public int getSize() {
		return this.bookList.size();
	}
	
	public Book bookSearch(String author, String title) {
		Book k = new Book(author, title, 0);
		int index = this.bookList.indexOf(k);
		
		if(index == -1)
			return null;
		
		return this.bookList.get(index);
	}
	
	public Book[] getArray() {
		Book[] bookArray = new Book[this.bookList.size()];
		
		return this.bookList.toArray(bookArray);
	}

	
	public void removeByTitle(String author, String title) {
		Book k = new Book(author, title, 0);
		
		if(!this.bookList.contains(k))
			return;
		
		this.bookList.remove(k);
	}
	
	public Book[] bookArrayOrderedByDeafult() {
		Book[] bookArray  = new Book[this.bookList.size()]; 
		
		this.bookList.toArray(bookArray);
				
		Arrays.sort(bookArray);
		
		return bookArray;
	}
	
	public Book[] bookArrayOrderedByPrice() {
		Book[] bookArray  = new Book[this.bookList.size()]; 
		
		this.bookList.toArray(bookArray);
				
		Arrays.sort(bookArray, new Book.PriceComparator().reversed());
		
		return bookArray;
	}
	
	public void listOrderedByPriceJava8Comp() {
		Collections.sort(this.bookList, Comparator.comparing(Book::getTitle).reversed());
	}
	
	public Book getMostExpensive() {
		if(this.bookList.isEmpty())
			return null;
		
		Book max = this.bookList.get(0);
		
		for(Book item : this.bookList) {
			if(item.compareByPrice(max) == 1)
				max = item;
		}
		
		return max;
	}

}