package com.red.circle.other.app.common.props;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.other.domain.gateway.props.ActivitySourceGroupGateway;
import com.red.circle.other.infra.database.rds.entity.activity.PropsActivityRuleConfig;
import com.red.circle.other.infra.database.rds.service.activity.PropsActivityRuleConfigService;
import com.red.circle.other.inner.enums.activity.PropsActivityTypeEnum;
import com.red.circle.other.inner.model.dto.activity.props.ActivityPropsGroup;
import com.red.circle.other.inner.model.dto.activity.props.ActivityRewardProps;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 多组奖励-奖励池
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class GroupRewardPoolCommon {

  private final ActivitySourceGroupGateway activitySourceGroupGateway;
  private final PropsActivityRuleConfigService propsActivityRuleConfigService;

  /**
   * 多个道具组中的详细道具
   */
  public List<List<ActivityRewardProps>> listRewardProps(SysOriginPlatformEnum sysOrigin,
      PropsActivityTypeEnum type) {

    List<PropsActivityRuleConfig> ruleConfigs = listByActivityTypeLimit(sysOrigin, type);

    List<List<ActivityRewardProps>> result = Lists.newArrayList();

    for (PropsActivityRuleConfig config : ruleConfigs) {
      result.add(getRewardPoolList(activitySourceGroupGateway.getActivityPropsGroup(
          sysOrigin.name(), config.getResourceGroupId()).getActivityRewardProps()));
    }

    return result;
  }

  /**
   * 多个道具组中的详细道具
   */
//  public List<ActivityRewardProps> listRewardProps(SysOriginPlatformEnum sysOrigin,
//      PropsActivityRuleConfig config) {
//
//    return getRewardPoolList(activitySourceGroupGateway.getActivityPropsGroup(
//        sysOrigin.name(), config.getResourceGroupId()).getActivityRewardProps());
//  }

  /**
   * 多个道具组中的详细道具
   */
  public Map<Long, List<ActivityRewardProps>> mapRewardProps(SysOriginPlatformEnum sysOrigin,
      Set<Long> groupIds) {

    Map<Long, ActivityPropsGroup> activityPropsGroupMap = activitySourceGroupGateway.mapActivityPropsGroup(
        sysOrigin.name(),
        groupIds);

    if (CollectionUtils.isEmpty(activityPropsGroupMap)) {
      return CollectionUtils.newHashMap();
    }

    Map<Long, List<ActivityRewardProps>> result = CollectionUtils.newHashMap();
    activityPropsGroupMap.forEach(
        (k, v) -> result.put(k, getRewardPoolList(v.getActivityRewardProps())));
    return result;
  }


  private List<ActivityRewardProps> getRewardPoolList(
      List<ActivityRewardProps> activityPropsGroups) {

    if (CollectionUtil.isEmpty(activityPropsGroups)) {
      return Lists.newArrayList();
    }

    return activityPropsGroups.stream()
        .map(rewardProps -> new ActivityRewardProps()
            .setId(rewardProps.getId())
            .setCover(rewardProps.getCover())
            .setGroupId(rewardProps.getGroupId())
            .setSort(rewardProps.getSort())
            .setQuantity(rewardProps.getQuantity())
            .setType(rewardProps.getType())
            .setDetailType(rewardProps.getDetailType())
            .setRemark(rewardProps.getRemark())
            .setAmount(rewardProps.getAmount())
            .setContent(rewardProps.getContent()))
        .toList();
  }


  public List<PropsActivityRuleConfig> listByActivityTypeLimit(SysOriginPlatformEnum sysOrigin,
      PropsActivityTypeEnum type) {

    return Optional.ofNullable(propsActivityRuleConfigService
            .listPlatformByActivityType(sysOrigin.name(), type))
        .orElseGet(Lists::newArrayList);
  }

}
