package com.red.circle.other.app.common.game;

import com.red.circle.external.inner.endpoint.message.ImGroupClient;
import com.red.circle.external.inner.model.cmd.message.BroadcastGroupMsgBodyCmd;
import com.red.circle.external.inner.model.enums.message.GroupMessageTypeEnum;
import com.red.circle.mq.business.model.event.game.GameRoomVoteEvent;
import com.red.circle.other.app.dto.clientobject.game.GameRoomVoteCO;
import com.red.circle.other.app.dto.clientobject.game.GameRoomVoteOptionCO;
import com.red.circle.other.infra.database.cache.service.other.GameRoomVoteCacheService;
import com.red.circle.other.infra.database.rds.entity.game.GameRoomVoteInfoRecord;
import com.red.circle.other.infra.database.rds.entity.game.GameRoomVoteRecord;
import com.red.circle.other.infra.database.rds.service.game.GameRoomVoteInfoRecordService;
import com.red.circle.other.infra.database.rds.service.game.GameRoomVoteRecordService;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.TimestampUtils;
import java.util.Comparator;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 处理房间投票业务.
 *
 * @author pengshigang
 * @since 2022-01-05
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class GameRoomVoteCommon {

  private final ImGroupClient imGroupClient;
  private final GameRoomVoteCacheService gameRoomVoteCacheService;
  private final GameRoomVoteRecordService gameRoomVoteRecordService;
  private final GameRoomVoteInfoRecordService gameRoomVoteInfoRecordService;

  public void processGame(GameRoomVoteEvent event) {

    if (Objects.isNull(event) || Objects.isNull(event.getGameId())) {
      return;
    }

    GameRoomVoteRecord gameRoomVoteRecord = gameRoomVoteRecordService.getById(event.getGameId());
    if (Objects.isNull(gameRoomVoteRecord) || gameRoomVoteRecord.getGameOver()) {
      return;
    }
    gameRoomVoteRecord.setGameOver(Boolean.TRUE);
    gameRoomVoteRecord.setUpdateTime(TimestampUtils.now());

    GameRoomVoteCO gameRoomVote = gameRoomVoteCacheService
        .getRoomVote(event.getGameId(), GameRoomVoteCO.class);

    if (Objects.isNull(gameRoomVote) || CollectionUtils.isEmpty(gameRoomVote.getOptions())) {
      gameRoomVoteRecordService.updateSelectiveById(gameRoomVoteRecord);
      return;
    }

    gameRoomVoteInfoRecordService.updateBatchById(
        gameRoomVote.getOptions().stream().map(option -> new GameRoomVoteInfoRecord()
            .setId(option.getId())
            .setVotes(option.getVotes())
        ).collect(Collectors.toList())
    );
    gameRoomVoteRecordService.updateSelectiveById(gameRoomVoteRecord);

    //降序票数生产列表返回结果
    gameRoomVote.setOptions(gameRoomVote.getOptions().stream().sorted(Comparator
        .comparing(GameRoomVoteOptionCO::getVotes).reversed()).collect(Collectors.toList()));

    sendMsg(gameRoomVote);

    gameRoomVoteCacheService.deleteRoomVote(gameRoomVote.getId());

  }

  /**
   * 发送消息
   *
   * @param gameRoomVote 投票信息
   */
  private void sendMsg(GameRoomVoteCO gameRoomVote) {

    imGroupClient.sendMessageBroadcast(BroadcastGroupMsgBodyCmd.builder()
        .allPlatform()
        .type(GroupMessageTypeEnum.GAME_ROOM_VOTE_END)
        .data(gameRoomVote)
        .build());
  }

}
