package pwc.taxtech.atms.common;

import javax.servlet.http.HttpServletRequest;

import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import pwc.taxtech.atms.dao.UserMapper;
import pwc.taxtech.atms.entitiy.User;

@Component
public class AuthUserHelperImpl implements AuditorAware<String>, AuthUserHelper {
    private static Logger logger = LoggerFactory.getLogger(AuthUserHelperImpl.class);
    @Autowired(required = false)
    private HttpServletRequest request;

    @Autowired
    private UserMapper userMapper;

    /*
     * (non-Javadoc)
     * 
     * @see pwc.taxtech.atms.common.AuthUserHelper#getCurrentAuditor()
     */
    @Override
    public String getCurrentAuditor() {
        SecurityContext context = SecurityContextHolder.getContext();
        if (context == null) {
            throw new ApplicationException("security context is null");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || !authentication.isAuthenticated()) {
            throw new ApplicationException("authentication failed");
        }
        String result = authentication.getName();
        if (Strings.isEmpty(result)) {
            throw new ApplicationException("failed to get user name from authentication");
        }
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see pwc.taxtech.atms.common.AuthUserHelper#getCurrentUserID()
     */
    @Override
    public String getCurrentUserID() {
        String userName = getCurrentAuditor();
        User user = userMapper.selectByUserNameIgnoreCase(userName);
        if (user == null) {
            return "";
        }
        return user.getID();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * pwc.taxtech.atms.common.AuthUserHelper#getIpaddressFromSecurityContextHolder(
     * )
     */
    @Override
    public String getIpaddressFromSecurityContextHolder() {
        // 默认ip地址
        String ipaddress = CommonConstants.DEFAULT_IP_ADDRESS;
        SecurityContext context = SecurityContextHolder.getContext();
        if (context == null) {
            throw new ApplicationException("security context is null");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            throw new ApplicationException("authentication is null");
        }
        Object authDetails = authentication.getDetails();
        if (authDetails instanceof WebAuthenticationDetails) {
            WebAuthenticationDetails webAuthenticationDetails = (WebAuthenticationDetails) authDetails;
            if (StringUtils.hasText(webAuthenticationDetails.getRemoteAddress())) {
                logger.debug("Fetch IP address from WebAuthenticationDetails");
                ipaddress = webAuthenticationDetails.getRemoteAddress();
            }
        }
        return ipaddress;
    }

    /*
     * (non-Javadoc)
     * 
     * @see pwc.taxtech.atms.common.AuthUserHelper#getClientIp()
     */
    @Override
    public String getClientIp() {
        return Lang.getIP(request);
    }
}