package com.bonus.canteen.core.config;//package com.bonus.canteen.core.config;
//
//import cn.hutool.core.map.MapUtil;
//import com.bonus.canteen.core.customer.service.AuthorizingService;
//import com.bonus.canteen.core.secure.PmsCache;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.google.common.collect.Maps;
//import com.google.common.collect.Sets;
//import org.apache.commons.collections4.CollectionUtils;
//import org.apache.commons.collections4.ListUtils;
//import org.apache.commons.lang3.BooleanUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.data.redis.core.BoundHashOperations;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import java.time.Instant;
//import java.util.*;
//import java.util.function.Function;
//import java.util.stream.Collectors;
//import java.util.stream.Stream;
//
//public class SecureManager {
//   private static final Logger log = LoggerFactory.getLogger(SecureManager.class);
//   private static SecureProperties secureProperties;
//   private static StringRedisTemplate redisTemplate;
//   private static AuthorizingService authorizingService;
//   private static ObjectMapper objectMapper;
//
//   private SecureManager() {
//   }
//
//   public static void setSecureProperties(SecureProperties secureProperties) {
//      if (SecureManager.secureProperties == null) {
//         SecureManager.secureProperties = secureProperties;
//      }
//   }
//
//   public static void setRedisTemplate(StringRedisTemplate redisTemplate) {
//      if (SecureManager.redisTemplate == null) {
//         SecureManager.redisTemplate = redisTemplate;
//      }
//   }
//
//   public static void setAuthorizingService(AuthorizingService authorizingService) {
//      if (SecureManager.authorizingService == null) {
//         SecureManager.authorizingService = authorizingService;
//      }
//   }
//
//   public static void setObjectMapper(ObjectMapper objectMapper) {
//      if (SecureManager.objectMapper == null) {
//         SecureManager.objectMapper = objectMapper;
//      }
//   }
//
//   private static String getPmsKey(long subjectId) {
//      return "permissions:subject_id:" + subjectId;
//   }
//
//   private static String getRoleKey(long subjectId) {
//      return "roles:subject_id:" + subjectId;
//   }
//
//   public static void clearAllRoleAndPermission() {
//      redisTemplate.delete(secureProperties.getPermissionKey());
//   }
//
//   public static void clearRoleAndPermission(long subjectId) {
//      redisTemplate.boundHashOps(secureProperties.getPermissionKey()).delete(new Object[]{getRoleKey(subjectId), getPmsKey(subjectId)});
//   }
//
//   public static void clearRole(long subjectId) {
//      redisTemplate.boundHashOps(secureProperties.getPermissionKey()).delete(new Object[]{getRoleKey(subjectId)});
//   }
//
//   public static void clearPermission(long subjectId) {
//      redisTemplate.boundHashOps(secureProperties.getPermissionKey()).delete(new Object[]{getPmsKey(subjectId)});
//   }
//
//   public static boolean isLogin() {
//      return (Boolean)WebContext.get().getAccessToken().map(AccessToken::isAuthenticated).orElse(false);
//   }
//
//   public static Optional<Long> getSubjectId() {
//      return WebContext.get().getAccessToken().map(AccessToken::getSubjectId);
//   }
//
//   public static Optional<String> getSubjectName() {
//      return WebContext.get().getAccessToken().map(AccessToken::getSubjectName);
//   }
//
//   public static Map<String, String> getSubjectData() {
//      return (Map)WebContext.get().getAccessToken().map(AccessToken::getSubjectData).orElse(Maps.newHashMap());
//   }
//
//   public static Map<String, String> attachData(Map<String, String> data) {
//      try {
//         if (MapUtil.isEmpty(data)) {
//            return Maps.newHashMapWithExpectedSize(0);
//         } else {
//            AccessToken accessToken = (AccessToken)WebContext.get().getAccessToken().orElse(AccessToken.create());
//            Objects.requireNonNull(accessToken);
//            data.forEach(accessToken::setData);
//            accessToken.bind().store();
//            return accessToken.getSubjectData();
//         }
//      } catch (Throwable var2) {
//         throw var2;
//      }
//   }
//
//   public static Map<String, String> attachData(String key, String value) {
//      try {
//         return attachData(Collections.singletonMap(key, value));
//      } catch (Throwable var3) {
//         throw var3;
//      }
//   }
//
//   public static void logout() {
//      WebContext.get().getAccessToken().ifPresent(AccessToken::clear);
//   }
//
//   public static void revokeAuthenticate() {
//      WebContext.get().getAccessToken().ifPresent((accessToken) -> {
//         accessToken.revokeAuthenticate().store();
//      });
//   }
//
//   public static void revokeAuthenticate(long subjectId, int reservedRecentNum) {
//      StringRedisTemplate var10000 = redisTemplate;
//      String var10001 = secureProperties.getServer().getSubjectRefTokenKey();
//      Set<String> keys = var10000.keys(var10001 + ":" + subjectId + ":*");
//      if (CollectionUtils.size(keys) > reservedRecentNum) {
//         assert keys != null;
//
//         Map<Long, List<String>> createTimeAsc_keys = (Map)keys.stream().collect(Collectors.groupingBy((key) -> {
//            return Long.parseLong(key.split(":")[4]);
//         }, TreeMap::new, Collectors.toList()));
//         List<String> keysAsc = createTimeAsc_keys.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
//         List<String> beDeleteRefKeys = keysAsc.subList(0, keysAsc.size() - reservedRecentNum);
//         List<String> beDeleteTokenKeys = beDeleteRefKeys.stream().map((dk) -> {
//            String var100001 = secureProperties.getServer().getStoreKey();
//            return var100001 + ":" + dk.split(":")[3];
//         }).collect(Collectors.toList());
//         redisTemplate.delete(ListUtils.sum(beDeleteRefKeys, beDeleteTokenKeys));
//      }
//   }
//
//   public String toString() {
//      return "SecureManager()";
//   }
//}
