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

import com.google.common.collect.Sets;
import com.red.circle.common.business.dto.cmd.app.AppIdLongCmd;
import com.red.circle.other.infra.database.cache.service.other.GameKtvCacheService;
import com.red.circle.other.infra.database.mongo.entity.game.ktv.GameKtvUserSongCount;
import com.red.circle.other.infra.database.mongo.service.game.GameKtvUserSongCountService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * KTV 移除准备播放的歌曲.
 *
 * @author pengshigang on 2023/5/6
 */
@Component
@RequiredArgsConstructor
public class GameKtvDelWaitSongExe {

  private final GameKtvCacheService gameKtvCacheService;
  private final GameKtvUserSongCountService gameKtvUserSongCountService;

  public void execute(AppIdLongCmd cmd) {

    GameKtvUserSongCount song = gameKtvUserSongCountService.getById(cmd.getId());
    if (Objects.isNull(song)) {
      return;
    }
    gameKtvUserSongCountService.deleteByIds(Sets.newHashSet(cmd.getId()));

    List<GameKtvUserSongCount> songs = listExistSong(song);
    if (CollectionUtils.isNotEmpty(songs)) {
      gameKtvUserSongCountService.updateStatus(songs.get(0).getId(), "WAIT");
    }
    gameKtvCacheService.cacheWaitCountDown(song.getRoomId());
    gameKtvCacheService.removeSongCountDown(song.getRoomId());

  }

  private List<GameKtvUserSongCount> listExistSong(GameKtvUserSongCount song) {
    return gameKtvUserSongCountService.list(song.getSysOrigin(), song.getRoomId());
  }

}
