package com.red.circle.other.app.inner.service.material.props.impl;

import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.inner.convertor.activity.PropsActivityInnerConvertor;
import com.red.circle.other.app.inner.service.material.props.PropsActivityClientService;
import com.red.circle.other.domain.gateway.props.ActivitySourceGroupGateway;
import com.red.circle.other.infra.common.activity.PropsActivitySendCommon;
import com.red.circle.other.infra.common.activity.send.SendRewardGroup;
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.cmd.activity.SendActivityRewardCmd;
import com.red.circle.other.inner.model.cmd.material.PropsActivityRuleConfigParamCmd;
import com.red.circle.other.inner.model.cmd.material.PropsActivityRuleConfigQryCmd;
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.ActivityPropsRuleDTO;
import com.red.circle.other.inner.model.dto.activity.props.ActivityResource;
import com.red.circle.other.inner.model.dto.material.PropsActivityRuleConfigDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 道具活动配置.
 *
 * @author pengliang on 2023/6/5
 */
@Service
@RequiredArgsConstructor
public class PropsActivityClientServiceImpl implements PropsActivityClientService {

  private final PropsActivitySendCommon propsActivitySendCommon;
  private final PropsActivityInnerConvertor propsActivityInnerConvertor;
  private final ActivitySourceGroupGateway activitySourceGroupGateway;
  private final PropsActivityRuleConfigService propsActivityRuleConfigService;

  @Override
  public List<ActivityPropsRule> listRule(SysOriginPlatformEnum sysOrigin,
      PropsActivityTypeEnum activityType) {
    return activitySourceGroupGateway.listRule(sysOrigin, activityType);
  }

  @Override
  public ActivityPropsRule getRuleGtLevelCrystal(SysOriginPlatformEnum sysOrigin,
      Integer level) {
    return propsActivityInnerConvertor.toActivityPropsRule(
        propsActivityRuleConfigService.getRuleGtLevelCrystal(sysOrigin, level));
  }

  @Override
  public ActivityPropsRule getRuleCrystalFirst(SysOriginPlatformEnum sysOrigin) {
    return propsActivityInnerConvertor.toActivityPropsRule(
        propsActivityRuleConfigService.getRuleCrystalFirst(sysOrigin));
  }

  @Override
  public ActivityPropsRule getRuleLuckyBoxFirst(SysOriginPlatformEnum sysOrigin) {
    return propsActivityInnerConvertor.toActivityPropsRule(
        propsActivityRuleConfigService.getRuleLuckyBoxFirst(sysOrigin));
  }

  @Override
  public ActivityPropsRule getRuleGameFruitBoxWinLast(SysOriginPlatformEnum sysOrigin) {
    return propsActivityInnerConvertor.toActivityPropsRule(
        propsActivityRuleConfigService.getRuleGameFruitBoxWinLast(sysOrigin));
  }

  @Override
  public ActivityPropsRule getRuleGameFruitBoxRoundsLast(SysOriginPlatformEnum sysOrigin) {
    return propsActivityInnerConvertor.toActivityPropsRule(
        propsActivityRuleConfigService.getRuleGameFruitBoxRoundsLast(sysOrigin));
  }

  @Override
  public ActivityPropsRule getByRuleId(Long id) {
    return activitySourceGroupGateway.getByRuleId(id);
  }

  @Override
  public List<ActivityResource> listActivityResource(SysOriginPlatformEnum sysOrigin,
      PropsActivityTypeEnum activityType) {
    return activitySourceGroupGateway.listActivityResource(sysOrigin, activityType);
  }

  @Override
  public List<Long> listActivityResourceGroupIds(SysOriginPlatformEnum sysOrigin,
      PropsActivityTypeEnum activityType) {
    return activitySourceGroupGateway.listActivityResourceGroupIds(sysOrigin, activityType);
  }

  @Override
  public ActivityResource getActivityResource(Long ruleId) {
    return activitySourceGroupGateway.getActivityResource(ruleId);
  }

  @Override
  public Map<Long, ActivityPropsGroup> mapActivityPropsGroup(String sysOrigin, Set<Long> groupIds) {
    return activitySourceGroupGateway.mapActivityPropsGroup(sysOrigin, groupIds);
  }

  @Override
  public ActivityPropsGroup getActivityPropsGroup(String sysOrigin, Long groupId) {
    return activitySourceGroupGateway.getActivityPropsGroup(sysOrigin, groupId);
  }

  @Override
  public void sendActivityReward(SendActivityRewardCmd cmd) {

    ActivityPropsGroup group = getActivityPropsGroup(cmd.getSysOrigin().name(),
        cmd.getSourceGroupId());

    if (Objects.isNull(group)) {
      throw new IllegalArgumentException(
          String.format("Not found group conf[%s,%s]", cmd.getSysOrigin(), cmd.getSourceGroupId()));
    }

    if (CollectionUtils.isEmpty(group.getActivityRewardProps())) {
      return;
    }

    propsActivitySendCommon.sendActivityGroup(SendRewardGroup.builder()
        .trackId(cmd.getTrackId())
        .sysOrigin(cmd.getSysOrigin())
        .acceptUserId(cmd.getAcceptUserId())
        .resourceGroupId(cmd.getSourceGroupId())
        .origin(cmd.getOrigin())
        .build());
  }

  @Override
  public Map<Long, ActivityPropsRuleDTO> mapRuleConfigByIds(Collection<Long> ids) {
    return propsActivityInnerConvertor.toActivityPropsRuleDTO(
        propsActivityRuleConfigService.mapByIds(ids));
  }

  @Override
  public PageResult<PropsActivityRuleConfigDTO> pagePropsActivityRuleConfig(
      PropsActivityRuleConfigQryCmd query) {

    PageResult<PropsActivityRuleConfig> pageResult = propsActivityRuleConfigService.pagePropsActivityRuleConfig(
        query);

    return pageResult.convert(
        ruleConfig -> {
          PropsActivityRuleConfigDTO ruleConfigDTO = propsActivityInnerConvertor.toPropsActivityRuleConfigDTO(
              ruleConfig);
          ruleConfigDTO.setActivityTypeName(ruleConfigDTO.getActivityType());
          return ruleConfigDTO;
        });
  }

  @Override
  public void propsActivityRuleConfigDeleteById(Long id) {
    propsActivityRuleConfigService.deleteById(id);
  }

  @Override
  public PropsActivityRuleConfigDTO getPropsActivityRuleConfig(
      PropsActivityRuleConfigParamCmd param) {

    return propsActivityInnerConvertor.toPropsActivityRuleConfigDTO(
        propsActivityRuleConfigService.getPropsActivityRuleConfig(
            param));
  }

  @Override
  public void updatePropsActivityRuleConfigById(PropsActivityRuleConfigParamCmd param) {
    propsActivityRuleConfigService.updateSelectiveById(
        propsActivityInnerConvertor.toPropsActivityRuleConfig(param));
  }

  @Override
  public void savePropsActivityRuleConfig(PropsActivityRuleConfigParamCmd param) {
    propsActivityRuleConfigService.save(
        propsActivityInnerConvertor.toPropsActivityRuleConfig(param));
  }
}
