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

import fi.helsinki.cs.tmc.core.TmcCore;
import fi.helsinki.cs.tmc.core.commands.GetUpdatableExercises.UpdateResult;
import fi.helsinki.cs.tmc.core.domain.Course;
import fi.helsinki.cs.tmc.core.domain.Exercise;
import fi.helsinki.cs.tmc.core.domain.ProgressObserver;

import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class ExerciseUpdater {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ExerciseUpdater.class);

    private final TmcCore core;
    private final Course course;

    private List<Exercise> newExercises;
    private List<Exercise> updatedExercises;

    public ExerciseUpdater(TmcCore core, Course course) {
        this.core = core;
        this.course = course;

        this.newExercises = new ArrayList<>();
        this.updatedExercises = new ArrayList<>();
    }

    public List<Exercise> getNewExercises() {
        return newExercises;
    }

    public List<Exercise> getUpdatedExercises() {
        return updatedExercises;
    }

    public List<Exercise> getNewAndUpdatedExercises() {
        List<Exercise> list = new ArrayList<>();
        list.addAll(newExercises);
        list.addAll(updatedExercises);
        return list;
    }

    public void setNewExercises(List<Exercise> newExercises) {
        this.newExercises = newExercises;
    }

    public void setUpdatedExercises(List<Exercise> updatedExercises) {
        this.updatedExercises = updatedExercises;
    }

    public boolean newExercisesAvailable() {
        return !newExercises.isEmpty();
    }

    public boolean updatedExercisesAvailable() {
        return !updatedExercises.isEmpty();
    }

    public boolean updatesAvailable() {
        UpdateResult result;
        try {
            result = core.getExerciseUpdates(ProgressObserver.NULL_OBSERVER, course).call();
        } catch (Exception ex) {
            LOGGER.warn("Failed to get exercise updates.", ex);
            return false;
        }

        if (result == null) {
            return false;
        }

        newExercises = result.getNewExercises();
        updatedExercises = result.getUpdatedExercises();

        return newExercisesAvailable() || updatedExercisesAvailable();
    }
}

@@ -34,16 +34,168 @@
 mmand;%0A%0A
+import fi.helsinki.cs.tmc.cli.tmcstuff.CourseInfo;%0Aimport fi.helsinki.cs.tmc.cli.tmcstuff.CourseInfoIo;%0Aimport fi.helsinki.cs.tmc.cli.tmcstuff.TmcUtil;%0A
 import f
@@ -486,16 +486,43 @@
 ctory;%0A%0A
+import java.nio.file.Path;%0A
 import j
@@ -2441,11 +2441,486 @@
 );%0A    %7D
+%0A%0A    public List%3CExercise%3E downloadUpdates() %7B%0A        return TmcUtil.downloadExercises(core, getNewAndUpdatedExercises());%0A    %7D%0A%0A    public boolean updateCourseJson(CourseInfo info, Path configFile) %7B%0A        Course newDetailsCourse = TmcUtil.findCourse(core, course.getName());%0A        if (newDetailsCourse == null) %7B%0A            return false;%0A        %7D%0A        info.setExercises(newDetailsCourse.getExercises());%0A        return CourseInfoIo.save(info, configFile);%0A    %7D
 %0A%7D%0A

Add download and updateJson methods