package com.qmth.paper.library.common.util;

import com.alibaba.fastjson.JSONObject;
import com.qmth.boot.core.enums.Platform;
import com.qmth.paper.library.common.entity.SysUser;
import com.qmth.paper.library.common.enums.ExceptionResultEnum;
import com.qmth.paper.library.common.contant.SystemConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @Description: http工具
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/4/10
 */
public class ServletUtil {
    private final static Logger log = LoggerFactory.getLogger(ServletUtil.class);

    /**
     * 输出错误
     *
     * @param response
     * @param code
     * @param message
     * @throws IOException
     */
    public static void responseError(ServletResponse response, int code, String message) throws IOException {
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        Result result = ResultUtil.error(code, message);
        String json = JSONObject.toJSONString(result);
        httpResponse.getWriter().print(json);
    }

    /**
     * 获取请求的sign
     *
     * @return
     */
    public static String getRequestSign() {
        try {
            HttpServletRequest request = getRequest();
            return Objects.isNull(request.getHeader(SystemConstant.SIGN)) ? request.getParameter(SystemConstant.SIGN) : request.getHeader(SystemConstant.SIGN);
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
            throw ExceptionResultEnum.SIGN_INVALID.exception();
        }
    }

    /**
     * 获取请求的token
     *
     * @return
     */
    public static String getRequestToken() {
        try {
            HttpServletRequest request = getRequest();
            return Objects.isNull(request.getHeader(SystemConstant.TOKEN)) ? request.getParameter(SystemConstant.TOKEN) : request.getHeader(SystemConstant.TOKEN);
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
            throw ExceptionResultEnum.TOKEN_INVALID.exception();
        }
    }

    /**
     * 获取请求的platform
     *
     * @return
     */
    public static Platform getRequestPlatform() {
        try {
            HttpServletRequest request = getRequest();
            String value = Objects.isNull(request.getHeader(SystemConstant.HEADER_PLATFORM)) ? request.getParameter(SystemConstant.HEADER_PLATFORM) : request.getHeader(SystemConstant.HEADER_PLATFORM);
            return Platform.valueOf(value);
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
            throw ExceptionResultEnum.PLATFORM_INVALID.exception();
        }
    }

    /**
     * 获取请求的deviceId
     *
     * @return
     */
    public static String getRequestDeviceId() {
        try {
            HttpServletRequest request = getRequest();
            return Objects.isNull(request.getHeader(SystemConstant.HEADER_DEVICE_ID)) ? request.getParameter(SystemConstant.HEADER_DEVICE_ID) : request.getHeader(SystemConstant.HEADER_DEVICE_ID);
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
            throw ExceptionResultEnum.DEVICE_ID_INVALID.exception();
        }
    }

    /**
     * 获取请求的Authorization
     *
     * @return
     */
    public static String getRequestAuthorization() {
        try {
            HttpServletRequest request = getRequest();
            return Objects.isNull(request.getHeader(SystemConstant.HEADER_AUTHORIZATION)) ? request.getParameter(SystemConstant.HEADER_AUTHORIZATION) : request.getHeader(SystemConstant.HEADER_AUTHORIZATION);
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
            throw ExceptionResultEnum.AUTHORIZATION_INVALID.exception();
        }
    }

    /**
     * 获取请求的Authorization
     *
     * @return
     */
    public static Long getRequestTime() {
        try {
            HttpServletRequest request = getRequest();
            String time = Objects.isNull(request.getHeader(SystemConstant.HEADER_TIME)) ? request.getParameter(SystemConstant.HEADER_TIME) : request.getHeader(SystemConstant.HEADER_TIME);
            return Objects.nonNull(time) ? Long.parseLong(time) : null;
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
            throw ExceptionResultEnum.TIME_INVALID.exception();
        }
    }

    /**
     * 获取请求的Session
     *
     * @return
     */
    public static Object getRequestSession() {
        Object object = getRequest().getAttribute(SystemConstant.SESSION);
        if (Objects.isNull(object)) {
            throw ExceptionResultEnum.NOT_LOGIN.exception();
        }
        return object;
    }

    /**
     * 获取请求的用户
     *
     * @return
     */
    public static Object getRequestUser() {
        Object object = getRequest().getAttribute(SystemConstant.USER);
        if (Objects.isNull(object)) {
            throw ExceptionResultEnum.NOT_LOGIN.exception();
        }
        return object;
    }

    /**
     * 获取header中schoolId
     *
     * @return
     */
    public static Object getRequestHeaderSchoolId() {
        Object object = getRequest().getHeader(SystemConstant.SCHOOL_ID);
        if (Objects.isNull(object)) {
            throw ExceptionResultEnum.NOT_LOGIN.exception();
        }
        return object;
    }

    /**
     * 获取header中schoolId
     *
     * @return
     */
    public static Object getRequestHeaderUserId() {
        Object object = getRequest().getHeader(SystemConstant.USER_ID);
        if (Objects.isNull(object)) {
            throw ExceptionResultEnum.NOT_LOGIN.exception();
        }
        return object;
    }

    /**
     * 获取header中schoolId
     *
     * @return
     */
    public static Object getRequestHeaderSchoolIdByNotVaild() {
        return getRequest().getHeader(SystemConstant.SCHOOL_ID);
    }

    /**
     * 获取请求的学校
     *
     * @return
     */
    public static Object getRequestSchool() {
        Object object = getRequest().getAttribute(SystemConstant.SCHOOL);
        if (Objects.isNull(object)) {
            throw ExceptionResultEnum.NOT_LOGIN.exception();
        }
        return object;
    }

    /**
     * 获取请求的学校
     *
     * @return
     */
    public static Object getRequestSchoolByNotVaild() {
        return getRequest().getAttribute(SystemConstant.SCHOOL);
    }

    /**
     * 获取请求的机构
     *
     * @return
     */
    public static Object getRequestOrg() {
        Object object = getRequest().getAttribute(SystemConstant.ORG);
        if (Objects.isNull(object)) {
            throw ExceptionResultEnum.NOT_LOGIN.exception();
        }
        return object;
    }

    /**
     * 获取请求的md5
     *
     * @return
     */
    public static String getRequestMd5() {
        String md5 = getRequest().getHeader(SystemConstant.MD5.toLowerCase());
        if (Objects.isNull(md5)) {
            throw ExceptionResultEnum.MD5_EMPTY.exception();
        }
        return md5;
    }

    /**
     * 设置id
     *
     * @param ids
     * @return
     */
    public static void setRequestId(List<Long> ids) {
        HttpServletRequest request = getRequest();
        request.setAttribute(SystemConstant.ID, ids);
    }

    /**
     * 获取id
     *
     * @return
     */
    public static List<Long> getRequestId() {
        return Objects.nonNull(getRequest().getAttribute(SystemConstant.ID)) ? (List<Long>) getRequest().getAttribute(SystemConstant.ID) : null;
    }

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

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