package com.red.circle.other.infra.database.cache.service.other.impl;

import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.component.redis.service.RedisService;
import com.red.circle.other.infra.database.cache.key.ActivityLotteryHashKeys;
import com.red.circle.other.infra.database.cache.key.LotteryKeys;
import com.red.circle.other.infra.database.cache.service.other.ActivityLotteryConfCacheService;
import com.red.circle.other.inner.model.cmd.activity.lottery.ActivityLotteryTurntableCache;
import com.red.circle.other.inner.model.cmd.activity.lottery.Prizes;
import com.red.circle.tool.core.parse.DataTypeUtils;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 活动抽奖配置 实现.
 *
 * @author lisizhe on 2023/9/11
 */
@Component
@RequiredArgsConstructor
public class ActivityLotteryConfCacheServiceImpl implements ActivityLotteryConfCacheService {

  private final RedisService redisService;


  @Override
  public void addLotteryTurntable(SysOriginPlatformEnum sysOrigin,
      ActivityLotteryTurntableCache cache) {
    redisService.hashPut(ActivityLotteryHashKeys.ACTIVITY_LOTTERY.name(),
        ActivityLotteryHashKeys.PET_TURNTABLE_LOTTERY.getKey(sysOrigin.name()),
        cache);
    resetConsumeStock(sysOrigin, cache);
  }

  private void resetConsumeStock(SysOriginPlatformEnum sysOrigin,
      ActivityLotteryTurntableCache cache) {
    for (int index = 0; index < cache.getPrizes().size(); index++) {
      Prizes prizes = cache.getPrizes().get(index);
      if (!prizes.checkAvailableInStock()) {
        redisService.increment(getStockConsumeCounter(sysOrigin, prizes.getCode()),
            0);
      }
    }
  }

  @Override
  public ActivityLotteryTurntableCache getLotteryTurntable(SysOriginPlatformEnum sysOrigin) {
    return Optional.ofNullable(redisService.hashGet(LotteryKeys.ACTIVITY_LOTTERY.name(),
            ActivityLotteryHashKeys.PET_TURNTABLE_LOTTERY.getKey(sysOrigin.name()),
            ActivityLotteryTurntableCache.class))
        .map(lottery -> {

          List<String> consumeInStocks = redisService.multiGet(lottery.getPrizes().stream()
              .map(prizes -> getStockConsumeCounter(sysOrigin, prizes.getCode()))
              .collect(Collectors.toList()), DataTypeUtils::toString);

          for (int index = 0; index < lottery.getPrizes().size(); index++) {
            Prizes prizes = lottery.getPrizes().get(index);
            prizes.setConsumeInStock(DataTypeUtils.toLong(consumeInStocks.get(index)));
          }

          return lottery;
        })
        .orElse(null);
  }

  @Override
  public Long consumeInStock(SysOriginPlatformEnum sysOrigin, String prizesCode) {
    return redisService.increment(getStockConsumeCounter(sysOrigin, prizesCode), 1);
  }


  @Override
  public Long minusConsumeInStock(SysOriginPlatformEnum sysOrigin, String prizesCode) {
    return redisService.decrement(LotteryKeys.ACTIVITY_LOTTERY.name(), -1);
  }

  private String getStockConsumeCounter(SysOriginPlatformEnum sysOrigin, String prizesCode) {
    return ActivityLotteryHashKeys.AL_PET_SC_COUNTER.getKey(sysOrigin.name(), prizesCode);
  }
}
