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


#import "WasteCardPileDelegate.h"
#import "../Solitaire/CardSet.subproj/cardset.h"
#import "Pyramid.h"

@implementation WasteCardPileDelegate

/*--------------------------------------------------------------------------
|
|    - setDiscardLeft:left discardRight:right
| 
|    returns: (id) self
|
|---------------------------------------------------------------------------
|
|    Set connections to other piles.
|	
\--------------------------------------------------------------------------*/

- setDiscardLeft:left discardRight:right
{
    discardCardPileViewL = left;
    discardCardPileViewR = right;
    return self;
}


/*--------------------------------------------------------------------------
|
|	- (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:aCardPile from:sender in:wasteCardPileView
{
    id card1 = [aCardPile cardAt:CS_TOP];
    id card2 = [[wasteCardPileView cardPile] cardAt:CS_TOP];

    /*----------------------------------------------------------------------
    |
    |	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
|
|	returns:		(id)		self
|
|----------------------------------------------------------------------------
|
|	Called by WasteCardPileView after cards have been successfully 
|	added to the pile as a result of cards being dropped on it.
|	
\---------------------------------------------------------------------------*/

- acceptPile:aCardPile in:wasteCardPileView
{
    id leftDiscardPile = [discardCardPileViewL cardPile];
    id rightDiscardPile = [discardCardPileViewR cardPile];
    id pile = [wasteCardPileView cardPile];
    id aCard;

    aCard = [pile cardAt:CS_TOP];
    [pile removeCard:aCard];
    [leftDiscardPile addCard:aCard];
    aCard = [pile cardAt:CS_TOP];
    [pile removeCard:aCard];
    [rightDiscardPile addCard:aCard];

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

    [SolGameController() checkForWin];
    return self;
}


/*--------------------------------------------------------------------------
|
|	- (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:aCardPile from:wasteCardPileView
{
    return YES;
}


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

- doubleClickedCard:aCard in:aCardPileView
{
    id cardPile = [aCardPileView cardPile];
    
    if (aCard && (aCard == [cardPile cardAt:CS_TOP]) && 
                                               [aCard value] == CS_KING)
    {
        [[discardCardPileViewL cardPile] addCard:[cardPile cardAt:CS_TOP]];
	[cardPile removeCard:[cardPile cardAt:CS_TOP]];
	[aCardPileView display];
	[discardCardPileViewL display];
	[SolGameController() checkForWin];

    }
    return self;
}

@end
