package it.polimi.ingsw.gc14.Model.Cards.TribeCards.Events;

import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
import it.polimi.ingsw.gc14.Model.Cards.TribeCard;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Character;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.CharacterType;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Characters.Artist;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.EventType;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.EventCard;
import it.polimi.ingsw.gc14.Model.Player;
import java.util.ArrayList;

public class CavePaintings extends EventCard {

    /**
     * The minimum number of Artist cards required to avoid the prestige penalty.
     * Also, the bottom number on the card.
     */
    private int NLower;

    /**
     * The amount of Prestige removed if the player has fewer Artist cards than {@code NLower}.
     */
    private int NPrestigeRem; // NPrestigeLower

    /**
     * The Prestige multiplier applied if the player has at least {@code NLower} Artist cards.
     */
    private int NPrestigeMul; // NPrestigeUpper

    /**
     * Creates a CavePaintings event card with the specified era and effect parameters.
     *
     * @param Era the era of the event card.
     * @param NLower the minimum number of Artist cards required to avoid the prestige penalty.
     * @param NPrestigeRem the amount of Prestige removed if the player has fewer Artist cards than {@code NLower}.
     * @param NPrestigeMul the Prestige multiplier applied if the player has at least {@code NLower} Artist cards.
     */
    public CavePaintings(int Era, int NLower, int NPrestigeRem, int NPrestigeMul) {
        super(Era, EventType.CAVE_PAINTINGS);
        this.NLower = NLower;
        this.NPrestigeRem = NPrestigeRem;
        this.NPrestigeMul = NPrestigeMul;
    }

    /**
     * Activates the CavePaintings event for the specified list of players.
     * For each player, the number of Artist cards is computed together with the number
     * of owned building cards having effect id equal to 9.
     * The player gains Food equal to the number of such buildings multiplied by the number of Artist cards.
     * If the player has fewer Artist cards than {@code NLower}, the player loses {@code NPrestigeRem} Prestige.
     * Otherwise, the player gains Prestige equal to {@code NPrestigeMul} multiplied by the number of Artist cards.
     *
     * @param playerList the list of players affected by the event.
     */
    @Override
    public void activateEvent (ArrayList <Player> playerList){
        for (Player player : playerList){
            int NArtists = player.getNType(CharacterType.ARTIST);
            int NBuildings = 0;
            ArrayList<BuildingCard> buildingCards = player.buildingCards;

            for(BuildingCard card : buildingCards){
                if(card.getEffectId() == 9){
                    NBuildings++;
                }
            }
            player.addFood(NBuildings * NArtists);
            if (NArtists < NLower){
                player.removePrestige(NPrestigeRem);
            }
            else{
                player.addPrestige(NPrestigeMul * NArtists);
            }
        }
    }

    /**
     * Creates and returns a copy of this CavePaintings event card.
     *
     * @return a clone of this CavePaintings event card.
     */
    @Override
    public EventCard clone()
    {
        return new CavePaintings(getEra(), NLower, NPrestigeRem, NPrestigeMul) ;
    }
}
