package fr.insee.pogues.transforms;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

@Service
public class DDIToLunaticXMLImpl implements DDIToLunaticXML {

	final static Logger logger = LogManager.getLogger(DDIToLunaticXMLImpl.class);
	
    @Override
    public void transform(InputStream input, OutputStream output, Map<String, Object> params, String surveyName) throws Exception {
    	logger.debug("Eno transformation");
    	if (null == input) {
            throw new NullPointerException("Null input");
        }
        if (null == output) {
            throw new NullPointerException("Null output");
        }
        String odt = transform(input, params,surveyName);
        logger.debug("Eno transformation finished");
        output.write(odt.getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public String transform(InputStream input, Map<String, Object> params, String surveyName) throws Exception {
        if (null == input) {
            throw new NullPointerException("Null input");
        }
        File enoInput;
        enoInput = File.createTempFile("eno", ".xml");
        FileUtils.copyInputStreamToFile(input, enoInput);
        return transform(enoInput, params,surveyName);
    }

    @Override
    public String transform(String input, Map<String, Object> params, String surveyName) throws Exception {
        File enoInput;
        if (null == input) {
            throw new NullPointerException("Null input");
        }
        enoInput = File.createTempFile("eno", ".xml");
        FileUtils.writeStringToFile(enoInput, input, StandardCharsets.UTF_8);
        return transform(enoInput, params, surveyName);
    }

    private String transform(File file, Map<String, Object> params, String surveyName) throws Exception {
        try {
            File output=null;
//            GenerationService genService = new GenerationService(new DDIPreprocessor(), new DDI2JSGenerator(), new Postprocessor[] {new NoopPostprocessor()});
//            output = genService.generateQuestionnaire(file, surveyName);
//            
            return FileUtils.readFileToString(output, StandardCharsets.UTF_8);
        } catch (Exception e) {
            throw new Exception(String.format("%s:%s", getClass().getName(), e.getMessage()));
        }
    }
}

@@ -345,16 +345,277 @@
 rvice;%0A%0A
+import fr.insee.eno.GenerationService;%0Aimport fr.insee.eno.generation.DDI2JSGenerator;%0Aimport fr.insee.eno.postprocessing.JSAddVariableReferencePostprocessor;%0Aimport fr.insee.eno.postprocessing.Postprocessor;%0Aimport fr.insee.eno.preprocessing.DDIPreprocessor;%0A%0A
 @Service
@@ -2327,18 +2327,16 @@
 t=null;%0A
-//
         
@@ -2467,12 +2467,30 @@
 new 
-Noop
+JSAddVariableReference
 Post
@@ -2504,18 +2504,16 @@
 or()%7D);%0A
-//
         
@@ -2581,10 +2581,8 @@
 e);%0A
-//
     

Add ddiToLunaticXmlImpl