/*
 * MIT License
 *
 * Copyright 2019 Crown Copyright
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.kscs.util.test;

import com.sun.tools.xjc.Driver;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

public class GenerateClasses {


    private static final String TEST_MODULE_NAME = "test-module";

    /**
     * Useful for debugging the plugin
     */
    public static void main(String[] args) throws Exception {

        Path rootDir = Paths.get(".").normalize().toAbsolutePath();
        if (!rootDir.endsWith(TEST_MODULE_NAME)) {
            rootDir = Paths.get("." + File.separator + TEST_MODULE_NAME).normalize().toAbsolutePath();
        }
        Path resourcesDir = rootDir.resolve("src").resolve("main").resolve("resources");
        Path schemaFile = resourcesDir.resolve("jaxb2-plugin-test.xsd");
        Path bindingFile = resourcesDir.resolve("binding-config.xjb");
        Path outputDir = rootDir.resolve("target").resolve("GenerateClassesOutput");

        System.out.println("rootDir: " + rootDir.toAbsolutePath().toString());
        System.out.println("schemaFile: " + schemaFile.toAbsolutePath().toString());
        System.out.println("bindingFile: " + bindingFile.toAbsolutePath().toString());
        System.out.println("outputDir: " + outputDir.toAbsolutePath().toString());

        Files.createDirectories(outputDir);

        final String[] xjcOptions = new String[]{
                "-xmlschema",
                "-nv",
                "-extension",
                "-verbose",
//                "-p", PACKAGE_NAME,
                "-d", outputDir.toAbsolutePath().toString(),
                "-b", bindingFile.toAbsolutePath().toString(),
//                "-quiet",
                schemaFile.toAbsolutePath().toString(), //the source schema to gen classes from
                "-Ximmutable",
                "-Xfluent-builder",
                "-generateJavadocFromAnnotations=true",
                "-Xclone",
                // TODO Not sure how to handle the episodes file that group-contract needs
//                "-Xgroup-contract",
        };

        System.out.println("Running XJC with arguments:");
        Arrays.stream(xjcOptions)
                .map(str -> "  " + str)
                .forEach(System.out::println);

        final int exitStatus = Driver.run(xjcOptions, System.out, System.out);

        if (exitStatus != 0) {
            System.out.print("Executing xjc failed");
            System.exit(1);
        }
    }
}

@@ -1211,16 +1211,44 @@
 o.File;%0A
+import java.io.IOException;%0A
 import j
@@ -1267,16 +1267,16 @@
 .Files;%0A
-
 import j
@@ -1346,16 +1346,45 @@
 .Arrays;
+%0Aimport java.util.Comparator;
 %0A%0Apublic
@@ -1831,24 +1831,30 @@
   %7D%0A        
+final 
 Path resourc
@@ -1926,24 +1926,30 @@
 %22);%0A        
+final 
 Path schemaF
@@ -2005,24 +2005,30 @@
 %22);%0A        
+final 
 Path binding
@@ -2082,24 +2082,85 @@
 %22);%0A        
+// Use the same dir that the maven plugin uses%0A        final 
 Path outputD
@@ -2163,32 +2163,49 @@
 putDir = rootDir
+%0A                
 .resolve(%22target
@@ -2206,16 +2206,33 @@
 target%22)
+%0A                
 .resolve
@@ -2237,29 +2237,57 @@
 ve(%22
-G
+g
 enerate
-ClassesOutput
+d-sources%22)%0A                .resolve(%22xjc
 %22);%0A
@@ -2618,24 +2618,99 @@
 String());%0A%0A
+        clearDirectory(outputDir);%0A%0A        // Ensure the full path exists%0A
         File
@@ -3657,24 +3657,71 @@
 :println);%0A%0A
+        // Run the xjc code generation process%0A
         fina
@@ -3869,24 +3869,24 @@
 c failed%22);%0A
-
             
@@ -3916,11 +3916,606 @@
  %7D%0A    %7D
+%0A%0A    private static void clearDirectory(final Path rootDir) %7B%0A        try %7B%0A            if (Files.isDirectory(rootDir)) %7B%0A                Files.walk(rootDir)%0A                        .sorted(Comparator.reverseOrder())%0A                        .peek(path -%3E System.out.println(%22Deleting %22 + path.toAbsolutePath().normalize().toString()))%0A                        .map(Path::toFile)%0A                        .forEach(File::delete);%0A            %7D%0A        %7D catch (IOException e) %7B%0A            throw new RuntimeException(%22Error deleting path %22 + rootDir.toAbsolutePath().toString(), e);%0A        %7D%0A    %7D
 %0A%7D%0A

Change GenerateClasses to output to different dir and to empty dir