// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.htmlunit;

import java.io.IOException;

import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.interactions.InvalidCoordinatesException;
import org.openqa.selenium.interactions.Mouse;
import org.openqa.selenium.interactions.internal.Coordinates;

import com.gargoylesoftware.htmlunit.ScriptException;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent;
import com.google.common.base.Preconditions;

/**
 * Implements mouse operations using the HtmlUnit WebDriver.
 *
 */
public class HtmlUnitMouse implements Mouse {
  private final HtmlUnitKeyboard keyboard;
  private HtmlElement currentActiveElement = null;

  public HtmlUnitMouse(HtmlUnitKeyboard keyboard) {
    this.keyboard = keyboard;
  }

  private HtmlElement getElementForOperation(Coordinates potentialCoordinates) {
    if (potentialCoordinates != null) {
      return (HtmlElement) potentialCoordinates.getAuxiliary();
    }

    if (currentActiveElement == null) {
      throw new InvalidCoordinatesException("About to perform an interaction that relies"
          + " on the active element, but there isn't one.");
    }

    return currentActiveElement;
  }

  @Override
  public void click(Coordinates elementCoordinates) {
    HtmlElement element = getElementForOperation(elementCoordinates);

    moveOutIfNeeded(element);

    try {
      element.mouseOver();
      element.mouseMove();

      element.click(keyboard.isShiftPressed(),
          keyboard.isCtrlPressed(), keyboard.isAltPressed());
      updateActiveElement(element);
    } catch (IOException e) {
      throw new WebDriverException(e);
    } catch (ScriptException e) {
      // TODO(simon): This isn't good enough.
      System.out.println(e.getMessage());
      // Press on regardless
    }
  }

  private void moveOutIfNeeded(HtmlElement element) {
    try {
      if ((currentActiveElement != element)) {
        if (currentActiveElement != null) {
          currentActiveElement.mouseOver(keyboard.isShiftPressed(),
              keyboard.isCtrlPressed(), keyboard.isAltPressed(), MouseEvent.BUTTON_LEFT);

          currentActiveElement.mouseOut(keyboard.isShiftPressed(),
              keyboard.isCtrlPressed(), keyboard.isAltPressed(), MouseEvent.BUTTON_LEFT);

          currentActiveElement.blur();
        }

        if (element != null) {
          mouseMove(element);
        }
      }
    } catch (ScriptException ignored) {
      System.out.println(ignored.getMessage());
    }
  }

  private void updateActiveElement(HtmlElement element) {
    if (element != null) {
      currentActiveElement = element;
    }
  }

  public void click(Coordinates where, long xOffset, long yOffset) {
    click(where);
  }

  @Override
  public void doubleClick(Coordinates elementCoordinates) {
    HtmlElement element = getElementForOperation(elementCoordinates);

    moveOutIfNeeded(element);

    // Send the state of modifier keys to the dblClick method.
    try {
      element.dblClick(keyboard.isShiftPressed(),
          keyboard.isCtrlPressed(), keyboard.isAltPressed());
      updateActiveElement(element);
    } catch (IOException e) {
      // TODO(eran.mes): What should we do in case of error?
      e.printStackTrace();
    }
  }

  @Override
  public void contextClick(Coordinates elementCoordinates) {
    HtmlElement element = getElementForOperation(elementCoordinates);

    moveOutIfNeeded(element);

    element.rightClick(keyboard.isShiftPressed(),
        keyboard.isCtrlPressed(), keyboard.isAltPressed());

    updateActiveElement(element);
  }

  @Override
  public void mouseDown(Coordinates elementCoordinates) {
    HtmlElement element = getElementForOperation(elementCoordinates);

    moveOutIfNeeded(element);

    element.mouseDown(keyboard.isShiftPressed(),
        keyboard.isCtrlPressed(), keyboard.isAltPressed(),
        MouseEvent.BUTTON_LEFT);

    updateActiveElement(element);
  }

  @Override
  public void mouseUp(Coordinates elementCoordinates) {
    HtmlElement element = getElementForOperation(elementCoordinates);

    moveOutIfNeeded(element);

    element.mouseUp(keyboard.isShiftPressed(),
        keyboard.isCtrlPressed(), keyboard.isAltPressed(),
        MouseEvent.BUTTON_LEFT);

    updateActiveElement(element);
  }

  @Override
  public void mouseMove(Coordinates elementCoordinates) {
    Preconditions.checkNotNull(elementCoordinates);
    HtmlElement element = (HtmlElement) elementCoordinates.getAuxiliary();

    moveOutIfNeeded(element);

    updateActiveElement(element);
  }

  private void mouseMove(HtmlElement element) {
    element.mouseMove(keyboard.isShiftPressed(),
        keyboard.isCtrlPressed(), keyboard.isAltPressed(),
        MouseEvent.BUTTON_LEFT);
    element.mouseOver(keyboard.isShiftPressed(),
        keyboard.isCtrlPressed(), keyboard.isAltPressed(),
        MouseEvent.BUTTON_LEFT);
  }

  @Override
  public void mouseMove(Coordinates where, long xOffset, long yOffset) {
    throw new UnsupportedOperationException("Moving to arbitrary X,Y coordinates not supported.");
  }
}

@@ -1141,24 +1141,78 @@
 tException;%0A
+import com.gargoylesoftware.htmlunit.html.DomElement;%0A
 import com.g
@@ -1534,36 +1534,35 @@
 oard;%0A  private 
-Html
+Dom
 Element currentA
@@ -1577,15 +1577,8 @@
 ment
- = null
 ;%0A%0A 
@@ -1673,20 +1673,19 @@
 private 
-Html
+Dom
 Element 
@@ -1797,20 +1797,19 @@
 return (
-Html
+Dom
 Element)
@@ -2151,36 +2151,35 @@
 rdinates) %7B%0A    
-Html
+Dom
 Element element 
@@ -2728,20 +2728,19 @@
 fNeeded(
-Html
+Dom
 Element 
@@ -3431,20 +3431,19 @@
 Element(
-Html
+Dom
 Element 
@@ -3689,36 +3689,35 @@
 rdinates) %7B%0A    
-Html
+Dom
 Element element 
@@ -4213,36 +4213,35 @@
 rdinates) %7B%0A    
-Html
+Dom
 Element element 
@@ -4534,36 +4534,35 @@
 rdinates) %7B%0A    
-Html
+Dom
 Element element 
@@ -4888,28 +4888,27 @@
 ates) %7B%0A    
-Html
+Dom
 Element elem
@@ -5461,20 +5461,19 @@
 useMove(
-Html
+Dom
 Element 

HtmlUnitMouse.currentActiveElement should be DomElement