package uk.gov.openregister;

import com.fasterxml.jackson.databind.JsonNode;
import play.libs.ws.WS;
import play.libs.ws.WSResponse;
import uk.gov.openregister.config.ApplicationConf;
import uk.gov.openregister.linking.Curie;
import uk.gov.openregister.linking.CurieResolver;
import uk.gov.openregister.model.Field;

import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

public class FieldProvider {
    public static final int TIMEOUT = 30000;

    public static List<Field> getFields(String registerName, BiFunction<Integer, String, Object> errorHandler) {
        CurieResolver curieResolver = new CurieResolver(ApplicationConf.getString("registers.service.template.url"));
        String rrUrl =  curieResolver.resolve(new Curie("register", registerName)) + ".json";
        WSResponse rr = WS.client().url(rrUrl).execute().get(TIMEOUT);

        if (rr.getStatus() == 200 ) {
            JsonNode rEntry = rr.asJson().get("entry");

            List<String> fieldNames = StreamUtils.asStream(rEntry.get("fields").elements()).map(JsonNode::textValue).collect(Collectors.toList());

            return fieldNames.stream().map(field -> {

                String frUrl = curieResolver.resolve(new Curie("field", field)) + ".json";
                WSResponse fr = WS.client().url(frUrl).execute().get(TIMEOUT);

                if (fr.getStatus() == 200) {
                    JsonNode fEntry = fr.asJson().get("entry");
                    return new Field(fEntry);
                } else {
                    errorHandler.apply(fr.getStatus(), frUrl);
                    return new Field("unknown");
                }

            }).collect(Collectors.toList());
        } else {
            errorHandler.apply(rr.getStatus(), rrUrl);
            return Collections.emptyList();
        }
    }
}

@@ -437,16 +437,48 @@
 lectors;
+%0Aimport java.util.stream.Stream;
 %0A%0Apublic
@@ -1050,20 +1050,22 @@
         
-List
+Stream
 %3CString%3E
@@ -1160,37 +1160,8 @@
 lue)
-.collect(Collectors.toList())
 ;%0A%0A 
@@ -1193,17 +1193,8 @@
 mes.
-stream().
 map(

remove unneeded List intermediate value