package de.hftstuttgart.models;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class LegacyMoodleResult {

    public List<LegacyMoodleTestResult> testResults;
    public List<LegacyMoodleCompilationError> compilationErrors;

    public static LegacyMoodleResult convertToModdleResult(ModocotResultSummary summary) {
        LegacyMoodleTestResult tests = new LegacyMoodleTestResult();
        tests.failureCount = summary.failureCount;
        tests.testCount = summary.testCount;
        tests.successfulTests = summary.successes.stream()
            .map(s -> s.name)
            .collect(Collectors.toList());
        tests.testFailures = summary.failures.stream()
            .map(f -> {
                LegacyMoodleTestFailure failure = new LegacyMoodleTestFailure();
                failure.testHeader = f.name;
                failure.message = f.failureReason;
                failure.trace = f.stacktrace;

                return failure;
            })
            .collect(Collectors.toList());

        List<LegacyMoodleCompilationError> compilationErrors;
        compilationErrors = summary.compilationErrors.stream()
            .map(f -> {
                LegacyMoodleCompilationError cError = new LegacyMoodleCompilationError();
                cError.javaFileName = f.name;
                cError.message = f.failureReason;
                cError.lineNumber = f.lineNumber;
                cError.columnNumber = f.columnNumber;
                cError.position = f.position;

                return cError;
            })
            .collect(Collectors.toList());

        LegacyMoodleResult result = new LegacyMoodleResult();
        result.testResults = Collections.singletonList(tests);
        result.compilationErrors = compilationErrors;

        return result;
    }

    private static class LegacyMoodleTestResult {
        public String testName = "UnitTests";
        public int testCount;
        public int failureCount;
        public List<String> successfulTests;
        public List<LegacyMoodleTestFailure> testFailures;
    }

    private static class LegacyMoodleTestFailure {
        public String testHeader;
        public String message;
        public String trace;
    }

    private static class LegacyMoodleCompilationError {
        public String message;
        public String javaFileName;
        public int lineNumber;
        public int columnNumber;
        public int position;
    }
}
