#import "RouteMap.h"
#import <appkit/appkit.h>
#import "IcaoController.h"
#import "mapobjects.h"

@implementation RouteMap

- initFrame:(const NXRect *)frameRect
{
	[super initFrame:frameRect];
	drawInternalRoute = NO;
	showsRoute = YES;

	return self;
}

/* routine that draws any route (from drawroutepoints) */
- drawRoute
{
	int                 i, x1, y1, x2, y2;

	if (drawroutenumpoints > 1)		/* a single point doesn't make a route */
	{
		gp_setcolor(RED);
		gp_setlinestyle(3, SOLID);

		internal2window(drawroutepoints[0], &x1, &y1);
		for (i = 1; i < drawroutenumpoints; i++)
		{
			internal2window(drawroutepoints[i], &x2, &y2);
			gp_drawline(x1, y1, x2, y2);

			x1 = x2;
			y1 = y2;
		}
	}
	return self;
}

- fillGlobalRoute
{
	if (!drawInternalRoute)
	{
		drawroutenumpoints = 0;

		/*
		 * We must get the Objects belonging to the Route from the T/O, VIA...
		 * DEST TextFieldCells here, because the Fields themself are changed at
		 * muliply points in the program. This is of course a subject to change 
		 */

		if ([takeOff stringValue][0] != '\000')
		{
			drawroutepoints[drawroutenumpoints++]
				= objectfromnamelist(
												objectThatMatches([takeOff stringValue]))->location;

			if ([via1 stringValue][0] != '\000')
				drawroutepoints[drawroutenumpoints++]
					= objectfromnamelist(
													 objectThatMatches([via1 stringValue]))->location;

			if ([via2 stringValue][0] != '\000')
				drawroutepoints[drawroutenumpoints++]
					= objectfromnamelist(
													 objectThatMatches([via2 stringValue]))->location;

			if ([via3 stringValue][0] != '\000')
				drawroutepoints[drawroutenumpoints++]
					= objectfromnamelist(
													 objectThatMatches([via3 stringValue]))->location;

			if ([destination stringValue][0] != '\000')
				drawroutepoints[drawroutenumpoints++]
					= objectfromnamelist(
										objectThatMatches([destination stringValue]))->location;
		}
	}
	if (drawroutenumpoints >= 2)
		return self;
	else
		return nil;
}

- drawSelf:(const NXRect *)rects :(int)rectCount;
{
	[super drawSelf:rects :rectCount];
	if (showsRoute)
	{
		if ([self fillGlobalRoute])
			return[self drawRoute];
		else
			return nil;
	}
	else
		return self;
}

- setDrawInternalRoute:(BOOL)flag
{
	drawInternalRoute = flag;

	[self display];
	return self;
}

- setShowsRoute:(BOOL)flag
{
	if (flag != showsRoute)
	{
		showsRoute = flag;
		[self display];
	}
	return self;
}

/* Querying the RouteView */
- (BOOL)showsRoute
{
	return showsRoute;
}

- takeShowsRouteFrom:sender
{
	return [self setShowsRoute:[sender state]];
}

- makeRouteFit:(BOOL)forPrinting
{
	LOCATION            topleft, botright, a, b;
	double              heightNM, widthNM, scalehor, scalever;
	BOOL                orientation;
	int                 i;

	/* Check if there is a route and set it */
	if ([self fillGlobalRoute])
	{
		/* find extension of area to be printed */

		topleft = drawroutepoints[0];
		botright = drawroutepoints[0];

		for (i = 1; i < drawroutenumpoints; i++)
		{
			if (drawroutepoints[i].longitude < topleft.longitude)
				topleft.longitude = drawroutepoints[i].longitude;
			if (drawroutepoints[i].latitude < topleft.latitude)
				topleft.latitude = drawroutepoints[i].latitude;
			if (drawroutepoints[i].longitude > botright.longitude)
				botright.longitude = drawroutepoints[i].longitude;
			if (drawroutepoints[i].latitude > botright.latitude)
				botright.latitude = drawroutepoints[i].latitude;
		}

		/* calc width and height of map in NM */

		a.longitude = b.longitude = topleft.longitude;
		a.latitude = topleft.latitude;
		b.latitude = botright.latitude;
		heightNM = distance(a, b);

		b.longitude = botright.longitude;
		b.latitude = topleft.latitude;
		widthNM = distance(a, b);

		if (forPrinting)
		{
			/* Space for the MarginData */
			NXCoord             mTop, mLeft, mRight, mBottom;

			PrintInfo          *printInfo = [NXApp printInfo];
			NXSize              pageSize;

			/* suggest either landscape (YES) or portrait (NO) */
			orientation = (widthNM > heightNM);
			[[NXApp printInfo] setOrientation:(orientation ? NX_LANDSCAPE : NX_PORTRAIT)
			 andAdjust :YES];

			[printInfo getMarginLeft:&mLeft right:&mRight top:&mTop bottom:&mBottom];

			pageSize = [printInfo paperRect]->size;
			pageSize.width -= (mLeft + mRight);
			pageSize.height -= (mBottom + mTop);

			//[self sizeTo:pageSize.width:pageSize.height];

			/* now we need to find a scale that makes everything fit: */
			scalehor = widthNM * 185200 / POINT2CM(pageSize.width);
			scalever = heightNM * 185200 / POINT2CM(pageSize.height);
			map_scale = (long)((scalehor > scalever) ? scalehor : scalever);
			map_scale = (long)(map_scale * 1.1);

			/* make this map_scale take effect */
			map_setscale(map_scale);

		}
		else
		{
			scalehor = widthNM * 185200 / [self widthCmFrom:NX_WIDTH(&frame)];
			scalever = heightNM * 185200 / [self heightCmFrom:NX_HEIGHT(&frame)];
			map_scale = (long)((scalehor > scalever) ? scalehor : scalever);

			/* Now search a Scale in the of scales > than map_scale */
			map_setscale(map_scale);
			if (scale < numscales - 1)
				map_scale = scales[++scale];
			else
				map_scale = scales[scale];

		}

		/* last but not least: choose map origin so that everything will fit */
		map_origin.longitude = (long)((topleft.longitude + botright.longitude) / 2);
		map_origin.latitude = (long)((topleft.latitude + botright.latitude) / 2);
		map_initcoord();
	}

	return self;
}


@end
@implementation RouteMap(Printing)

- openSpoolFile:(char *)filename
{
	Matrix             *popUp;

	[self setAutodisplay:NO];

	oldOrientation = [[NXApp printInfo] orientation];

	popUp = [[[[NXApp delegate] routeAdjust] target] itemList];
	if (!strcmp([[[NXApp delegate] routeAdjust] title]
							,[[popUp findCellWithTag:ADJUST_ROUTE] title]))
		[self makeRouteFit:YES];
	return[super openSpoolFile:filename];
}

- endPSOutput
{
	/* Get all the data from the buffer */
	map_origin = bufferData.theOrigin;
	map_scale = bufferData.scale;
	map_projection = bufferData.projection;
	map_origin = bufferData.theOrigin;
	map_origin = bufferData.theOrigin;
	/* What about the zeroMeridian ? */

	[super endPSOutput];

	[[NXApp printInfo] setOrientation:oldOrientation andAdjust:YES];
	return self;
}
