package de.retest.suite.flow;

import de.retest.report.ActionReplayResult;
import de.retest.report.ReplayResult;
import de.retest.report.SuiteReplayResult;
import de.retest.report.TestReplayResult;
import de.retest.ui.diff.ElementDifference;
import de.retest.ui.diff.IdentifyingAttributesDifference;
import de.retest.ui.diff.InsertedDeletedElementDifference;
import de.retest.ui.review.ActionChangeSet;
import de.retest.ui.review.ReviewResult;
import de.retest.ui.review.SuiteChangeSet;
import de.retest.ui.review.TestChangeSet;

public class CreateChangesetForAllDifferencesFlow {

	private final ReplayResult replayResult;
	private final ReviewResult reviewResult;

	private CreateChangesetForAllDifferencesFlow( final ReplayResult replayResult ) {
		this.replayResult = replayResult;
		reviewResult = new ReviewResult();
	}

	public static ReviewResult createChangesetForAllChanges( final ReplayResult replayResult ) {
		final CreateChangesetForAllDifferencesFlow flow = new CreateChangesetForAllDifferencesFlow( replayResult );
		flow.execute();
		return flow.reviewResult;
	}

	private void execute() {
		for ( final SuiteReplayResult suite : replayResult.getSuiteReplayResults() ) {
			final SuiteChangeSet suiteChangeSet =
					reviewResult.createSuiteChangeSet( suite.getSuiteName(), suite.getSuiteUuid() );
			for ( final TestReplayResult test : suite.getTestReplayResults() ) {
				final TestChangeSet testChangeSet = suiteChangeSet.createTestChangeSet();
				boolean first = true;
				for ( final ActionReplayResult actionReplayResult : test.getActionReplayResults() ) {
					if ( first ) {
						addAllElementDifferences( actionReplayResult, testChangeSet.getInitialStateChangeSet() );
						first = false;
					} else {
						final ActionChangeSet actionChangeSet = testChangeSet.createActionChangeSet();
						if ( actionReplayResult.getStateDifference() != null ) {
							addAllElementDifferences( actionReplayResult, actionChangeSet );
						}
					}
				}
			}
		}
	}

	private void addAllElementDifferences( final ActionReplayResult actionReplayResult,
			final ActionChangeSet actionChangeSet ) {
		for ( final ElementDifference elementDifference : actionReplayResult.getElementDifferences() ) {
			if ( elementDifference.isInsertion() ) {
				actionChangeSet.addInsertChange(
						((InsertedDeletedElementDifference) elementDifference.getIdentifyingAttributesDifference())
								.getActual() );
			} else if ( elementDifference.isDeletion() ) {
				actionChangeSet.addDeletedChange(
						((InsertedDeletedElementDifference) elementDifference.getIdentifyingAttributesDifference())
								.getExpected().getIdentifyingAttributes() );
			} else {
				if ( elementDifference.hasIdentAttributesDifferences() ) {
					actionChangeSet.getIdentAttributeChanges().addAll( elementDifference.getIdentifyingAttributes(),
							((IdentifyingAttributesDifference) elementDifference.getIdentifyingAttributesDifference())
									.getAttributeDifferences() );
				}
				if ( elementDifference.hasAttributesDifferences() ) {
					actionChangeSet.getAttributesChanges().addAll( elementDifference.getIdentifyingAttributes(),
							elementDifference.getAttributesDifference().getDifferences() );
				}
			}
		}
	}
}


@@ -1584,23 +1584,376 @@
 %09%09%09%09
-if ( first ) %7B%0A
+final String description = actionReplayResult.getDescription();%0A%09%09%09%09%09final String stateFilePath = actionReplayResult.getStateFilePath();%0A%09%09%09%09%09if ( first ) %7B%0A%09%09%09%09%09%09if ( testChangeSet.getInitialStateChangeSet().isEmpty() ) %7B%0A%09%09%09%09%09%09%09addAllElementDifferences( actionReplayResult, testChangeSet.createInitialActionChangeSet( description, stateFilePath ) );%0A%09%09%09%09%09%09%7D else %7B%0A%09
 %09%09%09%09
@@ -2040,24 +2040,32 @@
 ngeSet() );%0A
+%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09first 

Improve action change set creation