package uk.ac.ebi.biosamples.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jsunsoft.http.HttpRequest;
import com.jsunsoft.http.ResponseDeserializer;
import com.jsunsoft.http.ResponseHandler;
import org.springframework.stereotype.Service;
import uk.ac.ebi.biosamples.model.ENAHtsgetTicket;
import com.jsunsoft.http.HttpRequestBuilder;

import java.io.IOException;
import java.util.Iterator;

@Service
public class ENAHtsgetService {

    private final String host = "http://localhost:8080/ga4gh/sample"; //TODO change host to real

    public ENAHtsgetTicket getTicket(String accession, String format) {

        HttpRequest<String> request = HttpRequestBuilder.
                createGet(String.format("%s/%s?format=%s", host, accession, format), String.class)
                .responseDeserializer(ResponseDeserializer.ignorableDeserializer())
                .build();
        ResponseHandler<String> responseHandler = request.execute();
        String jsonResponse = responseHandler.get();
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = null;
        try {
            node = mapper.readTree(jsonResponse);
        } catch (IOException e) {
            e.printStackTrace();
        }
        node = node.get("htsget");

        ENAHtsgetTicket ticket = new ENAHtsgetTicket();
        ticket.setAccession(accession);
        ticket.setFormat(format);

        String md5 = node.get("md5Hash").asText();
        ticket.setMd5Hash(md5);

        JsonNode urls = node.get("urls");
        Iterator<JsonNode> urlsIterator = urls.elements();
        while (urlsIterator.hasNext()) {
            JsonNode currentUrl = urlsIterator.next();
            ticket.addFtpLink(currentUrl.get("url").asText());
        }
        return ticket;


    }


}

@@ -170,16 +170,61 @@
 equest;%0A
+import com.jsunsoft.http.HttpRequestBuilder;%0A
 import c
@@ -300,24 +300,59 @@
 nseHandler;%0A
+import org.apache.http.HttpStatus;%0A
 import org.s
@@ -440,53 +440,8 @@
 ket;
-%0Aimport com.jsunsoft.http.HttpRequestBuilder;
 %0A%0Aim
@@ -492,16 +492,43 @@
 terator;
+%0Aimport java.util.Optional;
 %0A%0A@Servi
@@ -672,16 +672,25 @@
  public 
+Optional%3C
 ENAHtsge
@@ -696,16 +696,17 @@
 etTicket
+%3E
  getTick
@@ -1076,24 +1076,164 @@
 .execute();%0A
+        Optional%3CENAHtsgetTicket%3E ticketOptional = Optional.empty();%0A        if (responseHandler.getStatusCode() == HttpStatus.SC_OK) %7B%0A    
         Stri
@@ -1281,16 +1281,20 @@
         
+    
 ObjectMa
@@ -1327,32 +1327,36 @@
 pper();%0A        
+  
+  
 JsonNode node = 
@@ -1373,14 +1373,22 @@
     
+    
 try %7B%0A
+    
     
@@ -1441,16 +1441,20 @@
         
+    
 %7D catch 
@@ -1483,16 +1483,20 @@
         
+    
 e.printS
@@ -1508,34 +1508,42 @@
 race();%0A        
+ 
-%7D%0A
+   %7D%0A    
         node = n
@@ -1570,16 +1570,20 @@
         
+  
+  
 ENAHtsge
@@ -1622,32 +1622,36 @@
 cket();%0A        
+    
 ticket.setAccess
@@ -1666,32 +1666,36 @@
 ssion);%0A        
+   
+ 
 ticket.setFormat
@@ -1701,24 +1701,28 @@
 t(format);%0A%0A
+    
         Stri
@@ -1756,24 +1756,28 @@
 ).asText();%0A
+    
         tick
@@ -1797,32 +1797,36 @@
 (md5);%0A%0A        
+    
 JsonNode urls = 
@@ -1843,16 +1843,20 @@
 urls%22);%0A
+    
         
@@ -1914,16 +1914,20 @@
         
+ 
+   
 while (u
@@ -1947,24 +1947,28 @@
 asNext()) %7B%0A
+    
             
@@ -2018,24 +2018,28 @@
             
+  
+  
 ticket.addFt
@@ -2077,16 +2077,88 @@
 ext());%0A
+            %7D%0A            ticketOptional = Optional.ofNullable(ticket);%0A
         
@@ -2180,16 +2180,24 @@
 n ticket
+Optional
 ;%0A%0A%0A    

change return type to Optional