package de.ugoe.cs.autoquest.tasktrees.nodeequality;

import de.ugoe.cs.autoquest.eventcore.gui.IInteraction;
import de.ugoe.cs.autoquest.eventcore.gui.MouseClick;
import de.ugoe.cs.autoquest.eventcore.gui.MouseDragAndDrop;
import de.ugoe.cs.autoquest.eventcore.gui.TextInput;
import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection;
import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;

/**
 * <p>
 * This rule compares GUI event tasks (i.e. it is more concrete, than the
 * {@link EventTaskComparisonRule}). Two GUI event tasks are only equal if their event type and
 * target are equal. The returned equality is even more fine-grained for events whose type is
 * {@link TextInput} and {@link ValueSelection}. For text inputs, lexical equality is returned if
 * the same text is entered using the same key interactions. Syntactical equality is returned if
 * the same text is entered using different key interactions. Semantical equality is returned if
 * different text is entered, but into the same event target. Value selections are syntactically
 * equal, if the same value is selected. Otherwise they are semantically equal.
 * </p>
 * 
 * @author Patrick Harms
 */
public class GUIEventTaskComparisonRule implements NodeComparisonRule {
    
    /*
     * (non-Javadoc)
     * 
     * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode)
     */
    @Override
    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) {
        if ((!(node1 instanceof IEventTask)) || (!(node2 instanceof IEventTask))) {
            return null;
        }
        
        IEventTask task1 = (IEventTask) node1;
        IEventTask task2 = (IEventTask) node2;
        
        if ((!(task1.getEventType() instanceof IInteraction)) ||
            (!(task2.getEventType() instanceof IInteraction)))
        {
            return null;
        }

        if (node1 == node2) {
            return NodeEquality.IDENTICAL;
        }

        if (!task1.getEventTarget().equals(task2.getEventTarget())) {
            return NodeEquality.UNEQUAL;
        }
        
        IInteraction interaction1 = (IInteraction) task1.getEventType();
        IInteraction interaction2 = (IInteraction) task2.getEventType();
        
        return compareInteractions(interaction1, interaction2);
    }

    /**
     * <p>
     * compares two interactions. The method delegates two
     * {@link #compareTextInputs(TextInput, TextInput)} and
     * {@link #compareValueSelections(ValueSelection, ValueSelection)} for text inputs and value
     * selections. Otherwise it uses the equal method of the interactions for comparison. In this
     * case, if the interactions equal method returns true, this method returns lexical equality.
     * </p>
     *
     * @param interaction1 the first interaction to compare
     * @param interaction2 the second interaction to compare
     * 
     * @return as described
     */
    private NodeEquality compareInteractions(IInteraction interaction1, IInteraction interaction2) {
        if (interaction1 == interaction2) {
            return NodeEquality.LEXICALLY_EQUAL;
        }
        else if ((interaction1 instanceof TextInput) && (interaction2 instanceof TextInput)) {
            return compareTextInputs((TextInput) interaction1, (TextInput) interaction2);
        }
        else if ((interaction1 instanceof ValueSelection) &&
                 (interaction2 instanceof ValueSelection))
        {
            return compareValueSelections
                ((ValueSelection<?>) interaction1, (ValueSelection<?>) interaction2);
        }
        else if ((interaction1 instanceof MouseClick) &&
                 (interaction2 instanceof MouseClick))
        {
            return compareMouseClicks((MouseClick) interaction1, (MouseClick) interaction2);
        }
        else if ((interaction1 instanceof MouseDragAndDrop) &&
                 (interaction2 instanceof MouseDragAndDrop))
        {
            return compareMouseDragAndDrops
                ((MouseDragAndDrop) interaction1, (MouseDragAndDrop) interaction2);
        }
        else if (interaction1.equals(interaction2)) {
            return NodeEquality.LEXICALLY_EQUAL;
        }
        else {
            return NodeEquality.UNEQUAL;
        }
    }

    /**
     * <p>
     * compares two text inputs. If both text inputs have the same entered text and text input
     * events, they are lexically equal. If they only have the same entered text, they are
     * syntactically equal. If they are only both text inputs, they are semantically equal.
     * (the equality of the event targets is checked beforehand).
     * </p>
     *
     * @param interaction1 the first text input to compare
     * @param interaction2 the second text input to compare
     * 
     * @return as described
     */
    private NodeEquality compareTextInputs(TextInput interaction1, TextInput interaction2) {
        if (interaction1.getEnteredText().equals(interaction2.getEnteredText())) {
            if (interaction1.getTextInputEvents().equals(interaction2.getTextInputEvents())) {
                return NodeEquality.LEXICALLY_EQUAL;
            }
            else {
                return NodeEquality.SYNTACTICALLY_EQUAL;
            }
        }
        else {
            return NodeEquality.SEMANTICALLY_EQUAL;
        }
    }

    /**
     * <p>
     * compares two value selections. If both value selections have the same selected value, they
     * are syntactically equal, otherwise they are semantically equal.
     * (the equality of the event targets is checked beforehand).
     * </p>
     *
     * @param interaction1 the first value selection to compare
     * @param interaction2 the second value selection to compare
     * 
     * @return as described
     */
    private NodeEquality compareValueSelections(ValueSelection<?> interaction1,
                                                ValueSelection<?> interaction2)
    {
        Object value1 = interaction1.getSelectedValue();
        Object value2 = interaction2.getSelectedValue();
        
        if ((value1 == value2) || ((value1 != null) && (value1.equals(value2)))) {
            return NodeEquality.LEXICALLY_EQUAL;
        }
        else {
            return NodeEquality.SEMANTICALLY_EQUAL;
        }
    }

    /**
     * <p>
     * compares two mouse clicks. If both clicks have the same coordinates, they are lexically
     * equal. Otherwise, they are semantically equal.
     * </p>
     *
     * @param interaction1 the first mouse click to compare
     * @param interaction2 the second mouse click to compare
     * 
     * @return as described
     */
    private NodeEquality compareMouseClicks(MouseClick interaction1,
                                            MouseClick interaction2)
    {
        int x1 = interaction1.getX();
        int x2 = interaction2.getX();
        int y1 = interaction1.getY();
        int y2 = interaction2.getY();
        
        if ((x1 == x2) && (y1 == y2)) {
            return NodeEquality.LEXICALLY_EQUAL;
        }
        else {
            return NodeEquality.SEMANTICALLY_EQUAL;
        }
    }

    /**
     * <p>
     * compares two mouse drag and drops. If both drag and drops have the same start and end
     * coordinates, they are lexically equal. Otherwise, they are semantically equal.
     * </p>
     *
     * @param interaction1 the first mouse drag and drop to compare
     * @param interaction2 the second mouse drag and drop to compare
     * 
     * @return as described
     */
    private NodeEquality compareMouseDragAndDrops(MouseDragAndDrop interaction1,
                                                  MouseDragAndDrop interaction2)
    {
        int x1 = interaction1.getX();
        int x1Start = interaction1.getXStart();
        int x2 = interaction2.getX();
        int x2Start = interaction2.getXStart();
        int y1 = interaction1.getY();
        int y1Start = interaction1.getYStart();
        int y2 = interaction2.getY();
        int y2Start = interaction2.getYStart();
        
        if ((x1Start == x2Start) && (x1 == x2) && (y1Start == y2Start) && (y1 == y2)) {
            return NodeEquality.LEXICALLY_EQUAL;
        }
        else {
            return NodeEquality.SEMANTICALLY_EQUAL;
        }
    }

}
