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.GameLuckBoxKeys;
import com.red.circle.other.infra.database.cache.service.other.GameLuckyBoxCacheService;
import com.red.circle.tool.core.date.LocalDateUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * LuckyBox 缓存.
 *
 * @author pengshigang on 2022/4/29
 */
@Service
@RequiredArgsConstructor
public class GameLuckyBoxCacheServiceImpl implements GameLuckyBoxCacheService {

  private final RedisService redisService;

  @Override
  public void incrThisMonthPrizePool(String sysOrigin, long quantity) {
    redisService.increment(
        GameLuckBoxKeys.POOL.getKey(sysOrigin, LocalDateUtils.nowThisMonthToInteger()),
        quantity, 31, TimeUnit.DAYS);
  }

  @Override
  public void decrThisMonthPrizePool(String sysOrigin, long quantity) {
    redisService.decrement(
        GameLuckBoxKeys.POOL.getKey(sysOrigin, LocalDateUtils.nowThisMonthToInteger()),
        quantity, 30, TimeUnit.DAYS);
  }

  @Override
  public Long getThisMonthPrizePool(String sysOrigin) {
    return Optional.ofNullable(redisService.getStringToLong(
            GameLuckBoxKeys.POOL.getKey(sysOrigin, LocalDateUtils.nowThisMonthToInteger())))
        .orElse(0L);
  }

  @Override
  public void removeThisMonthPrizePool(String sysOrigin) {
    redisService.delete(
        GameLuckBoxKeys.POOL.getKey(sysOrigin, LocalDateUtils.nowThisMonthToInteger()));
  }

  @Override
  public String getLuckyDrawRatio(String sysOrigin, String defaultRatio) {
    String val = redisService.getString(
        GameLuckBoxKeys.LUCKY_DRAW_RATIO.getKey(sysOrigin));
    return StringUtils.isBlank(val) ? defaultRatio : val;
  }

  @Override
  public void cacheLuckyDrawRatio(String sysOrigin, String ratio) {
    redisService.setString(GameLuckBoxKeys.LUCKY_DRAW_RATIO.getKey(sysOrigin),
        ratio);
  }

  @Override
  public String getPoolPutRatio(String sysOrigin, String defaultRatio) {
    String val = redisService.getString(
        GameLuckBoxKeys.POOL_PUT_RATIO.getKey(sysOrigin));
    return StringUtils.isBlank(val) ? defaultRatio : val;
  }

  @Override
  public void cachePoolPutRatio(String sysOrigin, String ratio) {
    redisService.setString(GameLuckBoxKeys.POOL_PUT_RATIO.getKey(sysOrigin),
        ratio);
  }

  @Override
  public void incrThisMonthExtract(String sysOrigin, long quantity) {
    redisService.increment(
        GameLuckBoxKeys.EXTRACT.getKey(sysOrigin, LocalDateUtils.nowThisMonthToInteger()),
        quantity, 31, TimeUnit.DAYS);
  }

  @Override
  public Long getThisMonthExtract(String sysOrigin) {
    String val = redisService.getString(
        GameLuckBoxKeys.EXTRACT.getKey(sysOrigin, LocalDateUtils.nowThisMonthToInteger()));
    return StringUtils.isBlank(val) ? 0L : Long.parseLong(val);
  }

  @Override
  public void removeThisMonthExtract(String sysOrigin) {
    redisService.delete(
        GameLuckBoxKeys.EXTRACT.getKey(sysOrigin, LocalDateUtils.nowThisMonthToInteger()));
  }

  @Override
  public void incrSystemIncome(Long candyQuantity, SysOriginPlatformEnum sysOrigin) {
    redisService
        .increment(GameLuckBoxKeys.LUCKY_BOX_INCOME.getKey(sysOrigin.name()), candyQuantity, 7,
            TimeUnit.DAYS);
  }

  @Override
  public Long getSystemIncome(SysOriginPlatformEnum sysOrigin) {

    return Optional.ofNullable(redisService
            .getStringToLong(GameLuckBoxKeys.LUCKY_BOX_INCOME.getKey(sysOrigin.name())))
        .orElse(0L);
  }

  @Override
  public void incrSystemExpenditure(Long candyQuantity, SysOriginPlatformEnum sysOrigin) {
    redisService
        .increment(GameLuckBoxKeys.LUCKY_BOX_EXPENDITURE.getKey(sysOrigin.name()),
            candyQuantity, 7, TimeUnit.DAYS);
  }

  @Override
  public Long getSystemExpenditure(SysOriginPlatformEnum sysOrigin) {

    return Optional.ofNullable(redisService.getStringToLong(
            GameLuckBoxKeys.LUCKY_BOX_EXPENDITURE.getKey(sysOrigin.name())))
        .orElse(0L);
  }

  @Override
  public String getLuckyUserListStr(SysOriginPlatformEnum sysOrigin) {
    return redisService
        .getString(GameLuckBoxKeys.LUCKY_BOX_TOP_USER.getKey(sysOrigin.name()));
  }

  @Override
  public void setLuckyUserListStr(SysOriginPlatformEnum sysOrigin, String listStr) {

    redisService.setString(GameLuckBoxKeys.LUCKY_BOX_TOP_USER.getKey(sysOrigin.name()),
        listStr, 30, TimeUnit.DAYS);
  }

}
