package com.red.circle.other.app.manager.activity;

import com.google.common.collect.Maps;
import com.red.circle.common.business.enums.PropsActivityRewardEnum;
import com.red.circle.other.app.convertor.material.PropsActivityRewardConfigConvertor;
import com.red.circle.other.infra.database.rds.entity.activity.PropsActivityRewardConfig;
import com.red.circle.other.infra.database.rds.entity.activity.PropsActivityRuleConfig;
import com.red.circle.other.infra.database.rds.entity.badge.BadgePictureConfig;
import com.red.circle.other.infra.database.rds.service.activity.PropsActivityRewardConfigService;
import com.red.circle.other.infra.database.rds.service.activity.PropsActivityRuleConfigService;
import com.red.circle.other.infra.database.rds.service.badge.BadgePictureConfigService;
import com.red.circle.other.inner.enums.activity.PropsActivityTypeEnum;
import com.red.circle.other.inner.model.dto.material.PropsActivityRewardConfigInfoDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

/**
 * 活动资源组.
 *
 * @author lixiaofei on 2024/06/25
 */
@Service
@AllArgsConstructor
public class ActivitySourceGroupManagerService {

  private final BadgePictureConfigService sysBadgePictureConfigService;
  private final PropsActivityRewardConfigService propsActivityRewardConfigService;
  private final PropsActivityRuleConfigService propsActivityRuleConfigService;
  private final PropsActivityRewardConfigConvertor propsActivityRewardConvertor;

  public Map<Long, List<PropsActivityRewardConfigInfoDTO>> mapSourceGroup(String sysOrigin,
                                                                          PropsActivityTypeEnum activityTypeEnum) {
    if (StringUtils.isBlank(sysOrigin)) {
      return Maps.newHashMap();
    }
    List<PropsActivityRuleConfig> propsActivityRuleConfigs = propsActivityRuleConfigService.listPlatformByActivityType(
        sysOrigin, activityTypeEnum);
    return getPropsActivityReward(sysOrigin, propsActivityRuleConfigs);
  }

  public Map<Long, List<PropsActivityRewardConfigInfoDTO>> getPropsActivityReward(String sysOrigin,
      List<PropsActivityRuleConfig> propsActivityRuleConfigs) {
    if (CollectionUtils.isEmpty(propsActivityRuleConfigs)) {
      return Maps.newHashMap();
    }

    LinkedHashMap<Long, List<PropsActivityRewardConfigInfoDTO>> result = propsActivityRuleConfigs.stream()
        .sorted(Comparator.comparing(PropsActivityRuleConfig::getSort))
        .map(e -> Map.entry(e.getResourceGroupId(), new ArrayList<PropsActivityRewardConfigInfoDTO>()))
        .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (o, o2) -> o2, LinkedHashMap::new));

    List<PropsActivityRewardConfig> configs = propsActivityRewardConfigService
              .listByGroupIds(result.keySet().stream().toList());
    List<PropsActivityRewardConfigInfoDTO> propsActivityRewardConfigInfoDTOS
            = propsActivityRewardConvertor.toPropsActivityRewardConfig(configs);

    propsActivityRewardConfigInfoDTOS.forEach(
        e -> {
          result.get(e.getGroupId()).add(e);
        }
    );

    Map<Long, BadgePictureConfig> badgePictureMap = sysBadgePictureConfigService.mapBadge(
        sysOrigin, getBadgeIds(result));
    result.values().stream().flatMap(Collection::stream)
        .filter(props -> Objects.equals(props.getType(),
            PropsActivityRewardEnum.BADGE.name()))
        .forEach(props -> {
          BadgePictureConfig picture = badgePictureMap.get(
              Long.valueOf(props.getContent()));
          if (Objects.isNull(picture)) {
            return;
          }
          props.setCover(picture.getSelectUrl());
          props.setSourceUrl(picture.getAnimationUrl());
        });
    return result;
  }

  private Set<Long> getBadgeIds(
      Map<Long, List<PropsActivityRewardConfigInfoDTO>> propsActivityRewardMap) {
    return propsActivityRewardMap.values().stream().flatMap(Collection::stream)
        .filter(props -> Objects.equals(props.getType(),
            PropsActivityRewardEnum.BADGE.name()))
        .map(props -> Long.valueOf(props.getContent()))
        .collect(Collectors.toSet());
  }
}
