package com.red.circle.mq.business.model.event.approval;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.google.common.collect.Lists;
import com.red.circle.common.business.core.enums.DataApprovalTypeEnum;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.parse.DataTypeUtils;
import jakarta.validation.constraints.NotNull;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 鉴定资源是否违规.
 *
 * @author pengliang on 2020/1/10 18:20
 */
@Data
@Accessors(chain = true)
public class CensorProfileEvent implements Serializable {

  @Serial
  private static final long serialVersionUID = 1L;

  /**
   * 用户id
   */
  @NotNull(message = "userId required.")
  @JsonSerialize(using = ToStringSerializer.class)
  private Long userId;

  /**
   * 违规类型
   */
  @NotNull(message = "approvalType required.")
  private DataApprovalTypeEnum approvalType;

  /**
   * 违规类容集合
   */
  @NotNull(message = "contents required.")
  private List<CensorContent> contents;

  /**
   * 扩展信息.
   */
  private Map<String, String> extValues = CollectionUtils.newHashMap();

  private static final String DYNAMIC_ID = "dynamicId";

  public Long dynamicId() {
    return Optional.ofNullable(extValues.get(DYNAMIC_ID))
        .map(DataTypeUtils::toLong)
        .orElse(null);
  }

  public CensorProfileEvent putDynamicId(Long dynamicId) {
    extValues.put(DYNAMIC_ID, Objects.toString(dynamicId));
    return this;
  }

  public boolean checkDynamicContent() {
    return Objects.equals(approvalType, DataApprovalTypeEnum.DYNAMIC_CONTENT);
  }

  public boolean checkUserAvatar() {
    return Objects.equals(approvalType, DataApprovalTypeEnum.AVATAR);
  }

  public boolean checkRoomAvatar() {
    return Objects.equals(approvalType, DataApprovalTypeEnum.ROOM_AVATAR);
  }

  public boolean checkBackgroundPhoto() {
    return Objects.equals(approvalType, DataApprovalTypeEnum.BACKGROUND_PHOTO);
  }

  public boolean checkPersonalPhoto() {
    return Objects.equals(approvalType, DataApprovalTypeEnum.PERSONAL_PHOTO);
  }

  public CensorProfileEvent addContent(CensorContent content) {
    if (Objects.isNull(this.contents)) {
      this.contents = Lists.newArrayList();
    }
    contents.add(content);
    return this;
  }

  /**
   * 审核必须参数验证
   *
   * @return true 不为空 false 存在空
   */
  public boolean requiredParamNone() {
    return Objects.nonNull(this.userId)
        && Objects.nonNull(this.approvalType)
        && CollectionUtils.isNotEmpty(this.contents);
  }

  /**
   * 验证图片类型.
   *
   * @return true 是图片 false 不是图片
   */
  public boolean isImageType() {
    return checkUserAvatar()
        || checkDynamicContent()
        || checkRoomAvatar()
        || checkBackgroundPhoto()
        || checkPersonalPhoto();
  }

}
