package com.red.circle.live.infra.database.cache.service.impl;

import com.red.circle.component.redis.service.RedisService;
import com.red.circle.live.domain.live.LiveMusicStatus;
import com.red.circle.live.infra.database.cache.key.LiveMusicKey;
import com.red.circle.live.infra.database.cache.service.LiveMusicHeartbeatService;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 直播播放音乐心跳.
 *
 * @author pengliang on 2024/1/8
 */
@Service
@RequiredArgsConstructor
public class LiveMusicHeartbeatServiceImpl implements LiveMusicHeartbeatService {

  private final RedisService redisService;

  @Override
  public LiveMusicStatus getMusicUser(Long roomId) {
    return redisService.getStringToObject(LiveMusicKey.ROOM.getKey(roomId),
        LiveMusicStatus.class);
  }

  @Override
  public String getMusicUserInfo(Long roomId) {
    return redisService.getString(LiveMusicKey.ROOM.getKey(roomId));
  }

  @Override
  public void setMusicUser(Long roomId, LiveMusicStatus status) {
    redisService.setString(LiveMusicKey.ROOM.getKey(roomId), status, 1, TimeUnit.DAYS);
  }

  @Override
  public void removeMickUser(Long roomId) {
    redisService.delete(LiveMusicKey.ROOM.getKey(roomId));
  }

  @Override
  public boolean lockMickOps(Long roomId) {
    return redisService.setIfAbsent(LiveMusicKey.LOCK.getKey(String.valueOf(roomId+"PLAY_LOCK")), 60,TimeUnit.SECONDS);
  }

  @Override
  public boolean existsMickOps(Long roomId) {
    return redisService.hash(LiveMusicKey.LOCK.getKey(roomId));
  }

  @Override
  public void unLockMickOps(Long roomId) {
    redisService.delete(LiveMusicKey.LOCK.getKey(String.valueOf(roomId+"PLAY_LOCK")));
  }
}
