package cz.vutbr.feec.gui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.Iterator;

import javax.swing.event.MouseInputAdapter;

import cz.vutbr.feec.math.GNPConfig;

/**
 * V tomto panelu jsou vykresleny vypoctene pozice landmarku.
 */
public class PanelLmComputed extends ImageSelectPanelDraggable {
	private static final long serialVersionUID = 3582247570022215648L;

	private Point cursorPosition = new Point();

	private Point estimatedPosition = new Point();
	
	public PanelLmComputed(int width, int height, String imgPath,
			GNPConfig conf) {
		super(width, height, imgPath, conf);
		draggingEnabled(false);
		addMouseMotionListener(new MouseInputAdapter() {
			@Override
			public void mouseMoved(MouseEvent e) {
				cursorPosition.x = e.getX();
				cursorPosition.y = e.getY();
			}
		});
	}

	@Override
	public void drawLandmark(Graphics2D g2d, LandmarkGui lm) {
		lm.drawShape(g2d, (int) lm.getComputedX(), (int) lm.getComputedY());
	}

	@Override
	public int getLmPositionX(LandmarkGui lm) {
		return (int) lm.getComputedX();
	}

	@Override
	public int getLmPositionY(LandmarkGui lm) {
		return (int) lm.getComputedY();
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = ((Graphics2D) g);
		boolean isOver = false;
		for (Iterator<LandmarkGui> iterator = config.getLandmarks().iterator(); iterator
				.hasNext();) {
			LandmarkGui landmark = iterator.next();
			if (landmark.isOver()) {
				drawOriginalLandmark(g2d, landmark);
				isOver = true;
			}
		}
		// draw cursor
		if (!isOver) {
			for(int i=0; i<500; i++) {
				g2d.translate(estimatedPosition.x, estimatedPosition.y);
				Paint defaultPaint = g2d.getPaint();
				Color def = g2d.getColor();
				g2d.setPaint(Color.RED);
				drawPointer(g2d);
				g2d.setPaint(defaultPaint);
				g2d.translate(-estimatedPosition.x, -estimatedPosition.y);
				g2d.setPaint(def);
			}
		}
	}

	private void drawPointer(Graphics2D g2d) {
//		Rectangle2D.Double rect = new Ellipse2D.Double(-2, -2, 4.0, 4.0);

		AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
				0.05f);
		// pruhlednost
		g2d.setComposite(ac);
		g2d.fill(new Ellipse2D.Double(0, 0, 8.0, 8.0));
//		g2d.fill(rect);
//
//		g2d.drawLine(-12, -1, -7, -1);
//		g2d.drawLine(7, -1, 12, -1);
//		g2d.drawLine(-12, 1, -7, 1);
//		g2d.drawLine(7, 1, 12, 1);
//
//		g2d.drawLine(-1, -12, -1, -7);
//		g2d.drawLine(-1, 7, -1, 12);
//		g2d.drawLine(1, -12, 1, -7);
//		g2d.drawLine(1, 7, 1, 12);

		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
	}

	public void drawOriginalLandmark(Graphics2D g2d, LandmarkGui lm) {
//		lm.drawShape(g2d, (int) lm.getPosition().x, lm.getPosition().y);
	}

}
