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

import com.red.circle.common.business.core.AppBusinessUtils;
import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheService;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamMemberService;
import com.red.circle.other.infra.database.rds.service.team.BusinessDevelopmentBaseInfoService;
import com.red.circle.other.infra.database.rds.service.team.RoomBdLeadService;
import com.red.circle.other.infra.database.rds.service.user.user.InviteUserRegisterService;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import com.red.circle.other.inner.enums.config.EnumConfigKey;
import com.red.circle.wallet.inner.endpoint.freight.FreightBalanceClient;
import com.red.circle.wallet.inner.endpoint.freight.FreightSellerClient;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 是否允许使用语音房业务(需要验证系统:YOLO).
 *
 * @author pengliang on 2021/4/14
 */
@Component
@RequiredArgsConstructor
public class UserVoiceRoomAuthCheckQryExe {

  private final TeamMemberService teamMemberService;
  private final RoomBdLeadService roomBdLeadService;
  private final UserProfileGateway userProfileGateway;
  private final FreightSellerClient freightSellerClient;
  private final FreightBalanceClient freightBalanceClient;
  private final EnumConfigCacheService enumConfigCacheService;
  private final InviteUserRegisterService inviteUserRegisterService;
  private final BusinessDevelopmentBaseInfoService businessDevelopmentBaseInfoService;

  public Boolean execute(AppExtCommand cmd) {

    ResponseAssert.isTrue(CommonErrorCode.SYSTEM_DOES_NOT_SUPPORT, cmd.originEqYolo());

    UserProfile userProfile = getUserProfile(cmd);
    ResponseAssert.notNull(UserErrorCode.USER_INFO_NOT_FOUND, userProfile);
    ResponseAssert.isTrue(CommonErrorCode.SYSTEM_DOES_NOT_SUPPORT,
        AppBusinessUtils.eqSysOrigin(cmd.requireReqSysOrigin()));

    // 最高优先级判断
    if (Boolean.TRUE.equals(isFullyOpen(cmd))) {
      return Boolean.TRUE;
    }

    // 登录人是否具备使用语音房权限.
    if (Boolean.TRUE.equals(checkAuth(cmd.requireReqSysOriginEnum(), cmd.getReqUserId()))) {
      return Boolean.TRUE;
    }

    // 邀请人是否具备使用语音房权限.
    Long inviterUserId = getUserIdByInviteUserId(cmd);
    if (Objects.nonNull(inviterUserId) && Boolean.TRUE.equals(
        checkAuth(cmd.requireReqSysOriginEnum(), inviterUserId))) {
      return Boolean.TRUE;
    }

    return Boolean.FALSE;

  }

  private Boolean isFullyOpen(AppExtCommand cmd) {
    return enumConfigCacheService.getValueBool(EnumConfigKey.QI_BLA_VOICE_ROOM_FULLY_OPEN,
        cmd.requireReqSysOrigin());
  }

  private Long getUserIdByInviteUserId(AppExtCommand cmd) {
    return inviteUserRegisterService.getUserIdByInviteUserId(cmd.getReqUserId());
  }

  private Boolean checkAuth(SysOriginPlatformEnum sysOrigin, Long userId) {

    if (Boolean.TRUE.equals(teamMemberService.existsMember(userId))) {
      return Boolean.TRUE;
    }

    if (Boolean.TRUE.equals(roomBdLeadService.checkBdLeader(userId))) {
      return Boolean.TRUE;
    }

    if (Boolean.TRUE.equals(businessDevelopmentBaseInfoService.checkBD(userId))) {
      return Boolean.TRUE;
    }

    if (Boolean.TRUE.equals(
        ResponseAssert.requiredSuccess(freightSellerClient.existsSeller(userId)))) {
      return Boolean.TRUE;
    }

    if (Boolean.TRUE.equals(ResponseAssert.requiredSuccess(
        freightBalanceClient.checkDealer(sysOrigin, userId)))) {
      return Boolean.TRUE;
    }

    if (Boolean.TRUE.equals(
        ResponseAssert.requiredSuccess(freightBalanceClient.checkFreightAgent(userId)))) {
      return Boolean.TRUE;
    }

    return Boolean.FALSE;
  }

  private UserProfile getUserProfile(AppExtCommand cmd) {
    return userProfileGateway.getByUserId(cmd.getReqUserId());
  }

}
