package fi.helsinki.cs.tmc.cli.command;

import fi.helsinki.cs.tmc.cli.Application;
import fi.helsinki.cs.tmc.cli.io.Io;
import fi.helsinki.cs.tmc.cli.io.TmcCliProgressObserver;
import fi.helsinki.cs.tmc.cli.tmcstuff.DirectoryUtil;
import fi.helsinki.cs.tmc.core.TmcCore;
import fi.helsinki.cs.tmc.core.domain.Exercise;
import fi.helsinki.cs.tmc.langs.domain.RunResult;
import fi.helsinki.cs.tmc.langs.domain.TestResult;

import java.nio.file.Path;

public class RunTestsCommand implements Command {

    private final Application app;

    public RunTestsCommand(Application app) {
        this.app = app;
    }

    @Override
    public String getDescription() {
        return "Run local exercise tests";
    }

    @Override
    public String getName() {
        return "run-tests";
    }

    @Override
    public void run(String[] args, Io io) {
        TmcCore core = app.getTmcCore();
        if (core == null) {
            return;
        }

        DirectoryUtil dirUtil = new DirectoryUtil();
        Path courseDir = dirUtil.getCourseDirectory();
        String courseName = courseDir.getName(courseDir.getNameCount() - 1).toString();
        String exerciseName = dirUtil.getExerciseName();

        Exercise exercise = new Exercise(exerciseName, courseName);
        RunResult runResult;

        System.out.println("Running tests...");

        try {
            runResult = core.runTests(new TmcCliProgressObserver(), exercise).call();
        } catch (Exception ex) {
            System.out.println("Failed to run tests.");
            return;
        }

        printRunResult(runResult);
    }

    private void printRunResult(RunResult runResult) {
        for (TestResult testResult : runResult.testResults) {
            if (testResult.passed) {
                System.out.println(colorString("Passed: " + testResult.name, ANSI_GREEN));
            } else {
                System.out.println(colorString("Failed: " + testResult.name
                        + "\n\t" + testResult.errorMessage, ANSI_RED));
            }
        }

        if (runResult.status == RunResult.Status.PASSED) {
            System.out.println("All tests passed! Submit to server with 'tmc submit'.");
        } else if (runResult.status == RunResult.Status.TESTS_FAILED) {
            System.out.println("Some tests did not pass, please review your "
                    + "answer before submitting.");
        }
    }

    private String colorString(String string, String color) {
        return color + string + ANSI_RESET;
    }

    public static final String ANSI_RESET = "\u001B[0m";
    public static final String ANSI_BLACK = "\u001B[30m";
    public static final String ANSI_RED = "\u001B[31m";
    public static final String ANSI_GREEN = "\u001B[32m";
    public static final String ANSI_YELLOW = "\u001B[33m";
    public static final String ANSI_BLUE = "\u001B[34m";
    public static final String ANSI_PURPLE = "\u001B[35m";
    public static final String ANSI_CYAN = "\u001B[36m";
    public static final String ANSI_WHITE = "\u001B[37m";
}

@@ -415,16 +415,74 @@
 esult;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import j
@@ -552,16 +552,118 @@
 mand %7B%0A%0A
+    private static final Logger logger%0A            = LoggerFactory.getLogger(RunTestsCommand.class);%0A%0A
     priv
@@ -1691,17 +1691,144 @@
 n tests.
-%22
+ Please make sure you are in%22%0A                    + %22 exercise directory.%22);%0A            logger.error(%22Failed to run tests.%22, ex
 );%0A     

Add logger