package ru.lanwen.raml.rarc.util;

import com.jayway.restassured.path.xml.XmlPath;
import org.apache.maven.shared.invoker.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Arrays;

import static org.apache.commons.lang3.StringUtils.capitalize;
import static ru.lanwen.raml.rarc.api.ApiResourceClass.sanitize;

/**
 * Created by stassiak
 */
public class XmlCodegen {
    private final Logger LOG = LoggerFactory.getLogger(XmlCodegen.class);
    private static final String POM_PATH = "jaxb2/pom.xml";
    private final ResponseCodegenConfig config;

    public XmlCodegen(ResponseCodegenConfig config) {
        this.config = config;
    }

    public String generate() throws MavenInvocationException, IOException {
        InvocationRequest request = new DefaultInvocationRequest();
        File pomFile = getPomFile();

        request.setPomFile(pomFile);
        request.setProperties(config.asJaxb2Properties());
        request.setGoals(Arrays.asList("clean", "generate-sources"));

        InvocationResult result = new DefaultInvoker().execute(request);

        if(result.getExitCode() != 0) {
            LOG.info("Xmlgen failed: {}", result.getExecutionException());
        }

        pomFile.delete();

        return capitalize(sanitize(XmlPath.from(new File(config.getInputPath()))
                .getNode("schema").getNode("element").getAttribute("name"))) + "Type";
    }

    File getPomFile() throws IOException {
        File file = File.createTempFile("pom", ".xml");
        try {
            InputStream input = getClass().getClassLoader().getResourceAsStream(POM_PATH);
            OutputStream out = new FileOutputStream(file);
            int read;
            byte[] bytes = new byte[1024];

            while ((read = input.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            file.deleteOnExit();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return file;
    }
}

@@ -191,17 +191,144 @@
 java.io.
-*
+File;%0Aimport java.io.IOException;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.StandardCopyOption
 ;%0Aimport
@@ -951,24 +951,25 @@
 uest();%0A
+%0A
         
 File pom
@@ -964,34 +964,198 @@
     
-File pomFile = getPomFile(
+Path pomPath = Files.createTempFile(%22pom%22, %22.xml%22);%0A        Files.copy(getClass().getClassLoader().getResourceAsStream(POM_PATH),%0A                pomPath, StandardCopyOption.REPLACE_EXISTING
 );%0A%0A
@@ -1184,20 +1184,29 @@
 File(pom
+Path.to
 File
+()
 );%0A     
@@ -1541,20 +1541,29 @@
      pom
+Path.to
 File
+()
 .delete(
@@ -1743,588 +1743,7 @@
    %7D
-%0A%0A    File getPomFile() throws IOException %7B%0A        File file = File.createTempFile(%22pom%22, %22.xml%22);%0A        try %7B%0A            InputStream input = getClass().getClassLoader().getResourceAsStream(POM_PATH);%0A            OutputStream out = new FileOutputStream(file);%0A            int read;%0A            byte%5B%5D bytes = new byte%5B1024%5D;%0A%0A            while ((read = input.read(bytes)) != -1) %7B%0A                out.write(bytes, 0, read);%0A            %7D%0A            file.deleteOnExit();%0A        %7D catch (IOException ex) %7B%0A            ex.printStackTrace();%0A        %7D%0A        return file;%0A    %7D
 %0A%7D%0A

use for coping is to file