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

#import "GameCardPileDelegate.h"
#import <Solitaire/CardSet.h>
#import "Pyramid.h"

@implementation GameCardPileDelegate

/*--------------------------------------------------------------------------
|
|    - setDiscardLeft:left discardRight:right
| 
|---------------------------------------------------------------------------
|
|    Set connections to other piles.
|	
\--------------------------------------------------------------------------*/

- (void) setDiscardLeft:(CardPileView*)left discardRight:(CardPileView*)right
{
    discardCardPileViewL = left;
    discardCardPileViewR = right; 
}


/*--------------------------------------------------------------------------
|
|    - (BOOL)draggedPile:aCardPile from:gameCardPileView
| 
|    returns: (BOOL) YES if it is legal to drag the card.
|
|             (BOOL) NO if it is not legal to drag the card.
|
|---------------------------------------------------------------------------
|
|    Called by a GamePileCardView when the user tries to drag a card.
|	
\--------------------------------------------------------------------------*/

- (BOOL) draggedPile:(CardPile*)aCardPile from:(CardPileView*)gameCardPileView
{

    /*-----------------------------------------------------------------------
     *
     * Dragging is only allowed if the pile is not covered.
     *
     *-----------------------------------------------------------------------*/
    if ([gameCardPileView pileCovered:self])
        return NO;
    else
        return YES;
}


/*--------------------------------------------------------------------------
|
|	- removedPile: aCardPile from: gameCardPileView
|
|---------------------------------------------------------------------------
|
|	Called by a GamePileCardView after cards have been dragged off 
|	the pile.
|	
\--------------------------------------------------------------------------*/

- (void) removedPile:(CardPile*)aCardPile from:(CardPileView*)aCardPileView
{
    /*-----------------------------------------------------------------------
    |
    |	Since the game piles only contain a single card, we
    |	know that the pile is now empty, so we remove it from
    |	its superview's list of subviews.  This prevents the pile
    |	from getting any more mouse events.
    |
    \----------------------------------------------------------------------*/
    
    [aCardPileView removeFromSuperview];
    [[Pyramid sharedInstance] checkForWin]; 
}


/*--------------------------------------------------------------------------
|
|	- (BOOL) canAcceptPile:aCardPile from:sender in:gameCardPileView
|
|	returns:	(BOOL)	YES if card pile will accept dropped cards.
|
|			(BOOL)	NO if this card pile rejects the dropped cards.
|
|---------------------------------------------------------------------------
|
|	Called by a GamePileCardView when the user tries to drop cards on it.
|	
\---------------------------------------------------------------------------*/

- (BOOL) canAcceptPile:(CardPile*)aCardPile from:sender in:(CardPileView*)gameCardPileView
{
    Card* card1 = [aCardPile topCard];
    Card* card2 = [[gameCardPileView cardPile] topCard];

    /*-----------------------------------------------------------------------
    |
    |	Can only accept a card if this pile is not covered by
    |	others, and the two cards add to 13.
    |
    \----------------------------------------------------------------------*/
    if (![gameCardPileView pileCovered:sender])
    {
/* #define PYRAMID_CHEAT_CHEAT_CHEAT */
#ifndef PYRAMID_CHEAT_CHEAT_CHEAT
	if (card1 && card2)
	{
	    if (([card1 value] + [card2 value]) == 11)
		return YES;
	}
#else
    return YES;
#endif

    }
    return NO;
}


/*--------------------------------------------------------------------------
|
|	- acceptPile: aCardPile in: gameCardPileView
|
|---------------------------------------------------------------------------
|
|	Called by a GamePileCardView after cards have been successfully 
|	added to the pile as a result of cards being dropped on it.
|	
\---------------------------------------------------------------------------*/

- (void) acceptPile:(CardPile*)aCardPile in:(CardPileView*)gameCardPileView
{
    CardPile* 	leftMatchPile = [discardCardPileViewL cardPile];
    CardPile* 	rightMatchPile = [discardCardPileViewR cardPile];
    CardPile* 	gamePile = [gameCardPileView cardPile];
    Card* 		aCard;

    /*-----------------------------------------------------------------------
    |
    |	Remove both cards from the game pile, and move
    |	them to the match piles.  Then remove the game pile
    |	view from it's superviews list, so that it doesn't get
    |	any more mouse events.
    \------------------------------------------------------------------------*/

    aCard = [gamePile dealTopCard];
    [leftMatchPile addCard:aCard];
    aCard = [gamePile dealTopCard];
    [rightMatchPile addCard:aCard];

    [discardCardPileViewL display];
    [discardCardPileViewR display];
    [gameCardPileView display];
    [gameCardPileView removeFromSuperview];

    [[Pyramid sharedInstance] checkForWin]; 
}

/*--------------------------------------------------------------------------
|
|    doubleClickedCard:aCard in:aCardPileView
|
|---------------------------------------------------------------------------
|
|    Double clicked on a card; discard if it is a King.
|	
\--------------------------------------------------------------------------*/

- (void) doubleClickedCard:(Card*)aCard in:(CardPileView*)aCardPileView
{
    CardPile* cardPile = [aCardPileView cardPile];
    
    // can't remove a card that's covered
    if ([aCardPileView pileCovered:self])
    {
        return;
    }

    if (aCard && (aCard == [cardPile topCard]) && [aCard value] == CS_KING)
    {
        [[discardCardPileViewL cardPile] addCard:[cardPile topCard]];
        [cardPile removeCard:[cardPile topCard]];
        [aCardPileView display];
        [aCardPileView removeFromSuperview];
        [discardCardPileViewL display];
        [[Pyramid sharedInstance] checkForWin];
    } 
}

@end

