package com.bbn.kbp.events2014.io;

import com.bbn.bue.common.scoring.Scored;
import com.bbn.bue.common.symbols.Symbol;
import com.bbn.kbp.events2014.AnswerKey;
import com.bbn.kbp.events2014.AssessedResponse;
import com.bbn.kbp.events2014.CharOffsetSpan;
import com.bbn.kbp.events2014.CorefAnnotation;
import com.bbn.kbp.events2014.FieldAssessment;
import com.bbn.kbp.events2014.KBPRealis;
import com.bbn.kbp.events2014.KBPString;
import com.bbn.kbp.events2014.Response;
import com.bbn.kbp.events2014.ResponseAssessment;
import com.bbn.kbp.events2014.ResponseAssessment.MentionType;
import com.bbn.kbp.events2014.SystemOutput;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.io.Files;

import junit.framework.TestCase;

import org.junit.Test;

import java.io.File;
import java.io.IOException;

public class TestLDCIO extends TestCase {

  private final Symbol docid = Symbol.from("AFP_ENG_20030304.0250");
  private Scored<Response> arg;
  private ResponseAssessment ann;
  private AssessedResponse annArg;

  @Override
  public void setUp() {
    final KBPString cas = KBPString.from("Phillipines",
        CharOffsetSpan.fromOffsetsOnly(42, 64));
    arg = Scored.from(Response.createFrom(docid,
        Symbol.from("Conflict.Attack"),
        Symbol.from("Attacker"),
        cas,
        cas.charOffsetSpan(),
        ImmutableSet.of(CharOffsetSpan.fromOffsetsOnly(327, 397)),
        ImmutableSet.of(CharOffsetSpan.fromOffsetsOnly(373, 383)),
        KBPRealis.Actual), 0.5465980768203735);
    ann = ResponseAssessment
        .create(Optional.of(FieldAssessment.CORRECT), Optional.of(FieldAssessment.CORRECT),
            Optional.of(FieldAssessment.CORRECT),
            Optional.of(KBPRealis.Actual),
            Optional.of(FieldAssessment.CORRECT),
            Optional.of(MentionType.NAME));
    annArg = AssessedResponse.from(arg.item(), ann);
  }

  @Test
  public void testArgumentRoundtrip2014() throws IOException {
    testArgumentRoundtrip(AssessmentSpecFormats.Format.KBP2014);
  }

  @Test
  public void testArgumentRoundtrip2015() throws IOException {
    testArgumentRoundtrip(AssessmentSpecFormats.Format.KBP2015);
  }

  public void testArgumentRoundtrip(AssessmentSpecFormats.Format format) throws IOException {
    final File tmpDir = Files.createTempDir();
    tmpDir.deleteOnExit();

    final SystemOutputStore store1 = AssessmentSpecFormats.createSystemOutputStore(tmpDir, format);
    store1.write(SystemOutput.from(docid, ImmutableList.of(arg)));
    store1.close();

    final SystemOutputStore source2 = AssessmentSpecFormats.openSystemOutputStore(tmpDir, format);
    final SystemOutput rereadArg = source2.read(docid);
    source2.close();

    assertEquals(1, rereadArg.size());
    assertEquals(arg, Iterables.getFirst(rereadArg.scoredResponses(), null));
  }

  @Test
  public void testAnnotationRoundtrip2014() throws IOException {
    testAnnotationRoundtrip(AssessmentSpecFormats.Format.KBP2014);
  }

  @Test
  public void testAnnotationRoundtrip2015() throws IOException {
    testAnnotationRoundtrip(AssessmentSpecFormats.Format.KBP2015);
  }

  public void testAnnotationRoundtrip(AssessmentSpecFormats.Format format) throws IOException {
    final File tmpDir = Files.createTempDir();
    tmpDir.deleteOnExit();

    final AnnotationStore store1 = AssessmentSpecFormats.createAnnotationStore(tmpDir, format);
    final CorefAnnotation coref = CorefAnnotation.strictBuilder(docid)
        .corefCAS(annArg.response().canonicalArgument(), 1).build();
    store1.write(
        AnswerKey.from(docid, ImmutableList.of(annArg), ImmutableList.<Response>of(), coref));
    store1.close();
    final AnnotationStore store2 = AssessmentSpecFormats.openAnnotationStore(tmpDir, format);
    final AnswerKey rereadArg = store2.read(docid);
    store2.close();
    assertEquals(1, rereadArg.annotatedResponses().size());
    assertEquals(annArg, Iterables.getFirst(rereadArg.annotatedResponses(), null));
  }

    /*@Ignore("temporarily ignored until we can update the file")
    @Test
    public void existing() throws IOException {
        final String docid = "APW_ENG_20030513.0139";

        final File tmpDir = Files.createTempDir();
        tmpDir.deleteOnExit();
        final File tmpFile = new File(tmpDir, docid);
        tmpFile.deleteOnExit();

        Resources.asCharSource(Resources.getResource(TestLDCIO.class, "/"+docid),
                Charsets.UTF_8).copyTo(Files.asCharSink(tmpFile, Charsets.UTF_8));
        final AnnotationStore annStore = AssessmentSpecFormats.openAnnotationStore(tmpDir);
        final AnswerKey key = annStore.read(Symbol.from(docid));

    } */
}

@@ -690,29 +690,118 @@
 mon.
-collect.ImmutableList
+base.Predicates;%0Aimport com.google.common.collect.ImmutableList;%0Aimport com.google.common.collect.ImmutableMap
 ;%0Aim
@@ -1245,16 +1245,46 @@
  annArg;
+%0A  private String argMetadata;
 %0A%0A  @Ove
@@ -2069,24 +2069,54 @@
 ype.NAME));%0A
+    argMetadata = %22metadata%22;%0A
     annArg =
@@ -2435,32 +2435,441 @@
 .KBP2015);%0A  %7D%0A%0A
+  @Test%0A  public void testFilter() throws IOException %7B%0A    SystemOutput raw = SystemOutput.from(docid, ImmutableList.of(arg), ImmutableMap.of(arg.item(), argMetadata));%0A    SystemOutput filtered = raw.copyWithFilteredResponses(Predicates.%3CScored%3CResponse%3E%3EalwaysTrue());%0A    assertEquals(raw.metadata(arg.item()), filtered.metadata(arg.item()));%0A    assertEquals(raw.metadata(arg.item()), argMetadata);%0A  %7D%0A%0A
   public void te
@@ -3184,16 +3184,58 @@
 .of(arg)
+, ImmutableMap.of(arg.item(), argMetadata)
 ));%0A    
@@ -3459,32 +3459,32 @@
 eadArg.size());%0A
-
     assertEquals
@@ -3541,24 +3541,87 @@
 (), null));%0A
+    assertEquals(argMetadata, rereadArg.metadata(arg.item()));%0A
   %7D%0A%0A  @Test

test metadata handling