package com.tuoheng.common.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tuoheng.common.core.entity.UserEntity;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.model.dto.LoginUser;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUserUtils {
    /**
     * 获取认证信息
     *
     * @return
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取用户登录信息
     *
     * @return
     */
    public static LoginUser getLoginUser() {
        // header中获取用户token
        String token = ServletUtils.getRequest().getHeader("th-token");
        String oUserJson = ServletUtils.getRequest().getHeader("o-user-json");
        String json = EncryptUtil.decodeUTF8StringBase64(oUserJson);
        JSONObject jsonObject = JSON.parseObject(json);
        String username = jsonObject.getString("username");
        Long oidcUserId = jsonObject.getLong("oUserId");
        // 这里可以自定义封装自己的用户信息
        //UserPo userPo = clientUserMapper.getUserByUserName(username);
        LoginUser user = new LoginUser()
                .setUsername(username)
                .setUserId(oidcUserId)
                .setThToken(token);
        return user;
    }

    public static String token() {
        check();
        // header中获取用户token
        String token = ServletUtils.getRequest().getHeader("th-token");
        if (StringUtils.isEmpty(token)) {
            throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "token不能为空");
        }
        return token;
    }

    /**
     * 获取username
     *
     * @return
     */
    public static String username() {
        check();
        // header中获取用户token
        String oUserJson = ServletUtils.getRequest().getHeader("o-user-json");
        if (StringUtils.isEmpty(oUserJson)) {
            throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "oUserJson不能为空");
        }
        String json = EncryptUtil.decodeUTF8StringBase64(oUserJson);
        JSONObject jsonObject = JSON.parseObject(json);
        String username = jsonObject.getString("username");
        return username;
    }

    public static void check() {
        String oUserJson = ServletUtils.getRequest().getHeader("o-user-json");
        String json = EncryptUtil.decodeUTF8StringBase64(oUserJson);
        JSONObject jsonObject = JSON.parseObject(json);
        Integer isAble = jsonObject.getInteger("isAble");
        Integer isExpire = jsonObject.getInteger("isExpire");
        if (StringUtils.isNotNull(isAble) && StringUtils.isNotNull(isExpire)) {
            if (0 == isAble) {
                throw new ServiceException(JsonResult.OIDC_ERROR, "该账号已被禁用，请联系系统管理员");
            }
            if (0 == isExpire) {
                throw new ServiceException(JsonResult.OIDC_ERROR, "系统有效期已过，请联系系统管理员");
            }
        }
    }
}
