package express.lines;

import express.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Vector;
import java.util.Date;

public class ColoredLine extends Vector implements RedrawableLine {

	long startTime;
	long currentTime;
	int startX, startY;
	int R, G, B;
	boolean dead;	
	boolean fading = true;
	Color lineColor;
	
	
	public Line001(int x, int y, long t, Color lineColor) {
		
		super();
		this.addElement( new TimedPoint(x,y,t) );
			
		this.startTime = t;
		this.startX = x;
		this.startY = y;
		
		this.lineColor = lineColor;
		
		this.R = lineColor.getRed();
		this.G = lineColor.getGreen();
		this.B = lineColor.getBlue();	
	}
	
	public void redraw(Graphics g) {
		paint(g);
	}
	
	public void addPoint(int x, int y, long t) {
		this.addElement( new TimedPoint(x,y,t) );
	}

	public void setLineColor(Color lineColor) {
		this.lineColor = lineColor;
		
	}

	public void paint(Graphics g) {
		//
		if (fading) {
			currentTime = new Date().getTime(); 
			lineColor = lighterColor(currentTime);
		}
		
		g.setColor(lineColor);
		for (int i = 0, len = this.size()-1; i < len; ++i) {
			drawLinePieces(g, 
						   ((TimedPoint)(this.elementAt(i))).x,
						   ((TimedPoint)(this.elementAt(i))).y,
						   ((TimedPoint)(this.elementAt(i+1))).x,
						   ((TimedPoint)(this.elementAt(i+1))).y);
		}
		
	}


	public void drawLinePieces( Graphics g, int x1, int y1, int x2, int y2) {
		
		int xStep = (int) ( (float)(x2-x1)/(float)8 );
		int yStep = (int) ( (float)(y2-y1)/(float)8 );
		for (int t=0; t<8; t=t+1) {	
			float r1 = (float)(Math.random()*5);
			float r2 = (float)(Math.random()*5);
			float s1 = (float)(t)/10;
			float X1 = (float) (r2*10 * Math.cos(r1*s1) * Math.cos(s1));
			float Y1 = (float) (r1*10 * Math.cos(r2*s1) * Math.sin(s1));
			g.drawLine(x1+(int)t*xStep, 0, x1+(int)X1*xStep, y1+(int)Y1*yStep);
		}
		
	}
	
	
	
	public Color lighterColor(long currentTime) {
		
		this.currentTime = currentTime;
		long differenceTime = currentTime - startTime;
		int t = (int)(0.0003*differenceTime);
		
		R = (int)(R+t);
		if (R > 255) {R = 255;}

		G = (int)(G+t);
		if (G > 255) {G = 255;}

		B = (int)(B+t);
		if (B > 255) {B = 255;}
		
		if (R==255 & G==255 & B==255) {dead = true;}
		
		lineColor = new Color(R,G,B);
		return lineColor;	
	}
	
	
}
