/* indent:4  tabsize:8  font:fixed-width */

#import "DiscardPileDelegate.h"
#import "SuitPileDelegate.h"
#import "Klondike.h"

@implementation DiscardPileDelegate

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

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

- setSuitCardPileViews:(id [])theViews
{
    int i;
    
    for (i=0; i<4; i++)
        suitCardPileViews[i] = *(theViews + i);

    return self;
}


- doubleClickedCard:aCard in:aCardPileView
{
    //int i;
    id tempCard;
    
    id	cardPile = [aCardPileView cardPile];
    
    if(!isEnabled) return nil;
		
    if ((aCard == [cardPile cardAt:CS_TOP] ) && (![aCard isFaceUp]))
    {
		[aCard flip];
		[aCardPileView display];
    }
    else if (aCard == [cardPile cardAt:CS_TOP])
    {
		id homePileView;
		id movePile = [[CardPile allocFromZone:[self zone]] 
	                           initForCardSize:[cardPile returnSize]];
        
		[movePile addCard:aCard];

		if(homePileView = [Dealer() findHomeFor:movePile fromPileView:aCardPileView])
		{
			[[homePileView cardPile] addCard:[cardPile cardAt:CS_TOP]];
			[cardPile removeCard:[cardPile cardAt:CS_TOP]];
			if (tempCard = [recycleCardPile cardAt:CS_TOP])
			{
				[recycleCardPile removeCard:tempCard];
				[cardPile insertCard:tempCard at:CS_BOTTOM];
			}
			[aCardPileView display];
			[homePileView display];
			[Dealer() checkForWin];
		}
    }
    return self;
}


- (BOOL)draggedPile:aCardPile from:discardCardPileView
{
    id discardCardPile = [discardCardPileView cardPile];

    if(!isEnabled) return NO;
		
    if (([aCardPile cardAt:CS_TOP]) == ([discardCardPile cardAt:CS_TOP]))
    {
		//[PBoss() playSound:NV_CARDSOUND];
		return YES;
    }
    return NO;
}


- removedPile:aCardPile from:discardCardPileView
{
    id discardCardPile = [discardCardPileView cardPile];
    id aCard;

    if (maxIndex == 1) return self;
    
    if ([recycleCardPile cardCount] != 0)
    {
        aCard = [recycleCardPile cardAt:CS_TOP];
	[recycleCardPile removeCard:aCard];
	[discardCardPile insertCard:aCard at:CS_BOTTOM];
	[discardCardPileView display];
    }

    return self;
}


- getOffset:(float *) xOffset :(float *) yOffset forSize:(CardSize)aSize
{
    if (aSize == CS_SMALL)
    {
        if (maxIndex == 1)
	    *xOffset = 0.5;
	else
	    *xOffset = 15.0;
    }
    else
    {
        if (maxIndex == 1)
	    *xOffset = 0.5;
	else
	    *xOffset = 20.0;
    }    
    *yOffset = 0.0;
    return self;
}

@end
