package com.red.circle.other.app.service.user.user;

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.app.command.user.UpdateUserProfileCmdExe;
import com.red.circle.other.app.command.user.query.UserProfileCardQryExe;
import com.red.circle.other.app.command.user.query.UserProfileUsePropsQryExe;
import com.red.circle.other.app.command.user.query.UserProfileUseQryExe;
import com.red.circle.other.app.command.user.query.UserProfileUseSearchQryExe;
import com.red.circle.other.app.command.user.query.UserVoiceRoomAuthCheckQryExe;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.clientobject.user.user.UserProfileCardCO;
import com.red.circle.other.app.dto.clientobject.user.user.UserProfileUsePropsCO;
import com.red.circle.other.app.dto.cmd.user.user.AccountQryCmd;
import com.red.circle.other.app.dto.cmd.user.user.UserCardQryCmd;
import com.red.circle.other.app.dto.cmd.user.user.UserProfileModifyCmd;
import com.red.circle.other.app.dto.cmd.user.user.UserProfileQryCmd;
import com.red.circle.other.domain.gateway.user.UserRunProfileTransportGateway;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.infra.database.mongo.entity.user.profile.UserSpecialId;
import com.red.circle.other.infra.database.mongo.service.user.profile.UserSpecialIdService;
import com.red.circle.other.infra.gateway.user.UserRegionGatewayImpl;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import java.util.Objects;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author pengliang on 2020/11/2
 */
@Service
@RequiredArgsConstructor
public class UserProfileServiceImpl implements UserProfileService {

  private final UserProfileUseQryExe userProfileUseQryExe;
  private final UserProfileCardQryExe userProfileCardQryExe;
  private final UpdateUserProfileCmdExe updateUserProfileCmdExe;
  private final UserProfileUsePropsQryExe userProfileUsePropsQryExe;
  private final UserProfileUseSearchQryExe userProfileUseByAccountQryExe;
  private final UserVoiceRoomAuthCheckQryExe userVoiceRoomAuthCheckQryExe;
  private final UserRunProfileTransportGateway userRunProfileTransportGateway;
  private final UserProfileAppConvertor userProfileAppConvertor;
  private final UserSpecialIdService userSpecialIdService;
  private final UserRegionGateway userRegionGateway;

  @Override
  public UserProfileDTO getUserProfile(UserProfileQryCmd cmd) {
    return userProfileUseQryExe.execute(cmd);
  }

  public UserProfileUsePropsCO getPropsUserProfile(UserProfileQryCmd cmd) {
    return userProfileUsePropsQryExe.execute(cmd);
  }

  @Override
  public UserProfileDTO getUserProfile(AccountQryCmd cmd) {
    UserProfileDTO userProfileDTO = userProfileUseByAccountQryExe.execute(cmd);
    if (Objects.isNull(userProfileDTO)) {
      return null;
    }
    if (userRegionGateway.checkEqRegion(cmd.getReqUserId(), userProfileDTO.getId())) {
      return userProfileDTO;
    }
    return null;
  }

    @Override
    public UserProfileDTO getUserOpenProfile(AccountQryCmd cmd) {
        return userProfileUseByAccountQryExe.execute(cmd);
    }

  @Override
  public UserProfileCardCO getUserProfileCard(UserCardQryCmd cmd) {
    return userProfileCardQryExe.execute(cmd);
  }

  @Override
  public UserProfileDTO updateUserProfile(UserProfileModifyCmd cmd) {
    return updateUserProfileCmdExe.execute(cmd);
  }

  @Override
  public Boolean checkVoiceRoomAuth(AppExtCommand cmd) {
    return userVoiceRoomAuthCheckQryExe.execute(cmd);
  }

  @Override
  public UserProfileDTO getByAccount(String sysOrigin, String account) {
    UserProfile userProfile = userRunProfileTransportGateway.getByAccount(account);
    if (Objects.nonNull(userProfile) && Objects.equals(userProfile.getOriginSys(), sysOrigin)) {
      return userProfileAppConvertor.toUserProfileDTO(userProfile);
    }
    /*UserSpecialId userSpecialId = userSpecialIdService.getUserSpecialId(sysOrigin, account);
    if (Objects.isNull(userSpecialId)) {
      userProfile = userRunProfileTransportGateway.getByUserId(userSpecialId.getUserId());
      return userProfileAppConvertor.toUserProfileDTO(userProfile);
    }
   return null;*/
    UserSpecialId userSpecialId = userSpecialIdService.getUserSpecialId(sysOrigin, account);
    if (Objects.isNull(userSpecialId)) {
      return null;
    } else if (userSpecialId.getExpiredTime() != null) {
      if (userSpecialId.getExpiredTime().getTime() < System.currentTimeMillis()) {
        return null;
      }
    }
    userProfile = userRunProfileTransportGateway.getByUserId(userSpecialId.getUserId());
    return userProfileAppConvertor.toUserProfileDTO(userProfile);
  }


}
