/*
 * Copyright 2014 Lukas Krejci
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package org.revapi.maven;

import java.io.IOException;
import java.io.Reader;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.revapi.AnalysisContext;
import org.revapi.Difference;
import org.revapi.DifferenceSeverity;
import org.revapi.Element;
import org.revapi.Report;
import org.revapi.Reporter;

/**
 * @author Lukas Krejci
 * @since 0.1
 */
final class BuildTimeReporter implements Reporter {

    private final DifferenceSeverity breakingSeverity;
    private StringBuilder allProblems;

    public BuildTimeReporter(DifferenceSeverity breakingSeverity) {
        this.breakingSeverity = breakingSeverity;
        allProblems = new StringBuilder();
    }

    public boolean hasBreakingProblems() {
        return allProblems.length() > 0;
    }

    public String getAllProblemsMessage() {
        return "The following API problems caused the build to fail:" + allProblems.toString();
    }

    @Nullable
    @Override
    public String[] getConfigurationRootPaths() {
        return null;
    }

    @Nullable
    @Override
    public Reader getJSONSchema(@Nonnull String configurationRootPath) {
        return null;
    }

    @Override
    public void initialize(@Nonnull AnalysisContext properties) {
    }

    @Override
    public void report(@Nonnull Report report) {
        Element element = report.getNewElement();
        if (element == null) {
            element = report.getOldElement();
        }

        if (element == null) {
            //wat? At least one of old and new should always be non-null
            return;
        }

        for (Difference d : report.getDifferences()) {
            DifferenceSeverity maxSeverity = DifferenceSeverity.NON_BREAKING;
            for (DifferenceSeverity s : d.classification.values()) {
                if (maxSeverity.compareTo(s) < 0) {
                    maxSeverity = s;
                }
            }

            if (maxSeverity.compareTo(breakingSeverity) >= 0) {
            String archive = element.getArchive() == null ? "<unknown-archive>" : element.getArchive().getName();
            allProblems.append("\n[").append(archive).append("] ").append(element.getFullHumanReadableString()).append(": ").append(d.code)
                    .append(": ").append(d.description);
            }
        }
    }

    @Override
    public void close() throws IOException {
    }
}

@@ -667,16 +667,41 @@
 .Reader;
+%0Aimport java.util.Locale;
 %0A%0Aimport
@@ -1139,16 +1139,43 @@
 roblems;
+%0A    private Locale locale;
 %0A%0A    pu
@@ -1885,24 +1885,65 @@
 operties) %7B%0A
+        locale = properties.getLocale();%0A
     %7D%0A%0A    @
@@ -2673,16 +2673,20 @@
         
+    
 String a
@@ -2775,24 +2775,28 @@
 .getName();%0A
+    
             
@@ -2943,16 +2943,20 @@
         
+    
 .append(
@@ -2984,16 +2984,76 @@
 ption);%0A
+                appendIgnoreRecipe(allProblems, report, d);%0A
         
@@ -3139,11 +3139,805 @@
  %7B%0A    %7D
+%0A%0A    private void appendIgnoreRecipe(StringBuilder bld, Report report, Difference difference) %7B%0A        bld.append(%22%5CnTo ignore this problem, add the following JSON snippet to your Revapi configuration under %22 +%0A                %22%5C%22revapi.ignore%5C%22 path:%5Cn%22);%0A        bld.append(%22%7B%5Cn%22);%0A        bld.append(%22  %5C%22code%5C%22: %5C%22%22).append(difference.code).append(%22%5C%22,%5Cn%22);%0A        if (report.getOldElement() != null) %7B%0A            bld.append(%22  %5C%22old%5C%22: %5C%22%22).append(report.getOldElement()).append(%22%5C%22,%5Cn%22);%0A        %7D%0A        if (report.getNewElement() != null) %7B%0A            bld.append(%22  %5C%22new%5C%22: %5C%22%22).append(report.getNewElement()).append(%22%5C%22,%5Cn%22);%0A        %7D%0A        bld.append(%22  %5C%22justification%5C%22: %3C%3C%3C%3C%3C ADD YOUR EXPLANATION FOR THE NECESSITY OF THIS CHANGE %3E%3E%3E%3E%3E%5Cn%22);%0A        bld.append(%22%7D%22);%0A    %7D
 %0A%7D%0A

Add a JSON snippet to the maven plugin output for easy copy&paste ignoring of the changes.