package de.retest.recheck.ignore;

import static de.retest.recheck.configuration.ProjectConfiguration.FILTER_FOLDER;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import de.retest.recheck.configuration.ProjectConfiguration;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SearchFilterFiles {

	public static final String FILTER_EXTENSION = ".filter";
	private static final String BASIC_FILTER_DIR = "/filter/";
	private static final String WEB_FILTER_DIR = BASIC_FILTER_DIR + "web/";
	private static final List<String> defaultWebFilter =
			Arrays.asList( WEB_FILTER_DIR + "positioning.filter", WEB_FILTER_DIR + "visibility.filter" );

	private SearchFilterFiles() {}

	public static List<Path> getAllFilterFiles() {
		return Stream.concat( getDefaultFilterFiles().stream(), getProjectFilterFiles().stream() ) //
				.collect( Collectors.toList() );
	}

	public static List<Path> getDefaultFilterFiles() {
		return defaultWebFilter.stream() //
				.map( filter -> SearchFilterFiles.class.getResource( filter ) ) //
				.filter( Objects::nonNull ) //
				.map( resource -> Paths.get( URI.create( resource.toString() ) ) ) //
				.collect( Collectors.toList() ); //
	}

	public static List<Path> getProjectFilterFiles() {
		final Path resolveFilterPath =
				ProjectConfiguration.getInstance().findProjectConfigFolder().resolve( FILTER_FOLDER );
		if ( !resolveFilterPath.toFile().exists() ) {
			return Collections.emptyList();
		}
		try ( Stream<Path> paths = Files.walk( resolveFilterPath ) ) {
			return paths.filter( Files::isRegularFile ) //
					.filter( file -> file.toString().endsWith( FILTER_EXTENSION ) ) //
					.collect( Collectors.toList() ); //
		} catch ( final IOException e ) {
			log.error( "Exception accessing user filter folder '{}'.", resolveFilterPath, e );
			return Collections.emptyList();
		}
	}
}

@@ -135,24 +135,61 @@
 OException;%0A
+import java.io.UncheckedIOException;%0A
 import java.
@@ -510,24 +510,75 @@
 figuration;%0A
+import de.retest.recheck.review.ignore.io.Loaders;%0A
 import lombo
@@ -2170,11 +2170,478 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%0A%09private static Filter toFilter( final Path filter ) %7B%0A%09%09try ( final Stream%3CString%3E filterFileLines = Files.lines( filter ) ) %7B%0A%09%09%09return Loaders.load( filterFileLines ) //%0A%09%09%09%09%09.filter( Filter.class::isInstance ) //%0A%09%09%09%09%09.map( Filter.class::cast )//%0A%09%09%09%09%09.collect( Collectors.collectingAndThen( Collectors.toList(), CompoundFilter::new ) );%0A%09%09%7D catch ( final IOException e ) %7B%0A%09%09%09throw new UncheckedIOException( %22Could not load '%22 + filter + %22' file.%22, e );%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Create filters from the given path