package com.red.circle.other.app.command.game.roompk;


import com.red.circle.common.business.core.enums.GameStateEnum;
import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.common.business.dto.cmd.RoomIdLastIdCmd;
import com.red.circle.other.app.dto.clientobject.game.GameRoomPkAwaitListCO;
import com.red.circle.other.app.dto.clientobject.game.GameRoomPkUserCO;
import com.red.circle.other.infra.database.cache.service.other.GameRoomPkCacheService;
import com.red.circle.other.infra.database.rds.entity.game.GameRoomPkRecord;
import com.red.circle.other.infra.database.rds.service.game.GameRoomPkRecordService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 房间PK - 等待中的PK.
 *
 * @author pengshigang on 2021/9/25
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class GameRoomPkAwaitListCmdExe {

  private final GameRoomPkCacheService gameRoomPkCacheService;
  private final GameRoomPkRecordService gameRoomPkRecordService;

  public List<GameRoomPkAwaitListCO> execute(RoomIdLastIdCmd cmd) {

    List<GameRoomPkRecord> pkList = listWait(cmd);

    GameRoomPkRecord ownRoomPkRecord = getByRoomIdByStatus(cmd);
    if (Objects.nonNull(ownRoomPkRecord)) {
      pkList.add(0, ownRoomPkRecord);
    }

    GameRoomPkRecord ownPkRecord = getByUserIdByStatus(cmd);
    if (Objects.nonNull(ownPkRecord) && Objects.isNull(ownRoomPkRecord)) {
      pkList.add(0, ownPkRecord);
    }

    List<Long> ids = new ArrayList<>();
    return pkList.stream().map(pk -> {

      GameRoomPkUserCO pkCO = gameRoomPkCacheService.getRoomPk(pk.getId(), GameRoomPkUserCO.class);
      if (Objects.isNull(pkCO)) {
        return null;
      }

      if (ids.size() >= 1 && ids.contains(pk.getId())){
        return null;
      }else {
        ids.add(pk.getId());
      }

      return new GameRoomPkAwaitListCO()
          .setId(pk.getId())
          .setSponsorUserId(pkCO.getSponsorUserId())
          .setSponsorRoomId(pkCO.getSponsorRoomId())
          .setSponsorCover(pkCO.getSponsorCover())
          .setSponsorName(pkCO.getSponsorName())
          .setSponsorUserAccount(pkCO.getSponsorUserAccount())
          .setSponsorUserRegion(pkCO.getSponsorUserRegion())
          .setPkType(pk.getPkType());
    }).filter(Objects::nonNull).collect(Collectors.toList());

  }

  private GameRoomPkRecord getByUserIdByStatus(RoomIdLastIdCmd cmd) {
    return gameRoomPkRecordService
        .getByUserIdByStatus(cmd.requiredReqUserId(), GameStateEnum.WAITING);
  }

  private GameRoomPkRecord getByRoomIdByStatus(RoomIdLastIdCmd cmd) {
    return gameRoomPkRecordService
        .getByRoomIdByStatus(cmd.getRoomId(), GameStateEnum.WAITING);
  }

  private List<GameRoomPkRecord> listWait(RoomIdLastIdCmd cmd) {
    return gameRoomPkRecordService
        .listByLastIdByStatus(cmd.getLastId(),
            SysOriginPlatformEnum.valueOf(cmd.getReqSysOrigin().getOrigin()),
            GameStateEnum.WAITING, cmd.getRoomId(), cmd.requiredReqUserId());
  }


}
