
#import "DrawPileDelegate.h"
#import "../NEXTVegas/CardSet.subproj/cardset.h"
#import "Klondike.h"

@implementation DrawPileDelegate

- setEnabled:(BOOL)flag
{
	isEnabled = flag;
	return self;
}

- setMaxPasses:(int)aNumber
{
	numPasses = 0;
	maxPasses = aNumber;
	
	if(maxPasses == KL_INFINITEPASS)
	{
		[passInfoText setStringValue:"Passes"];
		[passText setIntValue:numPasses+1];
	}
	else
	{
		[passInfoText setStringValue:"Passes Left"];
		[passText setIntValue:maxPasses-1];
	}
	
	return self;
}
	
- setDiscardCardPileView:theView
{
    discardCardPileView = theView;
    return self;
}

- setDrawCardCount:(int)theCount
{
    maxIndex = theCount;
    return self;
}

- clickedCard:aCard in:drawCardPileView
{
    id    discardCardPile = [discardCardPileView cardPile];
    id    drawCardPile = [drawCardPileView cardPile];
    int   drawIndex;
    id    drawCard;

    // Two cases where aCard will be nil:
    //
    // (1) if Draw Pile is empty
    //
    // (2) if Draw Pile is NOT empty but user has clicked on an exposed
    //     area of the pile
    //
    // This is all pretty ugly because we want the draw-three option to
    // behave "nicely."
    
    if(!isEnabled) return nil;
		
	if(maxPasses != KL_INFINITEPASS && numPasses >= maxPasses) return self;
	
	if (aCard)
	{
		if (maxIndex == 1)
		{
			if (drawCard = [drawCardPile cardAt:CS_TOP])
			{
				[drawCardPile removeCard:drawCard];
				[drawCard flip];
				[discardCardPile addCard:drawCard];
			}
		}
		else
		{
			int extra;
	    
			[recycleCardPile addPile:discardCardPile];
			[discardCardPile empty];
	    
			for (drawIndex = 0; drawIndex < maxIndex; drawIndex++)
			{
				if (drawCard = [drawCardPile cardAt:CS_TOP])
				{
					[drawCardPile removeCard:drawCard];
					[drawCard flip];
					[discardCardPile addCard:drawCard];
				}
			}
			extra = maxIndex - [discardCardPile cardCount];
			for (drawIndex = 0; drawIndex < extra; drawIndex++)
			{
				if (aCard = [recycleCardPile cardAt:CS_TOP])
				{
					[recycleCardPile removeCard:aCard];
					[discardCardPile insertCard:aCard at:CS_BOTTOM];
				}	    
			}
		}
    }
	else if ([drawCardPile cardCount] == 0)
    {
		numPasses++;
		if(maxPasses != KL_INFINITEPASS && numPasses >= maxPasses)
		{
			[Dealer() endGame];
			return self;
		}
		
		if(maxPasses == KL_INFINITEPASS)
		{
			[passText setIntValue:numPasses+1];
		}
		else
		{
			[passText setIntValue:maxPasses-numPasses-1];
		}
			
		if (maxIndex == 1)
		{
			[drawCardPile addPile:discardCardPile];
			[discardCardPile empty];
			[drawCardPile flip];
		}
		else
		{
			[recycleCardPile addPile:discardCardPile];
			[discardCardPile empty];
	    
			[recycleCardPile flip];
			[drawCardPile addPile:recycleCardPile];
			[recycleCardPile empty];
		}
	}    
    
	//[PBoss() playSound:NV_CARDSOUND];
    [discardCardPileView display];
    [drawCardPileView display];

	if([drawCardPile cardCount] == 0 &&
		maxPasses != KL_INFINITEPASS && (numPasses+1) >= maxPasses)
		[Dealer() endGame];

    return self;
}
 
@end
