package de.is24.deadcode.plugin;

import com.google.common.collect.Ordering;
import de.is24.deadcode.DeadCode;
import de.is24.deadcode.DeadCodeFinder;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;

import java.io.File;


/**
 * The FindDeadCodeMojo attempts to find unused code.
 *
 * @description Finds dead (i.e. unused) code
 * @goal find
 * @execute phase="compile"
 * @threadSafe true
 */
@SuppressWarnings("UnusedDeclaration")
public class FindDeadCodeMojo extends AbstractMojo {

    /**
     * The Maven Project.
     *
     * @parameter expression="${project}"
     * @readonly
     * @required
     */
    private MavenProject project;

    public void execute() throws MojoExecutionException, MojoFailureException {
        DeadCode deadCode = analyzeCode();
        log(deadCode);
    }

    private DeadCode analyzeCode() {
        DeadCodeFinder deadCodeFinder = new DeadCodeFinder();
        return deadCodeFinder.findDeadCode(outputDirectoryOfProject());
    }

    private void log(DeadCode deadCode) {
        Log log = getLog();

        log.info("Analyzed " + deadCode.getAnalyzedClasses().size() + " class(es).");

        int numberOfUnusedClasses = deadCode.getDeadClasses().size();
        if (numberOfUnusedClasses == 0) {
            log.info("No unused classes found. Rejoice!");
            return;
        }

        log.warn("Found " + numberOfUnusedClasses + " unused class(es):");
        for (String unusedClass : Ordering.natural().sortedCopy(deadCode.getDeadClasses())) {
            log.warn("  " + unusedClass);
        }
    }

    private File outputDirectoryOfProject() {
        return new File(project.getBuild().getOutputDirectory());
    }

}

@@ -1160,32 +1160,151 @@
 ject());%0A    %7D%0A%0A
+    private File outputDirectoryOfProject() %7B%0A        return new File(project.getBuild().getOutputDirectory());%0A    %7D%0A%0A
     private void
@@ -1879,125 +1879,6 @@
  %7D%0A%0A
-    private File outputDirectoryOfProject() %7B%0A        return new File(project.getBuild().getOutputDirectory());%0A    %7D%0A%0A
 %7D%0A

clean code: reorder methods