package Server;

import Server.Cards.BuildingCard;
import Server.Cards.CharacterCard;
import Server.Cards.CharacterType;
import Server.Cards.Trigger;
import Server.Era;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

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

class TribeTest {
    private Tribe tribe;

    @BeforeEach
    public void setUp(){
        tribe = new Tribe(); // creiamo una tribù nuova prima di ogni test
    }

    @Test
    public void testGathererDiscount() {
        // 2 gatherer -> 6 cibi scontati
        tribe.addCharacter(new CharacterCard(1, 2, Era.I, CharacterType.GATHERER, 3, 0));
        tribe.addCharacter(new CharacterCard(2, 2, Era.I, CharacterType.GATHERER, 3, 0));

        assertEquals(6,tribe.gathererDiscount());
    }

    @Test
    public void testArtistsEndPoints() {
        // 5 artisti = 2 coppie. 2 * 10 = 20 punti.
        tribe.addCharacter(new CharacterCard(14, 2, Era.I, CharacterType.ARTIST, 0, 0));
        tribe.addCharacter(new CharacterCard(15, 2, Era.I, CharacterType.ARTIST, 0, 0));
        tribe.addCharacter(new CharacterCard(16, 2, Era.I, CharacterType.ARTIST, 0, 0));
        tribe.addCharacter(new CharacterCard(17, 3, Era.I, CharacterType.ARTIST, 0, 0));
        tribe.addCharacter(new CharacterCard(18, 4, Era.I, CharacterType.ARTIST, 0, 0));

        assertEquals(20, tribe.artistsEndPoints());
    }

    @Test
    public void testInventorsEndPoints() {
        // 3 Inventori, ma solo 2 invenzioni diverse: 2 (amo da pesca) e 3 (corda)
        // 3 * 2= 6 punti
        tribe.addCharacter(new CharacterCard(19, 2, Era.I, CharacterType.INVENTOR, 2, 0)); //statuetta
        tribe.addCharacter(new CharacterCard(53, 2, Era.II, CharacterType.INVENTOR, 2, 0)); //statuetta
        tribe.addCharacter(new CharacterCard(20, 2, Era.I, CharacterType.INVENTOR, 0, 0)); // canoa

        assertEquals(6, tribe.inventorsEndPoints());
    }

    @Test
    public void testBuildingBonusPoints() {
        // Aggiungiamo l'edificio con id 117 che dà 25 punti
        BuildingCard card25 = new BuildingCard(117, 2, Era.III, 10, 0, Trigger.ENDGAME_BONUS_POINTS, null);
        tribe.addBuilding(card25);

        // Se la tribù è vuota, dovrebbe dare esattamente 25 punti
        assertEquals(25, tribe.endPoints(), "L'edificio bonus dovrebbe dare 25 punti");
    }

    @Test
    public void testComplexScenario() {
        // Due Artisti (1 coppia = 10 punti)
        tribe.addCharacter(new CharacterCard(14, 2, Era.I, CharacterType.ARTIST, 0, 0));
        tribe.addCharacter(new CharacterCard(15, 2, Era.I, CharacterType.ARTIST, 0, 0));

        // Un costruttore che vale 5 punti prestigio base
        tribe.addCharacter(new CharacterCard(62, 2, Era.II, CharacterType.BUILDER, 1, 5));

        // L'edificio che raddoppia i punti dei costruttori (card id: 107 - ENDGAME_BUILDER_BONUS)
        BuildingCard doubleBuilderPoints = new BuildingCard(107, 2, Era.II, 6, 4, Trigger.ENDGAME_BUILDER_BONUS, null);
        tribe.addBuilding(doubleBuilderPoints);

        /* CALCOLO ATTESO:
           - Artisti: 10 punti
           - Costruttori base: 5 punti
           - Bonus buildingAbilitiesEndPoints (buildersEndPoints * 2): 5 * 2 = 10 punti
           - Buildings: 4 punti
           TOTALE: 29 punti
        */
        assertEquals(29, tribe.endPoints(), "Il totale dovrebbe essere 29");
    }

    @Test
    public void testComplexScenario2() {
        // PERSONAGGI

        // 3 Artisti (1 coppia = 10 punti)
        tribe.addCharacter(new CharacterCard(14, 2, Era.I, CharacterType.ARTIST, 0, 0));
        tribe.addCharacter(new CharacterCard(15, 2, Era.I, CharacterType.ARTIST, 0, 0));
        tribe.addCharacter(new CharacterCard(16, 2, Era.I, CharacterType.ARTIST, 0, 0));

        // 2 Costruttori (7 punti base totali)
        tribe.addCharacter(new CharacterCard(6, 2, Era.I, CharacterType.BUILDER, 1, 3));
        tribe.addCharacter(new CharacterCard(36, 2, Era.II, CharacterType.BUILDER, 1, 4));

        // 4 Inventori (2 del gruppo 1, 1 del gruppo 2, 1 del gruppo 3 -> 4*3 = 12 punti)
        tribe.addCharacter(new CharacterCard(48, 2, Era.II, CharacterType.INVENTOR, 2, 0)); // Statuetta (iconValue=2)
        tribe.addCharacter(new CharacterCard(76, 2, Era.III, CharacterType.INVENTOR, 2, 0)); // Statuetta
        tribe.addCharacter(new CharacterCard(49, 4, Era.II, CharacterType.INVENTOR, 3, 0)); // Amo da pesca
        tribe.addCharacter(new CharacterCard(23, 4, Era.I, CharacterType.INVENTOR, 5, 0)); // Corda

        // Rimanenti per fare il set da 6
        tribe.addCharacter(new CharacterCard(10, 2, Era.I, CharacterType.GATHERER, 3, 0));
        tribe.addCharacter(new CharacterCard(11, 2, Era.I, CharacterType.GATHERER, 3, 0));
        tribe.addCharacter(new CharacterCard(1, 2, Era.I, CharacterType.HUNTER, 1, 0));
        tribe.addCharacter(new CharacterCard(27, 2, Era.I, CharacterType.SHAMAN, 2, 0));

        // EDIFICI

        // id 107 (Bonus Costruttori) - 4 Punti Base Edificio
        tribe.addBuilding(new BuildingCard(107, 2, Era.II, 6, 4, Trigger.ENDGAME_BUILDER_BONUS, null));

        // id 109 (Set da 6) - 6 Punti Base Edificio
        tribe.addBuilding(new BuildingCard(109, 2, Era.II, 5, 6, Trigger.ENDGAME_FOR_SIX, null));

        // id 112 (Bonus Sciamani) - 4 Punti Base Edificio
        tribe.addBuilding(new BuildingCard(112, 2, Era.III, 7, 4, Trigger.ENDGAME_BONUS_CHARACTER, null));

        // id 117 (25 Punti) - 0 Punti Base Edificio
        tribe.addBuilding(new BuildingCard(117, 2, Era.III, 10, 0, Trigger.ENDGAME_BONUS_POINTS, null));


        // VERIFICA
        // Totale atteso = 92
        assertEquals(92, tribe.endPoints(), "Il totale dovrebbe essere 92 punti");
    }
}