package com.tofly.feesapi.security.compent;

import com.tofly.common.core.constant.SecurityConstants;
import com.tofly.feesapi.security.auth.ToflyUser;
import com.tofly.feesapi.security.exception.ToflyAuth2Exception;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.token.UserAuthenticationConverter;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

public class ToFlyUserAuthenticationConverter implements UserAuthenticationConverter {

    private static final String N_A = "N/A";
    @Override
    public Map<String, ?> convertUserAuthentication(Authentication authentication) {
        Map<String,Object> returnMap=new LinkedHashMap();
        returnMap.put(USERNAME,authentication.getName());
        if (authentication.getAuthorities() != null && !authentication.getAuthorities().isEmpty()) {
            returnMap.put(AUTHORITIES, AuthorityUtils.authorityListToSet(authentication.getAuthorities()));
        }
        return returnMap;
    }

    @Override
    public Authentication extractAuthentication(Map<String, ?> map) {
        if (map.containsKey(USERNAME)) {
            Collection<? extends GrantedAuthority> authorities = getAuthorities(map);

            String username = (String) map.get(SecurityConstants.DETAILS_USERNAME);
            Long id=null;
            if(map.get(SecurityConstants.DETAILS_USER_ID)!=null){
                 id = ((Integer) map.get(SecurityConstants.DETAILS_USER_ID)).longValue();
            }
            Long deptId=null;
            if(map.get(SecurityConstants.DETAILS_DEPT_ID)!=null){
                 deptId = ((Integer) map.get(SecurityConstants.DETAILS_DEPT_ID)).longValue();
            }

            String userLevel = (String)map.get(SecurityConstants.DETAILS_USER_LEVEL);
            String realName = (String)map.get(SecurityConstants.DETAILS_REAL_NAME);

            String deptName = (String)map.get(SecurityConstants.DETAILS_DEPT_NAME);

            String avatar = (String)map.get(SecurityConstants.DETAILS_USER_AVATAR);

            ToflyUser user = new ToflyUser(id, deptId, userLevel,realName,deptName,avatar,username, N_A, true
                    , true, true, true, authorities);
            return new UsernamePasswordAuthenticationToken(user, N_A, authorities);
        }
        throw  new ToflyAuth2Exception("参数非法");
    }

    private Collection<? extends GrantedAuthority> getAuthorities(Map<String, ?> map) {
            Object authorities = map.get(AUTHORITIES);
        if (authorities instanceof String) {
            return AuthorityUtils.commaSeparatedStringToAuthorityList((String) authorities);
        }
        if (authorities instanceof Collection) {
            return AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils
                    .collectionToCommaDelimitedString((Collection<?>) authorities));
        }
        throw new IllegalArgumentException("Authorities must be either a String or a Collection");
    }

}
