package cd.casic.ci.process.util;

import cd.casic.framework.commons.config.WebProperties;
import cd.casic.framework.commons.enums.TerminalEnum;
import cd.casic.framework.commons.enums.UserTypeEnum;
import cd.casic.framework.commons.pojo.CommonResult;
import cd.casic.framework.security.core.LoginUser;
import cn.hutool.core.util.NumberUtil;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 专属于 web 包的工具类
 *
 * @author mianbin modified from yudao
 */
public class WebFrameworkUtils {

    public static String getLoginNickName() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            LoginUser principal = (LoginUser) authentication.getPrincipal();
            return principal.getInfo().get("nickName");
        } catch (Exception e){
            return "";
        }
    }

    public static Long getLoginUserId() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            LoginUser principal = (LoginUser) authentication.getPrincipal();
            return principal.getId();
        } catch (Exception e){
            return null;
        }
    }
    public static LoginUser getLoginUser() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            return (LoginUser) authentication.getPrincipal();
        } catch (Exception e){
            return null;
        }
    }
    public static String getLoginUserIdStr(){
        Long loginUserId = getLoginUserId();
        if (loginUserId!=null) {
            return String.valueOf(loginUserId);
        }
        return "";
    }



}
