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

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

@implementation GamePileDelegate

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

    return self;
}
    

- clickedCard:aCard in:aCardPileView
{
    id cardPile = [aCardPileView cardPile];

    if ((aCard == [cardPile cardAt:CS_TOP]) && (![aCard isFaceUp]))
    {
        [aCard flip];
        [aCardPileView display];
    }
    return self;
}


- doubleClickedCard:aCard in:aCardPileView
{
    int i;
    
    id    cardPile = [aCardPileView cardPile];
    
    if ((aCard == [cardPile cardAt:CS_TOP]) && (![aCard isFaceUp]))
    {
        [aCard flip];
        [aCardPileView display];
    }
    else if (aCard == [cardPile cardAt:CS_TOP])
    {
        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 cardAt:CS_TOP]];
                [cardPile removeCard:[cardPile cardAt:CS_TOP]];
                [aCardPileView display];
                [suitCardPileViews[i] display];
                [SolGameController() checkForWin];

                break;
            }
        }
    }
    return self;
}


- (BOOL)draggedPile:aCardPile from:aCardPileView
{
    id cardPile = [aCardPileView cardPile];
    id firstCard = [aCardPile cardAt:CS_TOP];
    int cardIndex = [cardPile cardIndex:firstCard];

    if ([firstCard isFaceUp])
    {
        while (++cardIndex < [cardPile cardCount])
        {
            [aCardPile addCard:[cardPile cardAt:cardIndex]];
        }
        return YES;
    }
    return NO;
}


- (BOOL)canAcceptPile:aCardPile from:sender in:aCardPileView
{
    id droppingCard = [aCardPile cardAt:CS_BOTTOM];
    id targetCard = [[aCardPileView cardPile] cardAt:CS_TOP];
    
    if (targetCard)
    {
        if (([targetCard isFaceUp]) &&
                ([targetCard cardColor] != [droppingCard cardColor]) &&
                ([targetCard value] == [droppingCard value] + 1))
        {
            return YES;
        }
    }
    else
    {
        if ([droppingCard value] == CS_KING)
        {
            return YES;
        }
    }
    return NO;
}


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

@end
