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

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.Color;
import fi.helsinki.cs.tmc.cli.io.Io;
import fi.helsinki.cs.tmc.cli.io.ResultPrinter;
import fi.helsinki.cs.tmc.cli.tmcstuff.CourseInfo;
import fi.helsinki.cs.tmc.cli.tmcstuff.CourseInfoIo;
import fi.helsinki.cs.tmc.cli.tmcstuff.TmcUtil;
import fi.helsinki.cs.tmc.cli.tmcstuff.WorkDir;
import fi.helsinki.cs.tmc.core.TmcCore;
import fi.helsinki.cs.tmc.core.domain.Course;
import fi.helsinki.cs.tmc.core.domain.submission.SubmissionResult;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;
import java.util.List;

@Command(name = "submit", desc = "Submit exercises")
public class SubmitCommand implements CommandInterface {

    private static final Logger logger = LoggerFactory.getLogger(SubmitCommand.class);
    private final Options options;

    private Application app;
    private Io io;
    private boolean showAll;
    private boolean showDetails;

    public SubmitCommand(Application app) {
        this.app = app;
        this.options = new Options();

        options.addOption("a", "all", false, "Show all test results");
        options.addOption("d", "details", false, "Show detailed error message");
    }

    @Override
    public void run(String[] args, Io io) {
        this.io = io;
        TmcCore core;
        WorkDir dirUtil;

        String[] exerciseNames = parseArgs(args);

        if (exerciseNames == null) {
            return;
        }
        dirUtil = new WorkDir();
        core = this.app.getTmcCore();
        if (core == null) {
            return;
        }
        Path courseDir = dirUtil.getCourseDirectory();

        if (courseDir == null) {
            System.out.println("Not a course directory");
            return;
        }

        CourseInfo info = CourseInfoIo.load(dirUtil.getConfigFile());
        String courseName = info.getCourseName();
        Course course = TmcUtil.findCourse(core, courseName);

        List<String> exercises;
        exercises = dirUtil.getExerciseNames(exerciseNames);
        
        if (exercises.isEmpty()) {
            io.println("You have to be in the exercise root directory to submit."
                    + " (This is a known problem.)");
            return;
        }

        ResultPrinter resultPrinter = new ResultPrinter(io, this.showDetails, this.showAll);
        SubmissionResult result;

        for (String exerciseName : exercises) {
            io.println(Color.colorString("Submitting: " + exerciseName, Color.ANSI_YELLOW));
            result = TmcUtil.submitExercise(core, course, exerciseName);
            resultPrinter.printSubmissionResult(result);
            io.println("");
        }
    }

    private String[] parseArgs(String[] args) {
        GnuParser parser = new GnuParser();
        CommandLine line;
        try {
            line = parser.parse(options, args);
        } catch (ParseException e) {
            io.println("Invalid command line arguments.");
            io.println(e.getMessage());
            return null;
        }
        this.showAll = line.hasOption("a");
        this.showDetails = line.hasOption("d");
        return line.getArgs();
    }
}

@@ -1641,55 +1641,8 @@
  io;
-%0A        TmcCore core;%0A        WorkDir dirUtil;
 %0A%0A  
@@ -1689,17 +1689,16 @@
 (args);%0A
-%0A
         
@@ -1760,48 +1760,24 @@
   %7D%0A
- 
+%0A
        
-dirUtil = new WorkDir();%0A       
+ TmcCore
  cor
@@ -1853,32 +1853,75 @@
 turn;%0A        %7D%0A
+%0A        WorkDir dirUtil = new WorkDir();%0A%0A
         Path cou
@@ -1955,25 +1955,24 @@
 irectory();%0A
-%0A
         if (
@@ -2288,27 +2288,8 @@
 ing%3E
- exercises;%0A       
  exe
@@ -2342,17 +2342,8 @@
 s);%0A
-        %0A
     
@@ -2636,41 +2636,8 @@
 ll);
-%0A        SubmissionResult result;
 %0A%0A  
@@ -2783,24 +2783,41 @@
             
+SubmissionResult 
 result = Tmc
@@ -3019,52 +3019,8 @@
 ) %7B%0A
-        GnuParser parser = new GnuParser();%0A
     
@@ -3078,21 +3078,35 @@
 e = 
-p
+new GnuP
 arser
+()
 .parse(
+this.
 opti

Clean up code