/**
 * 
 */
package ats_jp.activity.cardgame;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * @author TMS
 *
 */
public abstract class CardDeck {
	
	/**
	 * utlizzo una ArrayList in quanto più performante, non ho bisogno di tenere
	 * gli elementi ordinati in quanto ho un unico metodo di ricerca (l'altro 
	 * preleva unicamente un elemento Random)
	 */
	protected ArrayList<Card> store;

	/**
	 * metodo per l'inizializzazione del mazzo con valori Random; tale metodo 
	 * dovrà essere obbligatoriamente sovrascritto nelle classi successive
	 */
	protected abstract void initializeDeck();
	
	public CardDeck()
	{
		store=new ArrayList<Card>();

		initializeDeck();
	}
	
	/**
	 * elimina e restituisce una carta random dal mazzo;
	 * se il mazzo è vuoto restituisce null
	 * 
	 * @return
	 */
	public Card getCard()
	{
		Card returnCard=null;
		if(!store.isEmpty())
		{
			returnCard=store.remove(randomGenerator(0, store.size()-1));
		}
		
		return returnCard;
	}
	
	/**
	 * elimina e restituisce la carta uguale a paramCard;
	 * se non è presente o il mazzo è vuoto torna null
	 * 
	 * @param card
	 * @return
	 */
	public Card getCard(Card paramCard)
	{
		Card returnCard=null;
		if(paramCard!=null)
		{
			if(!store.isEmpty() && store.contains(paramCard))
			{
				returnCard=store.remove(store.indexOf(paramCard));
			}
		} else {
			throw new IllegalArgumentException("Null argument");
		}
		
		return returnCard;
	}
	
	/**
	 * inserisce paramCard nel mazzo;
	 * nel caso paramCard==null solleva un'eccezione (IllegalArgumentException)
	 * 
	 * @param paramCard
	 * @return
	 */
	public boolean put(Card paramCard)
	{
		boolean flag=false;
		
		if(paramCard!=null)
		{
			flag=store.add(paramCard);
		} else {
			throw new IllegalArgumentException("Null Argument");
		}
		
		return flag;
	}
	
	/**
	 * restituisce il numero di carte nel mazzo
	 * 
	 * @return
	 */
	public int getCurrentCount()
	{
		return store.size();
	}
	
	private int randomGenerator(int min, int max) 
	{
		int result=-1;
		if(min>max) {
			throw new IllegalArgumentException("Min value greatest!");
		} else {
			Random mRandom = new Random();
			result = (int) mRandom.nextInt(max-min+1)+min;
		}
		return result;
	}
	
}
