package hudson.plugins.warnings.tokens;

import hudson.plugins.analysis.core.BuildResult;
import hudson.plugins.analysis.core.ResultAction;
import hudson.plugins.analysis.tokens.AbstractTokenMacro;
import hudson.plugins.analysis.util.model.FileAnnotation;
import hudson.plugins.analysis.util.model.Priority;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang.StringUtils;

/**
 * Provides a token that evaluates detailed informations to the plug-in
 * build result.
 *
 * @author Benedikt Spranger
 */
public abstract class AbstractDetailedTokenMacro extends AbstractTokenMacro {

    public AbstractDetailedTokenMacro(String tokenName, Class<? extends ResultAction<? extends BuildResult>>... resultActions) {
        super(tokenName, resultActions);
    }

    private final List<String> modules = new ArrayList<String>();
    private boolean verbose;
    private boolean showLow = true;
    private boolean showNormal = true;
    private boolean showHigh = true;
    private String linechar = "-";
    private int indent;

    @Parameter(alias = "indent")
    public void setIndent(int indentation) {
        indent = indentation;
    }

    @Parameter(alias = "modules")
    public void setModules(String module) {
        modules.add(module);
    }

    @Parameter(alias = "verbose")
    public void setVerbosity(boolean verbosity) {
        verbose = verbosity;
    }

    @Parameter(alias = "low")
    public void setLow(boolean show) {
        showLow = show;
    }

    @Parameter(alias = "normal")
    public void setNormal(boolean show) {
        showNormal = show;
    }

    @Parameter(alias = "high")
    public void setHigh(boolean show) {
        showHigh = show;
    }

    @Parameter(alias = "linechar")
    public void setLineChar(String c) {
        linechar = c.substring(0, 1);
    }

    protected String evalWarnings(final BuildResult result, Collection<FileAnnotation> warnings) {
        String messages = "";

        if (modules.isEmpty()) {
            modules.add("all");
        }

        for (String module : modules) {
            boolean allWarn = "all".equals(module);
            String heading = module + " annotations:";
            String tmp = "";

            for (FileAnnotation annotation : warnings) {
                Priority prio = annotation.getPriority();
                if (prio == Priority.LOW && !showLow) {
                    continue;
                }

                if (prio == Priority.NORMAL && !showNormal) {
                    continue;
                }

                if (prio == Priority.HIGH && !showHigh) {
                    continue;
                }

                if (allWarn || annotation.getType().equals(module)) {
                    if (allWarn && verbose) {
                        tmp += annotation.getType() + ": ";
                    }
                    tmp += createMessage(annotation);
                }
            }

            if (tmp.length() > 0) {
                String ind = (indent > 0) ? StringUtils.repeat(" ", indent) : "";

                messages += ind;
                messages += heading + "\n";

                messages += ind;
                messages += StringUtils.repeat(linechar, heading.length()) + "\n";

                messages += tmp;
                messages += "\n";
            }
        }

        return messages;
    }

    private String createMessage(FileAnnotation annotation) {
        String ind = (indent > 0) ? StringUtils.repeat(" ", indent) : "";
        String message = ind;

        if (annotation.getPrimaryLineNumber() > 0) {
            message += annotation.getFileName().replaceAll("^.*workspace/", "");
            message += ":" + annotation.getPrimaryLineNumber() + " ";
        }

        message += annotation.getMessage().replace("<br>", "\n" + ind) + "\n";

        String toolTip;
        toolTip = annotation.getToolTip().replace("<br>", "\n");

        if (toolTip != null) {
            toolTip = ind + toolTip.replace("\n", "\n" + ind).trim();
            message += toolTip + "\n";
        }

        return message;
    }
}

@@ -1972,24 +1972,31 @@
   String
+Builder
  message
 s = %22%22;%0A
@@ -1991,14 +1991,30 @@
 sage
-s
  = 
-%22%22
+new StringBuilder()
 ;%0A%0A 
@@ -3126,32 +3126,36 @@
      message
-s += 
+.append(
 ind
+)
 ;%0A          
@@ -3171,27 +3171,62 @@
 sage
-s += heading + 
+.append(heading);%0A                message.append(
 %22%5Cn%22
+)
 ;%0A%0A 
@@ -3251,16 +3251,20 @@
 sage
-s += 
+.append(
 ind
+)
 ;%0A  
@@ -3284,21 +3284,24 @@
  message
-s += 
+.append(
 StringUt
@@ -3338,23 +3338,55 @@
 ength())
- + 
+);%0A                message.append(
 %22%5Cn%22
+)
 ;%0A%0A     
@@ -3407,16 +3407,20 @@
 sage
-s += 
+.append(
 tmp
+)
 ;%0A  
@@ -3444,17 +3444,21 @@
 sage
-s += 
+.append(
 %22%5Cn%22
+)
 ;%0A  
@@ -3502,17 +3502,27 @@
  message
-s
+.toString()
 ;%0A    %7D%0A
@@ -3672,16 +3672,23 @@
   String
+Builder
  message
@@ -3690,19 +3690,64 @@
 ssage = 
-ind
+new StringBuilder();%0A        message.append(ind)
 ;%0A%0A     
@@ -3805,36 +3805,40 @@
          message
- += 
+.append(
 annotation.getFi
@@ -3877,16 +3877,17 @@
 e/%22, %22%22)
+)
 ;%0A      
@@ -3903,18 +3903,49 @@
 sage
- += %22:%22 + 
+.append(%22:%22);%0A            message.append(
 anno
@@ -3977,14 +3977,42 @@
 er()
- + 
+);%0A            message.append(
 %22 %22
+)
 ;%0A  
@@ -4035,20 +4035,24 @@
  message
- += 
+.append(
 annotati
@@ -4094,23 +4094,47 @@
 %22 + ind)
- + 
+);%0A        message.append(
 %22%5Cn%22
+)
 ;%0A%0A     
@@ -4342,26 +4342,58 @@
 sage
- += toolTip + 
+.append(toolTip);%0A            message.append(
 %22%5Cn%22
+)
 ;%0A  
@@ -4423,16 +4423,27 @@
  message
+.toString()
 ;%0A    %7D%0A

use StringBuilder instead of String