/*
 * DotView.j
 *
 * Created by Philippe Laval on 2009/04/13.
 * Copyright 2009 Philippe Laval. All rights reserved.
 */

// http://www.quirksmode.org/js/detect.html

@import <Foundation/CPObject.j>

@import <AppKit/CPView.j>
@import <AppKit/CPColor.j>
@import <AppKit/CPGraphicsContext.j>

@import "PLBrowser.j"

@implementation GradientView : CPView
{
	CPArray hue;
}

- (id)initWithFrame:(CGRect)aFrame
{
    if (self = [super initWithFrame:aFrame])
    {
	  	// The hue spectrum used by HSV color picker charts.
		hue = [
		[255,   0,   0 ], // 0, Red,       0°
		[255, 255,   0 ], // 1, Yellow,   60°
		[  0, 255,   0 ], // 2, Green,   120°
		[  0, 255, 255 ], // 3, Cyan,    180°
		[  0,   0, 255 ], // 4, Blue,    240°
		[255,   0, 255 ], // 5, Magenta, 300°
		[255,   0,   0]]  // 6, Red,     360°
	}
	
	return self;
}


- (void)drawRect:(CPRect)aRect
{
    var context = [[CPGraphicsContext currentContext] graphicsPort];

	var drawText = NO;
	var drawRotatedText = NO;
	var drawGradient = NO;
	var drawRadialGradient = NO;
	
	var browserInfo = [[PLBrowser alloc] init];
	var browser = [browserInfo browser];
	var version = [browserInfo version];
	var os = [browserInfo OS];
	
	// debugger;
	// console.debug(browser);
	// console.debug(version);
	// console.debug(os);
	
	if (CPBrowserIsEngine(CPWebKitBrowserEngine))
	{
		drawText = YES;
		drawRotatedText = YES;
		drawGradient = YES;
	}
	else if (CPBrowserIsEngine(CPGeckoBrowserEngine))
	{
		if (version >= 3.5)
		{
			drawText = YES;
			drawRotatedText = NO;		// bug ?
		}
		drawGradient = YES;
	}
	else if (CPBrowserIsEngine(CPOperaBrowserEngine))
	{
		// Opera 10 should support this but don't yet
		drawText = NO;
		drawRotatedText = NO;
		drawGradient = YES;
	}

	if (drawGradient)
	{
		// Create the linear gradient: sx, sy, dx, dy.
		// That's the start (x,y) coordinates, followed by the destination (x,y).
		var gradient = context.createLinearGradient(0, 0, CPRectGetWidth([self bounds]), 0);

		// Add the color stops.
		for (var i = 0; i <= 6; i++)
		{
			var color = "rgb(" + hue[i][0] + ", " + hue[i][1] + ", " + hue[i][2] + ")";
			gradient.addColorStop(i * 1.0 / 6.0, color);
		}

		context.fillStyle = gradient;
		CGContextFillRect(context, [self bounds]);
	}
	else
	{
		CGContextSetFillColor(context, [CPColor redColor]);
		CGContextFillRect(context, [self bounds]);
	}
	
	if (drawText)
	{
		// debugger;
		var atext = "Wedding!";
	
	  	context.font = "bold 36px sans-serif";
		context.textBaseline = "alphabetic";
	  	context.fillStyle = "red";
	  	context.fillText(atext, 200, 120);

		var dim = context.measureText(atext);
  
	
	    var gradient2 = context.createLinearGradient(200, 0, 200 + dim.width, 0);

		// Add the color stops.
		for (var i = 0; i <= 6; i++)
		{
			var color = "rgb(" + hue[i][0] + ", " + hue[i][1] + ", " + hue[i][2] + ")";
			gradient2.addColorStop(i * 1.0 / 6.0, color);
		}

		// Use the gradient for the fillStyle.
		context.fillStyle = gradient2;


		// Now let's draw a rectangle with a black shadow.
		// Shadows only render in Konqueror 4.1 and Firefox 3.1 nightlies.
		context.shadowOffsetX = 5;
		context.shadowOffsetY = 5;
		context.shadowBlur    = 4;
		context.shadowColor   = "rgba(0, 0, 0, 0.5)";

		context.font          = "bold 36px sans-serif";
		context.textBaseline  = "alphabetic";

		if (context.fillText) 
		{
			context.fillText(atext, 200, 120, 200);
		}

		context.strokeStyle = "#666";
		if (context.strokeText) 
		{
			context.strokeText(atext, 200, 120, 200);
		}

	//
	//	CGContextTranslateCTM(context, 100, 100);
	//	CGContextRotateCTM(context, Math.PI / 2.0);

		context.font          = "italic 36px sans-serif";
		context.textBaseline  = "alphabetic";



		if (context.fillText) 
		{
			context.fillText(atext, 200, 220, 200);
		}

		context.strokeStyle = "#666";
		if (context.strokeText) 
		{
			context.strokeText(atext, 200, 220, 200);
		}


		context.save();
	
		context.translate(CPRectGetWidth([self bounds])/2.0, CPRectGetHeight([self bounds])/2.0);
	
		context.save();
		context.rotate(Math.PI / 4.0);
		CGContextSetFillColor(context, [CPColor whiteColor]);
		CGContextFillRect(context, CPMakeRect(0,0, 10, 10));
		context.restore();

		if (drawRotatedText)
		{
			context.font          = "36px sans-serif";
			context.textBaseline  = "middle";
	
			for(var index = 0; index < 8; index++)
			{
				context.save();
	
				context.rotate(index * 2.0 * Math.PI / 8.0);
	
				if (context.fillText) 
				{
					context.fillText(atext, 50, 0, 200);
				}
	
				context.restore();
			}
		}
	
		context.restore();
	}

	if (drawRadialGradient)
	{
		// Create gradients
		var radgrad = context.createRadialGradient(45,45,10,52,50,30);
		radgrad.addColorStop(0, '#A7D30C');
		radgrad.addColorStop(0.9, '#019F62');
		radgrad.addColorStop(1, 'rgba(1,159,98,0)');

		var radgrad2 = context.createRadialGradient(105,105,20,112,120,50);
		radgrad2.addColorStop(0, '#FF5F98');
		radgrad2.addColorStop(0.75, '#FF0188');
		radgrad2.addColorStop(1, 'rgba(255,1,136,0)');

		var radgrad3 = context.createRadialGradient(95,15,15,102,20,40);
		radgrad3.addColorStop(0, '#00C9FF');
		radgrad3.addColorStop(0.8, '#00B5E2');
		radgrad3.addColorStop(1, 'rgba(0,201,255,0)');

		var radgrad4 = context.createRadialGradient(0,150,50,0,140,90);
		radgrad4.addColorStop(0, '#F4F201');
		radgrad4.addColorStop(0.8, '#E4C700');
		radgrad4.addColorStop(1, 'rgba(228,199,0,0)');

		// draw shapes
		context.fillStyle = radgrad4;
		context.fillRect(0,0,150,150);
		context.fillStyle = radgrad3;
		context.fillRect(0,0,150,150);
		context.fillStyle = radgrad2;
		context.fillRect(0,0,150,150);
		context.fillStyle = radgrad;
		context.fillRect(0,0,150,150);
	}

}

- (BOOL)isOpaque {
    return YES;
}

@end
