//
// $Id: CityUnitsInspector.m,v 1.8 1997/10/31 04:51:36 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: CityUnitsInspector.m,v 1.8 1997/10/31 04:51:36 nygard Exp $");

#import "CityUnitsInspector.h"
#import "City.h"
#import "Unit.h"
#import "EmpireImageVendor.h"

static CityUnitsInspector *_instance = nil;

//======================================================================
// The City Units Inspector shows a list of all the units that are
// currently within the city.
//
// It could be extended, for example, so that double clicking a unit
// in the list will select it on the main map.
//======================================================================

@implementation CityUnitsInspector

+ instance
{
    if (_instance == nil)
        _instance = [[CityUnitsInspector alloc] init];

    return _instance;
}

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

- (void) awakeFromNib
{
    [super awakeFromNib];

    [iconColumn setDataCell:[[NSImageCell alloc] initImageCell:nil]];
}

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

- init
{
    self = [super initWithInspectorNibNamed:@"CityUnitsInspector.nib"];
    if (self != nil)
    {        
        browserValid = NO;

        [[EmpireImageVendor instance] attach:self];
    }

    return self;
}

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

- (void) dealloc
{
    [[EmpireImageVendor instance] detach:self];

    [super dealloc];
}

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

- (void) revert
{
    [unitTable reloadData];
}

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

- (void) setSubject:anObject
{
    [super setSubject:anObject];

    cityUnits = [subject cityUnits];
    browserValid = NO;

    [subject attach:self];
}

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

- (void) willStopInspecting
{
    [subject detach:self];
}

//======================================================================
// Empire Image Vendor notification
//======================================================================

- (void) vendorImagesUpdated:(BOOL)player1:(BOOL)player2:(BOOL)player3:(BOOL)other
{
    [unitTable reloadData];
}

//======================================================================
// Other notifications
//======================================================================

- (void) unitDidExit:(Unit *)aUnit
{
    [unitTable reloadData];
}

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

- (void) unitDidEnter:(Unit *)aUnit
{
    [unitTable reloadData];
}

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

- (void) cityWasLost
{
    // Need to check for nil before dettaching above?
    subject = nil;

    // Inspect nil...
}

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

// Passed by city.
- (void) unitIconHasChanged:(Unit *)aUnit
{
    [unitTable reloadData];
}

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

- (int) numberOfRowsInTableView:(NSTableView *)aTableView
{
    return [[subject cityUnits] count];
}

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

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

    NSParameterAssert (rowIndex >= 0 && rowIndex < [[subject cityUnits] count]);
    
    unit = [[subject cityUnits] objectAtIndex:rowIndex];
    identifier = [aTableColumn identifier];

    if ([identifier isEqualToString:@"Unit Name"])
    {
        return [unit unitName];
    }
    else if ([identifier isEqualToString:@"Unit Icon"])
    {
        return [unit iconImage];
    }

    return nil;
}

@end
