package it.polimi.ingsw.gc14.Model.Orders;

import it.polimi.ingsw.gc14.Model.OrderLogicCard;
import it.polimi.ingsw.gc14.Model.Player;

import java.util.ArrayList;
import java.util.NoSuchElementException;

public class Order4 extends OrderLogicCard {

    /**
     * Creates an Order4 logic card for a four-player game.
     *
     * @param players the list of players associated with this order card.
     * @throws NoSuchElementException if the number of players is not equal to 4.
     */
    public Order4(ArrayList<Player> players)throws NoSuchElementException {
        if(players.size()!=4)
            throw new NoSuchElementException();
        super(players);
    }

    /**
     * Applies the effect associated with the specified position index for the given player.
     * <p>If {@code index == 0}, the player gains 2 Food and the building effect is applied.
     * <p>If {@code index == 1}, the player gains 1 Food and the building effect is applied.
     * <p>If {@code index == 2}, no effect is applied.
     * <p>If {@code index == 3}, the player tries to remove 1 Food; if the removal succeeds,
     * the player loses 2 Prestige.
     *
     * @param player the player to whom the effect is applied.
     * @param index the position index of the effect to apply.
     * @throws IndexOutOfBoundsException if {@code index < 0} or {@code index >= 4}.
     */
    @Override
    protected void effect(Player player,int index) throws IndexOutOfBoundsException
    {
        if(index>=4 || index < 0)
        {
            throw new IndexOutOfBoundsException("index out of bounds");
        }
        if(index==0)
        {
            player.addFood(2);
            buildingEffect(player);
            return;
        }
        if(index==1)
        {
            player.addFood(1);
            buildingEffect(player);
            return;
        }
        if(index==3){
            if(player.removeFood(1)){
                player.removePrestige(2);
            }
        }
    }
}