//
// $Id: StatusController.m,v 1.7 1997/10/31 04:52:09 nygard Exp $
//

//
//  This file is a part of Empire, a game of exploration and conquest.
//  Copyright (C) 1996  Steve Nygard
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//  You may contact the author by:
//     e-mail:  nygard@telusplanet.net
//

#import "Empire.h"

RCSID ("$Id: StatusController.m,v 1.7 1997/10/31 04:52:09 nygard Exp $");

#import "StatusController.h"

#import "GameManager.h"

//======================================================================
// We need some visual representation of a game in progress, since one
// instance of the application can host a game of three remote players,
// and there would otherwise be no way of knowing when the game was
// over.
//======================================================================

#define StatusController_VERSION 1

@implementation StatusController

+ (void) initialize
{
    if (self == [StatusController class])
    {
        [self setVersion:StatusController_VERSION];
    }
}

//----------------------------------------------------------------------

- initWithGameManager:(GameManager *)theGameManager
{
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    NSString *nibFile;
    BOOL loaded;

    [super init];

    nibFile = @"GameStatus.nib";
    loaded = [NSBundle loadNibNamed:nibFile owner:self];
    if (loaded == NO)
    {
        NSLog (@"Could not load %@.", nibFile);
        [super dealloc];
        return nil;
    }

    // The game manager owns the status controller.
    gameManager = theGameManager;

    [self updateCurrentTurn];
    [self updateGameStatus];
    [self updatePlayerTable];

    [defaultCenter addObserver:self
                   selector:@selector (updateGameStatus)
                   name:EMGameStateChangedNotification
                   object:gameManager];

    [defaultCenter addObserver:self
                   selector:@selector (updateCurrentTurn)
                   name:EMTurnChangedNotification
                   object:gameManager];

    [defaultCenter addObserver:self
                   selector:@selector (updatePlayerTable)
                   name:EMPlayerStateChangedNotification
                   object:gameManager];

    return self;
}

//----------------------------------------------------------------------

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    SNRelease (statusWindow);

    [super dealloc];
}

//----------------------------------------------------------------------

- (void) showPanel
{
    [statusWindow orderFront:self];
}

//----------------------------------------------------------------------

- (void) stopGame:sender
{
    if (gameManager != nil)
        [gameManager stopGame];
}

//----------------------------------------------------------------------

- (void) updateCurrentTurn
{
    [currentTurnTextfield setIntValue:[gameManager turnNumber]];
}

//----------------------------------------------------------------------

- (void) updateGameStatus
{
    [gameStateTextfield setStringValue:[gameManager gameStatus]];
}

//----------------------------------------------------------------------

- (void) updatePlayerTable
{
    [playerTable reloadData];
}

//----------------------------------------------------------------------

- (BOOL) validateMenuItem:(NSMenuItem *)menuCell
{
    BOOL valid = NO;

    if (gameManager != nil)
        valid = [gameManager validateMenuItem:menuCell];

    return valid;
}

//======================================================================
// NSTable Data Source
//======================================================================

- (int) numberOfRowsInTableView:(NSTableView *)aTableView
{
    return 3;
}

//----------------------------------------------------------------------

- tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
    NSString *identifier;
    id tmp;

    tmp = nil;
    identifier = [aTableColumn identifier];

    if ([identifier isEqualToString:@"Player"] == YES)
    {
        tmp = [NSNumber numberWithInt:rowIndex + 1];
    }
    else
    {
        if (gameManager != nil)
            tmp = [[gameManager playerStatus:rowIndex + 1] objectForKey:identifier];;
    }

    return tmp;
}

@end
