package uk.co.automatictester.lightning.core.ci;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.co.automatictester.lightning.core.state.TestSet;
import uk.co.automatictester.lightning.core.data.JMeterTransactions;
import uk.co.automatictester.lightning.core.tests.base.LightningTest;

import java.util.Arrays;

public class TeamCityReporter extends CIReporter {

    private static final String TEAMCITY_BUILD_STATUS = "##teamcity[buildStatus text='%s']";
    private static final String TEAMCITY_BUILD_PROBLEM = "##teamcity[buildProblem description='%s']%n";
    private static final String TEAMCITY_STATISTICS = "##teamcity[buildStatisticValue key='%s' value='%s']%n";

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private TeamCityReporter(TestSet testSet) {
        super(testSet);
    }

    private TeamCityReporter(JMeterTransactions jmeterTransactions) {
        super(jmeterTransactions);
    }

    public static TeamCityReporter fromTestSet(TestSet testSet) {
        return new TeamCityReporter(testSet);
    }

    public static TeamCityReporter fromJMeterTransactions(JMeterTransactions jmeterTransactions) {
        return new TeamCityReporter(jmeterTransactions);
    }

    public TeamCityReporter printTeamCityBuildReportSummary() {
        log(getTeamCityBuildReportSummary());
        return this;
    }

    public String getTeamCityBuildReportSummary() {
        String outputTemplate = jmeterTransactions.getFailCount() > 0 ? TEAMCITY_BUILD_PROBLEM : TEAMCITY_BUILD_STATUS;
        String reportSummary = getReportSummary();
        return String.format(outputTemplate, reportSummary);
    }

    public TeamCityReporter printTeamCityVerifyStatistics() {
        log(getTeamCityVerifyStatistics());
        return this;
    }

    public String getTeamCityVerifyStatistics() {
        StringBuilder output = new StringBuilder();
        for (LightningTest test : testSet.getTests()) {
            String teamCityConsoleOutputEntry = String.format(TEAMCITY_STATISTICS, test.getName(), test.getActualResult());
            output.append(teamCityConsoleOutputEntry);
        }
        return output.toString();
    }

    public TeamCityReporter printTeamCityReportStatistics() {
        log(getTeamCityReportStatistics());
        return this;
    }

    public String getTeamCityReportStatistics() {
        String output = "";
        String failedTransactionsStats = String.format(TEAMCITY_STATISTICS, "Failed transactions", jmeterTransactions.getFailCount());
        output += failedTransactionsStats;
        String totalTransactionsStats = String.format(TEAMCITY_STATISTICS, "Total transactions", jmeterTransactions.size());
        output += totalTransactionsStats;
        return output;
    }

    private void log(String output) {
        for (String line : Arrays.asList(output.split(System.lineSeparator()))) {
            log.info(line);
        }
    }
}

@@ -47,124 +47,8 @@
 i;%0A%0A
-import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0Aimport uk.co.automatictester.lightning.core.state.TestSet;%0A
 impo
@@ -112,16 +112,75 @@
 ctions;%0A
+import uk.co.automatictester.lightning.core.state.TestSet;%0A
 import u
@@ -246,34 +246,8 @@
 t;%0A%0A
-import java.util.Arrays;%0A%0A
 publ
@@ -607,82 +607,8 @@
 %22;%0A%0A
-    private final Logger log = LoggerFactory.getLogger(this.getClass());%0A%0A
     
@@ -1080,146 +1080,8 @@
  %7D%0A%0A
-    public TeamCityReporter printTeamCityBuildReportSummary() %7B%0A        log(getTeamCityBuildReportSummary());%0A        return this;%0A    %7D%0A%0A
     
@@ -1371,142 +1371,8 @@
  %7D%0A%0A
-    public TeamCityReporter printTeamCityVerifyStatistics() %7B%0A        log(getTeamCityVerifyStatistics());%0A        return this;%0A    %7D%0A%0A
     
@@ -1759,142 +1759,8 @@
  %7D%0A%0A
-    public TeamCityReporter printTeamCityReportStatistics() %7B%0A        log(getTeamCityReportStatistics());%0A        return this;%0A    %7D%0A%0A
     
@@ -2210,172 +2210,7 @@
    %7D
-%0A%0A    private void log(String output) %7B%0A        for (String line : Arrays.asList(output.split(System.lineSeparator()))) %7B%0A            log.info(line);%0A        %7D%0A    %7D
 %0A%7D%0A

Delete unused print methods