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

#import "ShipReportController.h"

#import "EmPlayer.h"
#import "EmpireImageVendor.h"
#import "Unit.h"

//======================================================================
// The Ship Report provides a list of all the ships for a player, the
// ship types, names and locations.  In the future, the player should
// be able to change the ship names with this panel as well.  Also,
// having different sort orders would be useful.
//======================================================================

@implementation ShipReportController

- (void) awakeFromNib
{
    [iconColumn setDataCell:[[[NSImageCell alloc] initImageCell:nil] autorelease]];
}

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

- init
{
    NSString *nibFile;
    BOOL loaded;

    [super init];

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

    ships = [[NSMutableArray array] retain];

    [[EmpireImageVendor instance] attach:self];

    return self;
}

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

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

    SNRelease (ships);

    [super dealloc];
}

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

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

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

- tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
    id identifier;
    Unit *ship;
  
    NSParameterAssert (rowIndex >= 0 && rowIndex < [ships count]);

    identifier = [aTableColumn identifier];

    ship = [ships objectAtIndex:rowIndex];

    if ([identifier isEqual:@"Ship"] == YES)
        return [ship unitName];
    else if ([identifier isEqual:@"Type"] == YES)
        return EMUnitTypeName ([ship unitType]);
    else if ([identifier isEqual:@"Damage"] == YES)
        return [NSString stringWithFormat:@"%d/%d", [ship currentHitPoints], [ship maxHitPoints]];
    else if ([identifier isEqual:@"Location"] == YES)
    {
        EMMapLocation location = [ship unitLocation];
        return [NSString stringWithFormat:@"%d,%d", location.row, location.column];
    }
    else if ([identifier isEqual:@"Icon"] == YES)
        return [ship iconImage];
  
#if 0
    else if ([identifier isEqual:@"Number"] == YES)
        return [NSNumber numberWithInt:rowIndex];
#endif

    return nil;
}
  
//----------------------------------------------------------------------

- (void) vendorImagesUpdated:(BOOL)player1:(BOOL)player2:(BOOL)player3:(BOOL)other
{
    // n/a: modal
    //[shipTable reloadData];
}

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

- (void) shipReportForPlayer:(EmPlayer *)player
{
    NSMutableArray *unitList = [player unitList];
    NSEnumerator *unitEnumerator = [unitList objectEnumerator];
    Unit *unit;

    [ships removeAllObjects];

    while (unit = [unitEnumerator nextObject])
    {
        if ([unit isAShip] == YES)
            [ships addObject:unit];
    }

    [shipTable reloadData];

    [shipReportWindow makeKeyAndOrderFront:self];
    [NSApp runModalForWindow:shipReportWindow];
}

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

- (void) shipReportStopAction:sender
{
    [ships removeAllObjects];
    [NSApp stopModal];
    [[shipTable window] orderOut:self]; 
}

@end
