package org.elinker.core.rest;

import com.google.common.base.Strings;
import eu.freme.common.exception.BadRequestException;
import eu.freme.common.persistence.dao.DatasetMetadataDAO;
import eu.freme.common.persistence.repository.DatasetMetadataRepository;
import eu.freme.common.rest.NIFParameterSet;
import eu.freme.common.rest.OwnedResourceManagingController;
import eu.freme.common.persistence.model.DatasetMetadata;
import org.apache.log4j.Logger;
import org.elinker.core.api.scala.FremeNer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created by Arne Binder (arne.b.binder@gmail.com) on 08.04.2016.
 */
@RestController
@RequestMapping("/e-entity/freme-ner/datasets")
public class FremeNerManageDatasets extends OwnedResourceManagingController<DatasetMetadata>{

    @Value("${freme.ner.languages}")
    String languages = "";

    Set<String> SUPPORTED_LANGUAGES;

    Logger logger = Logger.getLogger(FremeNerManageDatasets.class);

    @PostConstruct
    public void init() {
        SUPPORTED_LANGUAGES = new HashSet<>();
        for (String lang : languages.split(",")) {
            SUPPORTED_LANGUAGES.add(lang);
        }
    }

    @Autowired
    org.elinker.core.api.java.FremeNer fremeNer;

    @Override
    protected DatasetMetadata createEntity(String body, Map<String, String> parameters, Map<String, String> headers) throws BadRequestException {

        String name = parameters.get(DatasetMetadata.getIdentifierName());
        if(Strings.isNullOrEmpty(name))
            throw new BadRequestException("No name provided! Please set the parameter \""+ DatasetMetadata.getIdentifierName() +"\" to a valid value.");
        DatasetMetadata metadata = new DatasetMetadata(name);

        updateEntity(metadata, body, parameters, headers);

        return metadata;
    }

    @Override
    protected void updateEntity(DatasetMetadata datasetMetadata, String body, Map<String, String> parameters, Map<String, String> headers) throws BadRequestException {


        String language = parameters.get("language");
        if (language != null) {
            if (!SUPPORTED_LANGUAGES.contains(language)) {
                // The language specified with the langauge parameter is not
                // supported.
                throw new BadRequestException("Unsupported language.");
            }
        }

        String endpoint = parameters.get("endpoint");
        String sparql = parameters.get("sparql");
        // first check if user wants to submit data via SPARQL
        if (endpoint != null && sparql == null) {
            // endpoint specified, but not sparql => throw exception
            throw new BadRequestException(
                    "SPARQL endpoint was specified but the SPARQL query is empty.");
        }

        NIFParameterSet nifParameters = this.normalizeNif(body,
                headers.get("accept"), headers.get("content-type"), parameters, false);

        String format = null;
        switch (nifParameters.getInformat()) {
            case TURTLE:
                format = "TTL";
                break;
            case JSON_LD:
                format = "JSON-LD";
                break;
            case RDF_XML:
                format = "RDF/XML";
                break;
            case N_TRIPLES:
                format = "N-TRIPLES";
                break;
            case N3:
                format = "N3";
                break;
            default:
                throw new BadRequestException("Bad input format "
                        + nifParameters.getInformat());
        }

        FremeNer.InputType inputType;

        if (endpoint != null) {
            // input from SPARQL
            inputType = new FremeNer.SparqlInput(sparql, endpoint);
        } else {
            // text input
            inputType = new FremeNer.TextInput(nifParameters.getInput());
        }

        long totalEntities = fremeNer.addToDataset(datasetMetadata.getName(), inputType, format, language,
                new String[] {});
        datasetMetadata.setTotalEntities(totalEntities);
    }

    @Override
    protected void preDelete(DatasetMetadata entity){
        fremeNer.deleteDataset(entity.getName());
    }
}

@@ -2289,17 +2289,16 @@
 tion %7B%0A%0A
-%0A
         
@@ -3211,12 +3211,11 @@
 rs, 
-fals
+tru
 e);%0A
@@ -3899,16 +3899,45 @@
 tType;%0A%0A
+        // input from SPARQL%0A
         
@@ -3964,41 +3964,8 @@
 ) %7B%0A
-            // input from SPARQL%0A
     
@@ -4028,24 +4028,25 @@
 point);%0A
+%0A
         
 %7D else %7B
@@ -4041,115 +4041,262 @@
     
-%7D else %7B%0A            // text input%0A            inputType = new FremeNer.TextInput(nifParameters.getInput())
+// text input%0A        %7D else if(!Strings.isNullOrEmpty(nifParameters.getInput())) %7B%0A            inputType = new FremeNer.TextInput(nifParameters.getInput());%0A%0A        // empty (just update metadata: owner and visibility)%0A        %7Delse%7B%0A            return
 ;%0A  

allow empty input when updating (change only owner or visibility or via sparql) or creating (via sparql)