package cn.superdesk.uniorder.common.rest.request.holder;


import cn.hutool.core.util.StrUtil;
import cn.superdesk.uniorder.common.constants.BaseMessageConf;
import cn.superdesk.uniorder.common.constants.BaseSysConf;
import cn.superdesk.uniorder.common.exception.BadRequestException;
import cn.superdesk.uniorder.common.numerical.AnStringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description: Biz 为权限服务
 * @Author: linjinyu
 * @CreateDate: 2021/10/27 下午3:12
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/10/27 下午3:12
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
public class RequestHolder {

	/**
	 * 获取request
	 *
	 * @return HttpServletRequest
	 */
	public static HttpServletRequest getRequest() {

		log.debug("getRequest -- Thread id :{}, name : {}", Thread.currentThread().getId(), Thread.currentThread().getName());

		ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
		if (null == servletRequestAttributes) {
			return null;
		}

		return servletRequestAttributes.getRequest();
	}

	/**
	 * 获取Response
	 *
	 * @return HttpServletRequest
	 */
	public static HttpServletResponse getResponse() {

		log.debug("getResponse -- Thread id :{}, name : {}", Thread.currentThread().getId(), Thread.currentThread().getName());

		ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
		if (null == servletRequestAttributes) {
			return null;
		}

		return servletRequestAttributes.getResponse();
	}

	/**
	 * 获取session
	 *
	 * @return HttpSession
	 */
	public static HttpSession getSession() {

		log.debug("getSession -- Thread id :{}, name : {}", Thread.currentThread().getId(), Thread.currentThread().getName());

		HttpServletRequest request = null;

		if (null == (request = getRequest())) {
			return null;
		}

		return request.getSession();
	}

	/**
	 * 获取session的Attribute
	 *
	 * @param name session的key
	 * @return Object
	 */
	public static Object getSession(String name) {

		log.debug("getSession -- Thread id :{}, name : {}", Thread.currentThread().getId(), Thread.currentThread().getName());

		ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
		if (null == servletRequestAttributes) {
			return null;
		}

		return servletRequestAttributes.getAttribute(name, RequestAttributes.SCOPE_SESSION);
	}

	/**
	 * 添加session
	 *
	 * @param name
	 * @param value
	 */
	public static void setSession(String name, Object value) {

		log.debug("setSession -- Thread id :{}, name : {}", Thread.currentThread().getId(), Thread.currentThread().getName());

		ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
		if (null == servletRequestAttributes) {
			return;
		}

		servletRequestAttributes.setAttribute(name, value, RequestAttributes.SCOPE_SESSION);
	}

	/**
	 * 清除指定session
	 *
	 * @param name
	 * @return void
	 */
	public static void removeSession(String name) {

		log.debug("removeSession -- Thread id :{}, name : {}", Thread.currentThread().getId(), Thread.currentThread().getName());

		ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
		if (null == servletRequestAttributes) {
			return;
		}

		servletRequestAttributes.removeAttribute(name, RequestAttributes.SCOPE_SESSION);
	}

	/**
	 * 获取所有session key
	 *
	 * @return String[]
	 */
	public static String[] getSessionKeys() {

		log.debug("getSessionKeys -- Thread id :{}, name : {}", Thread.currentThread().getId(), Thread.currentThread().getName());

		ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
		if (null == servletRequestAttributes) {
			return null;
		}

		return servletRequestAttributes.getAttributeNames(RequestAttributes.SCOPE_SESSION);
	}

	/**
	 * 目前不分 统一用 headers:key:token=xxxxx
	 *
	 * @return
	 */
	public static String getAccessToken() {

		HttpServletRequest request = RequestHolder.getRequest();

		if (request.getAttribute(BaseSysConf.TOKEN) != null) {
			return AnStringUtils.getString(request.getAttribute(BaseSysConf.TOKEN));
		} else {
			return null;
		}
	}


	/**
	 * 获取 redis 中 LoginSysUser
	 *
	 * @return
	 */
	public static String getLoginSysUser() {

		HttpServletRequest request = RequestHolder.getRequest();

		if (request.getAttribute(BaseSysConf.TOKEN) != null) {
			return request.getAttribute(BaseSysConf.TOKEN).toString();
		} else {
			return null;
		}
	}

	/**
	 * 检查当前用户是否登录【未登录操作将抛出QueryException异常】
	 */
	public static String checkLogin() {

		if (StrUtil.isBlank(getAccessToken())) {
			log.error("用户未登录");
			throw new BadRequestException(HttpStatus.UNAUTHORIZED, BaseMessageConf.INVALID_TOKEN);
		}

		return getAccessToken();
	}
}
