package de.retest.recheck.review.ignore.matcher;

import static java.util.stream.Collectors.toCollection;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import de.retest.recheck.review.ignore.io.RegexLoader;
import de.retest.recheck.ui.descriptors.Element;

public class ElementClassMatcher implements Matcher<Element> {

	public static final String CLASS_KEY = "class";

	private final Set<String> classValues;

	public ElementClassMatcher( final Element element ) {
		this( toClassValuesSet( toClassValueString( element ) ) );
	}

	private ElementClassMatcher( final Set<String> classValues ) {
		this.classValues = classValues;
	}

	@Override
	public boolean test( final Element element ) {
		final Set<String> classValues = toClassValuesSet( toClassValueString( element ) );
		if ( classValues.isEmpty() ) {
			return false;
		}
		return this.classValues.containsAll( classValues );
	}

	@Override
	public String toString() {
		final String classValueString = classValues.stream().collect( Collectors.joining( " " ) );
		return String.format( ElementClassMatcherLoader.FORMAT, classValueString );
	}

	public static final class ElementClassMatcherLoader extends RegexLoader<ElementClassMatcher> {

		private static final String CLASS = CLASS_KEY + "=";

		private static final String FORMAT = CLASS + "%s";
		private static final Pattern REGEX = Pattern.compile( CLASS + "(.+)" );

		public ElementClassMatcherLoader() {
			super( REGEX );
		}

		@Override
		protected ElementClassMatcher load( final MatchResult matcher ) {
			final String classValue = matcher.group( 1 );
			return new ElementClassMatcher( toClassValuesSet( classValue ) );
		}
	}

	private static String toClassValueString( final Element element ) {
		return element.getIdentifyingAttributes().get( CLASS_KEY );
	}

	private static Set<String> toClassValuesSet( final String classValue ) {
		if ( classValue == null ) {
			return Collections.emptySet();
		}
		return Stream.of( classValue.split( " " ) ).collect( toCollection( HashSet::new ) );
	}
}

@@ -50,23 +50,16 @@
 %0Aimport 
-static 
 java.uti
@@ -64,40 +64,15 @@
 til.
-stream.Collectors.toCollection;%0A
+Arrays;
 %0Aimp
@@ -119,36 +119,11 @@
 til.
-HashSet;%0Aimport java.util.Se
+Lis
 t;%0Ai
@@ -228,40 +228,8 @@
 ors;
-%0Aimport java.util.stream.Stream;
 %0A%0Aim
@@ -456,26 +456,27 @@
 ivate final 
-Se
+Lis
 t%3CString%3E cl
@@ -555,34 +555,35 @@
 s( toClassValues
-Se
+Lis
 t( toClassValueS
@@ -640,26 +640,27 @@
 cher( final 
-Se
+Lis
 t%3CString%3E cl
@@ -778,18 +778,19 @@
 %09%09final 
-Se
+Lis
 t%3CString
@@ -818,18 +818,19 @@
 ssValues
-Se
+Lis
 t( toCla
@@ -1704,18 +1704,19 @@
 ssValues
-Se
+Lis
 t( class
@@ -1885,18 +1885,19 @@
  static 
-Se
+Lis
 t%3CString
@@ -1911,18 +1911,19 @@
 ssValues
-Se
+Lis
 t( final
@@ -2002,18 +2002,19 @@
 ns.empty
-Se
+Lis
 t();%0A%09%09%7D
@@ -2027,17 +2027,21 @@
 urn 
-Stream.of
+Arrays.asList
 ( cl
@@ -2065,48 +2065,8 @@
  %22 )
- ).collect( toCollection( HashSet::new )
  );%0A

Use list to guarantee order