package cn.axzo.msg.center.service.pending.response.v3.model;

import cn.axzo.msg.center.service.domain.DetailConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

import javax.annotation.Nullable;
import java.util.List;

/**
 * @author yanglin
 */
@Setter
@Getter
public class ParsedGroupInfo {

    /**
     * key/value的列表
     */
    private List<ParsedKV> keyValues;

    /**
     * 详情的配置. 数据库存的配置名称叫detail
     */
    @JsonIgnore
    private DetailConfig detail;

    /**
     * 详情信息
     */
    @Nullable @JsonInclude(JsonInclude.Include.NON_NULL)
    private ParsedGroupDetail parsedDetail;

    @JsonIgnore @JSONField(serialize = false)
    public ParsedGroupDetail getOrCreateDetail() {
        if (parsedDetail == null)
            parsedDetail = new ParsedGroupDetail();
        return parsedDetail;
    }

    public void removeEmptyKValues() {
        if (keyValues == null) return;
        keyValues.removeIf(ParsedKV::isValueAbsent);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
