package de.redsix.junitextensions;

import java.io.IOException;
import java.lang.reflect.Parameter;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;

public class TempDirectoryExtension implements AfterEachCallback, ParameterResolver {

    private static final String KEY = "tempDirectory";

    @Override
    public boolean supportsParameter(ParameterContext paramContext, ExtensionContext extensionContext) throws ParameterResolutionException {
        final Parameter parameter = paramContext.getParameter();
        return parameter.getAnnotation(TempDirectory.class) != null && Path.class.equals(parameter.getType());
    }

    @Override
    public Object resolveParameter(ParameterContext paramContext, ExtensionContext extensionContext) throws ParameterResolutionException {
        final Parameter parameter = paramContext.getParameter();
        String parentPath = parameter.getAnnotation(TempDirectory.class).parentPath();
        return getLocalStore(extensionContext).getOrComputeIfAbsent(KEY, key -> createTempDirectory(parentPath, extensionContext));
    }

    @Override
    public void afterEach(ExtensionContext context) throws Exception {
        Path tempDirectory = (Path) getLocalStore(context).get(KEY);
        if (tempDirectory != null) {
//            delete(tempDirectory);
        }
    }

    private ExtensionContext.Store getLocalStore(ExtensionContext context) {
        return context.getStore(localNamespace(context));
    }

    private Namespace localNamespace(ExtensionContext context) {
        return Namespace.create(TempDirectoryExtension.class, context);
    }

    private Path createTempDirectory(final String parentPath, ExtensionContext context) {
        try {
            if (parentPath.length() > 0) {
                return Files.createTempDirectory(Paths.get(parentPath), getDirName(context));
            } else {
                return Files.createTempDirectory(getDirName(context));
            }
        } catch (IOException e) {
            throw new ParameterResolutionException("Could not create temp directory", e);
        }
    }

    private String getDirName(ExtensionContext context) {
        if (context.getTestMethod().isPresent()) {
            return context.getTestMethod().get().getName();
        } else if (context.getTestClass().isPresent()) {
            return context.getTestClass().get().getName();
        } else {
            return context.getDisplayName();
        }
    }

    private void delete(Path tempDirectory) throws IOException {
        Files.walkFileTree(tempDirectory, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                return deleteAndContinue(file);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return deleteAndContinue(dir);
            }

            private FileVisitResult deleteAndContinue(Path path) throws IOException {
                Files.delete(path);
                return FileVisitResult.CONTINUE;
            }
        });
    }
}
@@ -33,286 +33,8 @@
 s;%0A%0A
-import java.io.IOException;%0Aimport java.lang.reflect.Parameter;%0Aimport java.nio.file.FileVisitResult;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.nio.file.SimpleFileVisitor;%0Aimport java.nio.file.attribute.BasicFileAttributes;%0A%0A
 impo
@@ -68,33 +68,17 @@
 tension.
-AfterEachCallback
+*
 ;%0Aimport
@@ -130,115 +130,73 @@
 text
-;%0Aimport org.junit.jupiter.api.extension.ExtensionContext.Namespace;%0Aimport org.junit.jupiter.api.extension
+.Namespace;%0A%0Aimport java.io.IOException;%0Aimport java.lang.reflect
 .Par
@@ -201,23 +201,16 @@
 arameter
-Context
 ;%0Aimport
@@ -214,126 +214,75 @@
 ort 
-org.junit.jupiter.api.extension.ParameterResolutionException;%0Aimport org.junit.jupiter.api.extension.ParameterResolver
+java.nio.file.*;%0Aimport java.nio.file.attribute.BasicFileAttributes
 ;%0A%0Ap
@@ -1400,10 +1400,8 @@
 ) %7B%0A
-//
     

remove temp directory again