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


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

@implementation WasteCardPileDelegate

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

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


/*--------------------------------------------------------------------------
|
|	- (BOOL)canAcceptPile:aCardPile from:sender in:wasteCardPileView
|
|	returns: (BOOL)	YES if this pile will accept the cards dropped on it
|
|		 (BOOL)	NO if this pile will not accept the dropped cards
|
|---------------------------------------------------------------------------
|
|	Called by the WasteCardPileView when a card pile wants to be 
|	dropped on this pile.
|	
\---------------------------------------------------------------------------*/

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

    /*----------------------------------------------------------------------
    |
    |	If the card being dropped and the card on top of the
    |	pile add to thirteen, allow the card to be dropped.
    |	Both cards will be moved to the discardCardPiles in
    |	the acceptPile delegate method.
    |
    \---------------------------------------------------------------------*/

    if (card1 && card2 && ([card1 value] + [card2 value] == 11))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}


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

- (void) acceptPile:(CardPile*)aCardPile in:(CardPileView*)wasteCardPileView
{
    CardPile* 	leftDiscardPile = [discardCardPileViewL cardPile];
    CardPile* 	rightDiscardPile = [discardCardPileViewR cardPile];
    CardPile* 	pile = [wasteCardPileView cardPile];
    Card* 		aCard;

    aCard = [pile dealTopCard];
    [leftDiscardPile addCard:aCard];
    aCard = [pile dealTopCard];
    [rightDiscardPile addCard:aCard];

    [discardCardPileViewL display];
    [discardCardPileViewR display];
    [wasteCardPileView display];

    [[Pyramid sharedInstance] checkForWin]; 
}


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

- (BOOL) draggedPile:(CardPile*)aCardPile from:(CardPileView*)wasteCardPileView
{
    return YES;
}


/*--------------------------------------------------------------------------
|
|    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];
    
    if (aCard && (aCard == [cardPile topCard]) && [aCard value] == CS_KING)
    {
        [[discardCardPileViewL cardPile] addCard:[cardPile topCard]];
        [cardPile removeCard:[cardPile topCard]];
        [aCardPileView display];
        [discardCardPileViewL display];
        [[Pyramid sharedInstance] checkForWin];
    } 
}

@end

