package com.microservices.mon.k8s.domain.vo;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.common.core.utils.bean.BeanUtils;
import com.microservices.mon.k8s.domain.K8sApplication;
import com.microservices.mon.k8s.domain.K8sApplicationPod;
import com.microservices.mon.k8s.utils.MonUtils;
import lombok.Data;

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

@Data
public class RancherPodVo {
    // 标识
    String id;
    // 元数据
    RancherMetadataVo metadata;
    // 状态
    RancherPodStateVo status;
    // 状态
    JSONObject spec;

    public K8sApplicationPod toK8sApplicationPod() {
        K8sApplicationPod target = new K8sApplicationPod();
        BeanUtils.copyProperties(this, target);
        target.setIp(status.getPodIP());
        if (metadata != null) {
            target.setName(metadata.getName());
            if (metadata.getState() != null) {
                target.setState(metadata.getState().toK8sPodState());
            }
            if (metadata.getLabels() != null) {
                target.setStack(metadata.getLabels().getString("stack"));
            }
            target.setNamespace(metadata.getNamespace());
            target.setCreatedTime(metadata.getCreationTimestamp());
        }
        target.setIsRunning(false);
        if (status != null) {
            if (status.getContainerStatuses() != null && !status.getContainerStatuses().isEmpty()) {
                target.setRestartCount(status.getContainerStatuses().stream().mapToInt(x -> x.getInteger("restartCount")).sum());
                target.setReadyReplicas(Math.toIntExact(status.getContainerStatuses().stream().filter(x -> x.containsKey("ready") && x.getBoolean("ready")).count()));
                target.setReplicas(status.getContainerStatuses().size());
            }
            if (StringUtils.isNotEmpty(status.phase) && status.phase.equals("Running")) {
                target.setIsRunning(true);
            }
        }
        List<String> ports = new ArrayList<>();
        List<String> dockerImages = new ArrayList<>();
        if (spec != null) {
            if (spec.containsKey("containers")) {
                JSONArray containers = spec.getJSONArray("containers");
                for (int i = 0; i < containers.size(); i++) {
                    JSONObject container = containers.getJSONObject(i);
                    // 设置镜像
                    if (container.containsKey("image")) {
                        dockerImages.add(container.getString("image"));
                    }
                    // 设置端口
                    if (container.containsKey("ports")) {
                        JSONArray portsArray = container.getJSONArray("ports");
                        for (int j = 0; j < portsArray.size(); j++) {
                            JSONObject port = portsArray.getJSONObject(j);
                            String containerPort = port.getString("containerPort");
                            if (StringUtils.isNotEmpty(containerPort)) {
                                ports.add(containerPort);
                            }
                        }
                        break;
                    }
                }
            }
            if (spec.containsKey("serviceAccountName")) {
                target.setServiceAccountName(spec.getString("serviceAccountName"));
            }
        }
        target.setPorts(StringUtils.join(ports, ","));
        target.setDockerImage(StringUtils.join(dockerImages, ","));

        target.setSurvivalTime(
                MonUtils.getTimeDifferenceDescription(metadata.getCreationTimestamp())
        );
        return target;
    }
}
