package com.github.nodevops.confd.maven.plugin.processors.impl;

import com.github.nodevops.confd.maven.plugin.processors.Processor;
import com.github.nodevops.confd.maven.plugin.processors.ProcessorContext;
import com.github.nodevops.confd.maven.plugin.processors.ProcessorExecutionException;
import com.github.nodevops.confd.maven.plugin.templating.Parser;
import com.github.nodevops.confd.maven.plugin.utils.DictionaryException;
import com.github.nodevops.confd.maven.plugin.utils.DictionaryUtil;
import com.moandjiezana.toml.Toml;
import org.codehaus.plexus.util.FileUtils;

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

/**
 * Created by pseillier on 22/12/2015.
 */
public class JavaProcessorImpl implements Processor {

    @Override
    public void process(ProcessorContext context) throws ProcessorExecutionException {
        // the toml files are in the conf.d sub directory
        File tomlDirectory = new File(context.getWorkingDirectory(), "conf.d");
        File templatesDirectory = new File(context.getWorkingDirectory(), "templates");
        try {
            // load dictionary
            Map<String, String> env = DictionaryUtil.readDictionaryAsProperties(context.getDictionaryPath(), context.getEncoding());
            // browse the .toml files
            List<File> tomlFiles = FileUtils.getFiles(tomlDirectory, "*.toml", null);
            for (File tomlFile : tomlFiles) {
                // process each toml file
                try {
                    processToml(tomlFile, templatesDirectory, env, context.getEncoding());
                } catch (IOException e) {
                    throw new ProcessorExecutionException("unable to process toml file  " + tomlFile, e);
                }
            }
        } catch (DictionaryException e) {
            throw new ProcessorExecutionException("unable to load dictionary " + context.getDictionaryPath(), e);
        } catch (IllegalStateException e) {
            throw new ProcessorExecutionException("unable to process toml files from directory " + tomlDirectory, e);
        } catch (IOException e) {
            throw new ProcessorExecutionException("unable to process toml files from directory " + tomlDirectory, e);
        }
    }

    private void processToml(File tomlFile, File templatesDirectory, Map<String, String> env, String encoding) throws IOException {
        Toml toml = new Toml().read(tomlFile);
        String src = toml.getString("template.src");
        String dest = toml.getString("template.dest");
        List<String> keys = toml.getList("template.keys");

        File templateFile = new File(templatesDirectory, src);
        File destFile = new File(dest);


        Parser parser = new Parser(templateFile, encoding);

        String parsedTemplate = parser.parse(env);
        FileUtils.fileWrite(destFile, encoding, parsedTemplate);
    }
}

@@ -617,24 +617,50 @@
 OException;%0A
+import java.util.HashMap;%0A
 import java.
@@ -2635,24 +2635,612 @@
 te.keys%22);%0A%0A
+        if(keys==null %7C%7C keys.size()==0) %7B%0A            throw new IOException(%22toml file %22 + tomlFile + %22 keys section must exist and must contain at least one key%22);%0A        %7D%0A%0A        // filter the env map according to the keys defined in the toml%0A        HashMap%3CString,String%3E filteredEnv=new HashMap%3CString, String%3E();%0A        for(Map.Entry%3CString,String%3E entry : env.entrySet()) %7B%0A            for(String key : keys) %7B%0A                if(entry.getKey().startsWith(key)) %7B%0A                    filteredEnv.put(entry.getKey(),entry.getValue());%0A                %7D%0A            %7D%0A        %7D%0A
         File
@@ -3290,16 +3290,16 @@
 , src);%0A
-
         
@@ -3438,17 +3438,25 @@
 r.parse(
-e
+filteredE
 nv);%0A   

Fix : filter the dictionary entries according to the keys defined in the toml file