package com.red.circle.other.app.command.material.query;

import com.google.common.collect.Lists;
import com.red.circle.common.business.dto.cmd.app.AppRoomIdCmd;
import com.red.circle.other.app.convertor.material.BadgeAppConvertor;
import com.red.circle.other.app.dto.clientobject.material.BadgeBackpackCO;
import com.red.circle.other.app.dto.clientobject.material.BadgeConfigCO;
import com.red.circle.other.infra.database.rds.entity.badge.BadgeConfig;
import com.red.circle.other.infra.database.rds.entity.badge.BadgePictureConfig;
import com.red.circle.other.infra.database.rds.entity.badge.RoomBadgeBackpack;
import com.red.circle.other.infra.database.rds.service.badge.BadgeConfigService;
import com.red.circle.other.infra.database.rds.service.badge.BadgePictureConfigService;
import com.red.circle.other.infra.database.rds.service.badge.RoomBadgeBackpackService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 房间徽章背包.
 *
 * @author pengliang on 2021/3/12
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RoomBadgeBackpackQryExe {

  private final BadgeAppConvertor badgeAppConvertor;
  private final BadgeConfigService badgeConfigService;
  private final RoomBadgeBackpackService roomBadgeBackpackService;
  private final BadgePictureConfigService badgePictureConfigService;

  public List<BadgeBackpackCO> execute(AppRoomIdCmd cmd) {
    List<RoomBadgeBackpack> roomBadgeBackpacks = roomBadgeBackpackService
        .listBadgeBackpackByRoomId(cmd.getRoomId());

    if (CollectionUtils.isEmpty(roomBadgeBackpacks)) {
      return Lists.newArrayList();
    }

    Map<Long, BadgePictureConfig> badgePictureConfigMap = badgePictureConfigService
        .mapBadge(cmd.requireReqSysOrigin(),
            roomBadgeBackpacks.stream().map(RoomBadgeBackpack::getBadgeId)
                .collect(Collectors.toSet()));

    return assembly(roomBadgeBackpacks, badgePictureConfigMap);
  }

  private List<BadgeBackpackCO> assembly(List<RoomBadgeBackpack> badgeBackpacks,
      Map<Long, BadgePictureConfig> badgePictureConfigMap) {

    List<BadgeConfig> badgeConfigs = badgeConfigService.listByIds(getBadgeIds(badgeBackpacks));

    if (CollectionUtils.isEmpty(badgeConfigs) || CollectionUtils.isEmpty(badgePictureConfigMap)) {
      return Lists.newArrayList();
    }

    Map<Long, RoomBadgeBackpack> badgeBackpackMap = badgeBackpackToMap(badgeBackpacks);

    return badgeConfigs.stream().map(badgeConfig -> {
      BadgeConfigCO badgeConfigCo = badgeAppConvertor.toBadgeConfigCO(badgeConfig);
      RoomBadgeBackpack badgeBackpack = badgeBackpackMap.get(badgeConfig.getId());
      if (Objects.isNull(badgeBackpack)) {
        log.warn("【数据异常】没有找到徽章资源信息:{}", badgeConfig.getId());
        return null;
      }
      BadgePictureConfig badgePictureConfig = badgePictureConfigMap.get(badgeConfig.getId());
      if (Objects.nonNull(badgePictureConfig)) {
        badgeConfigCo.setAnimationUrl(badgePictureConfig.getAnimationUrl());
        badgeConfigCo.setSelectUrl(badgePictureConfig.getSelectUrl());
        badgeConfigCo.setNotSelectUrl(badgePictureConfig.getNotSelectUrl());
      }
      return new BadgeBackpackCO()
          .setId(badgeBackpack.getId())
          .setBadgeConfig(badgeConfigCo);
    }).filter(Objects::nonNull).collect(Collectors.toList());
  }

  private Map<Long, RoomBadgeBackpack> badgeBackpackToMap(List<RoomBadgeBackpack> badgeBackpacks) {
    return badgeBackpacks.stream()
        .collect(Collectors.toMap(RoomBadgeBackpack::getBadgeId, v -> v));
  }

  private List<Long> getBadgeIds(List<RoomBadgeBackpack> badgeBackpacks) {
    return badgeBackpacks.stream().map(RoomBadgeBackpack::getBadgeId).distinct()
        .collect(Collectors.toList());
  }

}
