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

import cn.axzo.msg.center.service.enums.KVContentType;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

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

    /**
     * 内容的类型. TEXT: 文本, PHONE: 手机号, ATTACHMENT: 附件, PERSON_ID: 自然人ID
     */
    private KVContentType contentType;

    /**
     * 键: 项目
     */
    private String key;

    /**
     * 值
     */
    private String value;

    /**
     * 是否显示在卡片上
     */
    @JsonIgnore
    private boolean displayOnCard;

    @JsonIgnore
    public boolean isValueEmpty() {
        return StringUtils.isBlank(value) || "null".equalsIgnoreCase(value);
    }

    @JsonIgnore
    public Long getValueAsLong() {
        if (isValueEmpty())
            return null;
        if (!NumberUtils.isDigits(value))
            return null;
        return Long.parseLong(value);
    }

    // !! 额外信息

    /**
     * contentType=PERSON_ID时的人员信息
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private PersonInfo personInfo;

    /**
     * contentType=PHONE时的手机信息
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private PhoneInfo phoneInfo;

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