package de.thpeetz.kontor.tysc.services;

import java.util.List;

import de.thpeetz.kontor.tysc.repository.CardRepository;
import de.thpeetz.kontor.tysc.repository.CardSetRepository;
import de.thpeetz.kontor.tysc.repository.VendorRepository;
import org.springframework.stereotype.Service;

import de.thpeetz.kontor.tysc.data.Card;
import de.thpeetz.kontor.tysc.data.CardSet;
import de.thpeetz.kontor.tysc.data.Vendor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class CardService {

    private VendorRepository vendorRepository;

    private CardSetRepository cardSetRepository;

    private CardRepository cardRepository;

    public CardService(VendorRepository vendorRepository, CardSetRepository cardSetRepository, CardRepository cardRepository) {
        this.vendorRepository = vendorRepository;
        this.cardSetRepository = cardSetRepository;
        this.cardRepository = cardRepository;
    }

    public List<Vendor> findAllVendors(String stringFilter) {
        if (stringFilter == null || stringFilter.isEmpty()) {
            return vendorRepository.findAll();
        } else {
            return vendorRepository.search(stringFilter);
        }
    }

    public void deleteVendor(Vendor vendor) {
        vendorRepository.delete(vendor);
    }

    public Vendor saveVendor(Vendor vendor) {
        if (vendor == null) {
            log.warn("Vendor is null. Are you sure you have connected your form to the application?");
            return null;
        }
        return vendorRepository.save(vendor);
    }

    public List<Card> findAllCards(String stringFilter) {
        log.info("find cards with filter: {}", stringFilter);
        if (stringFilter == null || stringFilter.isEmpty()) {
            List<Card> cards = cardRepository.findAll();
            log.debug("found {} cards", cards.size());
            return cards;
        } else {
            return cardRepository.search(stringFilter);
        }
    }

    public void deleteCard(Card card) {
        cardRepository.delete(card);
    }

    public Card saveCard(Card card) {
        if (card == null) {
            log.warn("Card is null. Are you sure you have connected your form to the application?");
            return null;
        }
        return cardRepository.save(card);
    }

	public List<CardSet> findAllCardSets(String stringFilter) {
        if (stringFilter == null || stringFilter.isEmpty()) {
            return cardSetRepository.findAll();
        } else {
            return cardSetRepository.search(stringFilter);
        }
	}

    public CardSet saveCardSet(CardSet cardSet) {
        if (cardSet == null) {
            log.warn("CardSet is null. Are you sure you have connected your form to the application?");
            return null;
        }
        return cardSetRepository.save(cardSet);
    }

    public void deleteCardSet(CardSet cardSet) {
        cardSetRepository.delete(cardSet);
    }
}
