package cn.util.core;

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;
import java.io.IOException;

public class ServletKit {
    /**
     * 获取request
     *
     * @return {@link HttpServletRequest}
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attr = getServletRequestAttributes();
        if (IsKit.isNull(attr)) {
            return null;
        }
        return getServletRequestAttributes().getRequest();
    }

    public static ServletRequestAttributes getServletRequestAttributes() {
        return (ServletRequestAttributes) getRequestAttributes();
    }

    private static RequestAttributes getRequestAttributes() {
        return RequestContextHolder.getRequestAttributes();
    }

    /**
     * 获取response
     */
    public static HttpServletResponse getResponse() {
        return getServletRequestAttributes().getResponse();
    }

    /**
     * 获取session
     *
     * @return {@link HttpSession}
     */
    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * 获取 session id
     *
     * @return {@link String}
     */
    public static String getSessionId() {
        return getRequest().getSession().getId();
    }

    /**
     * 设置session属性
     *
     * @param key  键
     * @param data 值
     */
    public static void setSessionAttribute(String key, Object data) {
        getSession().setAttribute(key, data);
    }

    /**
     * 设置session属性
     *
     * @param key  键
     * @param data 值
     */
    public static void setSessionAttribute(Enum<?> key, Object data) {
        getSession().setAttribute(key.name(), data);
    }

    /**
     * 获取session属性
     *
     * @param key 键
     * @return {@link Object}
     */
    public static Object getSessionAttribute(String key) {
        return getSession().getAttribute(key);
    }

    /**
     * 获取session属性
     *
     * @param key 键
     * @return {@link Object}
     */
    public static Object getSessionAttribute(Enum<?> key) {
        return getSession().getAttribute(key.name());
    }

    /**
     * 根据用户 id 创建 session key
     *
     * @param userId  用户 id
     * @param keyName key 名称
     * @return {String} SESSION_USER_[userId]_[keyName] 结构的 sessionKEY
     */
    public static String createUserSessionKey(Integer userId, String keyName) {

        return "SESSION_USER_" + userId + "_" + keyName;
    }

    /**
     * 将字符串渲染到客户端
     *
     * @param response 渲染对象
     * @param string   待渲染的字符串
     * @return null
     */
    public static String renderString(HttpServletResponse response, String string) {
        try {
            response.setStatus(200);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(string);
        } catch (IOException e) {
            LogKit.error(e);
        }
        return null;
    }


}

