#import "PlayerPileDelegate.h"

@implementation PlayerPileDelegate:Object

- acceptPile:aCardPile in:aCardPileView
/*
	Sent to the delegate after cards have been dropped on a CardPileView and accepted.
	aCardPile is the pile which was dropped on aCardPileView.  At this point, the cards
	which were dragged and dropped are in three piles:  (1) the pile which they were
	originally dragged from, (2) the temporary dragging pile (aCardPile) and (3) the
	receiving pile (aCardPileView).
*/
{
	//int tag = abs([aCardPileView tag]);
	
	//[dealer playerDidSplit:tag];
	
	return self;
}

- (BOOL)canAcceptPile:aCardPile from:sender in:aCardPileView
/*
	Sent to a CardPileView's delegate to determine if the pile will allow the cards in
	aCardPile which were dragged from the CardPileView sender to be dropped on
	aCardPileView.  This method should return YES if the cards can be dropped; NO if they
	can't.
	
	I think there's a bug in the CardSet.  The view that sends this message
	(aCardPileView) forwards "sender" from its methods "draggingEntered:sender".  In 
	this case, sender == Application.  I think that the CardSet authors intended
	sender to be the draggingSource of the drag session, so that sender should have
	been [sender draggingSource]....
*/
{
	int tag = abs([aCardPileView tag]);
	
	if(![dealer playerWillSplit:tag]) return NO;
	
	// sender and aCardPileView must be from the same player
	if(abs([[sender draggingSource] tag]) != abs([aCardPileView tag]))
		return NO;
		
	// aCardPileView can't have any cards in it
	if([[aCardPileView cardPile] cardCount] > 0)
		return NO;
	
	
	return YES;
}

- (BOOL)draggedPile:aCardPile from:aCardPileView
/*
	Sent to the delegate when the user tries to drag cards off a pile.  When this method
	is called, aCardPile will only contain the card which the user was pointing at when
	they began dragging.  Note that this can be any exposed card in the pile.  You may
	add additional cards from aCardPileView to this pile, and they will be dragged
	along with the original card.  If this method returns YES, the cards will be
	dragged.  Returning NO prevents the cards from being dragged.
*/

{
	id pile = [aCardPileView cardPile];
	
	// aCardPileView must be a split hand view (whose tags are less than zero)
	if([aCardPileView tag] < 0)
		return NO;
	
	// Only allow 1 card to be dragged, and only if aCardPileView has two cards in it
	// which are equal in value
	if([pile cardCount] == 2 && 
	   [[pile cardAt:CS_TOP] userValue] == [[pile cardAt:CS_BOTTOM] userValue] &&
	   [aCardPile cardCount] == 1)
		  return YES;
	else if([pile cardCount] == 2 && 
	   [[pile cardAt:CS_TOP] value] == [[pile cardAt:CS_BOTTOM] value] &&
	   [aCardPile cardCount] == 1)
		  return YES;

	return NO;
}

- getOffset:(float *)xOffset :(float *)yOffset
{
	*xOffset = 0.0;
	*yOffset = 30.0;
	
	return self;
}
 
- removedPile:aCardPile from:aCardPileView
/*
	Sent to the delegate after cards have been successfully dragged from aCardPileView
	and dropped on another pile.  When this message is sent, the cards in aCardPile have
	already been removed from the receiver.
*/
{
	int tag = abs([aCardPileView tag]);
	
	[dealer playerDidSplit:tag];
	
	return self;

}
@end
