package com.red.circle.other.inner.model.dto.user;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.other.inner.model.dto.user.props.UserUseBadgeDTO;
import com.red.circle.other.inner.model.dto.user.props.UserUsePropsDTO;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.Data;

/**
 * <p>
 * 用户的运行资料.
 * </p>
 *
 * @author pengliang
 * @since 2022-02-19
 */
@Data
public class UserRunProfileDTO implements Serializable {

  private static final long serialVersionUID = 1L;

  /**
   * 用户id.
   */
  private Long id;

  /**
   * 账号.
   */
  @JsonSerialize(using = ToStringSerializer.class)
  private Long account;

  /**
   * 用户头像.
   */
  private String userAvatar;

  /**
   * 用户昵称.
   */
  private String userNickname;

  /**
   * 用户性别:0 女，1 男.
   */
  private Integer userSex;

  /**
   * 用户类型:0.真实用户 1.马甲用户 2.vip马甲 3.视频号关联.
   */
  private Integer userType;

  /**
   * 国家id.
   */
  @JsonSerialize(using = ToStringSerializer.class)
  private Long countryId;

  /**
   * 国家名称.
   */
  private String countryName;

  /**
   * 国家编码.
   */
  private String countryCode;

  /**
   * 如果国家code是IA强制变更ID.适配印尼分国家
   */
  public void setCountryCode(String countryCode) {
    if (Objects.nonNull(countryCode) && Objects.equals(countryCode, "IA")) {
      this.countryCode = "ID";
    } else if (Objects.nonNull(countryCode) && Objects.equals(countryCode, "EY")) {
      this.countryCode = "EG";
    } else if (Objects.nonNull(countryCode) && Objects.equals(countryCode, "AB")) {
      this.countryCode = "AE";
    } else if (Objects.nonNull(countryCode) && Objects.equals(countryCode, "MB")) {
      this.countryCode = "MA";
    } else if (Objects.nonNull(countryCode) && Objects.equals(countryCode, "SP")) {
      this.countryCode = "SA";
    } else {
      this.countryCode = countryCode;
    }
  }

  /**
   * 年龄.
   */
  private Integer age;

  /**
   * 出身年.
   */
  private Integer bornYear;

  /**
   * 出身月.
   */
  private Integer bornMonth;

  /**
   * 出身日.
   */
  private Integer bornDay;

  /**
   * 账号状态.
   */
  private String accountStatus;

  /**
   * 冻结时间.
   */
  private Timestamp freezingTime;

  /**
   * 系统来源.
   */
  private String originSys;

  /**
   * 子系统.
   */
  private String sysOriginChild;

  /**
   * 0.未删除 1.已删除.
   */
  private Boolean del;

  /**
   * 创建时间.
   */
  private Timestamp createTime;

  /**
   * 修改时间.
   */
  private Timestamp updateTime;

  /**
   * 个性签名.
   */
  private String autograph;

  /**
   * 拥有的靓号.
   */
  private OwnSpecialIdDTO ownSpecialId;

  /**
   * 使用的道具.
   */
  private List<UserUsePropsDTO> useProps;

  /**
   * 用户使用的徽章.
   */
  private List<UserUseBadgeDTO> wearBadge;

  /**
   * 添加使用道具.
   */
  public void addUseProps(UserUsePropsDTO UserUsePropsDTO) {
    if (Objects.isNull(useProps)) {
      useProps = CollectionUtils.newArrayList();
    }
    if (Objects.isNull(UserUsePropsDTO)) {
      return;
    }
    useProps.add(UserUsePropsDTO);
  }

  /**
   * 添加使用道具.
   */
  public void addUseProps(List<UserUsePropsDTO> usePropsEntities) {
    if (Objects.isNull(useProps)) {
      useProps = CollectionUtils.newArrayList();
    }
    if (CollectionUtils.isEmpty(usePropsEntities)) {
      return;
    }
    useProps.addAll(usePropsEntities);
  }

  /**
   * 添加使用徽章.
   */
  public void addWearBadge(List<UserUseBadgeDTO> useBadgeEntities) {
    if (Objects.isNull(wearBadge)) {
      wearBadge = CollectionUtils.newArrayList();
    }
    if (CollectionUtils.isEmpty(useBadgeEntities)) {
      return;
    }
    wearBadge.addAll(useBadgeEntities);
  }

  public List<UserUsePropsDTO> getUseProps() {
    if (CollectionUtils.isNotEmpty(useProps)) {
      return useProps.stream()
          .filter(props -> Objects.nonNull(props) && checkAvailable(props.getExpireTime()))
          .collect(Collectors.toList());
    }
    return useProps;
  }

  public List<UserUseBadgeDTO> getWearBadge() {
    if (CollectionUtils.isNotEmpty(wearBadge)) {
      wearBadge = wearBadge.stream()
          .filter(props -> Objects.nonNull(props) && checkAvailable(props.getExpireTime()))
          .collect(Collectors.toList());
    }
    return wearBadge;
  }

  /**
   * true 可用. false 不可用.
   */
  private boolean checkAvailable(Long expireTime) {
    return Objects.isNull(expireTime) || expireTime > TimestampUtils.now().getTime();
  }

}
