package uk.ac.ebi.quickgo.annotation.download.converter;

import uk.ac.ebi.quickgo.annotation.model.Annotation;

import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * This class contains methods that are shared between the AnnotationToX converters.
 * @author Tony Wardell
 * Date: 20/01/2017
 * Time: 17:01
 * Created with IntelliJ IDEA.
 */
abstract class AnnotationTo {
    private static final String ID_DELIMITER = ":";
    private static final String COMMA = ",";
    private static final String PIPE = "|";
    private static final DateTimeFormatter YYYYMMDD_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyyMMdd");
    static final String OUTPUT_DELIMITER = "\t";

    String withFromAsString(List<Annotation.ConnectedXRefs<Annotation.SimpleXRef>> connectedXRefs) {
        if (connectedXRefs == null || connectedXRefs.isEmpty()) {
            return "";
        }
        return connectedXRefs.stream()
                .map(this::simpleRefAndToString)
                .collect(Collectors.joining(PIPE));
    }

    private String simpleRefAndToString(Annotation.ConnectedXRefs<Annotation.SimpleXRef> itemList) {
        return itemList.getConnectedXrefs()
                .stream()
                .map(Annotation.SimpleXRef::asXref)
                .collect(Collectors.joining(COMMA));
    }

    String extensionsAsString(List<Annotation.ConnectedXRefs<Annotation.QualifiedXref>> connectedXRefs) {
        if (connectedXRefs == null || connectedXRefs.isEmpty()) {
            return "";
        }
        return connectedXRefs.stream()
                .map(this::qualifiedRefAndToString)
                .collect(Collectors.joining(PIPE));
    }

    private String qualifiedRefAndToString(Annotation.ConnectedXRefs<Annotation.QualifiedXref> itemList) {
        return itemList.getConnectedXrefs()
                .stream()
                .map(Annotation.QualifiedXref::asXref)
                .collect(Collectors.joining(COMMA));
    }

    String[] idToComponents(String id) {
        return id == null ? new String[]{"", ""} : id.split(ID_DELIMITER);
    }

    String toYMD(Date date) {
        return date == null ?
                "" : YYYYMMDD_DATE_FORMAT.format(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
    }

    final Function<String, String> nullToEmptyString = s -> s == null ? "" : s;
}

@@ -291,16 +291,73 @@
 lectors;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0A/**%0A *
@@ -592,33 +592,155 @@
 nal 
-String ID_DELIMITER = %22:%22
+Logger LOGGER = LoggerFactory.getLogger(AnnotationTo.class);%0A    private static final String ID_DELIMITER = %22:%22;%0A    public static final int DB = 0
 ;%0A  
@@ -2642,11 +2642,423 @@
  %22%22 : s;
+%0A%0A    protected String toGeneProductType(String db) %7B%0A        switch (db) %7B%0A            case %22UniProtKB%22:%0A                return %22protein%22;%0A            case %22IntAct%22:%0A                return %22complex%22;%0A            case %22RNAcentral%22:%0A                return %22miRNA%22;%0A            default:%0A                LOGGER.error(%22Cannot determine gene product type for based on DB of %22 + db);%0A        %7D%0A        return %22%22;%0A    %7D
 %0A%7D%0A

Make toGeneProductType available to sub classes.