package cn.reghao.tnb.sb.aliyun;

import cn.reghao.jutil.jdk.serializer.JsonConverter;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.alidns20150109.AsyncClient;
import com.aliyun.sdk.service.alidns20150109.models.AddDomainRecordRequest;
import com.aliyun.sdk.service.alidns20150109.models.AddDomainRecordResponse;
import com.aliyun.sdk.service.alidns20150109.models.GetTxtRecordForVerifyRequest;
import com.aliyun.sdk.service.alidns20150109.models.GetTxtRecordForVerifyResponse;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author reghao
 * @date 2025-03-28 16:22:34
 */
public class AliyunDns {
    public static void addTxtRecord(String domain, String rr, String value) throws ExecutionException, InterruptedException {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                .accessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
                .build());

        // Endpoint 请参考 https://api.aliyun.com/product/Alidns
        String endpoint = "alidns.cn-chengdu.aliyuncs.com";
        String regionId = "cn-chengdu";
        AsyncClient client = AsyncClient.builder()
                .region(regionId)
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride(endpoint)
                )
                .build();

        /*String domain = "";
        String rr = "";
        String value = "";*/
        AddDomainRecordRequest addDomainRecordRequest = AddDomainRecordRequest.builder()
                .domainName(domain)
                .type("TXT")
                .rr(rr)
                .value(value)
                .build();

        // Asynchronously get the return value of the API request
        CompletableFuture<AddDomainRecordResponse> response = client.addDomainRecord(addDomainRecordRequest);
        // Synchronously get the return value of the API request
        AddDomainRecordResponse resp = response.get();
        String jsonData = new Gson().toJson(resp);
        JsonObject jsonObject = JsonConverter.jsonToObject(jsonData, JsonObject.class);
        JsonObject respBody = jsonObject.get("body").getAsJsonObject();
    }

    static void test() throws ExecutionException, InterruptedException {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                .accessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
                .build());

        // Endpoint 请参考 https://api.aliyun.com/product/Alidns
        String endpoint = "alidns.cn-chengdu.aliyuncs.com";
        String regionId = "cn-chengdu";
        AsyncClient client = AsyncClient.builder()
                .region(regionId)
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride(endpoint)
                )
                .build();

        GetTxtRecordForVerifyRequest getTxtRecordForVerifyRequest = GetTxtRecordForVerifyRequest.builder()
                .domainName("")
                .type("ADD_SUB_DOMAIN")
                .lang("en")
                .build();

        CompletableFuture<GetTxtRecordForVerifyResponse> response = client.getTxtRecordForVerify(getTxtRecordForVerifyRequest);
        GetTxtRecordForVerifyResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        client.close();
    }
}
