package com.bbn.kbp.events2014.io;

import com.bbn.bue.common.symbols.Symbol;
import com.bbn.kbp.events2014.ArgumentOutput;
import com.bbn.kbp.events2014.ResponseLinking;
import com.bbn.kbp.events2014.SystemOutput2015;
import com.bbn.kbp.events2014.SystemOutputLayout;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * Created by jdeyoung on 7/27/15.
 */
public final class ImportForeignIDs {

  private ImportForeignIDs() {
    throw new UnsupportedOperationException();
  }

  private static final Logger log = LoggerFactory.getLogger(ImportForeignIDs.class);

  public static void main(String... args) {
    try {
      trueMain(args);
    } catch (final Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }

  private static void trueMain(final String[] args) throws IOException {
    final File outputStoreBase = new File(args[0]);
    final File outputDirectory = new File(args[1]);
    importForeignIDs(outputStoreBase, outputDirectory);
  }

  public static void importForeignIDs(final File source, final File outputDirectory)
      throws IOException {
    final ArgumentStore originalArgumentStore =
        AssessmentSpecFormats.openSystemOutputStore(new File(source, "arguments"),
            AssessmentSpecFormats.Format.KBP2015);
    final LinkingStore originalLinkingStore =
        LinkingSpecFormats.openLinkingStore(new File(source, "linking"));

    final SystemOutputStore newOutput =
        SystemOutputLayout.KBP_EA_2015.openOrCreate(outputDirectory);

    for (final Symbol docid : originalArgumentStore.docIDs()) {
      log.info("Converting {}", docid);
      final ImmutableBiMap.Builder<String, String> originalToSystem = ImmutableBiMap.builder();
      final ArgumentOutput originalArguments = AssessmentSpecFormats
          .uncachedReadFromArgumentStoreCachingOldIDS(originalArgumentStore, docid,
              originalToSystem);

      final Optional<ResponseLinking> transformedLinking = LinkingSpecFormats
          .readFromLinkingStoreTransformingIDs(originalLinkingStore, docid,
              originalArguments.responses(),
              Optional.<ImmutableMap<String, String>>of(originalToSystem.build()));
      if (transformedLinking.isPresent()) {
        // create system output
        final SystemOutput2015 newSystemOutput =
            SystemOutput2015.from(originalArguments, transformedLinking.get());
        newOutput.write(newSystemOutput);
      } else {
        throw new IOException("No linking found for " + docid);
      }
    }
    originalArgumentStore.close();
    originalLinkingStore.close();
    newOutput.close();
  }
}

@@ -29,16 +29,65 @@
 14.io;%0A%0A
+import com.bbn.bue.common.parameters.Parameters;%0A
 import c
@@ -1065,50 +1065,243 @@
 nal 
-File outputStoreBase = new File(args%5B0%5D
+Parameters params = Parameters.loadSerifStyle(new File(args%5B0%5D));%0A%0A    if (params.getBoolean(%22doMultipleStores%22)) %7B%0A      processMultipleStores(params);%0A    %7D else %7B%0A      File outputStoreBase = params.getExistingDirectory(%22input%22
 );%0A
+  
     
@@ -1333,27 +1333,50 @@
 y = 
-new File(args%5B1%5D
+params.getCreatableDirectory(%22output%22
 );%0A
+  
     
@@ -1427,16 +1427,473 @@
 ctory);%0A
+    %7D%0A  %7D%0A%0A  private static void processMultipleStores(Parameters params) throws IOException %7B%0A    final File inputBase = params.getExistingDirectory(%22inputBase%22);%0A    final File outputBase = params.getCreatableFile(%22outputBase%22);%0A    for (final File f : inputBase.listFiles()) %7B%0A      if (f.isDirectory()) %7B%0A        final File outputDir = new File(outputBase, f.getName());%0A        outputDir.mkdirs();%0A        importForeignIDs(f, outputDir);%0A      %7D%0A    %7D%0A
   %7D%0A%0A  p

Allow ImportForeignIDs to operate over multiple stores