package com.red.circle.other.app.listener;


import com.red.circle.component.mq.MessageEventProcess;
import com.red.circle.component.mq.MessageEventProcessDescribe;
import com.red.circle.component.mq.config.RocketMqMessageListener;
import com.red.circle.component.mq.service.Action;
import com.red.circle.component.mq.service.ConsumerMessage;
import com.red.circle.component.mq.service.MessageListener;
import com.red.circle.mq.business.model.event.gift.GameLuckyGiftBusinessEvent;
import com.red.circle.mq.rocket.business.streams.GameLuckyGiftBusinessSink;
import com.red.circle.other.app.common.gift.GameLuckyGiftCommon;
import com.red.circle.other.app.common.gift.GameLuckyGiftParamCmd;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import com.red.circle.other.infra.database.mongo.entity.live.RoomProfile;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.tool.core.date.LocalDateTimeUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 游戏幸运礼物抽奖.
 *
 * @author pengshigang on 2023/7/28
 */
@Slf4j
@RocketMqMessageListener(groupId = GameLuckyGiftBusinessSink.INPUT, tag = GameLuckyGiftBusinessSink.TAG)
@RequiredArgsConstructor
public class GameLuckyGiftBusinessListener implements MessageListener {

  private final UserRegionGateway userRegionGateway;
  private final MessageEventProcess messageEventProcess;
  private final GameLuckyGiftCommon gameLuckyGiftManager;
  private final RoomProfileManagerService roomProfileManagerService;

  @Override
  public Action consume(ConsumerMessage message) {

    return messageEventProcess.consume(
        MessageEventProcessDescribe.builder()
            .logTag("游戏幸运礼物抽奖")
            .consumeMsgTimeoutMinute(60 * 3)
            .repeatConsumeMinute(30)
            .repeatConsumeTag("GameLuckyGiftBusiness")
            .checkConsumeTag(GameLuckyGiftBusinessSink.TAG)
            .message(message)
            .build(),
        GameLuckyGiftBusinessEvent.class,
        this::processLottery);
  }

  /**
   * 保存幸运礼物流水.
   */
  private void processLottery(GameLuckyGiftBusinessEvent event) {

    if (Objects.isNull(event.getRoomId())) {
      return;
    }

    long startTime = LocalDateTimeUtils.nowEpochMilli();

    try {
      RoomProfile roomProfile = roomProfileManagerService.getProfileById(event.getRoomId());
      if (Objects.isNull(roomProfile)) {
        return;
      }

      GameLuckyGiftParamCmd cmd = buildLuckyGiftRunParam(event, roomProfile);

      event.getUsers().forEach(user -> {

        cmd.setId(user.getId());
        cmd.setGiftsCount(user.getGiftsCount());
        cmd.setAcceptUserId(user.getAcceptUserId());
        cmd.setComboLossCount(user.getComboLossCount());
        gameLuckyGiftManager.run(cmd);
      });
    } finally {
      long endTime = LocalDateTimeUtils.nowEpochMilli() - startTime;
      if (endTime > (1000 * 20)) {
        log.warn("[游戏幸运礼物抽奖] 队列处理耗时:{}", endTime);
      }
    }

  }

  private GameLuckyGiftParamCmd buildLuckyGiftRunParam(GameLuckyGiftBusinessEvent event,
      RoomProfile roomProfile) {
    return new GameLuckyGiftParamCmd()
        .setUserId(event.getUserId())
        .setRoomId(event.getRoomId())
        .setRoomAccount(roomProfile.getRoomAccount())
        .setSysOrigin(event.getSysOrigin())
        .setGiftId(event.getGiftId())
        .setQuantity(event.getQuantity())
        .setCheckCombo(event.getCheckCombo())
        .setGiftCombos(event.getGiftCombos())
        .setOpenComboLoss(event.getOpenComboLoss())
        .setSysComboLoss(event.getSysComboLoss())
        .setOpenGiftGiveTotal(event.getOpenGiftGiveTotal())
        .setSysGiftGiveTotal(event.getSysGiftGiveTotal())
        .setRegionCode(userRegionGateway.getRegionCode(event.getUserId()));
  }


}
