package edu.pdx.cs410J.grader;

import org.junit.Test;

import edu.pdx.cs410J.grader.ProjectGradesImporter.ProjectScore;

import java.io.Reader;
import java.io.StringReader;
import java.util.regex.Matcher;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

public class ProjectGradesImporterTest {

  @Test(expected = IllegalStateException.class)
  public void gradedProjectWithNoGradeThrowsIllegalStateException() {
    GradedProject project = new GradedProject();
    project.addLine("asdfhjkl");
    project.addLine("iadguow");

    ProjectGradesImporter.getScoreFrom(project.getReader());
  }

  @Test
  public void scoreRegularExpressionWorkWithSimpleCase() {
    Matcher matcher = ProjectGradesImporter.scorePattern.matcher("3.0 out of 4.5");
    assertThat(matcher.matches(), equalTo(true));
    assertThat(matcher.groupCount(), equalTo(2));
    assertThat(matcher.group(1), equalTo("3.0"));
    assertThat(matcher.group(2), equalTo("4.5"));
  }

  @Test
  public void gradedProjectWithOutOfHasValidScore() {
    GradedProject project = new GradedProject();
    project.addLine("3.4 out of 3.5");
    project.addLine("iadguow");

    ProjectScore score = ProjectGradesImporter.getScoreFrom(project.getReader());
    assertThat(score.getTotalPoints(), equalTo(3.5));
    assertThat(score.getScore(), equalTo(3.4));
  }

  @Test
  public void scoreMatchesRegardlessOfCase() {
    Matcher matcher = ProjectGradesImporter.scorePattern.matcher("4.0 OUT OF 5.0");
    assertThat(matcher.matches(), equalTo(true));
    assertThat(matcher.groupCount(), equalTo(2));
    assertThat(matcher.group(1), equalTo("4.0"));
    assertThat(matcher.group(2), equalTo("5.0"));
  }

  private class GradedProject {
    private StringBuilder sb = new StringBuilder();

    public void addLine(String line) {
      sb.append(line);
      sb.append("\n");
    }

    public Reader getReader() {
      return new StringReader(sb.toString());
    }
  }


  // Multiple grades in a file (only first one)
}

@@ -1289,19 +1289,13 @@
 .get
-TotalPoints
+Score
 (), 
@@ -1304,17 +1304,17 @@
 ualTo(3.
-5
+4
 ));%0A    
@@ -1325,37 +1325,43 @@
 rtThat(score.get
-Score
+TotalPoints
 (), equalTo(3.4)
@@ -1354,25 +1354,25 @@
 , equalTo(3.
-4
+5
 ));%0A  %7D%0A%0A  @
@@ -1978,16 +1978,415 @@
  %7D%0A  %7D%0A%0A
+  @Test%0A  public void onlyFirstScoreIsReturned() %7B%0A    GradedProject project = new GradedProject();%0A    project.addLine(%223.4 out of 3.5%22);%0A    project.addLine(%22iadguow%22);%0A    project.addLine(%223.3 out of 3.4%22);%0A%0A    ProjectScore score = ProjectGradesImporter.getScoreFrom(project.getReader());%0A    assertThat(score.getScore(), equalTo(3.4));%0A    assertThat(score.getTotalPoints(), equalTo(3.5));%0A  %7D%0A
 %0A  // Mu

Verify that only the first score in a file is returned.