// Table.m
// Project: Cribbage
// Stephan Wacker
// 93-06-21


#import "deck.h"
#import "cribbage.h"

#import "Table.h"


@implementation Table


- initFrame: (const NXRect *) frameRect
//
// Background is transparent.
//
{
    [super initFrame: frameRect];
    [self setOpaque: NO];
    return self;
}


- clearSum
{
    sum = 0;
    [sumField setIntValue: sum];
    goCards = nCards;
    return self;
}


- empty
{
    nCards = 0;
    [self clearSum];
    [self displayFromOpaqueAncestor: &bounds : 1 : NO];
    return self;
}


- (int) playCard: (CARD) aCard
{
    int		result = [self score: aCard];
    
    cards[nCards++] = aCard;
    [self lockFocus];
    [self displayCardAt: nCards-1];
    [self unlockFocus];
    
    sum += VAL( aCard.rank );
    [sumField setIntValue: sum];
    
    return result;
}


- (int) score: (CARD) aCard
{
    return pegscore( aCard, cards+goCards, nCards-goCards, sum );
}


- (int) sumPlayed
{
    return sum;
}


////////////////////////////////////////////////////////////////////////


- drawSelf: (const NXRect *) rects: (int) rectCount
//
// Display all cards played so far.
//
{
    int		i;
    
    for( i = 0; i < nCards; i++ ) {
	[self displayCardAt: i];
    }
    
    return self;
}


- displayCardAt: (int) i
//
// Draw one played card.  All cards must have the same size!
//
{
    const char	*iconName;
    NXSize	size;
    NXPoint	point;
    NXImage	*image;
    
    iconName = [self iconOf: cards[i]];
    image = [NXImage findImageNamed: iconName];
    
    [image getSize: &size];
    point.x = i * (bounds.size.width - size.width) / (2*CINHAND-1);
    point.y = i * (bounds.size.height - size.height) / (2*CINHAND-1);
    point.y = bounds.size.height - point.y - size.height;
    
    [image composite: NX_SOVER toPoint: &point];
    
    return self;
}


////////////////////////////////////////////////////////////////////////


- hideCards: matrix
{
    [matrix setEnabled: NO];
    [[matrix cellList] makeObjectsPerform: @selector(setTransparent:) with: (id) (int) YES];
    [matrix display];
    
    return self;
}


- hideCard: buttonCell
//
// Make the buttonCell invisible.
// The argument buttonCell may be a ButtonCell or a Button.
//
{
    id		cell = (  [buttonCell isKindOf: [Cell class]]
			? buttonCell : [buttonCell cell]    );
    
    [cell setEnabled: NO];
    [cell setTransparent: YES];
    [[cell controlView] display];
    
    return self;
}


- downCard: buttonCell
{
    id		cell = (  [buttonCell isKindOf: [Cell class]]
			? buttonCell : [buttonCell cell]    );
    
    [cell setIntValue: 1];
    [cell setTransparent: NO];
    // [[cell controlView] display];
    
    return self;
}


- upCard: buttonCell card: (CARD) card
{
    id		cell = (  [buttonCell isKindOf: [Cell class]]
			? buttonCell : [buttonCell cell]    );
    
    [cell setIcon: [self iconOf: card]];
    [cell setIntValue: 0];
    [cell setTransparent: NO];
    // [[cell controlView] display];
    
    return self;
}


- (const char *) iconOf: (CARD) card
{
    static char buf[ 40 ];
    
    switch( card.rank ) {
      case ACE		: strcpy( buf, "ace" ); break;
      case TWO		: strcpy( buf, "two" ); break;
      case THREE	: strcpy( buf, "three" ); break;
      case FOUR		: strcpy( buf, "four" ); break;
      case FIVE		: strcpy( buf, "five" ); break;
      case SIX		: strcpy( buf, "six" ); break;
      case SEVEN	: strcpy( buf, "seven" ); break;
      case EIGHT	: strcpy( buf, "eight" ); break;
      case NINE		: strcpy( buf, "nine" ); break;
      case TEN		: strcpy( buf, "ten" ); break;
      case JACK		: strcpy( buf, "jack" ); break;
      case QUEEN	: strcpy( buf, "queen" ); break;
      case KING		: strcpy( buf, "king" ); break;
      default		: return "illegal Rank";
    }
    
    strcat( buf, "Of" );
    
    switch( card.suit ) {
      case SPADES	: strcat( buf, "Spades" ); break;
      case HEARTS	: strcat( buf, "Hearts" ); break;
      case DIAMONDS	: strcat( buf, "Diamonds" ); break;
      case CLUBS	: strcat( buf, "Clubs" ); break;
      default		: return "illegal Suit";
    }
    
    return buf;
}

@end
