package com.red.circle.other.app.command.party3rd.callback.trtc;

import com.red.circle.external.inner.model.callback.trtc.TrtcCallbackEvent;
import com.red.circle.other.infra.database.cache.service.other.RoomManagerCacheService;
import com.red.circle.other.infra.database.mongo.service.live.ActiveVoiceRoomService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 退出房间.
 *
 * @author pengliang on 2021/7/24
 */
@Slf4j
@Component("EVENT_TYPE_EXIT_ROOM")
@RequiredArgsConstructor
public class ExitRoomCallbackStrategy implements TrtcCallbackStrategy {

  private final ActiveVoiceRoomService activeVoiceRoomService;
  private final RoomManagerCacheService roomManagerCacheService;

  @Override
  public void doOperation(TrtcCallbackEvent event) {

    //    if (event.isReasonKill()) {
    //      log.warn("杀进：{},{}",event.getRoomId(),event.getUserIdLongValue());
    //      tencentTrtcService.removeUser(event.getRoomIdLongValve(),
    //          Collections.singletonList(event.getUserIdLongValue()));
    //    }

    //    Long roomId = roomProfileService.getRoomIdByAccount(event.getRoomIdLongValve());
    //    if (Objects.isNull(roomId)) {
    //      log.warn("数据异常:{}", roomId);
    //      return;
    //    }
    //    boosSeatCacheService.removeSeat(roomId, event.getRoomIdLongValve());

    if (!activeVoiceRoomService.existsByRoomAccount(event.getRoomId())) {
      roomManagerCacheService.removeNumberPeople(event.getRoomId());
      return;
    }

    Long quantity = roomManagerCacheService.decrementNumberPeople(event.getRoomId());

    if (quantity < 1) {
      roomManagerCacheService.removeNumberPeople(event.getRoomId());
      activeVoiceRoomService.removeByRoomAccount(event.getRoomId());
      return;
    }
    activeVoiceRoomService.updateQuantityByRoomAccount(event.getRoomId(),
        quantity);
  }

}
