package it.polimi.ingsw.gc14.Model;

import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
import it.polimi.ingsw.gc14.Model.Cards.TribeCard;
import org.junit.jupiter.api.Test;

import java.util.*;

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

class DecksCreatorTest {

    @Test
    void loadDeck() {
        List<TribeCard> cards = DecksCreator.loadTribeDeck("/Cards/tribe_era1.json");
        assertEquals(33,cards.size());
        List<TribeCard> cards2 = DecksCreator.loadTribeDeck("/Cards/tribe_era2.json");
        assertEquals(32,cards2.size());
        List<TribeCard> cards3 = DecksCreator.loadTribeDeck("/Cards/tribe_era3.json");
        assertEquals(29,cards3.size());
    }

    @Test
    void loadDeckWrongEra() {
        assertThrows(IllegalArgumentException.class, () -> DecksCreator.loadTribeDeckByEra(0));
        assertThrows(IllegalArgumentException.class, () -> DecksCreator.loadTribeDeckByEra(4));
        assertThrows(IllegalArgumentException.class, () -> DecksCreator.loadTribeDeckByEra(-1));
    }

    @Test
    void loadDeckWrongPath() {
        assertThrows(RuntimeException.class, () -> DecksCreator.loadTribeDeck("/Cards/not_found.json"));
    }

    @Test
    void loadBuildingDeck() {
        List<BuildingCard> allCards = DecksCreator.loadBuildingDeck("/Cards/buildingCards.json");

        assertNotNull(allCards);
        assertFalse(allCards.isEmpty());
        assertFalse(allCards.contains(null));
    }

    @Test
    void loadBuildingDeckByEra() {
        List<BuildingCard> allCards = DecksCreator.loadBuildingDeck("/Cards/buildingCards.json");

        List<BuildingCard> era1 = DecksCreator.loadBuildingDeckByEra(1);
        List<BuildingCard> era2 = DecksCreator.loadBuildingDeckByEra(2);
        List<BuildingCard> era3 = DecksCreator.loadBuildingDeckByEra(3);

        assertEquals(allCards.stream().filter(c -> c.getEra() == 1).count(), era1.size());
        assertEquals(allCards.stream().filter(c -> c.getEra() == 2).count(), era2.size());
        assertEquals(allCards.stream().filter(c -> c.getEra() == 3).count(), era3.size());

        assertTrue(era1.stream().allMatch(c -> c.getEra() == 1));
        assertTrue(era2.stream().allMatch(c -> c.getEra() == 2));
        assertTrue(era3.stream().allMatch(c -> c.getEra() == 3));
    }

    @Test
    void loadBuildingDeckWrongEra() {
        assertThrows(IllegalArgumentException.class, () -> DecksCreator.loadBuildingDeckByEra(0));
        assertThrows(IllegalArgumentException.class, () -> DecksCreator.loadBuildingDeckByEra(4));
        assertThrows(IllegalArgumentException.class, () -> DecksCreator.loadBuildingDeckByEra(-1));
    }

    @Test
    void loadBuildingDeckWrongPath() {
        assertThrows(RuntimeException.class, () -> DecksCreator.loadBuildingDeck("/Cards/not_found.json"));
    }

    @Test
    void loadSlotDeck() {
        List<Slot> slots = DecksCreator.loadSlotDeck();

        assertNotNull(slots);
        assertEquals(7, slots.size());
        assertFalse(slots.contains(null));
    }
}
