//
// $Id: HFlightPathState.m,v 1.2 1997/10/31 03:38:06 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: HFlightPathState.m,v 1.2 1997/10/31 03:38:06 nygard Exp $");

#import "HFlightPathState.h"

#import "City.h"
#import "HSetFlightPathState.h"
#import "Human.h"
#import "MapPathView.h"
#import "PathSegment.h"

//======================================================================
// Show the currently set flight paths, and allow new ones to be
// created, or old ones to be removed.
//======================================================================

@implementation HFlightPathState

- initWithGameManager:(GameManager *)theGameManager
{
    [super initWithGameManager:theGameManager];

    sourceCity = nil;

    return self;
}

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

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

//======================================================================
// Subclass responsibilities
//======================================================================

- (NSString *) stateName
{
    return @"Flight Paths";
}
  
//----------------------------------------------------------------------

- (NSString *) stateKey
{
    return HS_FLIGHT_PATH;
}
  
//----------------------------------------------------------------------

- (void) stateEntered:(Human *)context
{
    MapPathView *mapView = [context mapView];
    NSMutableArray *pathList = [[NSMutableArray array] retain];
    NSEnumerator *cityEnumerator = [[context cityList] objectEnumerator];
    City *city;
  
    // Set cursor
    [mapView useDefaultCursor];
    [mapView setCursorEnabled:YES];

    // Disable both buttons
    [context enableContinueButton:YES];
    [context enableEndTurnButton:NO];

    while (city = [cityEnumerator nextObject])
    {
        PathSegment *pathSegment = [city flightPathInMapView:mapView];

        if (pathSegment != nil)
            [pathList addObject:pathSegment];
    }

    [mapView usePathList:pathList];

    sourceCity = nil;
}
  
//----------------------------------------------------------------------

- (void) continue:(Human *)context
{
    MapPathView *mapView = [context mapView];

    [mapView usePathList:nil];

    [context restoreState];
}
  
//----------------------------------------------------------------------

- (void) mouseDown:(Human *)context:(unsigned int)modifierFlags atLocation:(EMMapLocation)target
{
    EMMapLocation location;
  
    [[context mapView] positionCursorAtLocation:target];

    location.row = -1;
    location.column = -1;
    if (sourceCity != nil)
        location = [sourceCity cityLocation];

    if (sourceCity == nil)
    {
        sourceCity = [context cityAtLocation:target];
        if (sourceCity != nil)
        {
            [self selectThing:context atLocation:target andInspect:YES];
        }
    }
    else if (sourceCity != nil && (target.row != location.row || target.column != location.column))
    {
        [context changeState:HS_SET_FLIGHT_PATH];
        // will also need a way to clear a flight path...
    }
}

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

- (void) mouseUp:(Human *)context:(unsigned int)modifierFlags atLocation:(EMMapLocation)target
{
    //[self selectThing:context at:row:col andInspect:YES];
    sourceCity = nil;
}

@end
