package de.darwinspl.feature.graphical.editor.commands.group;

import java.util.Date;

import org.eclipse.gef.commands.Command;

import de.darwinspl.feature.graphical.base.editor.DwGraphicalFeatureModelViewer;
import de.darwinspl.feature.graphical.base.model.DwFeatureModelWrapped;
import de.darwinspl.feature.graphical.base.model.DwFeatureWrapped;
import de.darwinspl.feature.graphical.base.model.DwGroupWrapped;
import de.darwinspl.feature.graphical.base.model.DwParentChildConnection;
import eu.hyvar.evolution.util.HyEvolutionUtil;
import eu.hyvar.feature.HyFeatureChild;
import eu.hyvar.feature.HyFeatureFactory;
import eu.hyvar.feature.HyGroup;

public class DwGroupMoveToAnotherHyFeatureCommand extends Command{
	DwGroupWrapped groupWrapped;
	DwFeatureModelWrapped featureModelWrapped;
	DwFeatureWrapped newParentFeatureWrapped;
	DwGraphicalFeatureModelViewer viewer;
	public DwGroupMoveToAnotherHyFeatureCommand(DwGroupWrapped group, 
												DwFeatureWrapped newParentFeatureWrapped, 
												DwFeatureModelWrapped featureModelWrapped,
												DwGraphicalFeatureModelViewer viewer){
		this.groupWrapped = group;
		this.featureModelWrapped = featureModelWrapped;
		this.newParentFeatureWrapped = newParentFeatureWrapped;
		this.viewer = viewer;
	}

	@Override
	public void execute(){
		redo();
	}
	
	@Override
	public void redo(){
		Date date = featureModelWrapped.getSelectedDate();
		if(date.equals(new Date(Long.MIN_VALUE)))
			date = null;
				
		HyGroup group = groupWrapped.getWrappedModelElement();
		HyFeatureChild oldParentFeatureChild = HyEvolutionUtil.getValidTemporalElement(group.getChildOf(), date);
		
		
		HyFeatureChild featureChild = HyFeatureFactory.eINSTANCE.createHyFeatureChild();
		featureChild.setValidSince(date);
		
		
		newParentFeatureWrapped.getWrappedModelElement().getParentOf().add(featureChild);
		featureChild.setChildGroup(group);
		group.getChildOf().add(featureChild);
		
		if(date == null){
			group.getChildOf().remove(oldParentFeatureChild);
			oldParentFeatureChild.getParent().getParentOf().remove(oldParentFeatureChild);
		}else{
			oldParentFeatureChild.setValidUntil(date);
		}
		
		
		
		
	
		// remove all old connections
		for(DwFeatureWrapped featureWrapped : groupWrapped.getFeaturesWrapped(date)){
			for(DwParentChildConnection connection : featureWrapped.getParentConnections(date)){
				connection.getSource().removeParentToChildConnection(connection);
				connection.getTarget().removeChildToParentConnection(connection);
			}
		}
		
		// add new connections
		for(DwFeatureWrapped featureWrapped : groupWrapped.getFeaturesWrapped(date)){
			DwParentChildConnection connection = new DwParentChildConnection();
			connection.setModel(featureModelWrapped);
			connection.setSource(newParentFeatureWrapped);
			connection.setTarget(featureWrapped);
			
			featureModelWrapped.addConnection(connection, date, group);
			
			featureWrapped.addChildToParentConnection(connection);
			newParentFeatureWrapped.addParentToChildConnection(connection);
		}
		
		
		

		featureModelWrapped.rearrangeFeatures();
		viewer.refreshView();
	}
	
	@Override
	public void undo(){
		System.err.println("Not implemented yet");
	}
}

@@ -829,16 +829,59 @@
 rapped;%0A
+%09DwFeatureWrapped oldParentFeatureWrapped;%0A
 %09DwGraph
@@ -1360,36 +1360,70 @@
 %0A%09%0A%09
-@Override%0A%09public void redo(
+private void moveToNewParentFeature(DwFeatureWrapped newParent
 )%7B%0A%09
@@ -1534,16 +1534,22 @@
 = null;%0A
+%09%09%0A%09%09%0A
 %09%09%09%09%0A%09%09H
@@ -1709,24 +1709,169 @@
 , date);%0A%09%09%0A
+%09%09// save old parent feature for undo%0A%09%09oldParentFeatureWrapped = viewer.getModelWrapped().getWrappedFeature(oldParentFeatureChild.getParent());%0A
 %09%09%0A%09%09HyFeatu
@@ -1997,30 +1997,16 @@
 ewParent
-FeatureWrapped
 .getWrap
@@ -2970,30 +2970,16 @@
 ewParent
-FeatureWrapped
 );%0A%09%09%09co
@@ -3155,30 +3155,16 @@
 ewParent
-FeatureWrapped
 .addPare
@@ -3199,26 +3199,107 @@
 on);%0A%09%09%7D
-%0A%09%09%0A%09%09%0A%09%09%0A
+%09%09%0A%09%7D%0A%09@Override%0A%09public void redo()%7B%0A%09%09moveToNewParentFeature(newParentFeatureWrapped);%0A%09%09
 %0A%09%09featu
@@ -3401,48 +3401,123 @@
 %7B%0A%09%09
-System.err.println(%22Not implemented yet%22
+moveToNewParentFeature(oldParentFeatureWrapped);%0A%09%0A%09%09featureModelWrapped.rearrangeFeatures();%0A%09%09viewer.refreshView(
 );%0A%09

Add undo for group move to another parent feature functionality