package de.thpeetz.kontor.bookshelf.services;

import java.util.List;

import de.thpeetz.kontor.bookshelf.data.*;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class BookshelfService {

    private final AuthorRepository authorRepository;
    @SuppressWarnings("unused")
    private final ArticleAuthorRepository articleAuthorRepository;
    private final ArticleRepository articleRepository;
    private final BookRepository bookRepository;
    @SuppressWarnings("unused")
    private final BookAuthorRepository bookAuthorRepository;
    private final BookshelfPublisherRepository publisherRepository;

    public BookshelfService(AuthorRepository authorRepository, ArticleAuthorRepository articleAuthorRepository,
            ArticleRepository articleRepository, BookRepository bookRepository,
            BookAuthorRepository bookAuthorRepository, BookshelfPublisherRepository publisherRepository) {
        this.authorRepository = authorRepository;
        this.articleAuthorRepository = articleAuthorRepository;
        this.articleRepository = articleRepository;
        this.bookRepository = bookRepository;
        this.bookAuthorRepository = bookAuthorRepository;
        this.publisherRepository = publisherRepository;
    }

    public List<BookshelfPublisher> findAllPublishers(String stringFilter) {
        if (stringFilter == null || stringFilter.isEmpty()) {
            return publisherRepository.findAll();
        } else {
            return publisherRepository.search(stringFilter);
        }
    }

    public BookshelfPublisher findPublisherByName(String publisherName) {
        return publisherRepository.findByName(publisherName);
    }

    public void deletePublisher(BookshelfPublisher publisher) {
        publisherRepository.delete(publisher);
    }

    public void savePublisher(BookshelfPublisher publisher) {
        if (publisher == null) {
            log.warn("Publisher is null. Are you sure you have connected your form to the application?");
            return;
        }
        publisherRepository.save(publisher);
    }

    public List<Author> findAllAuthors(String filter) {
        if (filter == null || filter.isEmpty()) {
            return authorRepository.findAll();
        } else {
            return authorRepository.search(filter);
        }
    }

    public void saveAuthor(Author author) {
        if (author == null) {
            log.warn("Author is null. Are you sure you have connected your form to the application?");
            return;
        }
        authorRepository.save(author);
    }

    public void deleteAuthor(Author author) {
        authorRepository.delete(author);
    }

    public List<Book> findAllBooks(String filter) {
        if (filter == null || filter.isEmpty()) {
            return bookRepository.findAll();
        } else {
            return bookRepository.search(filter);
        }
    }

    public void saveBook(Book book) {
        if (book == null) {
            log.warn("Book is null. Are you sure you have connected your form to the application?");
            return;
        }
        bookRepository.save(book);
    }

    public void deleteBook(Book book) {
        BookshelfPublisher publisher = book.getPublisher();
        publisher.getBooks().remove(book);
        publisherRepository.save(publisher);
        bookRepository.delete(book);
    }

    public List<Article> findAllArticles(String filter) {
        if (filter == null || filter.isEmpty()) {
            return articleRepository.findAll();
        } else {
            return articleRepository.search(filter);
        }
    }

    public void saveArticle(Article article) {
        if (article == null) {
            log.warn("Article is null, Are you sure you have connected your form to the application?");
            return;
        }
        articleRepository.save(article);
    }

    public void deleteArticle(Article article) {
        articleRepository.delete(article);
    }
}
