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


#import "StockCardPileDelegate.h"
#import "../Solitaire/CardSet.subproj/cardset.h"
#import "Pyramid.h"
#import "localstrings.h"
#import "AlertPanel.h"

@implementation StockCardPileDelegate


/*----------------------------------------------------------------------------
|
|	Delegate-setting methods
|
\---------------------------------------------------------------------------*/

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


/*--------------------------------------------------------------------------
|
|	- clickedCard:aCard in:stockCardPileView
|
|	returns:	(id)	self
|
|---------------------------------------------------------------------------
|
|	Called by the StockPileCardView when the mouse is clicked on the pile.
|	
\---------------------------------------------------------------------------*/

- clickedCard:aCard in:stockCardPileView
{
    id wasteCardPile = [wasteCardPileView cardPile];
    id stockCardPile = [stockCardPileView cardPile];

    /*-----------------------------------------------------------------------
    |
    |	If the stock pile is empty, turn over the waste pile
    |	and move the cards pack to the stock pile.
    |	
    \-----------------------------------------------------------------------*/

    if (!aCard)
    {
	/*-----------------------------------------------------------------
        |
        |	The pile may not actually be empty; the user may
        |	have clicked on an exposed area of the pile view.
        |
        \----------------------------------------------------------------*/

	if ([stockCardPile cardCount] > 0)
	{
	    return self;
	}
	if ([SolGameController() dealCount] < 3)
	{
	    [wasteCardPile flip];
	    [stockCardPile addPile:wasteCardPile];
	    [wasteCardPile empty];
	    [wasteCardPileView display];
	    [stockCardPileView display];
	    [SolGameController() incDealCount];
	}
	else
	{
	    NXRunAlertPanel(LOCALIZED_GAME_NAME, LOCALIZED_NO_FLIP, 
	                    NULL, NULL, NULL);
	    return self;
        }
    } else

    /*-----------------------------------------------------------------
    |
    |	If the top card was clicked, and it was face-down,
    |	turn it face-up.  Then, move it to the waste pile.
    |	If this card and the card currently on top of the waste
    |	pile add to 13, give the user the option to leave it on
    |	the waste pile, or to discard them both.
    \---------------------------------------------------------------*/

    if ((aCard == [stockCardPile cardAt:CS_TOP]) && (![aCard isFaceUp]))
    {
	id wasteCard = [wasteCardPile cardAt:CS_TOP];

	[aCard flip];
	[stockCardPileView display];

	if (wasteCard && ([aCard value] + [wasteCard value] == 11))
	{
	    int result;
	    
	    [[alertPanel first] setTitle:LOCALIZED_LEAVE_ON_WASTE];
	    [[alertPanel second] setTitle:LOCALIZED_REMOVE_THEM];
	    [[alertPanel message] setStringValue:LOCALIZED_TURN_MATCH];
	    [[alertPanel alert] setStringValue:LOCALIZED_GAME_NAME];
	    [alertPanel run:self];
	    result = [alertPanel lastResult];
	    
	    if (result == NX_ALERTALTERNATE)
	    {
		id leftDiscardPile = [discardCardPileViewL cardPile];
		id rightDiscardPile = [discardCardPileViewR cardPile];

		[stockCardPile removeCard:aCard];
		[leftDiscardPile addCard:aCard];
		[wasteCardPile removeCard:wasteCard];
		[rightDiscardPile addCard:wasteCard];
		[discardCardPileViewL display];
		[discardCardPileViewR display];
		[stockCardPileView display];
		[wasteCardPileView display];
                [SolGameController() checkForWin];
		return self;
	    }
	}
	[stockCardPile removeCard:aCard];
	[wasteCardPile addCard:aCard];
	[stockCardPileView display];
	[wasteCardPileView display];
        [SolGameController() checkForWin];
    }
    return self;
}


/*--------------------------------------------------------------------------
|
|	- (BOOL) canAcceptPile:aCardPile from:sender in:stockCardPileView
|
|	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 StockPileCardView when a card pile wants to be dropped 
|	on this pile.
|	
\--------------------------------------------------------------------------*/

- (BOOL)canAcceptPile:aCardPile from:sender in:stockCardPileView
{
    return NO;
}

@end
