package com.ruoyi.framework.interceptor.cas;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.sun.jna.platform.win32.Netapi32Util;
import lombok.extern.slf4j.Slf4j;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.util.AbstractCasFilter;
import org.jasig.cas.client.validation.Assertion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * TODO $CAS统一认证：CAS获取用户信息
 */
@Slf4j
@Component
public class CASUser {

    @Autowired
    private ISysUserService sysUserService;

    // 取cas登录后的用户信息
    public Long getSSOUserId() {
//        testGetUser();
        try {
//        ServletRequestAttributes requestAttributes = ServletUtils.getRequestAttributes();
            HttpServletRequest request = ServletUtils.getRequest();

//            log.info("【CAS获取用户】request = {}", request);
//            【CAS获取用户】request = org.jasig.cas.client.util.HttpServletRequestWrapperFilter$CasHttpServletRequestWrapper@7dd4a96b
            AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
            log.info("【CAS】统一认证获取用户方法：AttributePrincipal = {}", principal);
//            【CAS获取用户】AttributePrincipal = ganjiantest
            if (principal == null) {
//                return 1l;
                return null;
            }
            String userName = principal.getName();
            log.info("【CAS】统一认证获取用户方法：拿到的登录用户名principal.getName() = {}", userName);
//            【CAS获取用户】principal.getName() = ganjiantest
/*
            Map attributes = principal.getAttributes();
            log.info("【CAS获取用户】principal.getAttributes() = {}", attributes);
//            CAS获取用户】principal.getAttributes() = {}

            //用户登录名称
            Object userIdStr = attributes.get("userid");
            log.info("【CAS获取用户】userIdStr = {}", userIdStr);
//            【CAS获取用户】userIdStr = null
            */
            if (StringUtils.isBlank(userName)) {
                return null;
            }

            SysUser sysUser = sysUserService.selectUserByUserName(userName);
            if (sysUser == null) {
                log.info("【CAS】统一认证获取用户方法：本地数据库中未查到用户信息 userName = {}", userName);

                return null;
            }
            Long userId = sysUser.getUserId();
            log.info("【CAS】统一认证获取用户方法：本地数据库中查到用户信息 userName = {} userId：{} ", userName, userId);
            if (userId == null)
                return null;
            //雪花ID防止VUE精度丢失
            //   return userId.toString().length() > 16 ? Long.parseLong(userId.toString().substring(4)) : userId;
            return userId;//同步甲方用户信息时已截取过ID的长度，这里不会大于15位了
        } catch (Exception e) {
            log.info("【CAS】获取用户方法：出现异常： {}", e.getMessage());
            e.printStackTrace();
        }
//        return 1l;
        return null;
    }

/*    private void testGetUser() {
        try {
            HttpServletRequest request = ServletUtils.getRequest();

            //返回的登录用户信息
            //通过cas client获取
//            Object object = request.getSession().getAttribute("_const_cas_assertion_");
            Object object = request.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
            if (null != object) {
                Assertion assertion = (Assertion) object;
                String loginName = assertion.getPrincipal().getName();
                Map att = assertion.getPrincipal().getAttributes();//获取属性值，为一个Map类型。

                System.out.println("登陆用户名 assertion.getPrincipal().getName() = " + loginName);
                登陆用户名 assertion.getPrincipal().getName() = ganjiantest
                System.out.println("登陆返回的属性 assertion.getPrincipal().getAttributes() = " + att);
                登陆返回的属性 assertion.getPrincipal().getAttributes() = {}

                // UserInfo user = UserUtil.getCurrentUser(request.getSession());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

}
