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

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

@implementation GamePileDelegate

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

- 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(!isEnabled) return nil;
		
    if ((aCard == [cardPile cardAt:CS_TOP]) && (![aCard isFaceUp]))
    {
        [aCard flip];
		//[PBoss() playSound:NV_CARDSOUND];
        [aCardPileView display];
    }
    return self;
}


- doubleClickedCard:aCard in:aCardPileView
{
    int i;
    
    id    cardPile = [aCardPileView cardPile];
    
    if(!isEnabled) return nil;
		
    if ((aCard == [cardPile cardAt:CS_TOP]) && (![aCard isFaceUp]))
    {
        [aCard flip];
        [aCardPileView display];
    }
	else if([aCard isFaceUp])
	{
        id homePileView;
		id movePile = [[CardPile allocFromZone:[self zone]] 
	                           initForCardSize:[cardPile returnSize]];
        int cardIndex = [cardPile cardIndex:aCard];
		
		[movePile addCard:aCard];
		[self draggedPile:movePile from:aCardPileView];
		if(homePileView = [Dealer() findHomeFor:movePile fromPileView:aCardPileView])
		{
			[[homePileView cardPile] addPile:movePile];
			for(i=0; i<[movePile cardCount]; i++)
			{
				[cardPile removeCard:[movePile cardAt:i]];
			}
			[aCardPileView display];
			[homePileView display];
			[Dealer() checkForWin];
			
			// flip over card under the one we moved, if it isn't already.
			if(![[cardPile cardAt:cardIndex-1] isFaceUp])
			{
				[[cardPile cardAt:cardIndex-1] setFaceUp:YES];
				[aCardPileView display];
			}
		}

	}
	
    return self;
}


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

    if(!isEnabled) return NO;
		
    if ([firstCard isFaceUp])
    {
        while (++cardIndex < [cardPile cardCount])
        {
            [aCardPile addCard:[cardPile cardAt:cardIndex]];
        }
		//[PBoss() playSound:NV_CARDSOUND];
        return YES;
    }
    return NO;
}

// sender is the pasteboard, aCardPileView is the receiving view, aCardPile is the
// pile that is being dragged.
- (BOOL)canAcceptPile:aCardPile from:sender in:aCardPileView
{
    id droppingCard = [aCardPile cardAt:CS_BOTTOM];
    id targetCard = [[aCardPileView cardPile] cardAt:CS_TOP];
    
    if(!isEnabled) return NO;
		
    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 = 20.0;
    else
        *yOffset = 25.0;
        
    return self;
}

- removedPile:aCardPile from:gameCardPileView
/*
 *  Flip over top card if it isn't already.
 */
{
	if(![[[gameCardPileView cardPile] cardAt:CS_TOP] isFaceUp])
		[[[gameCardPileView cardPile] cardAt:CS_TOP] setFaceUp:YES];
	
	return self;
}

@end
