package org.gbif;

import org.gbif.api.model.checklistbank.NameUsageMatch;
import org.gbif.common.parsers.core.ParseResult;
import org.gbif.file.CSVReader;
import org.gbif.occurrence.processor.interpreting.util.NubLookupInterpreter;

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

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

public class AsiaNodesLookup {

  private static final Logger LOG = LoggerFactory.getLogger(AsiaNodesLookup.class);
  private static final String[] GBIF_HEADERS =
    {"MatchType", "Confidence", "Synonym", "Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "ScientificName"};

  public static void main(String[] args) throws IOException {
//    String inputFileName = "/Users/oliver/Documents/asian_nodes_mtg/ias_full.csv";
    String inputFileName = args[0];
    String outputFileName = args[1];
    File inputFile = new File(inputFileName);
    File outputFile = new File(outputFileName);
    FileOutputStream os = new FileOutputStream(outputFile);
    CSVReader csvReader = new CSVReader(inputFile, "UTF-8", ",", '"', 0);
    String[] headerRow = csvReader.next();
    byte[] comma = ",".getBytes();
    for (String field : headerRow) {
      os.write(field.getBytes());
      os.write(comma);
    }
    for (String field : GBIF_HEADERS) {
      String name = "GBIF_" + field;
      os.write(name.getBytes());
      os.write(comma);
    }
    os.write("\n".getBytes());

    while (csvReader.hasNext()) {
      String[] fields = csvReader.next();
      logFields(fields);
      // ID, K, P, C, O, F, G, sp epithet, sciName
      ParseResult<NameUsageMatch> result = NubLookupInterpreter
        .nubLookup(fields[1], fields[2], fields[3], fields[4], fields[5], fields[6], fields[7], null);
      //      System.out.println(result.getPayload().toString());
      for (String field : fields) {
        os.write(field.getBytes());
        os.write(comma);
      }
      NameUsageMatch match = result.getPayload();
      writeField(os, match.getMatchType().toString());
      writeField(os, match.getConfidence().toString());
      writeField(os, match.isSynonym() ? "y" : "n");
      writeField(os, match.getKingdom());
      writeField(os, match.getPhylum());
      writeField(os, match.getClazz());
      writeField(os, match.getOrder());
      writeField(os, match.getFamily());
      writeField(os, match.getGenus());
      writeField(os, match.getScientificName());

      os.write("\n".getBytes());
    } os.close();
  }

  private static void writeField(FileOutputStream os, String field) throws IOException {
    String quoted = "\"" + field + "\"";
    os.write(quoted.getBytes());
    os.write(",".getBytes());
  }

  private static void logFields(String[] fields) {
    StringBuilder sb = new StringBuilder();
    for (String field : fields) {
      sb.append(field + " ");
    }
    System.out.println(sb.toString());
  }
}

@@ -648,16 +648,112 @@
 ficName%22
+,%0A    %22KingdomKey%22, %22PhylumKey%22, %22ClassKey%22, %22OrderKey%22, %22FamilyKey%22, %22GenusKey%22, %22NameUsageKey%22
 %7D;%0A%0A  pu
@@ -2546,59 +2546,575 @@
 ));%0A
-%0A      os.write(%22%5Cn%22.getBytes());%0A    %7D os.close();
+      writeField(os, match.getKingdomKey());%0A      writeField(os, match.getPhylumKey());%0A      writeField(os, match.getClassKey());%0A      writeField(os, match.getOrderKey());%0A      writeField(os, match.getFamilyKey());%0A      writeField(os, match.getGenusKey());%0A      writeField(os, match.getUsageKey());%0A%0A      os.write(%22%5Cn%22.getBytes());%0A    %7D os.close();%0A  %7D%0A%0A  private static void writeField(FileOutputStream os, Integer key) throws IOException %7B%0A    if (key != null) %7B%0A      os.write(key.toString().getBytes(%22UTF-8%22));%0A      os.write(%22,%22.getBytes(%22UTF-8%22));%0A    %7D
 %0A  %7D
@@ -3266,32 +3266,39 @@
 quoted.getBytes(
+%22UTF-8%22
 ));%0A    os.write
@@ -3307,24 +3307,31 @@
 ,%22.getBytes(
+%22UTF-8%22
 ));%0A  %7D%0A%0A  p

add key cols