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

import com.red.circle.other.app.inner.service.game.GameEggClientService;
import com.red.circle.other.infra.convertor.game.GameEggInnerConvertor;
import com.red.circle.other.infra.database.mongo.service.game.GameEggStockConfigService;
import com.red.circle.other.infra.database.rds.service.game.GameEggCacheService;
import com.red.circle.other.inner.model.cmd.game.GameEggConfigCmd;
import com.red.circle.other.inner.model.dto.game.GameEggStockConfigDTO;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 游戏砸金蛋服务.
 *
 * @author pengliang on 2023/6/10
 */
@Service
@RequiredArgsConstructor
public class GameEggClientServiceImpl implements GameEggClientService {

  private final GameEggCacheService gameEggCacheService;
  private final GameEggInnerConvertor gameEggInnerConvertor;
  private final GameEggStockConfigService gameEggStockConfigService;


  @Override
  public List<GameEggStockConfigDTO> listConfig(String sysOrigin) {
    return gameEggInnerConvertor.toListGameEggStockConfigDTO(
        gameEggStockConfigService.listConfig(sysOrigin));
  }

  @Override
  public void updateConfig(GameEggConfigCmd param) {

    gameEggStockConfigService.removeConfig(param.getRemoveIds());

    gameEggStockConfigService.insertBatchConfig(param.getConfigs().stream()
        .map(gameEggInnerConvertor::toGameEggStockConfig)
        .filter(conf -> Objects.nonNull(conf) && Objects.isNull(conf.getId()))
        .peek(conf -> {
          conf.setId(IdWorkerUtils.getId());
          conf.setConsumeStockQuantity(0L);
          conf.setCreateTime(TimestampUtils.now());
          conf.setUpdateTime(TimestampUtils.now());
        })
        .collect(Collectors.toList()));

    gameEggStockConfigService.updateBatchConfig(param.getConfigs().stream()
        .map(gameEggInnerConvertor::toGameEggStockConfig)
        .filter(conf -> Objects.nonNull(conf) && Objects.nonNull(conf.getId()))
        .collect(Collectors.toList()));

    param.getConfigs().forEach(conf -> {
      if (Objects.nonNull(conf.getId())) {
        if (Objects.isNull(conf.getConsumeStockQuantity()) || conf.getConsumeStockQuantity() <= 0) {
          gameEggCacheService.removeConsumeStockQuantity(conf.getId());
          return;
        }
        gameEggCacheService.setConsumeStockQuantity(conf.getId(),
            conf.getConsumeStockQuantity());
      }
    });
  }

  @Override
  public void updateConsumeStockQuantityZero(Long id) {
    gameEggStockConfigService.updateConsumeStockQuantityZero(id);
  }

  @Override
  public String getPrizePool(String sysOrigin) {
    return Objects.toString(gameEggCacheService.getThisMonthPrizePool(sysOrigin), "0");
  }

  @Override
  public String getExtractRatio(String sysOrigin) {
    return gameEggCacheService.getExtractRatio(sysOrigin, "0");
  }

  @Override
  public void setExtractRatio(String sysOrigin, String ratio) {
    gameEggCacheService.setExtractRatio(sysOrigin, ratio);
  }
}
