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

#import "CityAttributesInspector.h"
#import "City.h"
#import "Unit.h"
#import "EmpireImageVendor.h"
#import "EmPlayer.h"
#import "EmInspectorManager.h"

static CityAttributesInspector *_instance = nil;

//======================================================================
// The City Attributes Inspector shows the name and location of a city,
// whether it is a port, and shows information about the type of unit
// the city is currently producing.
//
// The inspector allows the type of production to be changed.  In the
// future, it will also allow the name of the city to be changed.
//======================================================================

@implementation CityAttributesInspector

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

    return _instance;
}

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

- (void) awakeFromNib
{
    [super awakeFromNib];

    [[EmpireImageVendor instance] attach:self];

    [self useIconsForPlayer:p_player1];
}

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

- init
{
    self = [super initWithInspectorNibNamed:@"CityAttributesInspector.nib"];

    return self;
}

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

- (void) dealloc
{
    [super dealloc];
}

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

- (void) ok
{
    if ([[subject cityName] isEqualToString:[cityNameTextfield stringValue]] == NO)
        [subject setCityName:[cityNameTextfield stringValue]];
  
    [subject setProduction:[productionMatrix selectedRow] + u_army];

    [super ok];

    [self revert];  // to catch changed production times...
}

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

- (void) revert
{
    int l;
    BOOL tmp;
    int pt[UNIT_TYPE_COUNT];
    BOOL isFoe;
    Player player = [inspectorManager player];
    NSString *cityLocation;
    EMMapLocation location;
    WarStatistics warStatistics;

    [super revert];

    isFoe = player != [[subject owner] playerNumber] && player != p_neutral;

    //NSLog (@"isFoe: %@", (isFoe == YES) ? @"YES" : @"NO");

    [cityNameTextfield setStringValue:[subject cityName]];
    location = [subject cityLocation];
    cityLocation = [NSString stringWithFormat:@"%d,%d", location.row, location.column];
    [cityLocationTextfield setStringValue:cityLocation];

    [portSwitch setState:[subject isAPort]];

    if ([subject productionType] == u_unknown || isFoe == YES)
    {
        [productionMatrix setEnabled:NO];
        [productionMatrix setAllowsEmptySelection:YES];
        [productionMatrix selectCellAtRow:-1 column:-1];
    }
    else
    {
        [productionMatrix setEnabled:YES];
        [productionMatrix setAllowsEmptySelection:NO];
        [productionMatrix selectCellAtRow:[subject productionType] - u_army column:0];

        tmp = [subject isAPort];
        for (l = u_transport; l < u_hovercraft; l++)
            [[productionMatrix cellAtRow:l - u_transport + 2 column:0] setEnabled:tmp];
    }

    if (isFoe == YES)
        [subject defaultTurnsToConstruct:pt];
    else
        [subject turnsToConstruct:pt];

    for (l = u_army; l <= u_hovercraft; l++)
        [[turnsToConstructMatrix cellAtRow:l - u_army column:0] setIntValue:pt[l - u_army]];

    if (isFoe == YES || player == p_neutral)
        for (l = 1; l < UNIT_TYPE_COUNT; l++)
            [[underConstructionMatrix cellAtRow:l - 1 column:0] setIntValue:0];
    else
    {
        warStatistics = [[subject owner] warStatistics];
        for (l = 1; l < UNIT_TYPE_COUNT; l++)
            [[underConstructionMatrix cellAtRow:l - 1 column:0] setIntValue:warStatistics.under_construction[l]];
    }

    [self useIconsForPlayer:[[subject owner] playerNumber]];
}

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

- (BOOL) wantsButtons
{
    return YES;
}

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

- (void) changed:sender
{
    if (sender == cityNameTextfield)
    {
        [cityLocationTextfield selectText:self];
    }

    [inspectorManager setOkButtonEnabled:YES];
    [inspectorManager setRevertButtonEnabled:YES];
    [[inspectorView window] setDocumentEdited:YES]; 
}

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

- (void) useIconsForPlayer:(Player)playerNumber
{
    EmpireImageVendor *vendor = [EmpireImageVendor instance];
    int l;
    NSImage *city;
    NSImage **onLand;
    NSImage **onWater;
    NSImage **onCity;
    int loop_to_land[2] = {0, 2};
    int loop_to_water[7] = {1, 3, 4, 5, 6, 8, 9};

    [vendor player:playerNumber :&city :&onLand :&onWater :&onCity :NULL];

    [cityImageButton setImage:city];

    for (l = 0; l < 2; l++)
        [[unitImageMatrix cellAtRow:l column:0] setImage:onLand[loop_to_land[l]]];

    for (l = 0; l < 7; l++)
        [[unitImageMatrix cellAtRow:l+2 column:0] setImage:onWater[loop_to_water[l]]]; 
}

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

- (void) vendorImagesUpdated:(BOOL)player1:(BOOL)player2:(BOOL)player3:(BOOL)other
{
    [cityImageButton setNeedsDisplay:YES];
    [unitImageMatrix setNeedsDisplay:YES];
}

@end
