/*
 * Copyright (C) 2016 Hamburg Sud and the contributors.
 *
 * 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.aludratest.jenkins.aludratest;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

import hudson.Functions;
import hudson.model.Run;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

/**
 * Report for a single build.
 * 
 * @author falbrech
 * 
 */
public class AludratestBuildStatisticsReport {

	private Run<?, ?> build;

	public AludratestBuildStatisticsReport(Run<?, ?> build) {
		this.build = build;
	}

	public Run<?, ?> getBuild() {
		return build;
	}

	public String getTitle() {
		return "AludraTest Execution Report";
	}

	public String getResURL() {
		Jenkins jenkins = Jenkins.getInstance();
		if (jenkins == null) {
			return null;
		}

		return jenkins.getRootUrl() + Functions.getResourcePath();
	}

	public String generateJsData(String compareBuildId) {
		StringBuilder sb = new StringBuilder();
		sb.append(generateJsData(build, "current"));

		if (compareBuildId != null && !"".equals(compareBuildId)) {
			Run<?, ?> compareBuild = build.getParent().getBuild(compareBuildId);
			if (compareBuild != null) {
				sb.append(generateJsData(compareBuild, "compare"));
			}
		}

		return sb.toString();
	}

	private String generateJsData(Run<?, ?> build, String varNamePrefix) {
		// load data for current and compare build
		File statsFile = new File(build.getRootDir(), AludratestStatisticsPublisher.STATISTICS_FILE_NAME);
		if (!statsFile.isFile()) {
			return "";
		}

		StringBuilder sb = new StringBuilder();

		try {
			JSONObject object = (JSONObject) JSONSerializer.toJSON(FileUtils.readFileToString(statsFile, "UTF-8"));
			sb.append("var ").append(varNamePrefix).append("Statistics = ");
			sb.append(object.getJSONObject(ScriptConstants.GLOBAL_STATS_FIELD).toString()).append(";\n");

			sb.append("var ").append(varNamePrefix).append("TestCaseResults = ");
			sb.append(object.getJSONObject(ScriptConstants.SINGLE_TESTS_FIELD).toString()).append(";\n");
			
			if (object.has(ScriptConstants.HTML_LOG_PATH_FIELD)) {
				sb.append("var ").append(varNamePrefix).append("HtmlLogPath = '");
				sb.append(build.getUrl()).append(object.getString(ScriptConstants.HTML_LOG_PATH_FIELD)).append("';\n");
			}
		}
		catch (IOException e) {
			// TODO log error
		}

		return sb.toString();
	}

	public List<String> getPreviousBuildIds() {
		List<String> result = new ArrayList<String>();
		Run<?, ?> run = build.getPreviousBuild();
		while (run != null) {
			File f = new File(run.getRootDir(), AludratestStatisticsPublisher.STATISTICS_FILE_NAME);
			if (f.isFile()) {
				result.add(run.getId());
			}
			run = run.getPreviousBuild();
		}

		return result;
	}

	public String getBuildNumber(String buildId) {
		if (buildId == null) {
			return null;
		}
		Run<?, ?> run = build.getParent().getBuild(buildId);
		return run == null ? "???" : String.valueOf(run.getNumber());
	}

}

@@ -1714,34 +1714,30 @@
 d = 
-build.getParent().get
+getRunByCompare
 Build
+Id
 (com
@@ -3339,35 +3339,47 @@
 %0A%0A%09p
-ublic String getBuildNumber
+rivate Run%3C?, ?%3E getRunByCompareBuildId
 (Str
@@ -3442,51 +3442,431 @@
 %09%09%7D%0A
+%0A
 %09%09
-Run%3C?, ?%3E run = build.getParent().getBuil
+if (buildId.matches(%22-%5B0-9%5D+%22)) %7B%0A%09%09%09int buildIndex = Integer.parseInt(buildId) * -1 - 1;%0A%09%09%09List%3CString%3E buildIds = getPreviousBuildIds();%0A%09%09%09if (buildIds.size() %3E buildIndex) %7B%0A%09%09%09%09return build.getParent().getBuild(buildIds.get(buildIndex));%0A%09%09%09%7D%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09return build.getParent().getBuild(buildId);%0A%09%09%7D%0A%0A%09%09return null;%0A%09%7D%0A%0A%09public String getBuildNumber(String buildId) %7B%0A%09%09Run%3C?, ?%3E run = getRunByCompareBuildI
 d(bu

Add support for compareBuildId=-N parameter (prev N build)