//  Created by Joe Becker on 3/7/05.
//  Copyright 2005 __MyCompanyName__. All rights reserved.

#import <Cocoa/Cocoa.h>
#import "GraphView.h"
#import "NavigationView.h"
#import "DataView.h"
#import "StatView.h"

@implementation NavigationView

- (void)awakeFromNib
{
	[super awakeFromNib];
	dataTypeBeingPlotted = LAT_FIELD;
	return;
	
	NSString *lame = @"/Users/jj/Desktop/fireworks.jpg";
	NSString *absolute = [lame stringByStandardizingPath];
	NSImage *background = [[[NSImage alloc] initWithContentsOfFile:absolute] autorelease];
	[super addSubview:(NSView *)background positioned:NSWindowBelow relativeTo:nil];
}

-(BOOL)isOpaque { return NO; }   // Because NavView is on top of map...

- (NSPoint)dataPtAtRecord:(GmtShipTrackRecord *)obj
{
	NSPoint dataPt;
	return dataPt = NSMakePoint([obj lon],[obj lat]); 
}

- (void)mouseMoved:(NSEvent *)theEvent
{
	tagOfInterest = PREDICTED_TAG;
	distanceToLine = 10.;
	hotSpotIsCenterOfSegment = NO;
	[super mouseMoved:theEvent];
	
	float timeAtCursor = [objAtCursor time] - [[[[controller gmtShipTrack] gmtShipTrackRecordList] objectAtIndex:0] time];

	if (hotSpotFound) {
		[(DataView *)		[[controller dataController] graphView] moveCursorToRecord:objAtCursor];
		[(StatView *)		[[controller statController] graphView] moveCursorToRecord:objAtCursor];
		[tCell setStringValue:[NSString stringWithFormat:@"t: %12.5f", timeAtCursor] ];
	} else {
		[(DataView *)		[[controller dataController] graphView] dataMissing];		
		[(StatView *)		[[controller statController] graphView] dataMissing];
		[tCell setStringValue:@"t: no t seg"];
	}
	
}	

// This where the data read from the file is added to displayList, but not plotted.

- (void)gotData:(id)sender
{
	[super gotData:sender];

	GmtShipTrackRecord	*obj = [[[controller gmtShipTrack] gmtShipTrackRecordList] objectAtIndex:0];
	NSEnumerator		*en  = [[[controller gmtShipTrack] gmtShipTrackRecordList] objectEnumerator];
	
	first = YES;
	
	while (obj = [en nextObject]) {
		
		NSPoint dataPt = NSMakePoint([obj lon],[obj lat]);
		
		if (!first) {
			Segment *seg = [[[Segment alloc] initFrom:lastPt to:dataPt view:self record:obj] autorelease];
			if (([obj flag] & ~1) != 0) 
			{
				[seg  setTag:FLAG_TAG];
			}
			else
			{
				[seg  setTag:PREDICTED_TAG];
			}
			
			[self addGraphElement:seg];
		}
		
		first = NO;			// No longer first
		lastPt = dataPt;	// Remember this point
	}
}

- (id)zoomToDataExtent:(GmtShipTrack *)gmtShipTrack
{
	NSRect extent = NSMakeRect([[gmtShipTrack min] lon], 
							   [[gmtShipTrack min] lat], 
							   [[gmtShipTrack max] lon]-[[gmtShipTrack min] lon], 
							   [[gmtShipTrack max] lat]-[[gmtShipTrack min] lat]);	
	[self zoomToRect:extent];
	return self;
}

- (IBAction)zoomToFit:(id)sender {
	[self zoomToDataExtent:[controller gmtShipTrack]];
}

-(void)mouseDragged:(NSEvent *)theEvent
{
	[super mouseDragged:theEvent];
	weBeFlagging = NO;
	return;				
}

@end
