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 {
    private int PrestigeDebt;
    public int getPrestigeDebt() {
        return PrestigeDebt;
    }

    public Sustenance(int Era, int PrestigeDebt) {
        super(Era, EventType.SUSTENANCE);
        this.PrestigeDebt = PrestigeDebt;
    }

    // Sustenence va eseguito per ultimo tra gli eventi
    @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);
            }
        }
    }
    @Override
    public EventCard clone() {
        return new Sustenance(getEra(), PrestigeDebt);
    }
}
