/*
 * European Variation Archive (EVA) - Open-access database of all types of genetic
 * variation data from all species
 *
 * Copyright 2019 EMBL - European Bioinformatics Institute
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.ac.ebi.eva.server.ws;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import uk.ac.ebi.eva.commons.core.models.pipeline.Variant;
import uk.ac.ebi.eva.commons.core.models.ws.VariantWithSamplesAndAnnotation;
import uk.ac.ebi.eva.commons.mongodb.services.AnnotationMetadataNotFoundException;
import uk.ac.ebi.eva.commons.mongodb.services.VariantWithSamplesAndAnnotationsService;
import uk.ac.ebi.eva.lib.eva_utils.DBAdaptorConnector;
import uk.ac.ebi.eva.lib.eva_utils.MultiMongoDbFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping(value = "/v2/identifiers")
@Api(tags = "identifier")
public class IdentifierWSServerV2 {

    @Autowired
    private VariantWithSamplesAndAnnotationsService service;

    @GetMapping(value = "/{identifier}")
    public ResponseEntity getVariants(
            @ApiParam(value = "Numerical identifier of the field `ids` of a variant, e.g.: rs55880202", required = true)
            @PathVariable("identifier") String identifier,
            @ApiParam(value = "species can be looked up in /v1/meta/species/list/ in the field named taxonomyCode, " +
                    "e.g.: " + "hsapiens", required = true) @RequestParam(name = "species") String species,
            @ApiParam(value = "assemblies can be looked up in /v1/meta/species/list/ in the field named assemblyName," +
                    " e.g.: grch37", required = true) @RequestParam(name = "assembly") String assembly) throws
            AnnotationMetadataNotFoundException, IllegalArgumentException {
        checkParameters(species, assembly);

        MultiMongoDbFactory.setDatabaseNameForCurrentThread(DBAdaptorConnector.getDBName(species + "_" + assembly));
        List<VariantWithSamplesAndAnnotation> variantEntities = service.findByIdsAndComplexFilters(Arrays.asList
                (identifier), null, null, null, null);

        List<Variant> coreVariantInfo = new ArrayList<>();
        variantEntities.forEach(variantEntity -> {
            Variant variant = new Variant(variantEntity.getChromosome(), variantEntity.getStart(), variantEntity
                    .getEnd(), variantEntity.getReference(), variantEntity.getAlternate());
            variant.setIds(variantEntity.getIds());
            coreVariantInfo.add(variant);

        });
        if (coreVariantInfo.size() > 0) {
            return new ResponseEntity<>(coreVariantInfo, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(coreVariantInfo, HttpStatus.NOT_FOUND);
        }
    }

    private void checkParameters(String species, String assembly) throws IllegalArgumentException {
        if (species.isEmpty()) {
            throw new IllegalArgumentException("Please specify a species");
        }
        if (assembly.isEmpty()) {
            throw new IllegalArgumentException("Please specify an assembly");
        }
    }
}

@@ -2117,17 +2117,16 @@
  = %22
-Numerical
+RS or SS
  ide
@@ -2136,27 +2136,8 @@
 fier
- of the field %60ids%60
  of 
@@ -2182,28 +2182,16 @@
  = true)
-%0A           
  @PathVa
@@ -2200,22 +2200,28 @@
 able
-(%22identifier%22)
+%0A                   
  Str
@@ -2267,21 +2267,28 @@
 alue = %22
-speci
+Allowed valu
 es can b
@@ -2348,25 +2348,9 @@
 med 
-taxonomyCode, %22 +
+%22
 %0A   
@@ -2370,20 +2370,41 @@
     
-%22
+          + %22taxonomyCode, 
 e.g.: 
-%22 + %22
 hsap
@@ -2444,26 +2444,8 @@
 aram
-(name = %22species%22)
  Str
@@ -2492,16 +2492,20 @@
  = %22
-assembli
+Allowed valu
 es c
@@ -2569,24 +2569,9 @@
 med 
-assemblyName,%22 +
+%22
 %0A   
@@ -2587,17 +2587,42 @@
         
-%22
+          + %22assemblyCode,
  e.g.: g
@@ -2663,27 +2663,8 @@
 aram
-(name = %22assembly%22)
  Str
@@ -2680,16 +2680,10 @@
 bly)
- throws
 %0A
+ 
     
@@ -2689,16 +2689,22 @@
         
+throws
  Annotat

reword /v2/identifiers/ swagger descriptions