/**
 * Copyright (C) 2022 by Martin Robillard. See https://codesample.info/about.html
 */
package e2.chapter8;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * A card source composed of other card sources.
 */
public class CompositeCardSource implements CardSource, Iterable<CardSource>
{
	private final List<CardSource> aElements;
	
	@Override
	public Iterator<CardSource> iterator()
	{
		return aElements.iterator();
	}
	
	public CompositeCardSource(CardSource... pSources)
	{
		aElements = Arrays.asList(pSources);
	}
	
	@Override
	public Card draw()
	{
	  assert !isEmpty();
	  for( CardSource source : aElements )
	  {
	    if( !source.isEmpty() )
	    {
	      return source.draw();
	    }
	  }
	  assert false;
	  return null;
	}

	@Override
	public boolean isEmpty()
	{
		for( CardSource source : aElements )
		{
			if( !source.isEmpty() )
			{ return false; }
		}
		return true;
	}
	
	@Override
	public void accept(CardSourceVisitor pVisitor)
	{
		pVisitor.visitCompositeCardSource(this);
	}
}