package it.polimi.ingsw.gc14.Model.Cards.Building.Effects;

import it.polimi.ingsw.gc14.Model.Cards.Building.EffectType;
import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
import it.polimi.ingsw.gc14.Model.Player;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.CharacterType;



/**
 * From the moment you buy this building, every time you complete a set of 6 different characters, you gain 5 food.
 * Note: sets of characters already present before purchasing the building do not count.
 */
public class Building0 extends BuildingCard {

    /**
     * The purchased attribute is used to indicate whether the card has already been initialized.
     */
    boolean purchased ;

    /**
     * The numSet attribute indicates how many complete sets the player has.
     */
    private int numSet;

    /**
     * Constructor of the building
     * @param era The game era of the building.
     * @param price The price (in food) of the building.
     * @param prestigeValue The number of Prestige Points gained from this building at the end of the game.
     */
    public Building0(int era,int price,int prestigeValue) {
        super(era,price,prestigeValue);
        effectType=EffectType.CARD_SET;
        effectId=0;
        purchased = false;
        numSet = 0;
    }

    /**
     * Method used to purchase the building. It also handles its initialization.
     * @param player The player who buys the building.
     * @return The outcome of the operation.
     */
    @Override
    public boolean buy(Player player)
    {
        if(!super.buy(player))
            return false;
        initialize(player);
        return true;
    }

    /**
     * Initializes the building by counting the number of complete sets at the moment of purchase.
     * The building can only be initialized once.
     * @param player The player who owns the building, whose cards are counted to determine the number of sets.
     */
    public void initialize(Player player)
    {
        if(purchased)
        {
            return;
        }
        numSet=player.getNType(CharacterType.INVENTOR);
        for(CharacterType type : CharacterType.values())
        {
            if(player.getNType(type)<=numSet)
                numSet=player.getNType(type);
        }
        purchased = true;
    }

    /**
     * Method to clone the building: it creates an exact copy.
     * @return The new copy of the building
     */
    @Override
    public BuildingCard clone() {
        return new Building0(getEra(),getPrice(),getPrestigeValue());
    }

    /**
     * Calculates the current number of card sets and subtracts the previous value (i.e., the number of newly obtained sets).
     * The player gains an amount of food equal to 5 times this result.
     * @param player The player who owns the building.
     * @throws IllegalArgumentException thrown if the specified player does not own this card.
     */
    @Override
    public void applyEffect(Player player) throws IllegalArgumentException {
        if(!player.buildingCards.contains(this))
            throw new IllegalArgumentException();
        int min_temp=player.getNType(CharacterType.INVENTOR);
        for(CharacterType type : CharacterType.values())
        {
            if(player.getNType(type)<=min_temp)
                min_temp=player.getNType(type);
        }
        if(min_temp<=numSet)
        {
            return;
        }
        player.addFood(5*(min_temp-numSet));
        numSet=min_temp;
    };

}
