package de.htwberlin.selabs.ssme.operations;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.AbstractOperation;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.gef4.geometry.planar.Point;
import org.eclipse.gef4.mvc.operations.ITransactionalOperation;

import de.htwberlin.selabs.ssme.ConsoleOutput;
import de.htwberlin.selabs.ssme.ConsoleOutput._consoleLevel;
import de.htwberlin.selabs.ssme.parts.TransitionLabelPart;

public class TransitionLabelChangePositionOperation extends AbstractOperation implements ITransactionalOperation {

	private TransitionLabelPart part;
	private Point oldPos;
	private Point newPos;

	public TransitionLabelChangePositionOperation(TransitionLabelPart part, Point oldPos, Point newPos) {
		super("Change TextNode Position");
		this.oldPos = oldPos;
		this.newPos = newPos;
		this.part = part;
	}

	@Override
	public IStatus execute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		part.setPosition(newPos);
		ConsoleOutput.addConsoleLine(_consoleLevel.DEBUG,
				"Changed the position of the ( Transition Label ) with the id : ( " + part.getContent().getId()
						+ " ) from  ( " + (int) oldPos.x + " , " + (int) oldPos.y + " ) to ( " + (int) newPos.x + " , "
						+ (int) newPos.y + " )");

		return Status.OK_STATUS;
	}

	@Override
	public IStatus redo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		return execute(monitor, info);
	}

	@Override
	public IStatus undo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		part.setPosition(oldPos);
		ConsoleOutput.addConsoleLine(_consoleLevel.DEBUG,
				"Changed the position of the ( Transition Label ) with the id : ( " + part.getContent().getId()
						+ " ) from  ( " + (int) newPos.x + " , " + (int) newPos.y + " ) to ( " + (int) oldPos.x + " , "
						+ (int) oldPos.y + " )");
		return Status.OK_STATUS;
	}

	@Override
	public boolean isNoOp() {
		return newPos.equals(oldPos);
	}

	@Override
	public boolean isContentRelevant() {
		return true;
	}

}
