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;
import java.awt.geom.RoundRectangle2D;

/**
 * The Class LandmarkGui.
 * 
 * @author Radim Burget
 * 
 * Graficka nadstavba nad landmarkem.
 */
public class LandmarkGui extends LandmarkOnGui {
	
	/** pozadi objektu. */
	private Ellipse2D.Double blackBorder, whiteBorder, bgBorder;
	
	/** Okoli, zobrazovane pokud je objekt dvojklikem oznacovan jako FIXED.
	 * Znamena to, ze u prvku zname jeho pozici */
	private Ellipse2D.Double fixedBorder;

	/** The paint kreslici barva. */
	private final Paint paintWh = Color.WHITE;

	/** Barva, kterym je vyznacen prave oznaceny landmark. */
	private final Paint paintActive = Color.RED;
	
	/** Barva okoli. */
	private final Paint paintBl = Color.BLACK;

	/** The stroke enabled. */
	private final Stroke strokeEn = new BasicStroke(3.0f);
	
	/** The stroke disabled. */
	private final Stroke strokeDis = new BasicStroke(1.0f);

	/** Monitors if the object is dragged. */
	private boolean isDragged = false;
	
	/** Monitors if the mouse is over this object. */
	private boolean isOver = false;

	/** The WIDTH of this object. */
	private final int WIDTH = 18;
	
	/** The HEIGHT of this object. */
	private final int HEIGHT = 12;
	
	/** Delka popisku. */
	private final int CAPTION_LENGTH = 80;

	/**
	 * The Constructor.
	 */
	public LandmarkGui() {
		fixedBorder = new Ellipse2D.Double(0, 0, 8.0, 8.0);
		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);

	}

	/**
	 * Vykresli objekt.
	 * 
	 * @param g2d the g2d
	 * @param y the y
	 * @param x the x
	 */
	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);
		if(isFixedPosition()) {
			g2d.setPaint(Color.orange);
			g2d.draw(fixedBorder);
			g2d.setPaint(Color.BLACK);
		}

		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("" + getAddress().getHostName(), 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);
	}

	/**
	 * Sets to be dragged.
	 * 
	 * @param dragged the dragged
	 */
	public void setDragged(boolean dragged) {
		this.isDragged = dragged;
	}

	/**
	 * Checks if is dragged.
	 * 
	 * @return true, if is dragged
	 */
	public boolean isDragged() {
		return isDragged;
	}

	/**
	 * Checks if is over.
	 * 
	 * @param over the over
	 */
	public void isOver(boolean over) {
		this.isOver = over;
	}

	/**
	 * Checks if is over.
	 * 
	 * @return true, if is over
	 */
	public boolean isOver() {
		return isOver;
	}

	/**
	 * Contains.
	 * 
	 * @param p the p
	 * 
	 * @return true, if contains
	 */
	public boolean contains(Point p) {
		return blackBorder.contains(p);
	}

	/**
	 * Gets the width.
	 * 
	 * @return the width
	 */
	public int getWidth() {
		return WIDTH;
	}

	/**
	 * Gets the height.
	 * 
	 * @return the height
	 */
	public int getHeight() {
		return HEIGHT;
	}

	/** The fixed position. */
	private boolean fixedPosition = false;

	/**
	 * Sets the fixed position.
	 * 
	 * @param fixed the fixed position
	 */
	public void setFixedPosition(boolean fixed) {
		this.fixedPosition = fixed;
	}

	/**
	 * Checks if is fixed position.
	 * 
	 * @return true, if is fixed position
	 */
	public boolean isFixedPosition() {
		return fixedPosition;
	}
}
