// HumanPlayer.m
// Project: Cribbage
// Stephan Wacker
// 93-09-16


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

#import "HumanPlayer.h"
#import "Cribbage.h"
#import "Deck.h"
#import "Score.h"
#import "LocalizableStrings.h"


@implementation HumanPlayer


- setScoreMgr: aMgr
{
    scoreMgr = aMgr;
    return self;
}


- (char *) pronoun
{
    return "You";
}

- (char *) possessivePronoun
{
    return "your";
}

- (char *) reflexivePronoun
{
    return "you";
}


- prepareForDiscarding
{
    [self enableHand: @selector(markForDiscard:) type: NX_TOGGLE];
    foldedCards = 0;
//  msg( ">> clearing foldedCards: %d", foldedCards );
    
    return self;
}


- markForDiscard: sender
{
    id		cell = [sender selectedCell];
    int		state = [cell intValue];
    
    if( cell == nil ) {
	// ignore this message
    } else {
	foldedCards += (state == 1 ? +1 : -1);
//	msg( ">> card[%d] changes state to %d, foldedCards = %d",
//	     [cell tag], state, foldedCards );
	[discardButton setEnabled: (foldedCards == (FULLHAND - CINHAND))];
    }
    
    return self;
}


- discard
{
    int		i;
    
    for( i = FULLHAND-1; i >= 0; i-- ) {	/* COUNT BACKWARDS !!! */
	if( [[cardsInHand findCellWithTag: i] intValue] == 1 ) {
	    // [self displayCardAt: i];
	    [deck putIntoCrib: hand[i]];
	    face[i] = FACE_NONE;
	    hand[i].rank = hand[i].suit = EMPTY;
	}
    }
    
    [self sortHand];
    [self displayHand];
    
    return self;
}


- prepareForPegging
{
    [self enableHand: @selector(play:) type: NX_MOMENTARYPUSH];
    
    return [super prepareForPegging];
}


- playOneCard
//
// The player should select a card or press the GO button.
// If there are no more cards, he will go automatically.
//
{
    if( ! [self anyCards] ) {
	[self go];
    } else {
	if( !quiet ) {
	    msg( PLAY_A_CARD_OR_GO );
	}
    }
    
    return self;
}


- play: sender
{
    id		cell = [sender selectedCell];
    int		tag = [cell tag];
    
    if( cell == nil ) {
	// ignore this message
    } else if( [self validCardAt: tag] ) {
	[self playCardAt: tag];
    } else {
	NXBeep();
	msg( TOTAL_EXCEEDS_31 );
    }
    
    return self;
}


- go: sender
{
    if( [self canPlay] ) {
	NXBeep();
    } else {
	[self go];
    }
    
    return self;
}


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


- doScoring: (char *) what points: (int) points
{
    correctScore = points;
    scoreWhat = what;
    [scoreMgr doScoring: what for: self];
    
    return self;
}


- manualScore: (int) score
{
    if( score == correctScore ) {
	[super doScoring: scoreWhat points: score];
    } else {
	// It's a case for the referee...
	[cribbage wrongScore: self correct: correctScore incorrect: score];
    }
    return self;
}


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


- enableHand: (SEL) anAction type: (int) aButtonType
{
    int		i;
    
    for( i = 0; i < FULLHAND; i++ ) {
	id	cell = [cardsInHand findCellWithTag: i];
	[cell setType: aButtonType];
	[cell setHighlightsBy: [cell highlightsBy]
			     & ~(NX_CHANGEGRAY|NX_CHANGEBACKGROUND)];
	[cell setEnabled: (face[i] != FACE_NONE)];
    }
    
    [cardsInHand setAction: anAction];
    [cardsInHand setTarget: self];
    [self displayHand];
    
    return self;
}


- disableHand
{
    [cardsInHand setEnabled: NO];
    return self;
}


@end	// HumanPlayer
