package com.microservices.mon.k8s.utils;

import com.alibaba.cloud.commons.io.IOUtils;
import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.DateUtils;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.system.api.utils.FeignUtils;
import feign.Response;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;

@Slf4j
public class MonUtils {

    public static String getTimeDifferenceDescription(Date createTime) {
        if (createTime == null) {
            throw new IllegalArgumentException("时间参数不能为空");
        }
        Date now = DateUtils.getNowDate();
        long diffInMillis = now.getTime() - createTime.getTime();
        if (diffInMillis <= 0) {
            return "刚刚";
        }
        double seconds = (double) diffInMillis / 1000;
        if (seconds < 60) {
            return String.format("%.1f 秒", seconds);
        }
        double minutes = seconds / 60;
        if (minutes < 60) {
            return String.format("%.1f 分钟", minutes);
        }
        double hours = seconds / 3600;
        if (hours < 24) {
            // 保留一位小数
            return String.format("%.1f 小时", hours);
        }
        double days = hours / 24;
        return String.format("%.1f 天", days);
    }

    public static String getDockerImage(String dockerImage) {
        if (StringUtils.isEmpty(dockerImage)) {
            return "未知";
        }
        if (dockerImage.contains("cadvisor")) {
            return "ContainerProbe:1.0.0";
        } else if (dockerImage.contains("grafana")) {
            return "MonitoringStatus:1.0.0";
        } else if (dockerImage.contains("kube-state-metrics")) {
            return "K8SProbe:1.0.0";
        } else if (dockerImage.contains("nexus3")) {
            return "ProductStorage:1.0.0";
        } else if (dockerImage.contains("prometheus-nginxlog-exporter")) {
            return "NginxLogProbe:1.0.0";
        } else if (dockerImage.contains("node-exporter")) {
            return "ServerProbe:1.0.0";
        } else if (dockerImage.contains("nacos-server")) {
            return "RegistrationAndConfigurationCenter:1.0.0";
        } else if (dockerImage.contains("rancher")) {
            return "K8SManagement:1.0.0";
        } else if (dockerImage.contains("skywalking-oap-serve")) {
            return "LinkTracingOAP:1.0.0";
        } else if (dockerImage.contains("skywalking-ui")) {
            return "LinkTracingUI:1.0.0";
        } else if (dockerImage.contains("sentinel-dashboard")) {
            return "FlowControl:1.0.0";
        } else if (dockerImage.contains("prometheus")) {
            return "MonitorDataCollection:1.0.0";
        } else {
            return dockerImage;
        }
    }
}
