package com.red.circle.other.infra.common.user;

import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.component.redis.service.RedisService;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.ResponseErrorCode;
import com.red.circle.mq.business.model.event.room.LiveVoiceSessionEvent;
import com.red.circle.mq.business.model.event.user.UserOnlineStatusEvent;
import com.red.circle.mq.rocket.business.producer.RoomMqMessageService;
import com.red.circle.mq.rocket.business.producer.UserMqMessageService;
import com.red.circle.other.domain.gateway.props.PropsNobleVipGateway;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.inner.enums.user.UserOnlineStatusEnum;
import com.red.circle.other.inner.model.cmd.user.OnlineStatusUploadCmd;
import com.red.circle.other.inner.model.dto.material.props.PropsNobleVipAbilityDTO;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author pengliang on 2023/11/2
 */
@Component
@RequiredArgsConstructor
public class OnlineStatusCommon {

  private final RedisService redisService;
  private final UserProfileGateway userProfileGateway;
  private final PropsNobleVipGateway propsNobleVipGateway;
  private final RoomMqMessageService roomMqMessageService;
  private final UserMqMessageService userMqMessageService;

  /**
   * 上传在线状态.
   */
  public void upload(OnlineStatusUploadCmd cmd) {

    ResponseAssert.notNull(ResponseErrorCode.REQUEST_PARAMETER_ERROR,
        UserOnlineStatusEnum.toEnum(cmd.getStatus()));

    UserProfile userProfile = userProfileGateway.getByUserId(cmd.getUserId());

    if (Objects.isNull(userProfile) || !userProfile.checkAccountAvailable()) {
      return;
    }

    // 会话列表
    roomSessionMessage(cmd);

    // 在线状态消息
    onlineStatusMessage(cmd);
  }


  /**
   * 权重.
   */
  public Integer getWeights(Long userId) {
    // 语音厅 vip
    PropsNobleVipAbilityDTO nobleVipAbility = propsNobleVipGateway.getUserMaxAbilityDTO(userId);
    if (Objects.nonNull(nobleVipAbility)) {
      return 100;
    }
    return 0;
  }

  private void onlineStatusMessage(OnlineStatusUploadCmd cmd) {
    //客户端每60秒发送一次, 冗余网络延迟等问题 设置为50秒
    if (redisService.setIfAbsent("OLU:" + cmd.getUserId(), 50, TimeUnit.SECONDS)) {
      userMqMessageService.onlineStatus(new UserOnlineStatusEvent()
          .setUserId(cmd.getUserId())
          .setSysOrigin(SysOriginPlatformEnum.valueOf(cmd.getSysOrigin()))
          .setTask(cmd.getTask())
          .setStatus(cmd.getStatus())
          .setSessionId(cmd.getSessionId())
          .setRequestTime(TimestampUtils.now())
      );
    }
  }

  private void roomSessionMessage(OnlineStatusUploadCmd cmd) {

    if (StringUtils.isBlank(cmd.getSessionId())) {
      return;
    }

    if (redisService.setIfAbsent("OLR:" + cmd.getSessionId(), 1, TimeUnit.SECONDS)) {
      roomMqMessageService.liveSessionConnection(convertLiveVoiceSessionCmd(cmd));
    }

  }

  private LiveVoiceSessionEvent convertLiveVoiceSessionCmd(OnlineStatusUploadCmd cmd) {
    LiveVoiceSessionEvent event = new LiveVoiceSessionEvent();
    event.setStatus(cmd.getStatus());
    event.setSessionId(cmd.getSessionId());
    return event;
  }

}
