package com.red.circle.other.app.inner.service.sys.impl;

import com.google.common.collect.Lists;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.app.inner.convertor.sys.SysGiftPackConfigInnerConvertor;
import com.red.circle.other.app.inner.service.sys.SysGiftPackConfigClientService;
import com.red.circle.other.infra.database.rds.entity.gift.GiftConfig;
import com.red.circle.other.infra.database.rds.entity.gift.GiftPackConfig;
import com.red.circle.other.infra.database.rds.entity.props.PropsSourceRecord;
import com.red.circle.other.infra.database.rds.service.gift.GiftConfigService;
import com.red.circle.other.infra.database.rds.service.gift.GiftPackConfigService;
import com.red.circle.other.infra.database.rds.service.props.PropsSourceRecordService;
import com.red.circle.other.inner.asserts.GiftErrorCode;
import com.red.circle.other.inner.enums.game.GiftPackTypeEnum;
import com.red.circle.other.inner.model.cmd.sys.GiftPackConfigQryCmd;
import com.red.circle.other.inner.model.cmd.sys.SysGiftPackConfigParamCmd;
import com.red.circle.other.inner.model.cmd.sys.SysGiftPackParamCmd;
import com.red.circle.other.inner.model.dto.sys.SysGiftPackConfigDTO;
import com.red.circle.other.inner.model.dto.sys.SysGiftPackConfigParamDTO;
import com.red.circle.other.inner.model.dto.sys.SysGiftPackParamDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.tool.core.num.ArithmeticUtils;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 礼包配置
 *
 * @author zongpubin on 2023/10/31
 */
@Service
@RequiredArgsConstructor
public class SysGiftPackConfigClientServiceImpl implements SysGiftPackConfigClientService {

  private final GiftConfigService giftConfigService;
  private final GiftPackConfigService giftPackConfigService;
  private final PropsSourceRecordService propsSourceRecordService;
  private final SysGiftPackConfigInnerConvertor sysGiftPackConfigInnerConvertor;

  @Override
  public void giftPackConfig(SysGiftPackParamCmd sysGiftPackParam) {

    giftPackConfigService.delete()
        .eq(GiftPackConfig::getGiftPackId, sysGiftPackParam.getGiftPackId()).execute();
    insertGiftPackConfig(sysGiftPackParam);

  }

  @Override
  public void addGiftPackConfig(SysGiftPackParamCmd sysGiftPackParam) {

    List<GiftPackConfig> sysGiftPackConfigList = giftPackConfigService.listByGiftPackId(
        sysGiftPackParam.getGiftPackId());
    ResponseAssert.isTrue(GiftErrorCode.GIFT_BACK_EXISTS,
        CollectionUtils.isEmpty(sysGiftPackConfigList));
    insertGiftPackConfig(sysGiftPackParam);

  }

  @Override
  public SysGiftPackParamDTO getGiftPackConfig(Long giftPackId) {
    List<GiftPackConfig> sysGiftPackConfigList = giftPackConfigService.listByGiftPackId(giftPackId);
    SysGiftPackParamDTO sysGiftPackParam = new SysGiftPackParamDTO();
    sysGiftPackParam.setGiftPackId(giftPackId);
    sysGiftPackParam.setGold(BigDecimal.ZERO);
    if (CollectionUtils.isEmpty(sysGiftPackConfigList)) {
      return sysGiftPackParam;
    }
    List<SysGiftPackConfigParamDTO> sysGiftPackConfigParamList = Lists.newArrayList();
    for (GiftPackConfig sysGiftPackConfig : sysGiftPackConfigList) {
      if (Objects.equals(GiftPackTypeEnum.GOLD.name(), sysGiftPackConfig.getType())) {
        sysGiftPackParam.setGold(sysGiftPackConfig.getGold());
      }
      if (Objects.equals(GiftPackTypeEnum.RIDE.name(), sysGiftPackConfig.getType())
          || Objects.equals(GiftPackTypeEnum.AVATAR_FRAME.name(),
          sysGiftPackConfig.getType())) {
        GiftPackConfig giftPackConfig = new GiftPackConfig();
        giftPackConfig.setType(sysGiftPackConfig.getType());
        giftPackConfig.setRelevanceId(sysGiftPackConfig.getRelevanceId());
        giftPackConfig.setEffectDuration(sysGiftPackConfig.getEffectDuration());
        sysGiftPackConfigParamList.add(
            sysGiftPackConfigInnerConvertor.toSysGiftPackConfigParamDTO(giftPackConfig));
      }
      if (Objects.equals(GiftPackTypeEnum.GIFT.name(), sysGiftPackConfig.getType())) {
        GiftPackConfig giftPackConfig = new GiftPackConfig();
        giftPackConfig.setType(sysGiftPackConfig.getType());
        giftPackConfig.setRelevanceId(sysGiftPackConfig.getRelevanceId());
        giftPackConfig.setGiftQuantity(sysGiftPackConfig.getGiftQuantity());
        sysGiftPackConfigParamList.add(
            sysGiftPackConfigInnerConvertor.toSysGiftPackConfigParamDTO(giftPackConfig));
      }
    }
    sysGiftPackParam.setGiftPackConfigParams(sysGiftPackConfigParamList);
    return sysGiftPackParam;
  }

  @Override
  public BigDecimal getGoldByPackId(Long giftPackId) {
    GiftPackConfig sysGiftPackConfig = giftPackConfigService.getGoldByPackId(giftPackId);
    if (Objects.isNull(sysGiftPackConfig)) {
      return BigDecimal.ZERO;
    }
    return sysGiftPackConfig.getGold();
  }

