package jp.gr.java_conf.daisy.ajax_mutator.mutator;

import java.io.PrintStream;
import java.util.Collection;

import jp.gr.java_conf.daisy.ajax_mutator.JSType;
import jp.gr.java_conf.daisy.ajax_mutator.mutatable.DOMSelection;
import jp.gr.java_conf.daisy.ajax_mutator.mutatable.DOMSelection.SelectionMethod;
import jp.gr.java_conf.daisy.ajax_mutator.util.StringToAst;
import jp.gr.java_conf.daisy.ajax_mutator.util.Randomizer;

import org.mozilla.javascript.ast.AstNode;

/**
 * Mutator for {@code DOMSelection}, which mutate domSelection result to its
 * parent or child.
 *
 * @author Kazuki Nishiura
 */
public class DOMSelectionMutator extends AbstractMutator<DOMSelection> {
	public DOMSelectionMutator(Collection<DOMSelection> mutationTargets) {
		this(mutationTargets, DEFAULT_STREAM);
	}

	public DOMSelectionMutator(Collection<DOMSelection> mutationTargets,
			PrintStream printStream) {
		super(mutationTargets, printStream);
	}

	@Override
	protected AstNode selectReplacingCandidate(DOMSelection mutationTarget) {
		return mutationTarget.getAstNode();
	}

	@Override
	protected void replaceFocusedNodeOf(DOMSelection parent,
			AstNode replacingNode) {
		double randomValue = Randomizer.getDouble();
		AstNode node = parent.getAstNode();
		JSType domType
			= (parent.getSelectionMethod() == SelectionMethod.JQUERY)
				? JSType.JQUERY_OBJECT
				: JSType.DOM_ELEMENT;

		if (randomValue < 0.5) {
			parent.replace(node, StringToAst.createParentNode(node, domType));
		} else {
			parent.replace(node, StringToAst.createChildNode(node, domType));
		}
	}
}

@@ -349,27 +349,26 @@
 or.util.
-StringToAst
+Randomizer
 ;%0Aimport
@@ -408,26 +408,27 @@
 or.util.
-Randomizer
+StringToAst
 ;%0A%0Aimpor
@@ -811,32 +811,36 @@
 electionMutator(
+%0A%09%09%09
 Collection%3CDOMSe
@@ -864,20 +864,17 @@
 Targets,
-%0A%09%09%09
+ 
 PrintStr
@@ -1025,147 +1025,8 @@
 ) %7B%0A
-%09%09return mutationTarget.getAstNode();%0A%09%7D%0A%0A%09@Override%0A%09protected void replaceFocusedNodeOf(DOMSelection parent,%0A%09%09%09AstNode replacingNode) %7B%0A
 %09%09do
@@ -1085,21 +1085,29 @@
  node = 
-paren
+mutationTarge
 t.getAst
@@ -1137,21 +1137,29 @@
 e%0A%09%09%09= (
-paren
+mutationTarge
 t.getSel
@@ -1284,36 +1284,22 @@
 5) %7B%0A%09%09%09
-parent.replace(node,
+return
  StringT
@@ -1330,25 +1330,24 @@
 de, domType)
-)
 ;%0A%09%09%7D else %7B
@@ -1354,28 +1354,14 @@
 %0A%09%09%09
-parent.replace(node,
+return
  Str
@@ -1403,16 +1403,15 @@
 ype)
-)
 ;%0A%09%09%7D%0A%09%7D
 %0A%7D%0A
@@ -1407,11 +1407,190 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%0A%09@Override%0A%09protected void replaceFocusedNodeOf(%0A%09%09%09DOMSelection parent, AstNode replacingNode) %7B%0A%09%09AstNode node = parent.getAstNode();%0A%09%09parent.replace(node, replacingNode);%0A%09%7D
 %0A%7D%0A

fix bug around log from DOMSelectionMutator