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

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.dto.cmd.room.LockedRoomCmd;
import com.red.circle.other.domain.gateway.props.PropsNobleVipGateway;
import com.red.circle.other.infra.database.mongo.entity.live.RoomProfileManager;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.infra.database.rds.service.props.PropsRoomLockService;
import com.red.circle.other.inner.asserts.RoomErrorCode;
import com.red.circle.other.inner.model.dto.material.props.PropsNobleVipAbilityDTO;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 房间上锁.
 *
 * @author pengliang on 2021/6/3
 */
@Component
@RequiredArgsConstructor
public class LockedRoomCmdExe {

  private final PropsRoomLockService propsRoomLockService;
  private final PropsNobleVipGateway propsNobleVipGateway;
  private final RoomProfileManagerService roomProfileManagerService;

  public void execute(LockedRoomCmd cmd) {
    RoomProfileManager manager = roomProfileManagerService.getById(cmd.getRoomId());
    // 房间资料缺失
    ResponseAssert.notNull(RoomErrorCode.ROOM_INFO_MISSING, manager);
    // 权限不足
    ResponseAssert.isTrue(CommonErrorCode.INSUFFICIENT_PERMISSION,
        Objects.equals(cmd.requiredReqUserId(), manager.getUserId()));

//    checkAbility(cmd);

    roomProfileManagerService.updatePassword(cmd.getRoomId(), cmd.getPassword());
  }

  private void checkAbility(LockedRoomCmd cmd) {
    PropsNobleVipAbilityDTO nobleVipAbility = propsNobleVipGateway
        .getUserMaxAbilityDTO(cmd.requiredReqUserId());
    if (Objects.isNull(nobleVipAbility) || nobleVipAbility.getVipLevel() < 5) {
      // 请购买房间锁
      ResponseAssert.isTrue(RoomErrorCode.PLEASE_BUY_ROOM_LOCK,
          propsRoomLockService.existsExpireTime(cmd.requiredReqUserId()));
    }
  }

}
