package org.richfaces.renderkit.html.iconimages;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

import org.ajax4jsf.resource.ResourceContext;

/**
 * @author Anton Belevich
 *
 */
public abstract class PanelMenuIconChevronBasic extends PanelMenuIconBasic {

	protected void paintImage(ResourceContext context, Graphics2D graphics2d, Color color) {
		
		BufferedImage bufferedImage = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = bufferedImage.createGraphics();
			
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
		g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
		g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
		g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
		g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
		g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

		Dimension dimension = getDimensions(context);
		GeneralPath path = new GeneralPath();
		g2d.setStroke(new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER));
		g2d.translate(28,28);

		g2d.setColor(color);
			
		draw(path);
			
		g2d.fill(path);
		if (this instanceof PanelMenuIconChevron ||
				this instanceof PanelMenuIconChevronLeft)
			g2d.translate(24, 0);
		else
			g2d.translate(0, 24);
		g2d.fill(path);
		
		AffineTransform transform = AffineTransform.getScaleInstance(dimension.getHeight()/128, dimension.getHeight()/128);
		AffineTransformOp transformOp = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
			
		graphics2d.drawImage(bufferedImage,transformOp, 0, 0);
		g2d.dispose();
	}
	
	abstract void draw (GeneralPath draw);
}
