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

#import "Segment.h"

@implementation Segment

- initFrom:(NSPoint)theStart to:(NSPoint)theEnd view:(id)theView record:(GmtShipTrackRecord *)obj
{
	[super init];         // Init the NSObject superclass!
	
	start = theStart;
	end   = theEnd;
	center= NSMakePoint((start.x+end.x)/2.0, (start.y+end.y)/2.0);
	
	color = [ [NSColor orangeColor] retain]; //FIXME
	lineType = SOLID_LINE;
	view = theView;
	shipTrackRecord = obj;
	return self;
}

- (void)dealloc
{
	[color release];  // Release what you retain
	[super dealloc];  //  and dealloc the super class.
}

// Accessor methods
- (int)tag { return tag; }
- (void)setTag:(int)aTag { tag = aTag; }

- (void)setColor:(NSColor *)aColor
{
	[color release];
	color = aColor;
	[color retain];
	//[color set];
}

- (NSColor *)color
{
	return color;
}

- (void)setLineType:(int)aType
{
	lineType = aType;
}

- (int)lineType
{
	return lineType;
}

- (GmtShipTrackRecord *)shipTrackRecord { return shipTrackRecord; }
- (void)setShipTrackRecord:(GmtShipTrackRecord *)aShipTrackRecord {	shipTrackRecord = aShipTrackRecord; }

	// Methods that derive information for the caller

- (NSRect)bounds
{
	const float sloppyCoding = -1.; //FIXME: AppKit(???) has an "off by one" bug, leaves turds...
	
	return NSIntegralRect(
						  NSInsetRect(
									  NSMakeRect(MIN(start.x,end.x), MIN(start.y,end.y), fabs(start.x-end.x),fabs(start.y-end.y)), 
									  sloppyCoding, sloppyCoding)
						  );
	
}

- (NSPoint)start
{
	return start;
}

- (NSPoint)end
{
	return end;
}

- (NSPoint)center
{
	return center;
}

- (void)stroke
{	
	[color set];
    //float lineDash[2];
	//lineDash[0] = 40.0;
	//lineDash[1] = 12.0;
	//	[NSBezierPath setLineDash:lineDash count:2 phase:0.0];
	
	
	//NSDeviceResolution 
	//NSValue 
	//NSSize 
	//NSDeviceSize
	//NSScreen
	//
	// userSpaceScaleFactor
	
	[NSBezierPath strokeLineFromPoint:start toPoint:end];
}


-(void)addDashStyleToPath:(NSBezierPath *)thePath
{
    float lineDash[6];
	
	switch (lineType) {
		case DASHED_LINE: 
		case TICKED_LINE:
		default:
			lineDash[0] = 40.0;
			lineDash[1] = 12.0;
			lineDash[2] = 8.0;
			lineDash[3] = 12.0;
			lineDash[4] = 8.0;
			lineDash[5] = 12.0;
	}
	[thePath setLineDash:lineDash count:6 phase:0.0];
}

- (BOOL)pathContainsPoint:(NSPoint)point forMode:(CGPathDrawingMode)mode
{
//FIXME    CGPathRef       path = [self quartzPath];
    CGContextRef    cgContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
    CGPoint         cgPoint;
    BOOL            containsPoint = NO;
	
    cgPoint.x = point.x;
    cgPoint.y = point.y;
	
    // Save the graphics state before doing the hit detection.
	
    CGContextSaveGState(cgContext);
//FIXME    CGContextAddPath(cgContext, path); NSDeviceResolution NSValue NSSize NSDeviceSize NSScreen
    containsPoint = CGContextPathContainsPoint(cgContext, cgPoint, mode);
	
    CGContextRestoreGState(cgContext);
    return containsPoint;
}

@synthesize view;
@end

