package com.red.circle.other.app.inner.service.live.impl;

import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.live.inner.endpoint.LiveMicClient;
import com.red.circle.other.app.inner.service.live.ActiveVoiceRoomClientService;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import com.red.circle.other.domain.gateway.user.ability.UserSVipGateway;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.infra.convertor.live.ActiveVoiceInfraConvertor;
import com.red.circle.other.infra.database.cache.service.other.HotRoomCacheService;
import com.red.circle.other.infra.database.cache.service.other.RoomManagerCacheService;
import com.red.circle.other.infra.database.mongo.entity.live.*;
import com.red.circle.other.infra.database.mongo.service.live.ActiveVoiceRoomService;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.infra.database.mongo.service.live.RoomTopFixedService;
import com.red.circle.other.inner.model.cmd.live.AppActiveVoiceRoomQryCmd;
import com.red.circle.other.inner.model.dto.live.ActiveVoiceRoomDTO;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import com.red.circle.tool.core.text.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author pengliang on 2020/12/11
 */
@Service
@RequiredArgsConstructor
public class ActiveVoiceRoomClientServiceImpl implements ActiveVoiceRoomClientService {

  private final ActiveVoiceRoomService activeVoiceRoomService;
  private final ActiveVoiceInfraConvertor activeVoiceInfraConvertor;
  private final RoomProfileManagerService roomProfileManagerService;
  private final HotRoomCacheService hotRoomCacheService;
  private final LiveMicClient liveMicClient;
  private final RoomManagerCacheService roomManagerCacheService;
  private final UserSVipGateway userSVipGateway;
  private final UserProfileGateway userProfileGateway;
  private final UserRegionGateway userRegionGateway;
  private final RoomTopFixedService roomTopFixedService;

  @Override
  public Long count(String sysOrigin) {
    return activeVoiceRoomService.count(sysOrigin);
  }

  @Override
  public List<ActiveVoiceRoomDTO> listOps(AppActiveVoiceRoomQryCmd qryCmd) {
    return activeVoiceInfraConvertor.toListActiveVoiceRoomDTO(
        activeVoiceRoomService.listOps(qryCmd)
    );
  }

  @Override
  public ActiveVoiceRoom createNobodyRoom(Long roomId) {
    RoomProfileManager room = roomProfileManagerService.getById(roomId);
    UserProfile userProfile = userProfileGateway.getByUserId(room.getUserId());
    String roomOwnRegionCode = userRegionGateway.getRegionCode(room.getUserId());

    RoomTopFixed roomTopFixed = roomTopFixedService.getByRoomId(room.getId());
    Integer roomTopFixedWeights = Optional.ofNullable(roomTopFixed)
        .map(fixed -> Objects.nonNull(fixed.getWeights()) ? fixed.getWeights() : 0)
        .orElse(0);

    boolean topRoomForeverOnline = Optional.ofNullable(roomTopFixed)
        .map(fixed -> Objects.equals(fixed.getForeverOnline(), Boolean.TRUE))
        .orElse(Boolean.FALSE);

    // 房间上锁.
    Integer weights = StringUtils.isNotBlank(Optional.ofNullable(room.getSetting())
        .map(RoomSetting::getPassword)
        .orElse("")) ? 0 : 1;

    return new ActiveVoiceRoom()
        .setId(roomId)
        .setTimeId(IdWorkerUtils.getId())
        .setSysOrigin(room.getSysOrigin())
        .setUserId(room.getUserId())
        .setRoomAccount(room.getRoomAccount())
        .setFixedWeights(roomTopFixedWeights)
        .setRunGame(null)
        .setHot(
            Objects.nonNull(hotRoomCacheService.getAvailable(room.getSysOrigin(), room.getId())))
        .setCountryCode(StringUtils.toUpperCase(userProfile.getCountryCode()))
        .setCountryName(userProfile.getCountryName())
        // ALL所有、DEFAULT默认区域、其他根据语言分割.
        .setRegion(roomOwnRegionCode)
        .setExpiredTime(TimestampUtils.nowPlusMinutes(topRoomForeverOnline ? 60 : 3))
        .setCreateTime(TimestampUtils.now())
        .setUpdateTime(TimestampUtils.now())
        .setOnlineQuantity(0L)
        .setWeights(weights)
        .setSuperVipLevel(Objects.toString(
            userSVipGateway.checkSVipIdentity(room.getUserId()))
        );
  }


}
