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

import com.red.circle.other.app.inner.convertor.live.RoomProfileInnerConvertor;
import com.red.circle.other.app.inner.service.live.RoomManagerClientService;
import com.red.circle.other.infra.database.cache.service.other.RoomManagerCacheService;
import com.red.circle.other.infra.database.mongo.entity.live.RoomSetting;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.inner.model.cmd.room.RoomProfileOpsQryCmd;
import com.red.circle.other.inner.model.dto.live.RoomCounterDTO;
import com.red.circle.other.inner.model.dto.live.RoomProfileDTO;
import com.red.circle.other.inner.model.dto.live.RoomProfileManagerDTO;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 资料管理 实现.
 *
 * @author pengliang on 2023/5/31
 */
@Service
@RequiredArgsConstructor
public class RoomManagerClientServiceImpl implements RoomManagerClientService {

  private final RoomManagerCacheService roomManagerCacheService;
  private final RoomProfileInnerConvertor roomProfileInnerConvertor;
  private final RoomProfileManagerService roomProfileManagerService;

  @Override
  public void updateUserLangCode(Long userId, String langCode) {
    roomProfileManagerService.updateUserLangCode(userId, langCode);
  }

  @Override
  public Long getUserRoomId(Long userId) {
    return roomProfileManagerService.getRoomId(userId);
  }

  @Override
  public void updateRoomSettingMemberCapacity(Long userId, Integer maxMember, Integer adminNumber) {
    roomProfileManagerService.updateSelectiveSettingByUserId(userId,
        new RoomSetting()
            .setMaxMember(maxMember)
            .setMaxAdmin(adminNumber)
    );
  }

  @Override
  public RoomProfileDTO getProfileById(Long roomId) {
    return roomProfileInnerConvertor.toRoomProfileDTO(
        roomProfileManagerService.getProfileById(roomId));
  }

  @Override
  public RoomProfileManagerDTO getRoomProfileManagerById(Long roomId) {
    return roomProfileInnerConvertor.toRoomProfileManagerDTO(
        roomProfileManagerService.getById(roomId));
  }

  @Override
  public RoomProfileDTO getById(Long roomId) {
    return roomProfileInnerConvertor.toRoomProfileManagerDTO(
        roomProfileManagerService.getById(roomId));
  }

  @Override
  public RoomCounterDTO getRoomCounterByUserId(Long userId) {
    return roomProfileInnerConvertor.toRoomCounterDTO(
        roomProfileManagerService.getRoomCounterByUserId(userId)
    );
  }

  @Override
  public RoomProfileDTO getProfileByUserId(Long userId) {
    return roomProfileInnerConvertor.toRoomProfileDTO(
        roomProfileManagerService.getProfileByUserId(userId)
    );
  }

  @Override
  public List<RoomProfileDTO> listProfileByIds(Set<Long> userIds) {
    return roomProfileInnerConvertor.toListProfileManagerDTO(
        roomProfileManagerService.listProfileByUserIds(userIds)
    );
  }

  @Override
  public Map<Long, RoomProfileDTO> mapProfileByRoomIds(Set<Long> roomIds) {
    return roomProfileInnerConvertor.toMapRoomProfileDTO(
        roomProfileManagerService.mapProfileByRoomIds(roomIds)
    );
  }

  @Override
  public Map<Long, RoomProfileDTO> mapProfileByUserIds(Set<Long> userIds) {
    return roomProfileInnerConvertor.toMapRoomProfileDTO(
        roomProfileManagerService.mapProfileByUserIds(userIds)
    );
  }

  @Override
  public void updateUserCountry(Long userId, String countryCode, String countryName) {
    roomProfileManagerService.updateUserCountry(userId, countryCode, countryName);
  }

  @Override
  public List<RoomProfileManagerDTO> listOps(RoomProfileOpsQryCmd qryCmd) {
    return roomProfileInnerConvertor.toListRoomProfileManagerDTO(
        roomProfileManagerService.listOps(qryCmd)
    );
  }

  @Override
  public Boolean updateSelectiveProfile(RoomProfileDTO roomProfile) {
    return roomProfileManagerService.updateSelectiveProfile(
        roomProfileInnerConvertor.toRoomProfile(roomProfile)
    );
  }

  @Override
  public void removeCacheUserRole(Long userId, Long roomId) {
    roomManagerCacheService.removeRoomUserRoles(userId, roomId);
  }

  @Override
  public void incrCounterAdmin(Long roomId) {
    roomProfileManagerService.incrCounterAdminAndMember(roomId);
  }

  @Override
  public void incrCounterMember(Long roomId) {
    roomProfileManagerService.incrCounterMember(roomId);
  }

  @Override
  public void decrCounterAdmin(Long roomId) {
    roomProfileManagerService.decrCounterAdminAndMember(roomId);
  }

  @Override
  public void decrCounterMember(Long roomId) {
    roomProfileManagerService.decrCounterMember(roomId);
  }

  @Override
  public boolean checkOwn(Long roomId, Long userId) {
    return Objects.equals(roomProfileManagerService.getHomeownerId(roomId), userId);
  }

}
