package edu.pdx.cs410J.grader;

import edu.pdx.cs410J.ParserException;

import java.io.*;
import java.util.Optional;

public class GradesFromD2LImporter {

  public static void importGradesFromD2L(GradesFromD2L d2lGrades, GradeBook gradebook) {
    for (GradesFromD2L.D2LStudent d2LStudent : d2lGrades.getStudents()) {
      Student student = d2lGrades.findStudentInGradebookForD2LStudent(d2LStudent, gradebook)
        .orElseThrow(() -> new IllegalStateException("Could not find student " + d2LStudent + " in gradebook"));
      for (String quizName : d2LStudent.getQuizNames()) {
        Optional<Assignment> optional = d2lGrades.findAssignmentInGradebookForD2lQuiz(quizName, gradebook);
        Assignment quiz = optional.orElseThrow(() -> new IllegalStateException("No quiz named \"" + quizName + "\" in gradebook"));
        student.setGrade(quiz.getName(), new Grade(quiz, d2LStudent.getScore(quizName)));
      }
    }
  }

  public static void main(String[] args) throws IOException, ParserException {
    String d2lCsvFileName = null;
    String gradeBookFileName = null;

    for (String arg : args) {
      if (d2lCsvFileName == null) {
        d2lCsvFileName = arg;

      } else if (gradeBookFileName == null) {
        gradeBookFileName = arg;

      } else {
        usage("Extraneous command line argument: " + arg);
      }
    }

    if (d2lCsvFileName == null) {
      usage("Missing D2L CSV file name");
    }

    if (gradeBookFileName == null) {
      usage("Missing grade book file name");
    }

    GradesFromD2L d2lGrades = parseD2lCsvFile(d2lCsvFileName);
    GradeBook gradebook = parseGradeBook(gradeBookFileName);
    importGradesFromD2L(d2lGrades, gradebook);
  }

  private static GradeBook parseGradeBook(String gradeBookFileName) throws IOException, ParserException {
    File gradeBookFile = new File(gradeBookFileName);
    if (!gradeBookFile.exists()) {
      usage("Grade Boook file \"" + gradeBookFile + "\" does not exist");
    }

    XmlGradeBookParser parser = new XmlGradeBookParser(gradeBookFile);
    return parser.parse();
  }

  private static GradesFromD2L parseD2lCsvFile(String d2lCsvFileName) throws IOException {
    File d2lCsvFile = new File(d2lCsvFileName);
    if (!d2lCsvFile.exists()) {
      usage("D2L CSV file \"" + d2lCsvFile + "\" does not exist");
    }

    D2LCSVParser parser = new D2LCSVParser(new FileReader(d2lCsvFile));
    return parser.getGrades();
  }

  private static void usage(String message) {
    PrintStream err = System.err;

    err.println("+++ " + message);
    err.println();
    err.println("usage: java GradesFromD2LImporter d2lCsvFileName gradeBookFileName");
    err.println("    d2lCsvFileName       Name of the CSV file exported from D2L");
    err.println("    gradeBookFileName    Gradebook file");
    err.println();
    err.println("Imports grades from D2L into a gradebook");
    err.println();

    System.exit(1);
  }
}

@@ -1685,19 +1685,471 @@
 ebook);%0A
-  %7D
+%0A    saveGradeBookIfModified(gradebook, gradeBookFileName);%0A  %7D%0A%0A  private static void saveGradeBookIfModified(GradeBook gradeBook, String gradeBookFileName) %7B%0A    if (gradeBook.isDirty()) %7B%0A      File file = new File(gradeBookFileName);%0A      try %7B%0A        XmlDumper dumper = new XmlDumper(file);%0A        dumper.dump(gradeBook);%0A%0A      %7D catch (IOException e) %7B%0A        usage(%22Can't write grade book in %5C%22%22 + gradeBookFileName + %22%5C%22%22);%0A      %7D%0A    %7D%0A  %7D%0A
 %0A%0A  priv
@@ -2359,17 +2359,16 @@
 rade Boo
-o
 k file %5C

Save the modified gradebook.