package com.microservices.mon.k8s.domain;

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.vo.RancherNamespaceVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;


@Data
@ApiModel("命名空间")
public class K8sNamespace {
    @ApiModelProperty(value = "命名空间标识")
    private String id;

    @ApiModelProperty(value = "命名空间键值")
    private String key;

    @ApiModelProperty(value = "命名空间名称")
    private String name;

    @ApiModelProperty(value = "命名空间别名（中文）")
    private String alias;

    @ApiModelProperty(value = "应用列表")
    private List<K8sApplication> applicationList;

    public static K8sNamespace jsonObjectToK8sNamespace(JSONObject namespaceJson, boolean isNative) {
        K8sNamespace target = new K8sNamespace();
        String key = null;
        String alias = null;
        if (isNative) {
            JSONObject metadata = namespaceJson.getJSONObject("metadata");
            target.setId(metadata.getString("name"));
            JSONObject labelJson = metadata.getJSONObject("labels");
            if (labelJson != null) {
                key = labelJson.getString("mon-key");
            }

            JSONObject annotationJson = metadata.getJSONObject("annotations");
            if (annotationJson != null) {
                alias = annotationJson.getString("mon-name");
            }
            target.setName(metadata.getString("name"));
        } else {
            RancherNamespaceVo rancherNamespaceVo = namespaceJson.toJavaObject(RancherNamespaceVo.class);
            BeanUtils.copyProperties(rancherNamespaceVo, target);
            if (rancherNamespaceVo.getLabels() != null) {
                key = rancherNamespaceVo.getLabels().getString("mon-key");
            }
            if (rancherNamespaceVo.getAnnotations() != null) {
                alias = rancherNamespaceVo.getAnnotations().getString("mon-name");
            }


        }
        if (StringUtils.isNotEmpty(key)) {
            target.setKey(key);
        }
        if (StringUtils.isNotEmpty(alias)) {
            target.setAlias(alias);
        }
        return target;
    }
}
