package cn.axzo.msg.center.service.dto;

import cn.axzo.msg.center.service.enums.IdentityTypeEnum;
import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author cold_blade
 * @version 1.0
 * @description
 * @date 2023/9/26
 */
@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PersonDTO implements Serializable {

    private static final long serialVersionUID = 1231840051925115741L;

    /**
     * 自然人id
     */
    private Long id;
    /**
     * 身份信息
     */
    private IdentityDTO identity;
    /**
     * 姓名
     */
    private String name;

    public Long personIdOrDefault() {
        return id == null ? 0L : id;
    }

    public Long identityIdOrDefault() {
        if (identity == null)
            return 0L;
        return identity.getId() == null ? 0L : identity.getId();
    }

    public IdentityTypeEnum identityTypeOrDefault() {
        if (identity == null)
            return IdentityTypeEnum.NOT_SUPPORT;
        return identity.getType() == null ? IdentityTypeEnum.NOT_SUPPORT : identity.getType();
    }

    public static PersonDTO from(Long personId, Long identityId, IdentityTypeEnum identityType) {
        IdentityDTO identity = IdentityDTO.builder()
                .id(identityId)
                .type(identityType)
                .build();
        return PersonDTO.builder()
                .id(personId)
                .identity(identity)
                .build();
    }

    public boolean isValid() {
        return Objects.nonNull(id) && id > 0
                && Objects.nonNull(identity) && identity.isValid();
    }

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