package org.i2g.client;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.i2g.client.argument.converter.FileConverter;
import org.i2g.client.argument.converter.OutputTypeConverter;
import org.i2g.client.argument.validator.OutputDirectoryValidator;
import org.i2g.client.argument.validator.OutputTypeValidator;
import org.i2g.model.I2GContainer;
import org.i2g.service.FileReader;
import org.i2g.service.MetadataReader;
import org.i2g.service.writers.FileWriter;
import org.i2g.service.writers.OutputType;
import org.i2g.service.writers.WriterContext;
import org.springframework.boot.CommandLineRunner;

import java.io.File;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Images2GpxCommandLineRunner implements CommandLineRunner {

    @Parameter(names = {"-i", "--inputDirectory"},
            converter = FileConverter.class,
            description = "Directory containing your images", required = true)
    private File inputDirectory;

    @Parameter(names = {"-o", "--outputDirectory"},
            validateWith = OutputDirectoryValidator.class,
            converter = FileConverter.class,
            description = "Output directory")
    private File outputDirectory = new File(System.getProperty("user.dir"));

    @Parameter(names = {"-t", "outputType"},
            validateWith = OutputTypeValidator.class,
            converter = OutputTypeConverter.class,
            description = "Default: gpx, possible values:\n" +
                    "\tgpx\ta gpx file\n" +
                    "google-maps-markers\n" +
                    "google-maps-polylines")
    private OutputType outputType = OutputType.GPX;

    @Parameter(names = {"-r", "--recursive"},
            description = "Read the inputDirectory recursively"
    )
    private boolean recursive = false;

    @Parameter(names = {"--apikey"},
            description = "Google Maps API Key. Only used for OutputType google-maps-polyline"
    )
    private String apiKey;

    @NonNull
    private FileReader fileReaderService;

    @NonNull
    private MetadataReader metadataReaderService;

    @NonNull
    private Map<OutputType, FileWriter> writerRegistry;


    public void run(String[] args) {
        Images2GpxCommandLineRunner jcContext = new Images2GpxCommandLineRunner();
        new JCommander(jcContext, args);

        WriterContext wc = createWriterContext(jcContext);

        System.out.println(String.format("Processing all files in directory \"%s\"", wc.getInputDirectory()));
        System.out.println(String.format("Writing to %s", wc.getOutputDirectory()));

        List<File> allImageFiles = fileReaderService.readFiles(wc.getInputDirectory(), wc.getRecursive());
        List<I2GContainer> containers = metadataReaderService.getI2GContainers(allImageFiles);
        System.out.println(containers);

        // write coordinates to file
        FileWriter writer = writerRegistry.get(jcContext.outputType);
        writer.write(containers, wc);
        System.exit(0);
    }

    private WriterContext createWriterContext(Images2GpxCommandLineRunner args) {
        if (args.outputType == OutputType.GOOGLE_MAPS_POLYLINES && StringUtils.isEmpty(args.apiKey)) {
            System.out.println("for google-maps-polyline the attribute 'apikey' is required!");
            System.out.println("You can create a new API Key here: https://console.developers.google.com/apis/credentials");
        }

        return WriterContext.builder()
                .inputDirectory(args.inputDirectory)
                .outputDirectory(args.outputDirectory)
                .recursive(args.recursive)
                .apiKey(args.apiKey)
                .build();
    }
}

@@ -1417,17 +1417,17 @@
 %22Output 
-d
+D
 irectory
@@ -1688,155 +1688,51 @@
  = %22
-Default: gpx, possible values:%5Cn%22 +%0A                    %22%5Ctgpx%5Cta gpx file%5Cn%22 +%0A                    %22google-maps-markers%5Cn%22 +%0A                    %22
+supported types %5Bgpx, google-maps-markers, 
 goog
@@ -1748,16 +1748,17 @@
 olylines
+%5D
 %22)%0A    p
@@ -1914,21 +1914,16 @@
 rsively%22
-%0A    
 )%0A    pr
@@ -1980,16 +1980,22 @@
 ames = %7B
+%22-k%22, 
 %22--apike
@@ -2124,24 +2124,110 @@
 ng apiKey;%0A%0A
+    @Parameter(names = %7B%22?%22, %22-h%22, %22--help%22%7D, help = true)%0A    boolean help = false;%0A%0A
     @NonNull
@@ -2532,16 +2532,40 @@
 %0A       
+ JCommander jCommander =
  new JCo
@@ -2589,16 +2589,116 @@
 , args);
+%0A        if (jcContext.help) %7B%0A            jCommander.usage();%0A            System.exit(0);%0A        %7D
 %0A%0A      

print help