package com.red.circle.other.infra.database.mongo.service.game.impl;

import com.mongodb.client.model.Filters;
import com.red.circle.component.mongodb.config.MongoPrimaryService;
import com.red.circle.other.infra.database.mongo.entity.game.luckygift.GameLuckyGiftBonusPool;
import com.red.circle.other.infra.database.mongo.service.game.GameLuckyGiftBonusPoolService;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

/**
 * 幸运礼物游戏奖金池.
 *
 * @author pengshigang on 2023/3/22
 */
@Service
@RequiredArgsConstructor
public class GameLuckyGiftBonusPoolServiceImpl implements GameLuckyGiftBonusPoolService {

  private final MongoTemplate mongoTemplate;
  private final MongoPrimaryService mongoPrimaryService;

  @Override
  public void incr(String sysOrigin, Long userId, Long lowBonusPool, Long highBonusPool) {

    GameLuckyGiftBonusPool bonusPool = getByUserId(userId);
    if (Objects.isNull(bonusPool)) {
      mongoTemplate.save(new GameLuckyGiftBonusPool()
          .setId(IdWorkerUtils.getIdStr())
          .setSysOrigin(sysOrigin)
          .setUserId(userId)
          .setLowBonusPool(lowBonusPool)
          .setHighBonusPool(highBonusPool)
          .setCreateTime(TimestampUtils.now())
      );
      return;
    }

    mongoTemplate.updateFirst(Query.query(Criteria.where("id").is(bonusPool.getId())),
        new Update()
            .inc("lowBonusPool", lowBonusPool)
            .inc("highBonusPool", highBonusPool)
            .set("updateTime", TimestampUtils.now()),
        GameLuckyGiftBonusPool.class);

  }

  @Override
  public Boolean decrLow(Long userId, Long quantity) {

    GameLuckyGiftBonusPool bonusPool = getByUserId(userId);
    if (Objects.isNull(bonusPool)) {
      return Boolean.FALSE;
    }

    Long lowBonusPool = bonusPool.getLowBonusPool();
    if (lowBonusPool < quantity) {
      return Boolean.FALSE;
    }
//    if (lowBonusPool >= 0) {
//      lowBonusPool = bonusPool.getLowBonusPool() - quantity;
//    } else {
//      lowBonusPool = -quantity + bonusPool.getLowBonusPool();
//    }
//    if (lowBonusPool <= -200) {
//      return Boolean.FALSE;
//    }

    mongoTemplate.updateFirst(Query.query(Criteria.where("id").is(bonusPool.getId())),
        new Update()
            .inc("lowBonusPool", -quantity)
            .set("updateTime", TimestampUtils.now()),
        GameLuckyGiftBonusPool.class);

    return Boolean.TRUE;
  }

  @Override
  public Boolean decrHigh(Long userId, Long quantity) {

    GameLuckyGiftBonusPool bonusPool = getByUserId(userId);
    if (Objects.isNull(bonusPool)) {
      return Boolean.FALSE;
    }

    Long highBonusPool = bonusPool.getHighBonusPool();
//    if (highBonusPool >= 0) {
//      highBonusPool = bonusPool.getHighBonusPool() - quantity;
//    } else {
//      highBonusPool = -quantity + bonusPool.getHighBonusPool();
//    }
//    if (highBonusPool <= -400) {
//      return Boolean.FALSE;
//    }
    if (highBonusPool < quantity) {
      return Boolean.FALSE;
    }

    mongoTemplate.updateFirst(Query.query(Criteria.where("id").is(bonusPool.getId())),
        new Update()
            .inc("highBonusPool", -quantity)
            .set("updateTime", TimestampUtils.now()),
        GameLuckyGiftBonusPool.class);
    return Boolean.TRUE;
  }


  @Override
  public GameLuckyGiftBonusPool getByUserId(Long userId) {

    Document document = mongoPrimaryService.find("game_lucky_gift_bonus_pool",
        Filters.eq("userId", userId),
        documents -> CollectionUtils.isEmpty(documents) ? null : documents.get(0));

    if (Objects.isNull(document)) {
      return null;
    }

    return new GameLuckyGiftBonusPool()
        .setId(document.getString("_id"))
        .setSysOrigin(document.getString("sysOrigin"))
        .setUserId(document.getLong("userId"))
        .setLowBonusPool(document.getLong("lowBonusPool"))
        .setHighBonusPool(document.getLong("highBonusPool"));

  }

}
