package de.retest.recheck.ui.descriptors;

import java.util.ArrayList;
import java.util.List;

import de.retest.recheck.ui.components.Component;
import de.retest.recheck.ui.components.ComponentContainer;
import de.retest.recheck.ui.components.RootContainer;
import de.retest.recheck.ui.image.Screenshot;
import de.retest.recheck.util.RetestIdProviderUtil;

public class ElementUtil {

	public static List<Element> flattenAllElements( final List<Element> elements ) {
		final List<Element> flattened = new ArrayList<>();

		for ( final Element element : elements ) {
			flattened.add( element );
			flattened.addAll( flattenChildElements( element ) );
		}

		return flattened;
	}

	public static Element toElement( final Component<?> component ) {
		final IdentifyingAttributes identifyingAttributes = component.retrieveIdentifyingAttributes();
		final Attributes attributes = component.retrieveAttributes();
		final Screenshot screenshot = component.createScreenshot();
		final List<Element> containedComponents = new ArrayList<>();
		if ( component instanceof ComponentContainer<?> ) {
			for ( final Component<?> containedComponent : ((ComponentContainer<?>) component).getChildComponents() ) {
				containedComponents.add( containedComponent.getElement() );
			}
		}
		final String retestId = RetestIdProviderUtil.getRetestId( identifyingAttributes );
		final Element element =
				Element.create( retestId, new Element(), identifyingAttributes, attributes, screenshot );
		element.addChildren( containedComponents );
		return element;
	}

	public static RootElement toRootElement( final RootContainer<?> window ) {
		final IdentifyingAttributes identifyingAttributes = window.retrieveIdentifyingAttributes();
		final Attributes attributes = window.retrieveAttributes();
		final Screenshot screenshot = window.createScreenshot();
		final List<Element> containedComponents = new ArrayList<>();
		for ( final Component<?> containedComponent : ((ComponentContainer<?>) window).getChildComponents() ) {
			containedComponents.add( containedComponent.getElement() );
		}
		final String screen = window.getScreenId();
		final int screenId = screen.hashCode();
		final String title = window.getText();
		final String retestId = RetestIdProviderUtil.getRetestId( identifyingAttributes );
		final RootElement rootElement =
				new RootElement( retestId, identifyingAttributes, attributes, screenshot, screen, screenId, title );
		rootElement.addChildren( containedComponents );
		return rootElement;
	}

	public static List<Element> flattenChildElements( final Element element ) {
		final List<Element> flattened = new ArrayList<>();

		for ( final Element childElement : element.getContainedElements() ) {
			flattened.add( childElement );
			flattened.addAll( flattenChildElements( childElement ) );
		}

		return flattened;
	}

	public static boolean pathEquals( final Element element0, final Element element1 ) {
		return element0.getIdentifyingAttributes().getPathTyped()
				.equals( element1.getIdentifyingAttributes().getPathTyped() );
	}
}

@@ -92,270 +92,8 @@
 t;%0A%0A
-import de.retest.recheck.ui.components.Component;%0Aimport de.retest.recheck.ui.components.ComponentContainer;%0Aimport de.retest.recheck.ui.components.RootContainer;%0Aimport de.retest.recheck.ui.image.Screenshot;%0Aimport de.retest.recheck.util.RetestIdProviderUtil;%0A%0A
 publ
@@ -415,1826 +415,8 @@
 %09%7D%0A%0A
-%09public static Element toElement( final Component%3C?%3E component ) %7B%0A%09%09final IdentifyingAttributes identifyingAttributes = component.retrieveIdentifyingAttributes();%0A%09%09final Attributes attributes = component.retrieveAttributes();%0A%09%09final Screenshot screenshot = component.createScreenshot();%0A%09%09final List%3CElement%3E containedComponents = new ArrayList%3C%3E();%0A%09%09if ( component instanceof ComponentContainer%3C?%3E ) %7B%0A%09%09%09for ( final Component%3C?%3E containedComponent : ((ComponentContainer%3C?%3E) component).getChildComponents() ) %7B%0A%09%09%09%09containedComponents.add( containedComponent.getElement() );%0A%09%09%09%7D%0A%09%09%7D%0A%09%09final String retestId = RetestIdProviderUtil.getRetestId( identifyingAttributes );%0A%09%09final Element element =%0A%09%09%09%09Element.create( retestId, new Element(), identifyingAttributes, attributes, screenshot );%0A%09%09element.addChildren( containedComponents );%0A%09%09return element;%0A%09%7D%0A%0A%09public static RootElement toRootElement( final RootContainer%3C?%3E window ) %7B%0A%09%09final IdentifyingAttributes identifyingAttributes = window.retrieveIdentifyingAttributes();%0A%09%09final Attributes attributes = window.retrieveAttributes();%0A%09%09final Screenshot screenshot = window.createScreenshot();%0A%09%09final List%3CElement%3E containedComponents = new ArrayList%3C%3E();%0A%09%09for ( final Component%3C?%3E containedComponent : ((ComponentContainer%3C?%3E) window).getChildComponents() ) %7B%0A%09%09%09containedComponents.add( containedComponent.getElement() );%0A%09%09%7D%0A%09%09final String screen = window.getScreenId();%0A%09%09final int screenId = screen.hashCode();%0A%09%09final String title = window.getText();%0A%09%09final String retestId = RetestIdProviderUtil.getRetestId( identifyingAttributes );%0A%09%09final RootElement rootElement =%0A%09%09%09%09new RootElement( retestId, identifyingAttributes, attributes, screenshot, screen, screenId, title );%0A%09%09rootElement.addChildren( containedComponents );%0A%09%09return rootElement;%0A%09%7D%0A%0A
 %09pub

Remove obsolete util methods