package devops;

import cn.reghao.jutil.jdk.http.WebClient;
import cn.reghao.jutil.jdk.http.WebRequest;
import cn.reghao.jutil.jdk.http.WebResponse;
import cn.reghao.jutil.jdk.serializer.JsonConverter;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ThirdpartyServiceTest {
    private final WebRequest webRequest = new WebClient();
    private final String host = "http://consul.iquizoo.cn";

    public List<ConsulNode> getNodes() {
        List<ConsulNode> nodes = new ArrayList<>();
        String api = String.format("%s/%s", host, "v1/catalog/nodes");
        WebResponse webResponse = webRequest.get(api);
        if (webResponse.getStatusCode() == 200) {
            JsonArray jsonArray = JsonConverter.jsonToJsonElement(webResponse.getBody()).getAsJsonArray();
            for (JsonElement jsonElement : jsonArray) {
                JsonObject jsonObject = jsonElement.getAsJsonObject();
                String dc = jsonObject.get("Datacenter").getAsString();
                String address = jsonObject.get("Address").getAsString();
                nodes.add(new ConsulNode(dc, address));
            }
        }
        return nodes;
    }

    public List<String> getServices() {
        String api = String.format("%s/%s", host, "v1/catalog/services");
        WebResponse webResponse = webRequest.get(api);
        if (webResponse.getStatusCode() == 200) {
            JsonObject jsonObject = JsonConverter.jsonToJsonElement(webResponse.getBody()).getAsJsonObject();
            return new ArrayList<>(jsonObject.keySet());
        }
        return Collections.emptyList();
    }

    public List<String> getCriticalServices() {
        String api = String.format("%s/%s", host, "v1/health/state/critical");
        WebResponse webResponse = webRequest.get(api);
        if (webResponse.getStatusCode() == 200) {
            JsonObject jsonObject = JsonConverter.jsonToJsonElement(webResponse.getBody()).getAsJsonObject();
            return new ArrayList<>(jsonObject.keySet());
        }
        return Collections.emptyList();
    }

    public void deregister(String service) throws URISyntaxException, IOException, InterruptedException {
        String url = String.format("%s/%s/%s", host, "v1/agent/service/deregister", service);
        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(url)).version(HttpClient.Version.HTTP_1_1);

        HttpRequest httpRequest = builder.PUT(HttpRequest.BodyPublishers.noBody()).build();
        HttpClient httpClient = HttpClient.newBuilder().build();
        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        int statusCode = httpResponse.statusCode();
        String body = httpResponse.body();
    }

    @Test
    public void consulTest() throws Exception {
        //List<ConsulNode> consulNodes = getNodes();

        //System.out.println(getServices());

        String service = "FileGrpcService_172_16_45_66_8104";
        service = "FileService-WebApi_172_16_45_66_8004";
        //deregister(service);
    }

    @AllArgsConstructor
    @Getter
    static class ConsulNode {
        private String dc;
        private String address;
    }
}