package com.red.circle.mq.rocket.business.producer;


import com.red.circle.mq.business.model.event.game.GameBurstCrystalEvent;
import com.red.circle.mq.business.model.event.game.GameIndoorTeamPkEvent;
import com.red.circle.mq.business.model.event.game.GameRedPacketEvent;
import com.red.circle.mq.business.model.event.game.GameRoomPkEvent;
import com.red.circle.mq.business.model.event.game.GameRoomVoteEvent;
import com.red.circle.mq.business.model.event.game.GameTurntableEvent;
import com.red.circle.mq.rocket.business.streams.GameBurstCrystalSink;
import com.red.circle.mq.rocket.business.streams.GameIndoorTeamPkSink;
import com.red.circle.mq.rocket.business.streams.GameRedPacketSink;
import com.red.circle.mq.rocket.business.streams.GameRoomPkSink;
import com.red.circle.mq.rocket.business.streams.GameRoomVoteSink;
import com.red.circle.mq.rocket.business.streams.GameTurntableAutoEndSink;
import com.red.circle.component.mq.MessageEvent;
import com.red.circle.component.mq.service.FixedTopic;
import com.red.circle.component.mq.service.SenderService;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 游戏相关.
 *
 * @author pengliang on 2021/5/18
 */
@Component
@RequiredArgsConstructor
public class GameMqMessage {

  private final SenderService senderService;

  /**
   * 游戏转盘.
   */
  public void turntableDelayed(String consumeId, GameTurntableEvent event) {
    senderService.sendEventMessage(MessageEvent.builder()
        .consumeId(consumeId)
        .topic(FixedTopic.RC_DEFAULT_APP_DELAYED)
        .tag(GameTurntableAutoEndSink.TAG)
        .body(event)
        .startDeliverTime(event.getStartDeliverTime())
        .build());
  }

  /**
   * 红包游戏.
   */
  public void redPacket(String consumeId, GameRedPacketEvent event, Long startDeliverTime) {
    senderService.sendEventMessage(MessageEvent.builder()
        .consumeId(consumeId)
        .topic(FixedTopic.RC_DEFAULT_APP_DELAYED)
        .tag(GameRedPacketSink.TAG)
        .body(event)
        .startDeliverTime(startDeliverTime)
        .build());
  }

  /**
   * 爆水晶.
   */
  public void burstCrystal(GameBurstCrystalEvent event) {
    senderService.sendEventMessage(MessageEvent.builder()
        .consumeId(IdWorkerUtils.getIdStr())
        .tag(GameBurstCrystalSink.TAG)
        .body(event)
        .build());
  }

  /**
   * 房间pk.
   */
  public void roomPkDelayed(GameRoomPkEvent event) {
    senderService.sendEventMessage(MessageEvent.builder()
        .consumeId(IdWorkerUtils.getIdStr())
        .topic(FixedTopic.RC_DEFAULT_APP_DELAYED)
        .tag(GameRoomPkSink.TAG)
        .body(event)
        .startDeliverTime(TimestampUtils.nowPlusMinutes(event.getDelayMinutes()).getTime())
        .build());
  }

  /**
   * 房间内团队pk.
   */
  public void indoorTeamPkDelayed(GameIndoorTeamPkEvent event) {
    senderService.sendEventMessage(MessageEvent.builder()
        .consumeId(IdWorkerUtils.getIdStr())
        .topic(FixedTopic.RC_DEFAULT_APP_DELAYED)
        .tag(GameIndoorTeamPkSink.TAG)
        .body(event)
        .startDeliverTime(TimestampUtils.nowPlusMinutes(event.getDelayMinutes()).getTime())
        .build());
  }

  /**
   * 房间投票.
   */
  public void roomVoteDelayed(GameRoomVoteEvent event) {
    senderService.sendEventMessage(MessageEvent.builder()
        .consumeId(IdWorkerUtils.getIdStr())
        .topic(FixedTopic.RC_DEFAULT_APP_DELAYED)
        .tag(GameRoomVoteSink.TAG)
        .body(event)
        .startDeliverTime(TimestampUtils.nowPlusSeconds(event.getDelaySecond()).getTime())
        .build());
  }

}
