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

#import "FTGameDelegate.h"
#import "FTSuitDelegate.h"
//#import "../Solitaire/Solitaire.h"
#import "FortyThieves.h"

@implementation FTGameDelegate

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


- (void) doubleClickedCard:(Card*)aCard in:(CardPileView*)aCardPileView
{
    int i;
    
    CardPile*    cardPile = [aCardPileView cardPile];
    
    if ((aCard == [cardPile topCard]) && (![aCard isFaceUp]))
    {
        [aCard flip];
        [aCardPileView display];
    }
    else if (aCard == [cardPile topCard])
    {
        for (i=0; i<8; 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]];
                [aCardPileView display];
                [suitCardPileViews[i] display];
                [[FortyThieves sharedInstance] checkForWin];

                break;
            }
        }
    } 
}


- (BOOL) draggedPile:(CardPile*)aCardPile from:(CardPileView*)aCardPileView
{
    CardPile* 	cardPile = [aCardPileView cardPile];
    Card* 		theCard = [aCardPile topCard];
    
    // only move the top card
    if([cardPile topCard] == theCard)
    {
        return YES;
    }
    return NO;
}


- (BOOL) canAcceptPile:(CardPile*)aCardPile from:sender in:(CardPileView*)aCardPileView
{
    Card* droppingCard = [aCardPile bottomCard];
    Card* targetCard = [[aCardPileView cardPile] topCard];
    
    if (targetCard)
    {
        if (([targetCard value] == [droppingCard value] + 1) &&
	    ([targetCard suit] == [droppingCard suit]))
        {
            return YES;
        }
    }
    else
    {
        return YES;
    }
    return NO;
}


- (void) getOffset:(float *)xOffset :(float *)yOffset forSize:(CardSize)aSize
{
    *xOffset = 0.0;
    if (aSize == CS_SMALL)
        *yOffset = 20.0;
    else
        *yOffset = 25.0; 
}

@end
