package com.emergency.avenue.common.security.service;

import com.emergency.avenue.admin.api.dto.UserInfo;
import com.emergency.avenue.admin.api.feign.RemoteUserService;
import com.emergency.avenue.common.core.constant.CacheConstants;
import com.emergency.avenue.common.core.constant.SecurityConstants;
import com.emergency.avenue.common.core.util.R;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * @author aeizzz
 */
@Slf4j
@RequiredArgsConstructor
public class AvenueMobileUserDetailServiceImpl implements AvenueUserDetailsService {

	private final UserDetailsService avenueDefaultUserDetailsServiceImpl;

	private final RemoteUserService remoteUserService;

	private final CacheManager cacheManager;

	@Override
	@SneakyThrows
	public UserDetails loadUserByUsername(String phone) {
		Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS);
		if (cache != null && cache.get(phone) != null) {
			return cache.get(phone, AvenueUser.class);
		}

		R<UserInfo> result = remoteUserService.social(phone, SecurityConstants.FROM_IN);

		UserDetails userDetails = getUserDetails(result);
		if (cache != null) {
			cache.put(phone, userDetails);
		}
		return getUserDetails(result);
	}

	@Override
	public UserDetails loadUserByUser(AvenueUser avenueUser) {
		return avenueDefaultUserDetailsServiceImpl.loadUserByUsername(avenueUser.getUsername());
	}

	/**
	 * 支持所有的 mobile 类型
	 * @param clientId 目标客户端
	 * @param grantType 授权类型
	 * @return true/false
	 */
	@Override
	public boolean support(String clientId, String grantType) {
		return SecurityConstants.GRANT_MOBILE.equals(grantType);
	}

}
