
/* Generated by Interface Builder */

#import "DistanceCardStackView.h"
#import	"angled_title.h"
#import	"CardView.h"
#import	"StackView.h"
#import	"mille.h"
#import	"prototypes.h"
#import	<assert.h>
#import	<objc/List.h>


												// This is the number of card stacks built into a distance
												//	pile view.
#define	NUMBER_OF_CARD_STACK_VIEWS	5
												// This is the number of pixels that the top and bottom frames
												//	of the stack view should be inset from the frame of the
												//	distance view's frame.
#define	HEIGHT_INSET				5

												// Here's a trick.  The distance miles of the cards can be
												//	obtained by interrogating them (it is their tag value).  
												// I assign a tag value of the card's distance to the stack 
												//	views.  All of the cards in a distance stack has a tag
												//	value the same as the stack itself.  This shouldn't confuse
												//	the searching mechanism to find the stack for a distance
												//	value.
	static 	int		tagAssociate[] = { C_200, C_100, C_75, C_50, C_25 };


@implementation DistanceCardStackView


												// A distance view maintains 5 StackView objects.  These views
												//	are tiled within the distance view.  There is a stack for
												//	25, 50, 75, 100, and 200 mile cards.  These stacks are
												//	overlapping stacks. These stacks are subviews of this 
												//	view.
+ newFrame:( const NXRect * )frameRect
{

	int		i;
	float	x_displacement = (( NX_WIDTH( frameRect ) - ( CARD_WIDTH * NUMBER_OF_CARD_STACK_VIEWS )) / NUMBER_OF_CARD_STACK_VIEWS );
	
	self			= [ super newFrame:frameRect ];
	
	for( i = 0; i < NUMBER_OF_CARD_STACK_VIEWS; ++i ) {
		NXRect	stackFrame = {	(( x_displacement / 2 ) + ( i * ( x_displacement + CARD_WIDTH ))),
								HEIGHT_INSET, 
								CARD_WIDTH, 
								( NX_HEIGHT( frameRect ) - ( 2 * HEIGHT_INSET )) 
		};
		char	*subviewTitle = alloca( 64 );
		
		
		sprintf( subviewTitle, "%d Miles", tagAssociate[ i ]);
		[ self addSubview:[[[ StackView newFrame:&stackFrame ] setOverlap:YES ] setTag:tagAssociate[ i ]]];
	}
	
	cards = [ List new ];
	
	return self;
}


- free
{

	[ cards free ];
	
	return [ super free ];
}


- addCard:( CardView * )aCard :sender
{

	int		i;
	BOOL	cardAddedToStack = NO;
	
	
	for( i = 0; !cardAddedToStack && ( i < [ subviews count ] ); ++i ) {
		StackView	*aStack = [ subviews objectAt:i ];
		
		if([ aStack tag ] == [ aCard tag ]) {
			[ aStack addCard:aCard	:self ];
			cardAddedToStack = YES;
		}
	}
	assert( cardAddedToStack );
	
	return self;
}


- removeCard:( CardView * )aCard :sender
{

	int		i;
	
	
	assert([ aCard isDescendantOf:self ]);
	for( i = 0; i < [ subviews count ]; ++i ) {
		StackView	*aStack = [ subviews objectAt:i ];
		
		if([ aCard isDescendantOf:aStack ])
			[ aStack removeCard:aCard	:self ];
	}

	return self;
}


- sendCard:( CardView * )aCard to:anObject
{


	assert([ aCard isDescendantOf:self ]);
	[ self removeCard:aCard		:self ];
	[ anObject addCard:aCard	:self ];
	
	return [ self update ];
}

	 
- sendAllCardsTo:anObject
{

	int		i;
	
	
	for( i = 0; i < [ subviews count ]; ++i )
		[[ subviews objectAt:i ] sendAllCardsTo:anObject ];
		
	return [ self update ];
}


- ( List * )holderList
{

	int		i;
	
	
	[ cards empty ];
	for( i = 0; i < [ subviews count ]; ++i ) {
		List	*holderList = [[ subviews objectAt:i ] holderList ];
		int		j;
		
		for( j = 0; j < [ holderList count ]; ++j )
			[ cards addObject:[ holderList objectAt:j ]];
	}
	
	return cards;
}


- ( int )numCardTypeInHolder:( int )aCardTag
{

	List	*cardList = [ self holderList ];
	int		i, cnt;
	
	
	for( i = 0, cnt = 0; i < [ cardList count ]; ++i )
		if([[ cardList objectAt:i ] tag ] == aCardTag )
			++cnt;
	
	return cnt;
}


- ( int )numSafetiesInHolder
{

	List	*cardList = [ self holderList ];
	int		i, cnt;
	
	
	for( i = 0, cnt = 0; i < [ cardList count ]; ++i )
		if( isSafety([ cardList objectAt:i ]))
			++cnt;
	
	return cnt;
}


@end
