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

import static fi.helsinki.cs.tmc.cli.io.Color.ANSI_GREEN;
import static fi.helsinki.cs.tmc.cli.io.Color.ANSI_RED;
import static fi.helsinki.cs.tmc.cli.io.Color.colorString;

import fi.helsinki.cs.tmc.cli.Application;
import fi.helsinki.cs.tmc.cli.command.core.Command;
import fi.helsinki.cs.tmc.cli.command.core.CommandInterface;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;

/**
 * Quick and dirty.
 */
@Command(name = "run-tests", desc = "Run local exercise tests.")
public class RunTestsCommand implements CommandInterface {

    private static final Logger logger
            = LoggerFactory.getLogger(RunTestsCommand.class);

    private final Application app;
    private Io io;

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

    @Override
    public void run(String[] args, Io io) {
        this.io = io;
        DirectoryUtil dirUtil = new DirectoryUtil();
        String courseName = getCourseName(dirUtil);
        String exerciseName = dirUtil.getExerciseName();

        io.println("Running tests...");
        RunResult runResult;
        try {
            TmcCore core = app.getTmcCore();
            Exercise exercise = new Exercise(exerciseName, courseName);
            runResult = core.runTests(new TmcCliProgressObserver(), exercise).call();
        } catch (Exception ex) {
            io.println("Failed to run tests. Please make sure you are in"
                    + " course and exercise directory.");
            logger.error("Failed to run tests.", ex);
            return;
        }

        printRunResult(runResult);
    }

    private String getCourseName(DirectoryUtil dirUtil) {
        Path courseDir = dirUtil.getCourseDirectory();
        try {
            return courseDir.getName(courseDir.getNameCount() - 1).toString();
        } catch (Exception e) {
        }
        return null;
    }

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

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

}

@@ -501,60 +501,258 @@
 uff.
-DirectoryUtil;%0Aimport fi.helsinki.cs.tmc.core.TmcCor
+CourseInfo;%0Aimport fi.helsinki.cs.tmc.cli.tmcstuff.CourseInfoIo;%0Aimport fi.helsinki.cs.tmc.cli.tmcstuff.DirectoryUtil;%0Aimport fi.helsinki.cs.tmc.cli.tmcstuff.TmcUtil;%0Aimport fi.helsinki.cs.tmc.core.TmcCore;%0Aimport fi.helsinki.cs.tmc.core.domain.Cours
 e;%0Ai
@@ -958,16 +958,37 @@
 ctory;%0A%0A
+import java.io.File;%0A
 import j
@@ -1010,36 +1010,31 @@
 th;%0A
-%0A/**%0A * Quick and dirty.%0A */
+import java.util.List;%0A
 %0A@Co
@@ -1310,16 +1310,43 @@
 e Io io;
+%0A    private Course course;
 %0A%0A    pu
@@ -1609,22 +1609,28 @@
         
+List%3C
 String
+%3E
  exercis
@@ -1634,16 +1634,17 @@
 ciseName
+s
  = dirUt
@@ -1661,17 +1661,119 @@
 ciseName
-(
+s(args);%0A%0A        TmcCore core = app.getTmcCore();%0A        course = TmcUtil.findCourse(core, courseName
 );%0A%0A    
@@ -1837,16 +1837,17 @@
 Result;%0A
+%0A
         
@@ -1868,41 +1868,154 @@
     
-TmcCore core = app.getTmcCore();%0A
+for (String name : exerciseNames) %7B%0A                io.println(%22Testing: %22 + name);%0A                name = name.replace(%22-%22, File.separator);%0A    
     
@@ -2055,25 +2055,17 @@
 xercise(
-exerciseN
+n
 ame, cou
@@ -2070,24 +2070,29 @@
 ourseName);%0A
+%0A    
             
@@ -2161,24 +2161,82 @@
 se).call();%0A
+                printRunResult(runResult);%0A            %7D%0A%0A
         %7D ca
@@ -2454,65 +2454,9 @@
     
-    return;%0A        %7D%0A%0A        printRunResult(runResult);
+%7D
 %0A   

Fix RunTestsCommand