package com.red.circle.other.app.command.room.query;

import com.red.circle.common.business.dto.cmd.app.AppRoomIdCmd;
import com.red.circle.other.infra.database.mongo.entity.live.RoomSetting;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.infra.database.rds.service.live.RoomMemberService;
import com.red.circle.other.inner.enums.live.RoomUserRolesEnum;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 上/解锁锁麦克风操作验证.
 *
 * @author pengliang on 2021/03/30
 */
@Component
@RequiredArgsConstructor
public class RoomLockAndUnLockCheckQryExe {

  private final RoomMemberService roomMemberService;
  private final RoomProfileManagerService roomProfileManagerService;

  public boolean execute(AppRoomIdCmd cmd) {

    RoomSetting roomSetting = roomProfileManagerService
        .getRoomSettingById(cmd.getRoomId());

    // 锁麦克风-验证是否是房主
    RoomUserRolesEnum roles = roomMemberService
        .getRoomUserRoles(cmd.requiredReqUserId(), cmd.getRoomId());

    boolean isHomeownerOrAdmin = Objects.equals(roles, RoomUserRolesEnum.HOMEOWNER) || Objects
        .equals(roles, RoomUserRolesEnum.ADMIN);
    if (Objects.equals(roomSetting.getAdminLockSeat(), Boolean.TRUE) && isHomeownerOrAdmin) {
      return true;
    }

    return Objects.equals(roles, RoomUserRolesEnum.HOMEOWNER);
  }

}
