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

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


@implementation DiscardPileDelegate

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

- (void) setSuitCardPileViews:(id [])theViews
{
    int i;

    for (i=0; i<4; i++) {
        suitCardPileViews[i] = *(theViews + i);
    }
}


- (void) doubleClickedCard:(Card*)aCard in:(CardPileView*)aCardPileView
{
    int 		i;
    Card* 		tempCard;
    CardPile*	cardPile = [aCardPileView cardPile];
    
    if ((aCard == [cardPile topCard] ) && (![aCard isFaceUp]))
    {
        [aCard flip];
        [aCardPileView display];
    }
    else if (aCard == [cardPile topCard])
    {
        for (i=0;i<4;i++) {
            [[suitCardPileViews[i] delegate] setDoubleClickCheck];
            if ([[suitCardPileViews[i] delegate] canAcceptPile:cardPile from:aCardPileView in:suitCardPileViews[i]]) {
                [[suitCardPileViews[i] cardPile] addCard:[cardPile topCard]];
                [cardPile removeCard:[cardPile topCard]];
                if (tempCard = [recycleCardPile dealTopCard]) {
                    [cardPile insertCard:tempCard at:CS_BOTTOM];
                }
                [aCardPileView display];
                [suitCardPileViews[i] display];
                [[Klondike sharedInstance] checkForWin];
                break;
            }
        }
    } 
}


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

    if (([aCardPile topCard]) == ([discardCardPile topCard]))
    {
        return YES;
    }
    return NO;
}


- (void) removedPile:(CardPile*)aCardPile from:(CardPileView*)discardCardPileView
{
    CardPile* 	discardCardPile = [discardCardPileView cardPile];
    Card* 		aCard;

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


- (void) 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; 
}

@end