  @Override
  public List<SysGiftPackConfigDTO> getGiftPackConfigInfo(GiftPackConfigQryCmd query) {
    List<GiftPackConfig> sysGiftPackConfigList = giftPackConfigService.getGiftPackConfigInfo(query);

    if (CollectionUtils.isEmpty(sysGiftPackConfigList)) {
      return Lists.newArrayList();
    }

    Map<Long, GiftConfig> giftConfigMap = giftConfigService
        .mapByIds(sysGiftPackConfigList.stream()
            .filter(sysGiftPackConfig -> Objects
                .equals(GiftPackTypeEnum.GIFT.name(), sysGiftPackConfig.getType()))
            .map(GiftPackConfig::getRelevanceId).collect(Collectors.toSet()));

    Map<Long, PropsSourceRecord> propsConfigMap = propsSourceRecordService
        .mapByIds(sysGiftPackConfigList.stream()
            .filter(sysGiftPackConfig -> Objects
                .equals(GiftPackTypeEnum.RIDE.name(), sysGiftPackConfig.getType())
                || Objects
                .equals(GiftPackTypeEnum.AVATAR_FRAME.name(), sysGiftPackConfig.getType()))
            .map(GiftPackConfig::getRelevanceId).collect(Collectors.toSet()));

    return sysGiftPackConfigList.stream().map(sysGiftPackConfig -> {
      SysGiftPackConfigDTO sysGiftPackConfigDTO = new SysGiftPackConfigDTO();
      if (Objects.equals(GiftPackTypeEnum.GIFT.name(), sysGiftPackConfig.getType())) {
        sysGiftPackConfigDTO.setQuantity(sysGiftPackConfig.getGiftQuantity());
        GiftConfig sysGiftConfig = giftConfigMap.get(sysGiftPackConfig.getRelevanceId());
        if (Objects.nonNull(sysGiftConfig)) {
          sysGiftPackConfigDTO.setUrl(sysGiftConfig.getGiftPhoto());
          sysGiftPackConfigDTO.setName(sysGiftConfig.getGiftName());
          sysGiftPackConfigDTO.setCandy(sysGiftConfig.getGiftCandy());
        }
      }
      if (Objects.equals(GiftPackTypeEnum.RIDE.name(), sysGiftPackConfig.getType())
          || Objects.equals(GiftPackTypeEnum.AVATAR_FRAME.name(),
          sysGiftPackConfig.getType())) {
        sysGiftPackConfigDTO.setQuantity(sysGiftPackConfig.getEffectDuration());
        PropsSourceRecord propsSourceRecord = propsConfigMap
            .get(sysGiftPackConfig.getRelevanceId());
        if (Objects.nonNull(propsSourceRecord)) {
          sysGiftPackConfigDTO.setUrl(propsSourceRecord.getCover());
          sysGiftPackConfigDTO.setName(propsSourceRecord.getName());
          sysGiftPackConfigDTO.setCandy(propsSourceRecord.getAmount());
        }
      }
      return sysGiftPackConfigDTO;
    }).collect(Collectors.toList());
  }

  private void insertGiftPackConfig(SysGiftPackParamCmd sysGiftPackParam) {
    List<GiftPackConfig> sysGiftPackConfigList = Lists.newArrayList();
    if (ArithmeticUtils.gt(sysGiftPackParam.getGold(), BigDecimal.ZERO)) {
      GiftPackConfig sysGiftPackConfig = new GiftPackConfig();
      sysGiftPackConfig.setGiftPackId(sysGiftPackParam.getGiftPackId());
      sysGiftPackConfig.setType(GiftPackTypeEnum.GOLD.name());
      sysGiftPackConfig.setGold(sysGiftPackParam.getGold());
      sysGiftPackConfig.setCreateTime(TimestampUtils.now());
      sysGiftPackConfigList.add(sysGiftPackConfig);
    }

    if (CollectionUtils.isNotEmpty(sysGiftPackParam.getGiftPackConfigParams())) {
      for (SysGiftPackConfigParamCmd sysGiftPackConfigParam : sysGiftPackParam
          .getGiftPackConfigParams()) {
        GiftPackConfig sysGiftPackConfig = new GiftPackConfig();
        sysGiftPackConfig.setGiftPackId(sysGiftPackParam.getGiftPackId());
        sysGiftPackConfig.setType(sysGiftPackConfigParam.getType());
        sysGiftPackConfig.setRelevanceId(sysGiftPackConfigParam.getRelevanceId());
        if (Objects.equals(GiftPackTypeEnum.RIDE.name(), sysGiftPackConfigParam.getType())
            || Objects.equals(GiftPackTypeEnum.AVATAR_FRAME.name(),
            sysGiftPackConfigParam.getType())) {
          sysGiftPackConfig.setEffectDuration(sysGiftPackConfigParam.getEffectDuration());
        }
        if (Objects.equals(GiftPackTypeEnum.GIFT.name(), sysGiftPackConfigParam.getType())) {
          sysGiftPackConfig.setGiftQuantity(sysGiftPackConfigParam.getGiftQuantity());
        }
        sysGiftPackConfig.setCreateTime(TimestampUtils.now());
        sysGiftPackConfigList.add(sysGiftPackConfig);
      }
    }

    if (CollectionUtils.isEmpty(sysGiftPackConfigList)) {
      return;
    }

    giftPackConfigService.saveBatch(sysGiftPackConfigList);
  }
}
