/*
 * Copyright (C) by Courtanet, All Rights Reserved.
 */
package com.vackosar.gitflowincrementalbuild.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;

import org.apache.maven.model.Plugin;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PluginUtils {

    private static Logger logger = LoggerFactory.getLogger(PluginUtils.class);

    public static String extractPluginConfigValue(String parameter, Plugin plugin) {
        String value = extractConfigValue(parameter, plugin.getConfiguration());
        for (int i = 0; i < plugin.getExecutions().size() && value == null; i++) {
            value = extractConfigValue(parameter, plugin.getExecutions().get(i).getConfiguration());
        }
        return value;
    }

    private static String extractConfigValue(String parameter, Object configuration) {
        try {
            return ((Xpp3Dom) configuration).getChild(parameter).getValue();
        } catch (Exception ex) {
        }
        return null;
    }

    public static void writeChangedProjectsToFile(Collection<MavenProject> projects, File outputFile,
            StringJoiner joiner) {
        try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile)))) {
            writer.write(joinProjectIds(projects, joiner).toString());
        } catch (IOException e) {
            logger.warn("Error writing changed projects to file on path :" + outputFile.getPath(), e);
        }
    }

    public static void writeChangedProjectsToFile(Collection<MavenProject> projects, File outputFile) {
        writeChangedProjectsToFile(projects, outputFile, new StringJoiner("\n"));
    }

    public static StringJoiner joinProjectIds(Collection<MavenProject> projects, StringJoiner joiner) {
        for (MavenProject changedProject : projects) {
            joiner.add(changedProject.getGroupId() + ":" + changedProject.getArtifactId());
        }
        return joiner;
    }

    public static List<String> separatePattern(String patternString) {
        if (patternString.isEmpty()) {
            return Collections.emptyList();
        }
        patternString = StringUtils.deleteWhitespace(patternString);
        return Arrays.asList(patternString.split(","));
    }
}

@@ -413,16 +413,52 @@
 gJoiner;
+%0Aimport java.util.function.Function;
 %0A%0Aimport
@@ -791,16 +791,191 @@
 lass);%0A%0A
+    private static Function%3CMavenProject, String%3E projectIdWriter = project -%3E%0A            project.getGroupId() + %22:%22 + project.getArtifactId() + %22:%22 + project.getVersion();%0A%0A
     publ
@@ -1906,16 +1906,33 @@
 , joiner
+, projectIdWriter
 ).toStri
@@ -2440,24 +2440,24 @@
 projects) %7B%0A
-
             
@@ -2568,32 +2568,354 @@
  joiner;%0A    %7D%0A%0A
+    public static StringJoiner joinProjectIds(Collection%3CMavenProject%3E projects, StringJoiner joiner,%0A            Function%3CMavenProject, String%3E projectIdWriter) %7B%0A        for (MavenProject changedProject : projects) %7B%0A            joiner.add(projectIdWriter.apply(changedProject));%0A        %7D%0A        return joiner;%0A    %7D%0A%0A
     public stati

write project id w/ g:a:v