package org.gbif.nub.build;

import org.gbif.api.model.checklistbank.NameUsage;
import org.gbif.api.model.checklistbank.ParsedName;
import org.gbif.api.model.common.paging.Pageable;
import org.gbif.api.model.common.paging.PagingResponse;
import org.gbif.checklistbank.model.Usage;
import org.gbif.checklistbank.service.ParsedNameService;
import org.gbif.checklistbank.service.UsageService;
import org.gbif.utils.file.InputStreamUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

/**
 * Test mock implementation of the batch service loading json files from the dataset test directory
 * to answer the list and listNames methods. Paging is entirely ignored and always the full list of
 * records is returned!
 */
public class JsonMockService implements UsageService, ParsedNameService {
  private InputStreamUtils isu = new InputStreamUtils();
  private final ObjectMapper mapper;

  public JsonMockService() {
    mapper = new ObjectMapper();
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  }

  @Override
  public List<Integer> listAll() {
    // TODO: Write implementation
    throw new UnsupportedOperationException("Not implemented yet");
  }

  @Override
  public Integer maxUsageKey(UUID datasetKey) {
    return 100000;
  }

  @Override
  public List<NameUsage> listRange(int usageKeyStart, int usageKeyEnd) {
    // TODO: Write implementation
    throw new UnsupportedOperationException("Not implemented yet");
  }

  @Override
  public List<Integer> listParents(int usageKey) {
    // TODO: Write implementation
    throw new UnsupportedOperationException("Not implemented yet");
  }

  @Override
  public Map<Integer, List<Integer>> listParentsRange(int usageKeyStart, int usageKeyEnd) {
    // TODO: Write implementation
    throw new UnsupportedOperationException("Not implemented yet");
  }

  @Override
  public PagingResponse<Usage> list(UUID datasetKey, Pageable page) {
    System.out.println("Read usages for " + datasetKey);
    InputStream json = usagesJson(datasetKey);
    TypeReference ref = new TypeReference<List<Usage>>() { };
    return list(json, ref, page);
  }

  @Override
  public ParsedName get(int key) {
    // TODO: Write implementation
    throw new UnsupportedOperationException("Not implemented yet");
  }

  @Override
  public ParsedName createOrGet(String scientificName) {
    // TODO: Write implementation
    throw new UnsupportedOperationException("Not implemented yet");
  }

  @Override
  public PagingResponse<ParsedName> listNames(UUID datasetKey, Pageable page) {
    System.out.println("Read names for " + datasetKey);
    InputStream json = namesJson(datasetKey);
    TypeReference ref = new TypeReference<List<ParsedName>>() { };
    return list(json, ref, page);
  }

  private <T> PagingResponse<T> list(InputStream json, TypeReference<List<T>> ref, Pageable page) {
    try {
      List<T> data = mapper.readValue(json, ref);
      json.close();
      PagingResponse<T> resp = new PagingResponse<T>(page, null, data);
      resp.setEndOfRecords(true);
      return resp;
    } catch (IOException e) {
      throw new IllegalStateException(e);
    }
  }

  private InputStream namesJson(UUID datasetKey) {
    String file = prefix(datasetKey) + "-names.json";
    return isu.classpathStream(file);
  }

  private InputStream usagesJson(UUID datasetKey) {
    String file = prefix(datasetKey) + "-usages.json";
    return isu.classpathStream(file);
  }

  private String prefix(UUID datasetKey) {
    return "datasets/"+datasetKey.toString();
  }

}

@@ -512,30 +512,8 @@
 st;%0A
-import java.util.Map;%0A
 impo
@@ -1841,219 +1841,8 @@
  %7D%0A%0A
-  @Override%0A  public Map%3CInteger, List%3CInteger%3E%3E listParentsRange(int usageKeyStart, int usageKeyEnd) %7B%0A    // TODO: Write implementation%0A    throw new UnsupportedOperationException(%22Not implemented yet%22);%0A  %7D%0A%0A
   @O

Remove removed method