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.Cards.TribeCards.Character;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Characters.Inventor;
import it.polimi.ingsw.gc14.Model.Player;

import java.util.HashMap;

public class Building4 extends BuildingCard {

    boolean purchased ;
    int numPair;
    public Building4(int era, int price,int prestigeValue) {
        super(era,price,prestigeValue);
        effectType= EffectType.INVENTOR_PAIR;
        effectId=4;
    }

    public void initialize(Player player)
    {
        if(purchased)
        {
            return;
        }
        HashMap<Integer,Integer> map = new HashMap<>();
        for(int i=0;i<10;i++)
        {
            map.put(i,0);
        }
        for(Inventor inv : player.inventors)
        {
            map.merge(inv.Icon(),1,Integer::sum);
        }
        numPair=0;
        for(Integer i : map.values())
        {
            numPair+=(i/2);
        }
        purchased = true;

    }

    @Override
    public boolean buy(Player player)
    {
        if(!super.buy(player))
            return false;
        initialize(player);
        return true;
    }

    @Override
    public BuildingCard clone() {
        return new Building4(getEra(),getPrice(),getPrestigeValue());
    }
    @Override
    public void applyEffect(Player player) throws IllegalArgumentException {
        if(!player.buildingCards.contains(this))
            throw new IllegalArgumentException();
        HashMap<Integer,Integer> map = new HashMap<>();
        for(int i=0;i<10;i++)
        {
            map.put(i,0);
        }
        for(Inventor inv : player.inventors)
        {
            map.merge(inv.Icon(),1,Integer::sum);
        }
        int temp=0;
        for(Integer i : map.values())
        {
            temp+=(i/2);
        }
        if(temp<= numPair)
        {
            return;
        }
        player.addFood(3*(temp-numPair));
        numPair = temp;
    };
}
