package de.thpeetz.kontor.tysc.services;

import static org.junit.jupiter.api.Assertions.*;

import java.util.List;

import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import de.thpeetz.kontor.tysc.TestConstants;
import de.thpeetz.kontor.tysc.data.Card;
import de.thpeetz.kontor.tysc.data.CardSet;
import de.thpeetz.kontor.tysc.data.Rooster;
import de.thpeetz.kontor.tysc.data.Vendor;

@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class CardServiceTest {

    @Autowired
    private CardService cardService;

    @Autowired
    private SportService sportService;

    @Test
    @Order(1)
    void testFindAllVendors() {
        assertEquals(TestConstants.VENDOR_COUNT, cardService.findAllVendors(null).size());
        assertEquals(2, cardService.findAllVendors("pp").size());
    }

    @Test
    @Order(2)
    void testSaveVendor() {
        Vendor vendor = new Vendor();
        vendor.setName(TestConstants.VENDOR_NAME);
        vendor = cardService.saveVendor(vendor);
        assertNotNull(vendor);
        assertEquals(TestConstants.VENDOR_COUNT + 1, cardService.findAllVendors(null).size());
    }

    @Test
    @Order(3)
    void testDeleteVendor() {
        List<Vendor> vendors = cardService.findAllVendors(TestConstants.VENDOR_NAME);
        assertEquals(1, vendors.size());
        Vendor vendor = vendors.get(0);
        cardService.deleteVendor(vendor);
        assertEquals(TestConstants.VENDOR_COUNT, cardService.findAllVendors(null).size());
    }

    @Test
    @Order(4)
    void testFindAllCardSets() {
        assertEquals(TestConstants.CARDSET_COUNT, cardService.findAllCardSets(null).size());
        assertEquals(1, cardService.findAllCardSets("Ultra").size());
    }

    @Test
    @Order(5)
    void testSaveCardSet() {
        List<Vendor> vendors = cardService.findAllVendors(TestConstants.VENDOR_FLEER);
        assertEquals(1, vendors.size());
        Vendor vendor = vendors.get(0);
        CardSet cardSet = new CardSet();
        cardSet.setName(TestConstants.CARDSET_NAME);
        cardSet.setVendor(vendor);
        cardSet.setInsertSet(false);
        cardSet.setParallelSet(false);
        cardService.saveCardSet(cardSet);
        assertEquals(TestConstants.CARDSET_COUNT + 1, cardService.findAllCardSets(null).size());
    }

    @Test
    @Order(6)
    void testDeleteCardSet() {
        List<CardSet> cardSets = cardService.findAllCardSets(TestConstants.CARDSET_NAME);
        assertEquals(1, cardSets.size());
        CardSet cardSet = cardSets.get(0);
        cardService.deleteCardSet(cardSet);
        assertEquals(TestConstants.CARDSET_COUNT, cardService.findAllCardSets(null).size());
    }

    @Test
    @Order(7)
    void testFindAllCards() {
        assertEquals(TestConstants.CARD_COUNT, cardService.findAllCards(null).size());
        assertEquals(1, cardService.findAllCards("112").size());
    }

    @Test
    @Order(8)
    void testSaveCard() {
        List<Vendor> vendors = cardService.findAllVendors(TestConstants.VENDOR_FLEER);
        assertEquals(1, vendors.size());
        Vendor vendor = vendors.get(0);
        List<CardSet> cardSets = cardService.findAllCardSets(TestConstants.CARDSET_MYSTIQUE_NAME);
        assertEquals(3, cardSets.size());
        CardSet cardSet = cardSets.get(0);
        List<Rooster> roosters = sportService.findAllRoosters();
        Rooster rooster = roosters.get(0);
        Card card = new Card();
        card.setCardNumber(TestConstants.CARD_CARDNUMBER);
        card.setCardSet(cardSet);
        card.setVendor(vendor);
        card.setRooster(rooster);
        card.setYear(TestConstants.CARD_YEAR);
        cardService.saveCard(card);
        assertEquals(TestConstants.CARD_COUNT + 1, cardService.findAllCards(null).size());
    }

    @Test
    @Order(9)
    void testDeleteCard() {
        List<Card> cards = cardService.findAllCards(TestConstants.CARD_CARDNUMBER.toString());
        assertEquals(1, cards.size());
        Card card = cards.get(0);
        cardService.deleteCard(card);
        assertEquals(TestConstants.CARD_COUNT, cardService.findAllCards(null).size());
    }
}
