package com.red.circle.other.app.command.game.roompk;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.red.circle.other.app.dto.clientobject.activity.RewardPoolCO;
import com.red.circle.other.app.dto.cmd.activity.RewardPoolCmd;
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.model.dto.activity.props.ActivityPropsGroup;
import com.red.circle.other.inner.model.dto.activity.props.ActivityRewardProps;
import com.red.circle.other.inner.model.dto.activity.props.ActivityRewardPropsDTO;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 奖励池.
 *
 * @author pengshigang on 2021/7/8
 */
@Deprecated
@Service
@RequiredArgsConstructor
public class RewardPoolQryExe {

  private final ActivitySourceGroupGateway activitySourceGroupGateway;
  private final PropsActivityRuleConfigService propsActivityRuleConfigService;

  public List<RewardPoolCO> execute(RewardPoolCmd cmd) {
    List<PropsActivityRuleConfig> ruleConfigs = listByActivityTypeLimit(cmd);

    Map<Long, ActivityPropsGroup> activityResources = mapActivityPropsGroup(cmd,
        getGroupIds(ruleConfigs));

    return ruleConfigs.stream()
        .map(weeklyGiftConfig -> getWeekRewardPoolCO(
            activityResources.get(weeklyGiftConfig.getResourceGroupId()).getActivityRewardProps()))
        .collect(Collectors.toList());
  }


  private RewardPoolCO getWeekRewardPoolCO(
      List<ActivityRewardProps> activityPropsGroups) {

    return new RewardPoolCO()
        .setActivityRewardProps(activityPropsGroups.stream()
            .map(rewardProps -> new ActivityRewardPropsDTO()
                .setId(rewardProps.getId())
                .setCover(rewardProps.getCover())
                .setGroupId(rewardProps.getGroupId())
                .setSort(rewardProps.getSort())
                .setQuantity(rewardProps.getQuantity())
                .setType(rewardProps.getType())
                .setAmount(rewardProps.getAmount())
                .setContent(rewardProps.getContent()))
            .toList());
  }

  private Map<Long, ActivityPropsGroup> mapActivityPropsGroup(RewardPoolCmd cmd,
      Set<Long> groupIds) {
    return Optional.ofNullable(activitySourceGroupGateway
            .mapActivityPropsGroup(cmd.getReqSysOrigin().getOrigin(), groupIds))
        .orElse(Maps.newHashMap());
  }

  private List<PropsActivityRuleConfig> listByActivityTypeLimit(RewardPoolCmd cmd) {
    return Optional.ofNullable(propsActivityRuleConfigService
            .listPlatformByActivityType(cmd.getReqSysOrigin().getOrigin(), cmd.getType()))
        .map(propsActivityRuleConfigs -> propsActivityRuleConfigs.stream().
            limit(cmd.getGroupLimit()).collect(Collectors.toList())).orElseGet(Lists::newArrayList);
  }

  private Set<Long> getGroupIds(List<PropsActivityRuleConfig> propsActivityRuleConfigList) {
    return propsActivityRuleConfigList.stream().map(PropsActivityRuleConfig::getResourceGroupId)
        .collect(Collectors.toSet());
  }


}
