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

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.context.SecurityContextHolder;
import com.microservices.common.core.utils.DateUtils;
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.K8sApplicationAndPodState;
import com.microservices.mon.k8s.domain.K8sApplicationPod;
import com.microservices.mon.k8s.utils.MonUtils;
import lombok.Data;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class RancherApplicationVo {
    // 标识
    String id;
    // 应用类型
    String kind;
    // 元数据
    RancherMetadataVo metadata;
    // 状态
    RancherApplicationStateVo status;
    // 规格
    JSONObject spec;


    public K8sApplication toK8sApplication() {
        K8sApplication target = new K8sApplication();
        BeanUtils.copyProperties(this, target);

        if ("DaemonSet".equals(kind)) {
            target.setReplicas(status.getDesiredNumberScheduled());
            target.setReadyReplicas(status.getNumberReady());
        } else if ("StatefulSet".equals(kind)) {
            target.setReplicas(status.getReplicas());
            target.setReadyReplicas(status.getAvailableReplicas());
        } else {
            target.setReplicas(status.getReplicas());
            target.setReadyReplicas(status.getReadyReplicas());
        }

        if (spec != null) {
            JSONObject templateObj = spec.getJSONObject("template");
            if (templateObj != null) {
                JSONObject specObj = templateObj.getJSONObject("spec");
                if (specObj != null) {
                    JSONArray containersArray = specObj.getJSONArray("containers");
                    if (containersArray != null && !containersArray.isEmpty()) {
                        JSONObject firstContainer = containersArray.getJSONObject(0);
                        if (firstContainer != null && firstContainer.containsKey("image")) {
                            target.setDockerImage(firstContainer.getString("image"));
                        }
                    }
                }
            }
            if (spec.containsKey("paused")) {
                target.setPaused(spec.getBoolean("paused"));
            } else {
                target.setPaused(false);
            }
        }

        if (metadata != null) {
            target.setName(metadata.getName());
            target.setAlias(metadata.getName());
            if (metadata.getAnnotations() != null && metadata.getAnnotations().containsKey("mon-name")) {
                target.setAlias(metadata.getAnnotations().getString("mon-name"));
            }

            if (metadata.getState() != null) {
                target.setState(metadata.getState().toK8sApplicationState());
            }
            target.setSurvivalTime(
                    MonUtils.getTimeDifferenceDescription(metadata.getCreationTimestamp())
            );
            target.setNamespace(metadata.getNamespace());

            JSONArray relationships = metadata.getRelationships();
            for (int i = 0; i < relationships.size(); i++) {
                JSONObject relationship = relationships.getJSONObject(i);
                if (relationship.containsKey("toType") && "pod".equals(relationship.getString("toType"))) {
                    String selector = relationship.getString("selector");
                    if (StringUtils.isNotEmpty(selector) && selector.contains("=") && selector.split("=").length >= 2) {
                        target.setPodSelector(selector);
                    }

                }
            }
        }

        if(target.getReplicas()==null){
            target.setReplicas(0);
        }
        if(target.getReadyReplicas()==null){
            target.setReadyReplicas(0);
        }

        if (target.getReplicas() == 0) {
            target.setState(K8sApplicationAndPodState.k8sApplicationStopState("stop"));
        }
        if(target.getPaused()){
            target.setState(K8sApplicationAndPodState.k8sApplicationStopState("paused"));
        }

        return target;
    }
}
