package uk.ac.ebi.spot.goci.service.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import uk.ac.ebi.spot.goci.model.SnpLookupJson;
import uk.ac.ebi.spot.goci.utils.RestUrlBuilder;

import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by Emma on 22/04/16.
 *
 * @author emma
 *         <p>
 *         Checks a SNP identifier is valid using standard Spring mechanism to consume a RESTful service
 */
@Service
public class SnpCheckingRestService {

    @NotNull @Value("${mapping.snp_lookup_endpoint}")
    private String endpoint;

    private final Logger log = LoggerFactory.getLogger(getClass());

    protected Logger getLog() {
        return log;
    }

    private RestUrlBuilder restUrlBuilder;

    @Autowired
    public SnpCheckingRestService(RestUrlBuilder restUrlBuilder) {
        this.restUrlBuilder = restUrlBuilder;
    }

    /**
     * Check gene name returns a response
     *
     * @param snp Snp identifier to check
     * @return Error message
     */
    public String checkSnpIdentifierIsValid(String snp) {

        String error = null;

        try {
            restUrlBuilder.getRestTemplate().getForObject(restUrlBuilder.createUrl(getEndpoint(), snp), String.class);
        }
        // The query returns a 400 error if response returns an error
        catch (Exception e) {
            error = "SNP identifier ".concat(snp).concat(" is not valid");
            getLog().error("Checking SNP identifier failed", e);
        }
        return error;
    }

    /**
     * Get the chromosome a SNP resides on
     *
     * @param snp Snp identifier to check
     * @return Set of all SNP chromosome names
     */
    public Set<String> getSnpLocations(String snp) {

        Set<String> snpChromosomeNames = new HashSet<>();
        SnpLookupJson snpLookupJson = new SnpLookupJson();
        try {
            snpLookupJson =
                    restUrlBuilder.getRestTemplate()
                            .getForObject(restUrlBuilder.createUrl(getEndpoint(), snp), SnpLookupJson.class);
            snpLookupJson.getMappings().forEach(snpMappingsJson -> {
                snpChromosomeNames.add(snpMappingsJson.getSeq_region_name());
            });
        }
        // The query returns a 400 error if response returns an error
        catch (Exception e) {
            getLog().error("Getting locations for SNP ".concat(snp).concat("failed"), e);
        }
        return snpChromosomeNames;
    }

    public String getEndpoint() {
        return endpoint;
    }
}
@@ -1296,16 +1296,47 @@
 = null;%0A
+        String response = null;
 %0A       
@@ -1345,32 +1345,43 @@
 ry %7B%0A           
+ response =
  restUrlBuilder.
@@ -1397,16 +1397,37 @@
 mplate()
+%0A                    
 .getForO
@@ -1484,32 +1484,172 @@
 , String.class);
+%0A%0A            if (response.contains(%22error%22)) %7B%0A                error = %22SNP identifier %22.concat(snp).concat(%22 is not valid%22);%0A            %7D
 %0A        %7D%0A     
@@ -1747,83 +1747,8 @@
 ) %7B%0A
-            error = %22SNP identifier %22.concat(snp).concat(%22 is not valid%22);%0A
     
@@ -1810,32 +1810,33 @@
 , e);%0A        %7D%0A
+%0A
         return e
@@ -2858,10 +2858,98 @@
 t;%0A    %7D
+%0A%0A    public void setEndpoint(String endpoint) %7B%0A        this.endpoint = endpoint;%0A    %7D
 %0A%7D

Update to method to check response contents