package com.docus.server.common.auth;

import com.docus.server.constants.BusinessConstant;
import com.docus.server.vo.recovery.UserInfoView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

public class ApiRequestContext {
    //线程变量，请求线程内有效
    private static final ThreadLocal<ApiRequestContext> current = new ThreadLocal<>();
    //#xreales t
    private HttpServletRequest request;
    //i#Rresponse
    private HttpServletResponse response;
    //请求的方法
    private String action;
    private Integer adminCode;
    //当前访问的子系统
    private String systemCode;
    //子系统分组，( PC端和移动端设置同一 分组，以便短信、通知等信息能过滤出来)
    private String appGroup;
    private String ownerId;
    //当前访问的应用ID (公众出行子系统有效)
    private String appId;
    //当前登录的用户信息
    private UserInfoView userInfoView;
    private boolean isInternal = true;
    //是否授杖内部微服务接口(跳过token验证)访问
    private boolean grantInternalRequest = false;

    private ApiRequestContext() {

    }

    //初始化
    public static void init(HttpServletRequest request, HttpServletResponse response, UserInfoView userInfoview, String action) {
        current.remove();
        ApiRequestContext context = new ApiRequestContext();
        context.request = request;
        context.response = response;
        context.userInfoView = userInfoview;
        context.action = action;
        String ownerId = request.getHeader(BusinessConstant.HEADER_OWNER_ID);
        String systemCode = request.getHeader(BusinessConstant.HEADER_SYSTEM_CODE);
        String appId = request.getHeader(BusinessConstant.HEADER_APP_ID);
        String networkType = request.getHeader(BusinessConstant.HEADER_NETWORK_TYPE); //0:FIP, 1:H01
        context.systemCode = systemCode;
        context.ownerId = ownerId;
        context.appId = appId;
        context.isInternal = Objects.equals(networkType, "0");
        current.set(context);
    }

    //初始化
    public static void init(HttpServletRequest request, HttpServletResponse response, UserInfoView userInfoview, String action, String appGroup) {
        current.remove();
        ApiRequestContext context = new ApiRequestContext();
        context.request = request;
        context.response = response;
        context.userInfoView = userInfoview;
        context.action = action;
        String ownerId = request.getHeader(BusinessConstant.HEADER_OWNER_ID);
        String systemCode = request.getHeader(BusinessConstant.HEADER_SYSTEM_CODE);
        String appId = request.getHeader(BusinessConstant.HEADER_APP_ID);
        String networkType = request.getHeader(BusinessConstant.HEADER_NETWORK_TYPE); //0:FIP, 1:H01
        context.systemCode = systemCode;
        context.ownerId = ownerId;
        context.appId = appId;
        context.appGroup = appGroup;
        context.isInternal = Objects.equals(networkType, "0");
        current.set(context);
    }

    //获取当前现场request
    public static ApiRequestContext get() {
        return current.get();
    }

    //清除线程变量
    public static void clear() {
        current.remove();
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public Integer getAdminCode() {
        return adminCode;
    }

    public void setAdminCode(Integer adminCode) {
        this.adminCode = adminCode;
    }

    public String getSystemCode() {
        return systemCode;
    }

    public void setSystemCode(String systemCode) {
        this.systemCode = systemCode;
    }

    public String getAppGroup() {
        return appGroup;
    }

    public void setAppGroup(String appGroup) {
        this.appGroup = appGroup;
    }

    public String getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(String ownerId) {
        this.ownerId = ownerId;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public UserInfoView getUserInfoView() {
        return userInfoView;
    }

    public void setUserInfoView(UserInfoView userInfoView) {
        this.userInfoView = userInfoView;
    }

    public boolean isInternal() {
        return isInternal;
    }

    public void setInternal(boolean internal) {
        isInternal = internal;
    }

    public boolean getIsGrantInternalRequest() {
        if (this.grantInternalRequest) {
            this.grantInternalRequest = false;
            return true;
        }
        return false;
    }

    public void setGrantInternalRequest(boolean grantInternalRequest) {
        this.grantInternalRequest = grantInternalRequest;
    }

    //授权本线程访问内部微服务接口一次，跳过token一次
    public static void grantInternalRequestOnce() {
        if (current.get() == null) {
            ApiRequestContext apiRequestContext = new ApiRequestContext();
            current.set(apiRequestContext);
        }
        current.get().grantInternalRequest = true;
    }

    public String getUserId() {
        return userInfoView != null ? userInfoView.getUserId() : null;
    }

    public String getToken() {
        return userInfoView != null ? userInfoView.getToken() : null;
    }
}
