package com.red.circle.other.app.dto.clientobject.material;

import com.red.circle.framework.dto.ClientObject;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 徽章分组表格.
 * </p>
 *
 * @author pengliang
 * @since 2021-03-12
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class BadgeGroupTableCO extends ClientObject {

  private static final long serialVersionUID = 1L;

  /**
   * 徽章key.
   */
  private String badgeKey;

  /**
   * 徽章表格.
   */
  private List<BadgeTableCO> badgeTables;

  /**
   * 展示徽章.
   */
  private BadgeTableCO badgeTable;

  public Integer displayBadgeSort() {
    return Optional.ofNullable(badgeTable)
        .map(badgeTable -> Optional.ofNullable(badgeTable.getBadgeConfig())
            .map(BadgeConfigCO::getBadgeLevel)
            .orElse(Integer.MAX_VALUE))
        .orElse(Integer.MAX_VALUE);
  }

  public BadgeGroupTableCO putShowBadgeTable() {
    if (CollectionUtils.isEmpty(badgeTables)) {
      return this;
    }
    List<BadgeTableCO> badgeTableActivations = badgeTables.stream()
        .filter(badgeTableCO -> Objects.equals(badgeTableCO.getActivation(), Boolean.TRUE))
        .collect(Collectors.toList());

    if (CollectionUtils.isEmpty(badgeTableActivations)) {
      this.badgeTable = badgeTables.get(0);
      return this;
    }
    this.badgeTable = badgeTableActivations.get(badgeTableActivations.size() - 1);
    return this;
  }
}
