package cn.reghao.tnb.admin.service;

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 cn.reghao.tnb.admin.config.AppProperties;
import cn.reghao.tnb.admin.model.dto.RouteAdd;
import cn.reghao.tnb.admin.model.dto.RouteDelete;
import cn.reghao.tnb.admin.model.vo.RouteDefinitionInfo;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * @author reghao
 * @date 2024-11-22 13:46:46
 */
@Slf4j
@Service
public class RouteService {
    private final WebRequest webRequest;
    private final String host;

    public RouteService(AppProperties appProperties) {
        this.host = appProperties.getGatewayAdminUrl();
        this.webRequest = new WebClient();
    }

    public void addRoute() {
        RouteAdd routeAdd = new RouteAdd();
        routeAdd.setRouteId("test-route");

        String url = String.format("%s/route", host);
        WebResponse webResponse = webRequest.postJson(url, JsonConverter.objectToJson(routeAdd));
        String data = getWebResponse(webResponse);
        if (data != null) {
            System.out.println(data);
        }
    }

    public void updateRoute() {
        String upstreamUrl = "lb://auth-service";
        RouteAdd routeAdd = new RouteAdd();
        routeAdd.setRouteId("test-route");
        routeAdd.setUpstreamUrl(upstreamUrl);

        String url = String.format("%s/route/update", host);
        WebResponse webResponse = webRequest.postJson(url, JsonConverter.objectToJson(routeAdd));
        String data = getWebResponse(webResponse);
        if (data != null) {
            System.out.println(data);
        }
    }

    public void deleteRoute() {
        RouteDelete routeDelete = new RouteDelete();
        routeDelete.setRouteId("test-route");

        String url = String.format("%s/route/delete", host);
        WebResponse webResponse = webRequest.postJson(url, JsonConverter.objectToJson(routeDelete));
        String data = getWebResponse(webResponse);
        if (data != null) {
            System.out.println(data);
        }
    }

    public List<RouteDefinitionInfo> getRouteInfos() {
        String url = String.format("%s/routes", host);
        WebResponse webResponse = webRequest.get(url);
        String data = getWebResponse(webResponse);
        List<RouteDefinitionInfo> list = new ArrayList<>();
        if (data != null) {
            JsonArray jsonArray = JsonConverter.jsonToJsonElement(data).getAsJsonArray();
            for (JsonElement jsonElement : jsonArray) {
                JsonObject jsonObject = jsonElement.getAsJsonObject();
                String id = jsonObject.get("id").getAsString();
                String uri = jsonObject.get("uri").getAsString();
                String path = "";
                JsonArray predicates = jsonObject.get("predicates").getAsJsonArray();
                for (JsonElement predicate : predicates) {
                    JsonObject predicateObject = predicate.getAsJsonObject();
                    String name = predicateObject.get("name").getAsString();
                    if ("Path".equals(name)) {
                        JsonObject pathObject = predicateObject.get("args").getAsJsonObject();
                        path = pathObject.get("_genkey_0").getAsString();
                    }
                }
                list.add(new RouteDefinitionInfo(id, uri, path));
            }
        }

        return list;
    }

    public RouteDefinitionInfo getRouteInfo(String routeId) {
        String url = String.format("%s/route?id=%s", host, routeId);
        WebResponse webResponse = webRequest.get(url);
        String data = getWebResponse(webResponse);
        if (data != null) {
        }

        return null;
    }

    private String getWebResponse(WebResponse webResponse) {
        int statusCode = webResponse.getStatusCode();
        if (statusCode != 200) {
            log.error("请求失败");
            return null;
        }

        String body = webResponse.getBody();
        return body;

        /*Type type = new TypeToken<WebResult<String>>(){}.getType();
        WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
        if (webResult.getCode() != 0) {
            String errMsg = webResult.getMsg();
            log.error("请求失败 -> {}", errMsg);
            return null;
        }

        return webResult.getData();*/
    }
}
