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

import it.polimi.ingsw.gc14.Model.Cards.Building.Effects.Building1;
import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.CharacterType;
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 Sustenance extends EventCard {

    /**
     * The prestige penalty multiplier applied for each unpaid Food unit.
     */
    private int PrestigeDebt;

    /**
     * Returns the prestige penalty multiplier associated with this Sustenance event.
     *
     * @return the prestige penalty multiplier associated with this Sustenance event.
     */
    public int getPrestigeDebt() {
        return PrestigeDebt;
    }

    /**
     * Creates a Sustenance event card with the specified era and prestige debt value.
     *
     * @param Era the era of the event card.
     * @param PrestigeDebt the prestige penalty multiplier for unpaid Food units.
     */
    public Sustenance(int Era, int PrestigeDebt) {
        super(Era, EventType.SUSTENANCE);
        this.PrestigeDebt = PrestigeDebt;
    }

    /**
     * Activates the Sustenance event for the specified list of players.
     * For each player, the required Food is computed from the total number of characters,
     * reduced by the contribution of Gatherers and by any applicable character discounts
     * granted by owned building cards with effect id equal to 1.
     * If the resulting Food debt is positive, the player must pay it with available Food.
     * If the player does not have enough Food, all remaining Food is removed and the player
     * loses Prestige equal to the unpaid Food debt multiplied by {@code PrestigeDebt}.
     * This event is intended to be executed last among event effects.
     *
     * @param playerList the list of players affected by the event.
     * @throws NullPointerException if {@code playerList} or one of its required elements is {@code null}.
     */
    @Override
    public void activateEvent (ArrayList <Player> playerList) throws NullPointerException {
        for(Player player : playerList){
            int NChar = player.getTotCharacters();
            int NGatherers = player.getNType(CharacterType.GATHERER);

            int NCharDiscount = 0;
            for(Player p : playerList){
                for(BuildingCard b : player.buildingCards){
                    if(b.getEffectId() == 1){
                        CharacterType c = ((Building1)b).getIcon();
                        NCharDiscount += p.getNType(c);
                    }
                }
            }

            int FoodDebt = NChar - (3 * NGatherers + NCharDiscount);

            if(FoodDebt <= 0){
                continue;
            }
            if(player.getFoodValue() >= FoodDebt){
                player.removeFood(FoodDebt);
            }
            else{
                FoodDebt -= player.getFoodValue();
                player.removeFood(player.getFoodValue());               // player.getFoodValue() == 0
                player.removePrestige(FoodDebt * PrestigeDebt);
            }
        }
    }

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