package cn.reghao.tnb.admin.service;

import cn.reghao.jutil.jdk.converter.DateTimeConverter;
import cn.reghao.tnb.admin.model.vo.EurekaInstance;
import cn.reghao.tnb.admin.model.vo.EurekaService;
//import com.netflix.discovery.EurekaClient;
//import com.netflix.discovery.shared.Applications;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author reghao
 * @date 2025-07-18 17:31:05
 */
@Service
public class TnbEurekaService {
    private final DiscoveryClient discoveryClient;
    //private final EurekaClient eurekaClient;

    public TnbEurekaService(DiscoveryClient discoveryClient/*, EurekaClient eurekaClient*/) {
        this.discoveryClient = discoveryClient;
        //this.eurekaClient = eurekaClient;
    }

    public List<EurekaInstance> getRegistryServices() {
        List<EurekaInstance> list = new ArrayList<>();
        /*Applications applications = eurekaClient.getApplications();
        applications.getRegisteredApplications().forEach(application -> {
            String name = application.getName();
            application.getInstances().forEach(instanceInfo -> {
                String instanceId = instanceInfo.getInstanceId();
                String appName = instanceInfo.getAppName().toLowerCase(Locale.ROOT);
                String ipAddr = instanceInfo.getIPAddr();
                int port = instanceInfo.getPort();
                String hostPort = String.format("%s:%s", ipAddr, port);
                long updateTime = instanceInfo.getLastUpdatedTimestamp();
                String updateTimeStr = DateTimeConverter.msTimestamp(updateTime);
                long dirtyTime = instanceInfo.getLastDirtyTimestamp();
                String dirtyTimeStr = DateTimeConverter.msTimestamp(dirtyTime);
                list.add(new EurekaInstance(instanceId, appName, hostPort, updateTimeStr, dirtyTimeStr));
            });
        });*/

        return list;
    }

    public List<EurekaService> getServiceList() {
        /*Map<String, List<EurekaInstance>> serviceMap = new HashMap<>();
        Applications applications = eurekaClient.getApplications();
        applications.getRegisteredApplications().forEach(application -> {
            String name = application.getName();
            List<EurekaInstance> serviceInstances = serviceMap.computeIfAbsent(name, v -> new ArrayList<>());
            application.getInstances().forEach(instanceInfo -> {
                String instanceId = instanceInfo.getInstanceId();
                String appName = instanceInfo.getAppName().toLowerCase(Locale.ROOT);
                String ipAddr = instanceInfo.getIPAddr();
                int port = instanceInfo.getPort();
                String hostPort = String.format("%s:%s", ipAddr, port);
                long updateTime = instanceInfo.getLastUpdatedTimestamp();
                String updateTimeStr = DateTimeConverter.msTimestamp(updateTime);
                long dirtyTime = instanceInfo.getLastDirtyTimestamp();
                String dirtyTimeStr = DateTimeConverter.msTimestamp(dirtyTime);
                serviceInstances.add(new EurekaInstance(instanceId, appName, hostPort, updateTimeStr, dirtyTimeStr));
            });
        });*/

        List<EurekaService> serviceList = new ArrayList<>();
        /*serviceMap.forEach((name, instanceList) -> {
            int total = instanceList.size();
            serviceList.add(new EurekaService(name, total, instanceList));
        });*/
        return serviceList;
    }

    public void getServices() {
        List<String> list = discoveryClient.getServices();
        //eurekaClient.getApplicationInfoManager();
    }
}
