import sk.uniba.fmph.pocprak.simplegraphics.GrGraphics;
import java.awt.event.*;

public class point {
  public double x;
  public double y;

  private GrGraphics gr; //for handlimg mouse events
  private boolean active = false;


  public point() {
    this(0., 0.);
  }

  public point(double x, double y) {
    this.x = x;
    this.y = y;
  }
  
  public point(point p){
      this.x=p.x;
      this.y=p.y;
  }

  public static double distance(point A, point B) {
    double x = B.x - A.x;
    double y = B.y - A.y;
    return Math.sqrt(x * x + y * y);
  }

  /**
   * Bod sa vykresli na zobrazovacej ploche, ktora je dana
   * hodnotou GrGraphics g. Vyuziva sa tu balik
   * sk.uniba.fmph.pocprak.simplegraphics
   */
  public void draw(GrGraphics g) {
    g.drawPoint(x, y);
  }

  /**
   * Bod sa vykresli na zobrazovacej ploche, ktora je dana
   * hodnotou GrGraphics g. Velkost bodu na obrazovke bude size pixelov
   */

  public void draw(GrGraphics g, int size) {
    g.drawPoint(x, y, size);
  }

  /**
   * Bod this nastavi do polohy danej kliknutim mysou
   * @param g GrGraphics grafika okna na ktorom sa bude klikat
   */
  public void getMousePosition(GrGraphics g) {
    gr = g;
    gr.display.addMouseListener(mouse);
    System.out.println("Cakam na kliknutie mysou na obrazok!!!");
    setActive(true);
    while (isActive()) {
      try {
        Thread.sleep(100);
      }
      catch (InterruptedException ex) {
        throw new Error(ex);
      }
    }
    gr.display.removeMouseListener(mouse);
  }


  private void Clicpoint(double displayx, double displayy) {
    this.x = gr.userX(displayx);
    this.y = gr.userY(displayy);
    setActive(false);
  }

  private synchronized boolean isActive() {
    return active;
  }

  private synchronized void setActive(boolean active) {
    this.active = active;
  }

  private MouseListener mouse = new MouseAdapter() {
    public long lastclicked = 0;

    public void mouseClicked(MouseEvent e) {
      if(!isActive()) return;
      if (e.getButton() != e.BUTTON1) {
        return;
      }
      Clicpoint(e.getX(), e.getY());
    }
  };

}
