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

import com.red.circle.common.business.dto.cmd.app.AppIdLongCmd;
import com.red.circle.common.business.dto.cmd.app.AppRoomIdCmd;
import com.red.circle.other.app.command.game.ludo.GameLudoCreateCmdExe;
import com.red.circle.other.app.command.game.ludo.GameLudoJoinCmdExe;
import com.red.circle.other.app.command.game.ludo.GameLudoLatestDisbandCmdExe;
import com.red.circle.other.app.command.game.ludo.GameLudoLatestEndCmdExe;
import com.red.circle.other.app.command.game.ludo.GameLudoRoomRunQueryExe;
import com.red.circle.other.app.command.game.ludo.GameLudoStartCmdExe;
import com.red.circle.other.app.command.game.ludo.GameRoomExistsQueryExe;
import com.red.circle.other.app.dto.clientobject.game.GameLudoRunCO;
import com.red.circle.other.app.dto.cmd.game.GameLudoCreateCmd;
import com.red.circle.other.infra.database.mongo.entity.game.ludo.GameLudoRun;
import com.red.circle.other.infra.database.mongo.service.game.GameLudoRunService;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import com.red.circle.other.app.dto.cmd.game.GameLudoEndCmd;
import com.red.circle.other.app.dto.cmd.game.GameLudoJoinCmd;
import com.red.circle.other.app.dto.cmd.game.GameLudoStartCmd;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * lodu 游戏服务实现.
 *
 * @author pengliang on 2021/7/27
 */
@Service
@RequiredArgsConstructor
public class GameLudoServiceImpl implements GameLudoService {

  private final GameLudoJoinCmdExe gameLudoJoinCmdExe;
  private final GameLudoStartCmdExe gameLudoStartCmdExe;
  private final GameLudoCreateCmdExe gameLudoCreateCmdExe;
  private final GameRoomExistsQueryExe gameRoomExistsQueryExe;
  private final GameLudoRoomRunQueryExe gameLudoRoomRunQueryExe;
  private final GameLudoLatestEndCmdExe gameLudoLatestEndCmdExe;
  private final GameLudoLatestDisbandCmdExe gameLudoLatestDisbandCmdExe;
  private final GameLudoRunService gameLudoRunService;

  @Override
  public GameLudoRunCO getRoomGame(AppRoomIdCmd cmd) {
    return gameLudoRoomRunQueryExe.execute(cmd);
  }

  @Override
  public Boolean roomExistsGame(AppRoomIdCmd cmd) {
    return gameRoomExistsQueryExe.execute(cmd);
  }

  @Override
  public GameLudoRunCO create(GameLudoCreateCmd cmd) {
    return gameLudoCreateCmdExe.execute(cmd);
  }

  @Override
  public void start(GameLudoStartCmd cmd) {
    gameLudoStartCmdExe.execute(cmd);
  }

  @Override
  public void join(GameLudoJoinCmd cmd) {
    gameLudoJoinCmdExe.execute(cmd);
  }

  @Override
  public void disband(AppIdLongCmd cmd) {
    gameLudoLatestDisbandCmdExe.execute(cmd);
  }

  @Override
  public void end(GameLudoEndCmd cmd) {
    gameLudoLatestEndCmdExe.execute(cmd);
  }

  @Override
  public Map<Long, String> listGameCoverByRoomIds(List<Long> roomIds) {
    return gameLudoRunService.listByRoomIds(roomIds).stream()
        .filter(run -> Objects.nonNull(run.getGameInfo()) && Objects.nonNull(run.getGameInfo().getCover()))
        .collect(Collectors.toMap(GameLudoRun::getRoomId, run -> run.getGameInfo().getCover(), (a, b) -> a));
  }

}
