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

import com.red.circle.common.business.dto.cmd.app.AppIdLongCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
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.other.infra.database.rds.entity.game.GameKtvConfig;
import com.red.circle.other.infra.database.rds.service.game.GameKtvConfigService;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 播放歌曲.
 *
 * @author pengshigang on 2023/5/6
 */
@Component
@RequiredArgsConstructor
public class GameKtvSongPlayExe {

  private final GameKtvCacheService gameKtvCacheService;
  private final GameKtvConfigService gameKtvConfigService;
  private final GameKtvUserSongCountService gameKtvUserSongCountService;

  public void execute(AppIdLongCmd cmd) {

    GameKtvUserSongCount song = gameKtvUserSongCountService.getById(cmd.getId());
    ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_RECORD_INFO, song);
    ResponseAssert.isTrue(CommonErrorCode.DATA_ERROR, Objects.equals(song.getStatus(), "WAIT"));

    GameKtvConfig gameKtvConfig = gameKtvConfigService.getById(song.getSongId());
    ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_RECORD_INFO, gameKtvConfig);

    // 修改歌曲状态
    gameKtvUserSongCountService.updateStatus(cmd.getId(), "PLAYING");
    gameKtvCacheService
        .cacheSongCountDown(song.getRoomId(), gameKtvConfig.getSongDuration() + 10);

  }

}
