package com.red.circle.other.infra.database.cache.service.user.impl;

import com.google.gson.Gson;
import com.red.circle.component.redis.service.RedisService;
import com.red.circle.other.infra.database.cache.key.user.UserHashKey;
import com.red.circle.other.infra.database.cache.key.user.UserKey;
import com.red.circle.other.infra.database.cache.service.user.RegionRoomCacheService;
import com.red.circle.other.infra.database.cache.service.user.UserRegionCacheService;
import com.red.circle.other.infra.database.mongo.entity.live.ActiveVoiceRoom;
import com.red.circle.tool.core.collection.CollectionUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 区域房间缓存服务.
 *
 * @author lixiaofei on 2024/9/4
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RegionRoomCacheServiceImpl implements RegionRoomCacheService {

  private final RedisService redisService;
  private static final long CACHE_DAYS = 3;
  private static final long CACHE_TIME = 10;

  @Override
  public void remove(String key) {
    redisService.delete(getRegionsRoomKey(key));
  }

  @Override
  public void save(List<ActiveVoiceRoom> activeVoiceRooms, String key) {
    Gson gson = new Gson();
    String jsonString = gson.toJson(activeVoiceRooms);
    log.warn("jsonString：{}", jsonString);
    redisService.setIfAbsent(getRegionsRoomKey(key), jsonString, CACHE_TIME, TimeUnit.MINUTES);
  }

  @Override
  public void saveRegionsRoom(String jsonString, String key) {
    redisService.setIfAbsent(getRegionsRoomKey(key), jsonString, CACHE_TIME, TimeUnit.MINUTES);
  }

  @Override
  public String getRegionsRoom(String key) {
    return redisService.getString(getRegionsRoomKey(key));
  }

  private String getRegionsRoomKey(String userId) {
    return UserKey.REGION_ROOM.getKey(userId);
  }

}
