package de.retest.recheck.printer;

import java.io.Serializable;

import de.retest.recheck.ui.DefaultValueFinder;
import de.retest.recheck.ui.descriptors.IdentifyingAttributes;
import de.retest.recheck.ui.diff.AttributeDifference;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class AttributeDifferencePrinter implements Printer<AttributeDifference> {

	private static final String IS_DEFAULT = "(default or absent)";

	private static final String KEY_EXPECTED_ACTUAL_FORMAT = "%s: expected=\"%s\", actual=\"%s\"";

	private final IdentifyingAttributes attributes;
	private final DefaultValueFinder defaultProvider;

	@Override
	public String toString( final AttributeDifference difference, final String indent ) {
		return indent + format( difference );
	}

	private String format( final AttributeDifference difference ) {
		if ( isExpectedDefault( difference ) ) { // The attribute changed from default to a non-default
			return printExpectedDefaultDifference( difference );
		}
		if ( isActualDefault( difference ) ) { // The attribute changed back to default
			return printActualDefaultDifference( difference );
		}
		return printBothDifferences( difference );
	}

	private boolean isActualDefault( final AttributeDifference difference ) {
		return defaultProvider.isDefaultValue( attributes, difference.getKey(), difference.getActual() );
	}

	private String printActualDefaultDifference( final AttributeDifference difference ) {
		final String key = difference.getKey();
		final Serializable expected = difference.getExpected();
		return String.format( KEY_EXPECTED_ACTUAL_FORMAT, key, expected, IS_DEFAULT );
	}

	private boolean isExpectedDefault( final AttributeDifference difference ) {
		return difference.getExpected() == null; // We do not save defaults, thus this is null
	}

	private String printExpectedDefaultDifference( final AttributeDifference difference ) {
		final String key = difference.getKey();
		final Serializable actual = difference.getActual();
		return String.format( KEY_EXPECTED_ACTUAL_FORMAT, key, IS_DEFAULT, actual );
	}

	private String printBothDifferences( final AttributeDifference difference ) {
		final String key = difference.getKey();
		final Serializable expected = difference.getExpected();
		final Serializable actual = difference.getActual();
		return String.format( KEY_EXPECTED_ACTUAL_FORMAT, key, expected, actual );
	}
}

@@ -57,16 +57,75 @@
 lizable;
+%0Aimport java.util.List;%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -283,16 +283,79 @@
 erence;%0A
+import de.retest.recheck.ui.diff.ElementIdentificationWarning;%0A
 import l
@@ -653,24 +653,104 @@
 l=%5C%22%25s%5C%22%22;%0A%0A
+%09private static final String WARNING_FILENAME_LINE_FORMAT = %22, breaks=%5C%22%25s%5C%22%22;%0A%0A
 %09private fin
@@ -849,16 +849,16 @@
 verride%0A
-
 %09public 
@@ -932,24 +932,154 @@
  indent ) %7B%0A
+%09%09if ( difference.hasElementIdentificationWarning() ) %7B%0A%09%09%09return indent + format( difference ) + printWarning( difference );%0A%09%09%7D%0A
 %09%09return ind
@@ -2723,11 +2723,497 @@
 al );%0A%09%7D
+%0A%0A%09private String printWarning( final AttributeDifference difference ) %7B%0A%09%09final List%3CElementIdentificationWarning%3E warnings = difference.getElementIdentificationWarnings();%0A%09%09return String.format( WARNING_FILENAME_LINE_FORMAT, warnings.stream() //%0A%09%09%09%09.map( this::formatWarning ) //%0A%09%09%09%09.collect( Collectors.joining( %22, %22 ) ) );%0A%09%7D%0A%0A%09private String formatWarning( final ElementIdentificationWarning warning ) %7B%0A%09%09return warning.getTestFileName() + %22:%22 + warning.getTestLineNumber();%0A%09%7D
 %0A%7D%0A

Add the warning printer to AttributeDifferencePrinter