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

#import "define.h"
#import "defaults.h"
#import "ColorGraphView.h"
#import "Segment.h"
#import "Label.h"

NSData *DataForColor(NSColor *aColor)
{
    return [NSArchiver archivedDataWithRootObject:aColor];
}

NSColor *ColorForData(NSData *data)
{
    return [NSUnarchiver unarchiveObjectWithData:data];
}


@implementation ColorGraphView


- (void)dealloc
{
    [axesColor release];
    [labelColor release];
    [predictedColor release];
	[flagColor release];
    [super dealloc];
}


-(BOOL)isOpaque { return NO; }   // Because DataView, and StatView are opaque

- (NSColor *)colorForTag:(int)aTag
{
    switch(aTag) {
    case AXES_TAG:      return axesColor;
    case LABEL_TAG:     return labelColor;
    case PREDICTED_TAG:	return predictedColor;
    case MEASURED_TAG:	return measuredColor;
    case FLAG_TAG:		return flagColor;
    }
    return nil;                         // no color?
}


- (void)setObjectsToColor:(NSColor *)theColor forTag:(int)aTag
{
    NSEnumerator *en;
    id obj=nil;

    /* First set the correct instance variable */
    switch(aTag){
    case AXES_TAG:
        [axesColor release];
        axesColor = [theColor retain];
        break;

    case LABEL_TAG:
        [labelColor release];
        labelColor = [theColor retain];
        break;
		
    case PREDICTED_TAG:
        [predictedColor release];
        predictedColor = [theColor retain];
        break;
		
    case FLAG_TAG:
        [flagColor release];
        flagColor = [theColor retain];
        break;

	case MEASURED_TAG:
        [measuredColor release];
        measuredColor = [theColor retain];
        break;
	}

    // Now set the elements in the display list
	en = [displayList objectEnumerator];
    while(obj = [en nextObject]){
        if([obj tag]==aTag){
            [obj setColor:theColor];
            [self setNeedsDisplayInRect:[obj bounds]];
        }
    }
}


- (void)addGraphElement:(id)element
{
    /* Set the color to match the request */
    [element setColor:[self colorForTag:[element tag]]];
	
    /* Add the element to the display list */
    [super addGraphElement:element];
}


- (IBAction)setObjectsToColor:(id)sender
{
    [self setObjectsToColor:[sender color] forTag:[sender tag]];
}


- initWithFrame:(NSRect)frame
{
    [super initWithFrame:frame];
    axesColor =		[[NSColor lightGrayColor] retain];	// defines all axes to be a light gray line
    labelColor =	[[NSColor darkGrayColor] retain];	// defines all labels to be a dark gray line
    predictedColor =[[NSColor blackColor] retain];		// defines all predicted to be a black line
	flagColor =		[[NSColor redColor] retain];		// defines all flagged to be a red line
    measuredColor = [[NSColor blueColor] retain];		// defines all measured to be a green line

    // Dragging
    [self registerForDraggedTypes:[NSArray arrayWithObjects:NSStringPboardType, NSColorPboardType,nil]];

    return self;
}



// Dragging Methods

- (int)tagAtPoint:(NSPoint)pt
{
    NSEnumerator *en;
    id obj;

    en = [displayList objectEnumerator];
    while(obj = [en nextObject]){
        if(NSPointInRect(pt,[obj bounds])){
            return [obj tag];
        }
    }
    return 0;
}


- (unsigned int)draggingEntered:(id <NSDraggingInfo>)sender
{
    return NSDragOperationCopy;
}


- (unsigned int)draggingUpdated:(id <NSDraggingInfo>)sender
{
    NSPasteboard *pboard = [sender draggingPasteboard];

    // If it is a string, we can take it here
    if ([pboard stringForType:NSStringPboardType]) {
        return NSDragOperationCopy;
    }

    // If is a color, we only support dropping 
    //  on objects in the display list 
    if ([pboard dataForType:NSColorPboardType]) {

       // Get the dragging location in the view's coordinates
       NSPoint pt = [self convertPoint:[sender draggingLocation]
                           fromView:nil];

       // See if there is an intersection.
       // If so, say we support a copy.
        if ([self tagAtPoint:pt]) {
            return NSDragOperationCopy;
        }
    }
    return NSDragOperationNone;
}


- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    NSPasteboard *pboard = [sender draggingPasteboard];
    NSString *str =0 ;
    NSColor  *color=0;

    // If there is text, do the graph
    [pboard types];
    str = [pboard stringForType:NSStringPboardType];
    if (str) {
        [self setFormula:str];
        [self graph:nil];
        return YES;
    }

    // If there is color, find the tag that the user is dragging
    //  onto and set the objects with that tag to be that color.
    color = [NSColor colorFromPasteboard:pboard];
    if (color) {
        NSPoint pt = [self convertPoint:[sender draggingLocation]
                           fromView:nil];
        int tag = [self tagAtPoint:pt];

        if (tag!=-1) {
            [self setObjectsToColor:color forTag:tag];
            return YES;
        }
    }
    return NO;
}


+(void)initialize
{
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *appDefs = [NSMutableDictionary dictionary];

    [appDefs setObject:DataForColor([NSColor cyanColor])
			forKey:GP_AxesColor];

    [appDefs setObject:DataForColor([NSColor blueColor])
			forKey:GP_LabelColor];

    [appDefs setObject:DataForColor([NSColor blackColor])
			forKey:GP_PredictedColor];

    [appDefs setObject:DataForColor([NSColor greenColor])
			forKey:GP_MeasuredColor];

    [defs registerDefaults:appDefs];
}


- (void)awakeFromNib
{
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];

    [super awakeFromNib];

    [self setObjectsToColor:
		ColorForData([defs dataForKey:GP_AxesColor])
        forTag:AXES_TAG];

    [self setObjectsToColor:
		ColorForData([defs dataForKey:GP_MeasuredColor])
		forTag:MEASURED_TAG];
	
    [self setObjectsToColor:
		ColorForData([defs dataForKey:GP_PredictedColor])
        forTag:PREDICTED_TAG];

    [self setObjectsToColor:
		ColorForData([defs dataForKey:GP_LabelColor])
        forTag:LABEL_TAG];
}


@end
