package com.qmth.themis.business.util;

import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.common.contanst.Constants;
import com.qmth.themis.common.enums.ExceptionResultEnum;
import com.qmth.themis.common.enums.Platform;
import com.qmth.themis.common.exception.BusinessException;
import com.qmth.themis.common.util.Result;
import com.qmth.themis.common.util.ResultUtil;
import org.apache.commons.lang3.StringUtils;
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 java.io.IOException;
import java.util.Objects;

/**
 * @Description: http工具
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/4/10
 */
public class ServletUtil {

    /**
     * 输出错误
     *
     * @param code
     * @param message
     * @throws IOException
     */
    public static void responseError(int code, String message) throws IOException {
        Result result = ResultUtil.error(code, message);
        String json = JacksonUtil.parseJson(result);
        getResponse().getWriter().print(json);
    }

    /**
     * 获取请求的sign
     *
     * @return
     */
    public static String getRequestSign() {
        HttpServletRequest request = getRequest();
        // 从header中获取token
        return request.getHeader(SystemConstant.SIGN);
    }

    /**
     * 获取请求的accessToken
     *
     * @return
     */
    public static String getRequestToken() {
        HttpServletRequest request = getRequest();
        // 从header中获取token
        String token = request.getHeader(SystemConstant.ACCESS_TOKEN);
        // 如果header中不存在token，则从参数中获取token
        if (Objects.isNull(token)) {
            token = request.getParameter(SystemConstant.ACCESS_TOKEN);
        }
        return token;
    }

    /**
     * 获取请求的platform
     *
     * @return
     */
    public static Platform getRequestPlatform() {
        HttpServletRequest request = getRequest();
        // 从header中获取platform
        String value = request.getHeader(Constants.HEADER_PLATFORM);
        // 如果header中不存在platform，则从参数中获取platform
        if (Objects.isNull(value)) {
            value = request.getParameter(Constants.HEADER_PLATFORM);
            if (Objects.isNull(value)) {
                throw new BusinessException(ExceptionResultEnum.PLATFORM_INVALID);
            }
        }
        Platform platform = Platform.findByName(value);
        if (platform == null) {
            throw new BusinessException(ExceptionResultEnum.PLATFORM_INVALID);
        }
        return platform;
    }

    /**
     * 获取请求的deviceId
     *
     * @return
     */
    public static String getRequestDeviceId() {
        HttpServletRequest request = getRequest();
        // 从header中获取deviceId
        String deviceId = StringUtils.trimToNull(request.getHeader(Constants.HEADER_DEVICE_ID));
        // 如果header中不存在deviceId，则从参数中获取deviceId
        if (Objects.isNull(deviceId)) {
            deviceId = StringUtils.trimToNull(request.getParameter(Constants.HEADER_DEVICE_ID));
            if (Objects.isNull(deviceId)) {
                throw new BusinessException(ExceptionResultEnum.DEVICE_ID_INVALID);
            }
        }
        return deviceId;
    }

    /**
     * 获取请求的time
     *
     * @return
     */
    public static long getRequestTime() {
        HttpServletRequest request = getRequest();
        // 从header中获取time
        String time = request.getHeader(Constants.HEADER_TIME);
        // 如果header中不存在time，则从参数中获取time
        if (Objects.isNull(time)) {
            time = request.getParameter(Constants.HEADER_TIME);
            if (Objects.isNull(time)) {
                throw new BusinessException(ExceptionResultEnum.TIME_INVALID);
            }
        }
        try {
            return Long.parseLong(time);
        } catch (Exception e) {
            throw new BusinessException(ExceptionResultEnum.TIME_INVALID);
        }
    }

    /**
     * 获取请求的Authorization
     *
     * @return
     */
    public static String getRequestAuthorization() {
        HttpServletRequest request = getRequest();
        // 从header中获取authorization
        String authorization = request.getHeader(Constants.HEADER_AUTHORIZATION);
        // 如果header中不存在authorization，则从参数中获取authorization
        if (Objects.isNull(authorization)) {
            authorization = request.getParameter(Constants.HEADER_AUTHORIZATION);
            if (Objects.isNull(authorization)) {
                throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_INVALID);
            }
        }
        return authorization;
    }

    /**
     * 获取请求的Authorization
     *
     * @return
     */
    public static String getRequestAuthorizationForAspect() {
        HttpServletRequest request = getRequest();
        // 从header中获取authorization
        String authorization = request.getHeader(Constants.HEADER_AUTHORIZATION);
        // 如果header中不存在authorization，则从参数中获取authorization
        if (Objects.isNull(authorization)) {
            authorization = request.getParameter(Constants.HEADER_AUTHORIZATION);
        }
        return authorization;
    }

    /**
     * 获取请求的time
     *
     * @return
     */
    public static String getRequestTimeForTime() {
        HttpServletRequest request = getRequest();
        // 从header中获取time
        String time = request.getHeader(Constants.HEADER_TIME);
        // 如果header中不存在time，则从参数中获取time
        if (Objects.isNull(time)) {
            time = request.getParameter(Constants.HEADER_TIME);
        }
        return time;
    }

    /**
     * 获取请求的md5
     *
     * @return
     */
    public static String getRequestMd5() {
        return getRequest().getHeader(SystemConstant.MD5);
    }

    /**
     * 获取请求的path
     *
     * @return
     */
    public static String getRequestPath() {
        return getRequest().getHeader(SystemConstant.PATH);
    }

    /**
     * 获取请求的Session
     *
     * @return
     */
    public static Object getRequestSession() {
        return getRequest().getAttribute(SystemConstant.SESSION);
    }

    /**
     * 获取请求的account
     *
     * @return
     */
    public static Object getRequestAccount() {
        return getRequest().getAttribute(SystemConstant.ACCOUNT);
    }

    /**
     * 获取请求的org
     *
     * @return
     */
    public static Object getRequestOrg() {
        return getRequest().getAttribute(SystemConstant.ORG);
    }

    /**
     * 获取HttpServletRequest
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return Objects.nonNull(servletRequestAttributes) ? servletRequestAttributes.getRequest() : null;
    }

    /**
     * 获取HttpServletResponse
     *
     * @return
     */
    public static HttpServletResponse getResponse() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return servletRequestAttributes.getResponse();
    }

    /**
     * 获取请求的student account
     *
     * @return
     */
    public static Object getRequestStudentAccount() {
        return getRequest().getAttribute(SystemConstant.STUDENT_ACCOUNT);
    }
}
