package org.cbioportal.pdb_annotation.web.controllers;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.cbioportal.pdb_annotation.web.domain.AlignmentRepository;
import org.cbioportal.pdb_annotation.web.domain.EnsemblRepository;
import org.cbioportal.pdb_annotation.web.domain.PdbRepository;
import org.cbioportal.pdb_annotation.web.models.Alignment;
import org.cbioportal.pdb_annotation.web.models.Ensembl;
import org.cbioportal.pdb_annotation.web.models.Residue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * Controller of the main API: AlignmentController
 *
 * @author Juexin Wang
 *
 */
@RestController // shorthand for @Controller, @ResponseBody
@CrossOrigin(origins = "*") // allow all cross-domain requests
@RequestMapping(value = "/pdb_annotation/")
public class AlignmentController {
    @Autowired
    private AlignmentRepository alignmentRepository;
    @Autowired
    private EnsemblRepository ensemblRepository;
    @Autowired
    private PdbRepository pdbRepository;

    @ApiOperation(value = "get pdb alignments by ensemblId", nickname = "getPdbAlignmentByEnsemblId")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Success", response = Alignment.class, responseContainer = "List"),
            @ApiResponse(code = 400, message = "Bad Request") })
    @RequestMapping(value = "/StructureMappingQuery", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public List<Alignment> getPdbAlignmentByEnsemblId (
            @RequestParam @ApiParam(value = "Input ensembl id. For example ENSP00000483207.2", required = true, allowMultiple = true) String ensemblId) {
        return alignmentRepository.findByEnsemblId(ensemblId);
    }

    @ApiOperation(value = "get whether ensemblId exists by ensemblId", nickname = "getExistedEnsemblidinAlignment")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Success", response = Alignment.class, responseContainer = "boolean"),
            @ApiResponse(code = 400, message = "Bad Request") })
    @RequestMapping(value = "/ProteinIdentifierRecognitionQuery", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public boolean getExistedEnsemblidinAlignment(
            @RequestParam @ApiParam(value = "Input ensembl id. For example ENSP00000483207.2", required = true, allowMultiple = true) String ensemblId) {
        return ensemblRepository.findByEnsemblId(ensemblId).size() != 0;
    }
    
    
    @ApiOperation(value = "get residue mapping from pdb alignments by ensemblId and Amino Acid Number", nickname = "getPdbResidueByEnsemblId")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Success", response = Alignment.class, responseContainer = "List"),
            @ApiResponse(code = 400, message = "Bad Request") })
    @RequestMapping(value = "/ResidueMappingQuery", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public List<Residue> getPdbResidueByEnsemblId (
            @RequestParam
            @ApiParam(value = "Input ensembl id and Amio Acid Number. For example http://localhost:8080/pdb_annotation/ResidueMappingQuery?ensemblId=ENSP00000483207.2&aaNumber=117", required = true, allowMultiple = true) Map<String, String> input ) {
        String ensemblId = input.get("ensemblId"); 
        String aaNumber = input.get("aaNumber");
        List<Alignment> it = alignmentRepository.findByEnsemblId(ensemblId);
        List<Residue> outit = new ArrayList<Residue> ();
        int inputAA = Integer.parseInt(aaNumber);
        for(Alignment ali:it){
            if(inputAA>=ali.getEnsemblFrom() && inputAA<=ali.getEnsemblTo()){
                Residue re = new Residue(); 
                re.setAlignmentId(ali.getAlignmentId());
                re.setBitscore(ali.getBitscore());
                re.setChain(ali.getChain());
                re.setEnsemblAlign(ali.getEnsemblAlign());
                re.setEnsemblFrom(ali.getEnsemblFrom());
                re.setEnsemblId(ali.getEnsemblId());
                re.setEnsemblTo(ali.getEnsemblTo());
                re.setEvalue(ali.getEvalue());
                re.setIdentity(ali.getIdentity());
                re.setIdentp(ali.getIdentp());
                re.setMidlineAlign(ali.getMidlineAlign());
                re.setPdbAlign(ali.getPdbAlign());
                re.setPdbFrom(ali.getPdbFrom());
                re.setPdbId(ali.getPdbId());
                re.setPdbNo(ali.getPdbNo());
                re.setPdbSeg(ali.getPdbSeg());
                re.setPdbTo(ali.getPdbTo());
                re.setUpdateDate(ali.getUpdateDate());                              
                re.setResidueName(ali.getPdbAlign().substring(inputAA-ali.getEnsemblFrom(), inputAA-ali.getEnsemblFrom()+1));
                re.setResidueNum(ali.getPdbFrom()+(inputAA-ali.getEnsemblFrom())); 
                outit.add(re);
            }
        }
        return outit;
    }
    
 
}

@@ -3836,16 +3836,33 @@
 estParam
+(required = true)
 %0A       
@@ -3905,110 +3905,19 @@
 l id
- and Amio Acid Number. For example http://localhost:8080/pdb_annotation/ResidueMappingQuery?ensemblId=
+. Example: 
 ENSP
@@ -3933,21 +3933,8 @@
 07.2
-&aaNumber=117
 %22, r
@@ -3975,135 +3975,194 @@
 ue) 
-Map%3C
 String
-, String%3E input ) %7B%0A        String ensemblId = input.get(%22ensemblId%22); %0A        String aaNumber = input.get(%22
+ ensemblId, %0A            @RequestParam(required = true)%0A            @ApiParam(value = %22Input Amio Acid Number. Example: 117%22, required = true, allowMultiple = true) String 
 aaNumber
 %22);%0A
@@ -4161,11 +4161,11 @@
 mber
-%22);
+) %7B
 %0A   

Solve A small bug of two variables in Swagger