package cz.vutbr.feec.gui;


import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

public class DraggableObject extends LandmarkOnGui {
	private Ellipse2D.Double blackBorder,  whiteBorder, bgBorder;
	
//	private Point position = new Point();
	
	private final Paint paintWh = Color.WHITE;

	private final Paint paintActive = Color.RED;
	private final Paint paintBl = Color.BLACK;
	
	private final Stroke strokeEn = new BasicStroke(3.0f);
	private final Stroke strokeDis = new BasicStroke(1.0f);
	
	private boolean isDragged = false;
	private boolean isOver = false;

	private final int WIDTH = 18;
	private final int HEIGHT = 12;
	private final int CAPTION_LENGTH = 80;
	
	public DraggableObject() {
		blackBorder = new Ellipse2D.Double(0, 0, 8.0, 8.0);
		whiteBorder = new Ellipse2D.Double(0, 0, 8.0, 8.0);
		bgBorder = new Ellipse2D.Double(0, 0, 8.0, 8.0);
	}
	public void drawShape(Graphics2D g2d, int x, int y) {
		// prepare
		Paint p = g2d.getPaint();
		Stroke s = g2d.getStroke();
		AlphaComposite ac =
			AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.5f);

		g2d.setPaint(this.paintBl);
		g2d.setStroke(this.strokeEn);
		g2d.translate(x, y);
		g2d.draw(blackBorder);

		g2d.setStroke(this.strokeDis);
		g2d.setPaint(Color.RED);
		AlphaComposite acDef = (AlphaComposite)g2d.getComposite();
  	   	g2d.setComposite(ac);
		g2d.fill(whiteBorder);
		g2d.setComposite(acDef);
		g2d.setPaint(Color.BLACK);
		g2d.drawString(""+getId(), 3, 11);
		g2d.setPaint(Color.WHITE);
		if(isOver()){
			if(x > 400) {
				g2d.translate(-CAPTION_LENGTH-WIDTH-10, 0);
			}
			g2d.setComposite(ac);
			g2d.setPaint(Color.ORANGE);
			g2d.fill(bgBorder);
			g2d.setPaint(Color.BLACK);
			g2d.drawString(getHostname(), 20, -10);
			g2d.drawString("(x:"+x+", y:"+y+")", 20, 10);
			g2d.setPaint(Color.ORANGE);
			g2d.setComposite(acDef);
			if(x > 400) {
				g2d.translate(CAPTION_LENGTH+WIDTH+10, 0);
			}
		} 
		if(isDragged()) {
			g2d.setPaint(this.paintActive);
		} 
		
		
		g2d.draw(whiteBorder);

		// restore
		g2d.translate(-x, -y);
		g2d.setStroke(s);
		g2d.setPaint(p);
	}
	
	public void isDragged(boolean dragged) {
		this.isDragged = dragged;
	}
	public boolean isDragged() {
		return isDragged;
	}
	public void isOver(boolean over) {
		this.isOver = over;
	}
	public boolean isOver() {
		return isOver;
	}
	
	public boolean contains(Point p) {
		return blackBorder.contains(p);
	}
	public int getWidth() {
		return WIDTH;
	}
	public int getHeight() {
		return HEIGHT;
	}
}
