package plot;

import java.awt.*;
import java.util.Stack;
import real.Formats;

// -- DrawPath ----------------------------------------------

class DrawPath implements ActivePath {
	final static int None = 0, Stroke = 1,
		Fill = 2;
	// type = a bit set of (Stroke|Fill)
	int type;
	PathSegment segment[];
	Color strokeColour;
	Color fillColour;
	public double node[][];
	
	DrawPath(PathConstructor p) {
		this.type = 0;
		int n = p.segmentStack.size();
		segment = new PathSegment[n];
		for (int i=0;i<n;i++) {
			segment[i] = (PathSegment) p.segmentStack.elementAt(i);
		}
	}
	
	DrawPath(PathConstructor p, 
			int type, Color c) {
		this(p);
		this.type = type;
		if ((type & Stroke) != 0) {
			strokeColour = c;
		} 
		if ((type & Fill) != 0) {
			fillColour = c;
		} 
	}
	
	public void stroke(Color c) {
		type ^= Stroke;
		strokeColour = c;
	}
	
	public void fill(Color c) {
		type ^= Fill;
		fillColour = c;
	}
	
	// --------------------------------------------------
	
	public void draw(RealGraphics gfx) {
		PlotPath p = new PlotPath(gfx);
		for (int i=0;i<segment.length;i++) {
			segment[i].addTo(p);
		}
		if (type == 0) {
			p.stroke(Color.gray);
		} else {
			if ((type & Fill) != 0) {
				p.fill(fillColour);
			}
			if ((type & Stroke) != 0) {
				p.stroke(strokeColour);
			}
		}
		for (int i=0;i<segment.length;i++) {
			segment[i].draw();
		}
	}
	
	public void setActive(boolean a) {
		for (int i=0;i<segment.length;i++) {
			segment[i].setActive(a);
		}
	}
	
	public Object mouseDown(double x[]) {
		Object active = null;
		for (int i=0;i<segment.length;i++) {
			Object y = segment[i].mouseDown(x);
			if (y != null) active = y;
		}
		return(active);
	}

	public Object mouseDrag(double x[]) {
		return(null);
	}

	public Object mouseUp(double x[]) {
		return(null);
	}
	
	public static double[] envelope(double b[], double c[]) {
		if (b!= null && c != null) {
			if (c[0] < b[0]) b[0] = c[0];
			if (c[1] < b[1]) b[1] = c[1];
			if (c[2] > b[2]) b[2] = c[2];
			if (c[3] > b[3]) b[3] = c[3];
			return(b);
		} else if (b!= null) { return(b); }
		else if (c != null) { return(c); }
		else return(null);
	}
	
	public double[] box() {
		double b[] = null;
		for (int i=0;i<segment.length;i++) {
			b = envelope(b, segment[i].box());
		}
		/* System.out.println("<" + (int) b[0] 
				+ " " + (int) b[1] 
				+ " " + (int) b[2] 
				+ " " + (int) b[3] + ">"); */
		return(b);
	}
	
	public String toPS() {
		StringBuffer sb = new StringBuffer();
		double r, g, b;
		String R, G, B;
		if (type == None) {
			return("");
		}
		switch(type) {
			case Stroke: {
				sb.append("gsave\n");
				r = strokeColour.getRed()/255.0;
				g = strokeColour.getGreen()/255.0;
				b = strokeColour.getBlue()/255.0;
				R = Formats.toString(r);
				G = Formats.toString(g);
				B = Formats.toString(b);
				sb.append(R + " " + G + " " + B + " setrgbcolor\n");
				sb.append("newpath\n");
				for (int i=0;i<segment.length;i++) {
					sb.append(segment[i].toPS());
				}
				sb.append("stroke\n");
				sb.append("grestore\n");
				break;
			}
			case Fill: {
				sb.append("gsave\n");
				r = fillColour.getRed()/255.0;
				g = fillColour.getGreen()/255.0;
				b = fillColour.getBlue()/255.0;
				R = Formats.toString(r);
				G = Formats.toString(g);
				B = Formats.toString(b);
				sb.append(R + " " + G + " " + B + " setrgbcolor\n");
				sb.append("newpath\n");
				for (int i=0;i<segment.length;i++) {
					sb.append(segment[i].toPS());
				}
				sb.append("fill\n");
				sb.append("grestore\n");
				break;
			}
			case (Stroke|Fill): {
				sb.append("gsave\n");
				sb.append("newpath\n");
				for (int i=0;i<segment.length;i++) {
					sb.append(segment[i].toPS());
				}
				sb.append("gsave\n");
				r = fillColour.getRed()/255.0;
				g = fillColour.getGreen()/255.0;
				b = fillColour.getBlue()/255.0;
				R = Formats.toString(r);
				G = Formats.toString(g);
				B = Formats.toString(b);
				sb.append(R + " " + G + " " + B + " setrgbcolor\n");
				sb.append("fill\n");
				sb.append("grestore\n");
				r = strokeColour.getRed()/255.0;
				g = strokeColour.getGreen()/255.0;
				b = strokeColour.getBlue()/255.0;
				R = Formats.toString(r);
				G = Formats.toString(g);
				B = Formats.toString(b);
				sb.append(R + " " + G + " " + B + " setrgbcolor\n");
				sb.append("stroke\n");
				sb.append("grestore\n");
				break;
			}
		}
		return(new String(sb));
	}
}

