
#import "ErgoApp.h"
#import <appkit/Panel.h>	/* for NXRunAlertPanel() */
#import <appkit/View.h>		/* for subviews message */
#import <appkit/nextstd.h>	/* for sleep() */
#import <objc/List.h>		/* for makeObjectsPerform: messages */

@implementation ErgoApp

- allowOtherColorMove {
    ++numberThatAllowed;
    return self;
}

- gameOverDueTo: (short) type winner: (short) winType {
    [self makeSquaresPerform: @selector(gameOver)];
    [boardWindow display];
    NXPing();		/* this and below make final results visible to user */
    sleep(1);		/* ties v only occur with even #s of takable squares */
    NXRunAlertPanel(winType == GAME_TIED  ? "Tie Game!" : winType == WHITE_WINS
						 ? "White Wins" : "Black Wins",
	  type == BOARD_FILLED ? "The board is filled." : type == NO_OPPONENTS
	  		? "Complete and total victory!" : "The timer ran out.",
							     NULL, NULL, NULL);
    return [self terminate: self];
}

- highlightedSquare {
    return highlightedSquare;
}

- (BOOL) isSomeoneSelected {
    return isSomeoneSelected;
}

- (BOOL) isWhiteMove {
    return isWhiteMove;
}

- letOtherColorMove {	   /* someone else will check if other side can move */
    numberThatAllowed = 0;
    [self makeSquaresPerform: @selector(letOtherColorMove)];
    if (numberThatAllowed > 1) {   /* at least 1 if ScoreView returned to us */
	[self setIsWhiteMove: ![self isWhiteMove]];
	[self makeSquaresPerform: @selector(startOfNewMove)];
    }				/* else, don't let the other color move! */
    return [boardWindow display];
}	/* maybe re-highlight current square if no side-switch? */
	/* (and on null move pull to different square?) */

- makeSquaresPerform: (SEL) aMessage {
    return [[self viewList] makeObjectsPerform: aMessage];
}

- makeSquaresPerform: (SEL) aMessage with: sender {
    return [[self viewList] makeObjectsPerform: aMessage with: sender];
}

- setHighlightedSquare: aSquare {
    highlightedSquare = aSquare;
    return self;
}

- setIsSomeoneSelected: (BOOL) state {
    isSomeoneSelected = state;
    return self;
}

- setIsWhiteMove: (BOOL) state {
    isWhiteMove = state;
    return self;
}

/* Private methods: */

- appDidInit: sender {		/* our return value is ignored, so any is OK */
    isWhiteMove = YES;
    return [self makeSquaresPerform: @selector(appDidInit)];
}

- viewList {
    return [[boardWindow contentView] subviews];
}

@end
