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

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.app.convertor.material.GiftAppConvertor;
import com.red.circle.other.app.dto.clientobject.game.GameLuckyBoxCO;
import com.red.circle.other.app.dto.clientobject.game.GameLuckyBoxRuleCO;
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.service.activity.PropsActivityRewardConfigService;
import com.red.circle.other.infra.database.rds.service.activity.PropsActivityRuleConfigService;
import com.red.circle.other.infra.database.rds.service.gift.GiftConfigService;
import com.red.circle.other.inner.enums.game.GiftPackTypeEnum;
import com.red.circle.other.inner.model.dto.material.GiftConfigDTO;
import com.red.circle.tool.core.json.JacksonUtils;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * <p>
 * lucky box 礼物与抽奖规则.
 * </p>
 *
 * @author pengshigang
 * @since 2021-07-13
 */
@Component
@RequiredArgsConstructor
public class GameLuckyBoxGiftQryExe {

  private final GiftAppConvertor giftAppConvertor;
  private final GiftConfigService giftConfigService;
  private final PropsActivityRuleConfigService propsActivityRuleConfigService;
  private final PropsActivityRewardConfigService propsActivityRewardConfigService;

  public GameLuckyBoxCO execute(AppExtCommand cmd) {

    PropsActivityRuleConfig ruleConfig = getLuckyBoxFirstRule(cmd);
    if (Objects.isNull(ruleConfig)) {
      return new GameLuckyBoxCO();
    }

    return new GameLuckyBoxCO()
        .setRules(getLuckyRule(ruleConfig))
        .setGifts(getGiftConfigs(ruleConfig));
  }

  private List<GiftConfigDTO> getGiftConfigs(PropsActivityRuleConfig ruleConfig) {
    if (Objects.isNull(ruleConfig)) {
      return Lists.newArrayList();
    }

    List<PropsActivityRewardConfig> rewardConfigs = getRewardConfigs(ruleConfig);
    if (CollectionUtils.isEmpty(rewardConfigs)) {
      return Lists.newArrayList();
    }
    List<GiftConfigDTO> configList = giftAppConvertor.toListGiftConfigDTO(
        giftConfigService.listByIds(giftIds(rewardConfigs)));

    if (CollectionUtils.isEmpty(configList)) {
      return Lists.newArrayList();
    }
    List<GiftConfigDTO> resultCO = Lists.newArrayList();
    for (PropsActivityRewardConfig rewardConfig : rewardConfigs) {
      for (GiftConfigDTO giftConfigCO : configList) {
        if (Objects.equals(Long.valueOf(rewardConfig.getContent()), giftConfigCO.getId())) {
          resultCO.add(giftConfigCO);
          break;
        }
      }
    }
    return resultCO;
  }

  private Set<Long> giftIds(List<PropsActivityRewardConfig> rewardConfig) {
    if (CollectionUtils.isEmpty(rewardConfig)) {
      return Sets.newHashSet();
    }
    return rewardConfig.stream().map(reward -> Long.parseLong(reward.getContent()))
        .collect(Collectors.toSet());
  }

  private List<GameLuckyBoxRuleCO> getLuckyRule(PropsActivityRuleConfig ruleConfig) {
    return JacksonUtils
        .readValue(ruleConfig.getJsonData(), new TypeReference<List<GameLuckyBoxRuleCO>>() {
        });
  }

  private List<PropsActivityRewardConfig> getRewardConfigs(PropsActivityRuleConfig config) {
    return propsActivityRewardConfigService
        .listByGroupIdType(config.getResourceGroupId(), GiftPackTypeEnum.GIFT.name());
  }

  private PropsActivityRuleConfig getLuckyBoxFirstRule(AppExtCommand cmd) {
    return propsActivityRuleConfigService.getRuleLuckyBoxFirst(cmd.requireReqSysOriginEnum());
  }

}
