package cn.ibizlab.core.extensions.dto;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@Setter
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Accessors(chain = true)
public class VMConfig {
    private String headerName;
    private Integer width;

    public Integer getWidth() {
        if(width==null&&(!StringUtils.isEmpty(field)))
            return 100;
        return width;
    }

    @JSONField(name = "entity")
    @JsonProperty("entity")
    private String entity;
    @JSONField(name = "field")
    @JsonProperty("field")
    private String field;
    private String pinned;
    private Boolean hide;
    private String valueGetter;
    private String valueSetter;
    private String valueFormatter;
    private String valueParser;
    private List<VMConfig> children;

    @JsonIgnore
    @JSONField(serialize = false)
    public boolean isHide()
    {
        return hide!=null&&hide;
    }

    public void setChildren(List<VMConfig> children) {
        if(children!=null)
            children.forEach(item->{
                if(this.getLayer()!=null)
                    item.setLayer(this.getLayer()+1);
                item.setParent(this);
            });
        this.children = children;
    }
    @JsonIgnore
    @JSONField(serialize = false)
    private VMConfig parent;

    @JsonIgnore
    @JSONField(serialize = false)
    private Integer layer;
    @JsonIgnore
    @JSONField(serialize = false)
    public Integer getLayer() {
        if(layer==null&&parent==null)
            layer=1;
        return layer;
    }

    public Map<String, Set<String>> getBuilds(Map<String,Set<String>> builds)
    {
        if(builds == null)
        {
            builds = new LinkedHashMap<>();
        }

        if(!ObjectUtils.isEmpty(children))
        {
            for(VMConfig item:children)
                item.getBuilds(builds);
        }
        else if((!StringUtils.isEmpty(entity)) && (!StringUtils.isEmpty(field)))
        {
            if(builds.containsKey(entity))
            {
                builds.get(entity).add(field);
            }
            else
            {
                Set<String> metricIds = new LinkedHashSet<>();
                metricIds.add(field);
                builds.put(entity,metricIds);
            }
        }

        return builds;
    }

    public static Map<String, Set<String>> getBuilds(String vmConfig)
    {
        if(!StringUtils.isEmpty(vmConfig))
        {
            List<VMConfig> cols= JSONArray.parseArray(vmConfig,VMConfig.class);
            return getBuilds(cols);
        }
        return new LinkedHashMap<>();
    }

    public static Map<String, Set<String>> getBuilds(List<VMConfig> list)
    {
        Map<String,Set<String>> builds = new LinkedHashMap<>();
        list.forEach(item->{
            item.getBuilds(builds);
        });
        return builds;
    }


}
