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.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.ActivityPropsRule;
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.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 单组奖励-奖励池
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class SingleGroupRewardPoolCommon {

  private final ActivitySourceGroupGateway activitySourceGroupGateway;

  /**
   * 单个道具组中的详细道具
   */
  public List<ActivityRewardPropsDTO> listRewardProps(String sysOrigin,
      PropsActivityTypeEnum type) {

    List<ActivityPropsRule> ruleConfigs = listByActivityTypeLimit(sysOrigin, type);
    if (CollectionUtil.isEmpty(ruleConfigs)) {
      return Lists.newArrayList();
    }
    ActivityPropsGroup activityPropsGroup = activitySourceGroupGateway.getActivityPropsGroup(
        sysOrigin,
        ruleConfigs.get(0).getResourceGroupId());

    if (Objects.isNull(activityPropsGroup)) {
      return Lists.newArrayList();
    }
    return getRewardPoolList(activityPropsGroup.getActivityRewardProps());
  }


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

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

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


  private List<ActivityPropsRule> listByActivityTypeLimit(String sysOrigin,
      PropsActivityTypeEnum type) {
    return Optional.ofNullable(
            activitySourceGroupGateway.listRule(SysOriginPlatformEnum.valueOf(sysOrigin), type))
        .orElseGet(Lists::newArrayList);
  }

}
