/*
 * Copyright 2015 Delft University of Technology
 *
 * 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 nl.tudelft.graphalytics.reporting.html;

import nl.tudelft.graphalytics.domain.BenchmarkSuiteResult;
import nl.tudelft.graphalytics.reporting.BenchmarkReport;
import nl.tudelft.graphalytics.reporting.BenchmarkReportData;
import nl.tudelft.graphalytics.reporting.BenchmarkReportFile;
import nl.tudelft.graphalytics.reporting.BenchmarkReportGenerator;

import java.net.URL;
import java.util.Collection;
import java.util.LinkedList;

/**
 * Utility class for generating an HTML-based BenchmarkReport from a BenchmarkSuiteResult.
 *
 * @author Tim Hegeman
 */
public class HtmlBenchmarkReportGenerator implements BenchmarkReportGenerator {

	public static final String REPORT_TYPE_IDENTIFIER = "html";

	private static final String INDEX_HTML = "index";
	private static final String[] STATIC_RESOURCES = new String[]{
			// Bootstrap CSS and JS
			"lib/bootstrap/css/bootstrap.min.css",
			"lib/bootstrap/css/bootstrap-theme.min.css",
			"lib/bootstrap/fonts/glyphicons-halflings-regular.eot",
			"lib/bootstrap/fonts/glyphicons-halflings-regular.svg",
			"lib/bootstrap/fonts/glyphicons-halflings-regular.ttf",
			"lib/bootstrap/fonts/glyphicons-halflings-regular.woff",
			"lib/bootstrap/js/bootstrap.min.js",
			"lib/bootstrap/js/jquery.js",
			// Report CSS
			"lib/graphalytics/css/carousel.css",
			"lib/graphalytics/css/report.css"
	};

	@Override
	public BenchmarkReport generateReportFromResults(BenchmarkSuiteResult result) {
		// Initialize the template engine
		TemplateEngine templateEngine = new TemplateEngine();
		templateEngine.putVariable("report", new BenchmarkReportData(result));
		templateEngine.putVariable("util", new TemplateUtility());

		// Generate the report files
		Collection<BenchmarkReportFile> reportFiles = new LinkedList<>();
		// 1. Generate the index page
		String indexHtml = templateEngine.processTemplate(INDEX_HTML);
		reportFiles.add(new GeneratedHtmlPage(indexHtml, ".", INDEX_HTML));
		// 2. Copy the static resources
		for (String resource : STATIC_RESOURCES) {
			URL resourceUrl = HtmlBenchmarkReportGenerator.class.getResource("/graphalytics/reporting/html/" + resource);
			reportFiles.add(new StaticResource(resourceUrl, resource));
		}

		return new BenchmarkReport(REPORT_TYPE_IDENTIFIER, reportFiles);
	}

}

@@ -1048,16 +1048,39 @@
 kedList;
+%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -1982,16 +1982,75 @@
 s%22%0A%09%7D;%0A%0A
+%09private final List%3CPlugin%3E plugins = new LinkedList%3C%3E();%0A%0A
 %09@Overri
@@ -2133,16 +2133,127 @@
 sult) %7B%0A
+%09%09// Callback to plugins before generation%0A%09%09for (Plugin plugin : plugins) %7B%0A%09%09%09plugin.preGenerate(this);%0A%09%09%7D%0A%0A
 %09%09// Ini
@@ -3003,72 +3003,1456 @@
 %0A%0A%09%09
-return new BenchmarkReport(REPORT_TYPE_IDENTIFIER, reportFiles);
+// Callback to plugins after generation for additional files%0A%09%09for (Plugin plugin : plugins) %7B%0A%09%09%09Collection%3CBenchmarkReportFile%3E additionalFiles = plugin.generateAdditionalReportFiles(this, result);%0A%09%09%09if (additionalFiles != null) %7B%0A%09%09%09%09reportFiles.addAll(additionalFiles);%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return new BenchmarkReport(REPORT_TYPE_IDENTIFIER, reportFiles);%0A%09%7D%0A%0A%09/**%0A%09 * Adds a plugin instance to the list of plugins that will receive callbacks throughout the generation process.%0A%09 *%0A%09 * @param plugin the plugin instance to add%0A%09 */%0A%09public void registerPlugin(Plugin plugin) %7B%0A%09%09plugins.add(plugin);%0A%09%7D%0A%0A%09/**%0A%09 * Callback interface for plugins to inject custom HTML pages and resources into the benchmark report.%0A%09 */%0A%09public interface Plugin %7B%0A%0A%09%09/**%0A%09%09 * Callback before generation of the default Graphalytics benchmark report starts.%0A%09%09 *%0A%09%09 * @param generator the benchmark report generator instance%0A%09%09 */%0A%09%09void preGenerate(HtmlBenchmarkReportGenerator generator);%0A%0A%09%09/**%0A%09%09 * Callback during benchmark report generation to add additional pages and resources to the report.%0A%09%09 *%0A%09%09 * @param generator the benchmark report generator instance%0A%09%09 * @param result    the results of running a benchmark suite from which a report is to be generated%0A%09%09 * @return a collection of additional pages and resources%0A%09%09 */%0A%09%09Collection%3CBenchmarkReportFile%3E generateAdditionalReportFiles(HtmlBenchmarkReportGenerator generator,%0A%09%09%09%09BenchmarkSuiteResult result);%0A
 %0A%09%7D%0A

Add plugin integration to html report generator