/*
   * (non-Javadoc)
   *
   * @see br.org.archimedes.factories.SelectionPointVectorFactory#drawVisualHelper(br.org.archimedes.model.writers.Writer,
   *      java.util.Set, br.org.archimedes.model.Point,
   *      br.org.archimedes.model.Vector)
   */
  @Override
  protected void drawVisualHelper(Set<Element> selection, Point reference, Vector vector) {

    OpenGLWrapper opengl = br.org.archimedes.Utils.getOpenGLWrapper();
    for (Element element : selection) {
      Element copied = element.clone();
      try {
        Point start = reference;
        Point end = start.addVector(vector);

        List<Point> points = new LinkedList<Point>();
        points.add(start);
        points.add(end);
        opengl.drawFromModel(points);

        copied.mirror(start, end);
        copied.draw(opengl);
      } catch (NullArgumentException e) {
        // Should not happen
        e.printStackTrace();
      } catch (Exception e) {
        // Might happen, just ignore
      }
    }
  }
  public String begin() {

    String returnValue = null;
    done = false;
    Set<Element> selection = null;
    try {
      selection = br.org.archimedes.Utils.getController().getCurrentSelectedElements();
    } catch (NoActiveDrawingException e) {
      // Shouldn't happen since I am an active factory
      e.printStackTrace();
    }

    boolean needsAValidSelection = true;
    if (selection != null && !selection.isEmpty()) {
      try {
        returnValue = next(selection);
        needsAValidSelection = false;
      } catch (InvalidParameterException e) {
        // Can happen if the selection is not valid.
        // Just work like if nothing was selected.
      }
    }

    if (needsAValidSelection) {
      parser = new SimpleSelectionParser();
      returnValue = Messages.SelectorFactory_Select;
    }

    return returnValue;
  }
  /** @param newContextId The new context id */
  private void changeContext(String newContextId) {

    IContextService contextService = Utils.getContextService();
    if (previousActivation != null) {
      contextService.deactivateContext(previousActivation);
    }
    previousActivation = contextService.activateContext(newContextId);
  }
  @Before
  public void setUp() {

    drawing = new Drawing("Test");
    controller.setActiveDrawing(drawing);
    manager = new MousePositionManager();
    controller.deselectAll();
    br.org.archimedes.Utils.getWorkspace().setWindowSize(new Rectangle(-1000, -1000, 1000, 1000));
  }
  /*
   * (non-Javadoc)
   *
   * @see com.tarantulus.archimedes.factories.TwoPointFactory#drawVisualHelper(com.tarantulus.archimedes.model.writers.Writer,
   *      com.tarantulus.archimedes.model.Point,
   *      com.tarantulus.archimedes.model.Point)
   */
  @Override
  protected void drawVisualHelper(Point start, Point end) {

    try {
      InfiniteLine xline = new InfiniteLine(start.getX(), start.getY(), end.getX(), end.getY());
      xline.draw(br.org.archimedes.Utils.getOpenGLWrapper());
    } catch (InvalidArgumentException e) {
      // Draw nothing
    }
  }
  /** @param drawing True if the interpreter is enabled, false otherwise. */
  public void setDrawing(Drawing drawing) {

    Controller controller = br.org.archimedes.Utils.getController();
    if (!controller.isThereActiveDrawing()) {
      controller.setActiveDrawing(drawing);
    } else if (this.getCurrentFactory() == null || this.getCurrentFactory().isDone()) {
      controller.setActiveDrawing(drawing);
      controller.deselectAll();
    }

    changeState(currentState.changedDrawing(drawing));
    setChanged();
    notifyObservers(drawing);
  }
/** Belongs to package br.org.archimedes.gui.model. */
public class MousePositionManagerTest extends Tester {

  private Drawing drawing;

  private MousePositionManager manager;

  private Controller controller = br.org.archimedes.Utils.getController();

  @Before
  public void setUp() {

    drawing = new Drawing("Test");
    controller.setActiveDrawing(drawing);
    manager = new MousePositionManager();
    controller.deselectAll();
    br.org.archimedes.Utils.getWorkspace().setWindowSize(new Rectangle(-1000, -1000, 1000, 1000));
  }

  @Test
  public void testNoGrips() throws InvalidArgumentException {

    Point mouse, grip, actual;
    ReferencePoint refPoint;
    mouse = new Point(100, 50);
    manager.setMousePosition(mouse);
    refPoint = manager.getGripMousePosition();
    grip = (refPoint == null ? null : refPoint.getPoint());
    Assert.assertEquals(null, grip);
    actual = manager.getActualMousePosition();
    Assert.assertEquals(mouse, actual);

    // TODO Arrumar
    // putSafeElementOnDrawing(new Line(0, 0, 100, 100), drawing);

    refPoint = manager.getGripMousePosition();
    grip = (refPoint == null ? null : refPoint.getPoint());
    Assert.assertEquals(null, grip);
    actual = manager.getActualMousePosition();
    Assert.assertEquals(mouse, actual);
  }

  @Test
  public void testWithOneLine() throws InvalidArgumentException {

    ReferencePoint refPoint;
    Point mouse, grip, actual, expected;
    putSafeElementOnDrawing(new Line(0, 0, 100, 100), drawing);

    // Grip to an extreme
    mouse = new Point(0.1, 0.1);
    manager.setMousePosition(mouse);

    refPoint = manager.getGripMousePosition();
    grip = (refPoint == null ? null : refPoint.getPoint());
    expected = new Point(0, 0);
    Assert.assertEquals(expected, grip);
    actual = manager.getActualMousePosition();
    Assert.assertEquals(mouse, actual);

    // Grip to a central point
    mouse = new Point(50.1, 50.1);
    manager.setMousePosition(mouse);
    refPoint = manager.getGripMousePosition();
    grip = (refPoint == null ? null : refPoint.getPoint());
    expected = new Point(50, 50);
    Assert.assertEquals(expected, grip);
    actual = manager.getActualMousePosition();
    Assert.assertEquals(mouse, actual);

    // Grip perpendicular point
    Point firstPoint = new Point(80, 70);
    manager.setPerpendicularGripReferencePoint(firstPoint);
    mouse = new Point(74.9, 74.9);
    manager.setMousePosition(mouse);
    refPoint = manager.getGripMousePosition();
    grip = (refPoint == null ? null : refPoint.getPoint());
    expected = new Point(75, 75);
    Assert.assertEquals(expected, grip);
    actual = manager.getActualMousePosition();
    Assert.assertEquals(mouse, actual);
  }

  @Test
  public void testWithTwoLines() throws InvalidArgumentException {

    Point mouse, grip, actual, expected;
    putSafeElementOnDrawing(new Line(0, 0, 100, 100), drawing);
    putSafeElementOnDrawing(new Line(50, 75, 150, 75), drawing);

    // Grip to intersection
    mouse = new Point(74.9, 74.9);
    manager.setMousePosition(mouse);
    ReferencePoint refPoint;
    refPoint = manager.getGripMousePosition();
    grip = (refPoint == null ? null : refPoint.getPoint());
    expected = new Point(75, 75);
    Assert.assertEquals(expected, grip);
    actual = manager.getActualMousePosition();
    Assert.assertEquals(mouse, actual);
  }
}