package com.judong.chuanyiserver.util;

import com.judong.chuanyiserver.enums.ResultEnum;
import com.judong.chuanyiserver.exception.CustomException;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig;
import org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider;
import org.eclipse.milo.opcua.stack.client.DiscoveryClient;
import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
import org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned;
import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;

public class Test {

    private final CompletableFuture<OpcUaClient> future = new CompletableFuture<>();
    private static Logger logger = LoggerFactory.getLogger(Test.class);
    private final static String endPointUrl = "opc.tcp://192.168.0.138:37800";
    private static AtomicInteger atomic = new AtomicInteger(1);
    private final static String certPath = "C:/Users/Administrator/Desktop/";

    public static void main(String[] args) throws Exception {

    }

//    private OpcUaClient createClient() throws Exception {
//
//        Path securityTempDir = Paths.get(certPath, "security");
//
//        Files.createDirectories(securityTempDir);
//        if (!Files.exists(securityTempDir)) {
//            throw new CustomException(ResultEnum.SERVER_ERROR.getRespCode(), "无法创建安全目录:" + securityTempDir);
//        }
//
//        KeyStoreLoader loader = KeyStoreLoader.load(securityTempDir);
//
//        // 搜索OPC节点
//        List<EndpointDescription> endpoints = null;
//        try {
//            endpoints = DiscoveryClient.getEndpoints(properties.getEndpointUrl()).get();
//        } catch (Throwable e) {
//            // try the explicit discovery endpoint as well
//            String discoveryUrl = properties.getEndpointUrl();
//
//            if (!discoveryUrl.endsWith("/")) {
//                discoveryUrl += "/";
//            }
//            discoveryUrl += "discovery";
//
//            log.info("Trying explicit discovery URL: {}", discoveryUrl);
//            endpoints = DiscoveryClient.getEndpoints(discoveryUrl).get();
//        }
//
//        EndpointDescription endpoint = endpoints.stream()
//                .filter(e -> e.getSecurityPolicyUri().equals(SecurityPolicy.None.getUri())).filter(endpointFilter())
//                .findFirst().orElseThrow(() -> new Exception("no desired endpoints returned"));
//
//        OpcUaClientConfig config = OpcUaClientConfig.builder()
//                .setApplicationName(LocalizedText.english("my"))
//                .setApplicationUri("urn:Jellyleo:UnifiedAutomation:UaExpert@Jellyleo")
//                .setCertificate(loader.getClientCertificate()).setKeyPair(loader.getClientKeyPair())
//                .setEndpoint(endpoint)
////				.setIdentityProvider(new UsernameProvider("jellyleo", "123456"))
//                .setIdentityProvider(new AnonymousProvider()) // 匿名验证
//                .setRequestTimeout(Unsigned.uint(5000)).build();
//
//        return OpcUaClient.create(config);
//    }
//
//    public static void xxx() {
//        OpcUaClient opcUaClient = null;
//        if (Blank.isEmpty(opcUaClient)) {
//            throw new CustomException(ResultEnum.SERVER_ERROR.getRespCode(), ResultEnum.SERVER_ERROR.getRespMsg());
//        }
//    }


}
