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

#import "TenpileGameDelegate.h"
#import "TenpileSuitDelegate.h"
//#import "../Solitaire/Solitaire.h"
#import "Tenpile.h"

@implementation TenpileGameDelegate

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

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


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

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


- (void) clickedCard:(Card*)aCard in:(CardPileView*)aCardPileView
{
    CardPile* cardPile = [aCardPileView cardPile];

    if ((aCard == [cardPile topCard]) && (![aCard isFaceUp]))
    {
        [aCard flip];
        [aCardPileView display];
    } 
}


- (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 < 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]];
                [aCardPileView display];
                [suitCardPileViews[i] display];
                [[Tenpile sharedInstance] checkForWin];

                break;
            }
        }
    } 
}


- (BOOL) draggedPile:(CardPile*)aCardPile from:(CardPileView*)aCardPileView
{
    CardPile* 	cardPile = [aCardPileView cardPile];
    Card* 		firstCard = [aCardPile topCard];
    int 		cardIndex = [cardPile indexOfCard:firstCard];
    int 		i;
    int 		total;
    Card* 		newCard;
    CardColor 	color, lastColor;
    CardValue 	lastValue;
    int 		freePiles;

    if ([firstCard isFaceUp])
    {
        total = [cardPile cardCount];
        lastValue = [firstCard value];
        lastColor = [firstCard cardColor];

	/* Can only drag if all cards above are alternating color and in
	   descending sequence */
        for (i = cardIndex + 1; i < total; i++)
            {
            newCard = [cardPile cardAtIndex:i];
            if ([newCard value] != --lastValue) return NO;
            color = [newCard cardColor];
            if (color == lastColor) return NO;
            lastColor = color;
            }

        /* Count number of empty temporary piles.  */
        for (i = freePiles = 0; i < 4; i++)
            if ([[tempCardPileViews[i] cardPile] cardCount] == 0)
                freePiles++;

        /* Move cards above dragged card onto temporary pile */
        while (++cardIndex < [cardPile cardCount] && freePiles-- > 0)
        {
            [aCardPile addCard:[cardPile cardAtIndex:cardIndex]];
        }
        if (cardIndex == [cardPile cardCount])
            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 isFaceUp]) &&
                ([targetCard cardColor] != [droppingCard cardColor]) &&
                ([targetCard value] == [droppingCard value] + 1))
        {
            return YES;
        }
    }
    else
    {
        if ([droppingCard value] == CS_KING)
        {
            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

