package ahlers.phantom.embedded;

import com.google.common.collect.ImmutableList;
import de.flapdoodle.embed.process.distribution.Distribution;
import de.flapdoodle.embed.process.distribution.IVersion;
import de.flapdoodle.embed.process.extract.IExtractedFileSet;

import java.io.IOException;
import java.util.List;

import static java.lang.String.format;

/**
 * @author [[mailto:michael@ahlers.consulting Michael Ahlers]]
 */
public enum PhantomCommand
        implements IPhantomCommand {

    Any {
        @Override
        public boolean matches(final IVersion version) {
            return true;
        }

        @Override
        public List<String> emit(final IPhantomConfig config, final IExtractedFileSet files) throws IOException {
            return ImmutableList
                    .<String>builder()
                    .add(files.executable().getAbsolutePath())
                    .add(format("--debug=%s", config.debug()))
                    .build();
        }
    },

    V21 {
        @Override
        public boolean matches(final IVersion version) {
            return PhantomVersion.V211 == version;
        }

        @Override
        public List<String> emit(IPhantomConfig config, IExtractedFileSet exe) throws IOException {
            return ImmutableList
                    .<String>builder()
                    .addAll(Any.emit(config, exe))
                    .build();
        }
    };

    /**
     * {@link PhantomCommand#Any} is guaranteed to match.
     */
    public static IPhantomCommand valueFor(final IVersion version) {
        for (final IPhantomCommand emitter : values()) {
            if (emitter.matches(version)) {
                return emitter;
            }
        }

        return Any;
    }

    public static IPhantomCommand valueFor(final Distribution distribution) {
        return valueFor(distribution.getVersion());
    }

}

@@ -282,24 +282,50 @@
 OException;%0A
+import java.util.EnumSet;%0A
 import java.
@@ -1209,16 +1209,22 @@
 g%3E emit(
+final 
 IPhantom
@@ -1237,16 +1237,22 @@
  config,
+ final
  IExtrac
@@ -1262,19 +1262,21 @@
 FileSet 
-exe
+files
 ) throws
@@ -1407,19 +1407,21 @@
 config, 
-exe
+files
 ))%0A     
@@ -1655,15 +1655,44 @@
 r : 
-values(
+EnumSet.complementOf(EnumSet.of(Any)
 )) %7B

Tweak selection logic