package ink.wgink.gateway.filter.local;

import ink.wgink.gateway.consts.ISystemConst;
import ink.wgink.gateway.pojo.route.Route;
import ink.wgink.gateway.pojo.route.request.RequestHeader;
import ink.wgink.gateway.pojo.route.request.RequestParameter;
import ink.wgink.gateway.pojo.route.request.ResponseHeader;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.filter.factory.RewriteLocationResponseHeaderGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.SetPathGatewayFilterFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: CommonFilters
 * @Description: 通用过滤器
 * @Author: WangGeng
 * @Date: 2021/4/24 20:31
 * @Version: 1.0
 **/
public class CommonFiltersDefinition {

    /**
     * 请求头参数过滤器
     *
     * @param requestHeaders
     * @return
     */
    public FilterDefinition addRequestHeader(List<RequestHeader> requestHeaders) {
        FilterDefinition addRequestHeader = new FilterDefinition();
        Map<String, String> addRequestHeaderMap = new HashMap<>(requestHeaders.size() * 2);
        for (RequestHeader requestHeader : requestHeaders) {
            addRequestHeaderMap.put("name", requestHeader.getName());
            addRequestHeaderMap.put("value", requestHeader.getValue());
        }
        addRequestHeader.setArgs(addRequestHeaderMap);
        addRequestHeader.setName("AddRequestHeader");
        return addRequestHeader;
    }

    /**
     * 请求头参数过滤器
     *
     * @param requestHeaders
     * @return
     */
    public FilterDefinition setRequestHeader(List<RequestHeader> requestHeaders) {
        FilterDefinition setRequestHeader = new FilterDefinition();
        Map<String, String> setRequestHeaderMap = new HashMap<>(requestHeaders.size() * 2);
        for (RequestHeader requestHeader : requestHeaders) {
            setRequestHeaderMap.put("name", requestHeader.getName());
            setRequestHeaderMap.put("value", requestHeader.getValue());
        }
        setRequestHeader.setArgs(setRequestHeaderMap);
        setRequestHeader.setName("SetRequestHeader");
        return setRequestHeader;
    }

    /**
     * 请求头参数过滤器
     *
     * @param requestHeaders
     * @return
     */
    public FilterDefinition removeRequestHeader(List<RequestHeader> requestHeaders) {
        FilterDefinition removeRequestHeader = new FilterDefinition();
        Map<String, String> removeRequestHeaderMap = new HashMap<>(requestHeaders.size() * 2);
        for (RequestHeader requestHeader : requestHeaders) {
            removeRequestHeaderMap.put("name", requestHeader.getName());
        }
        removeRequestHeader.setArgs(removeRequestHeaderMap);
        removeRequestHeader.setName("RemoveRequestHeader");
        return removeRequestHeader;
    }

    /**
     * 请求参数过滤器
     *
     * @param requestParameters
     * @return
     */
    public FilterDefinition addRequestParameter(List<RequestParameter> requestParameters) {
        FilterDefinition addRequestParameter = new FilterDefinition();
        Map<String, String> addRequestParameterMap = new HashMap<>(requestParameters.size() * 2);
        for (RequestParameter requestParameter : requestParameters) {
            addRequestParameterMap.put("name", requestParameter.getName());
            addRequestParameterMap.put("value", requestParameter.getValue());
        }
        addRequestParameter.setArgs(addRequestParameterMap);
        addRequestParameter.setName("AddRequestParameter");
        return addRequestParameter;
    }

    /**
     * 请求参数过滤器
     *
     * @param requestParameters
     * @return
     */
    public FilterDefinition removeRequestParameter(List<RequestParameter> requestParameters) {
        FilterDefinition removeRequestParameter = new FilterDefinition();
        Map<String, String> removeRequestParameterMap = new HashMap<>(requestParameters.size() * 2);
        for (RequestParameter requestParameter : requestParameters) {
            removeRequestParameterMap.put("name", requestParameter.getName());
        }
        removeRequestParameter.setArgs(removeRequestParameterMap);
        removeRequestParameter.setName("RemoveRequestParameter");
        return removeRequestParameter;
    }

    /**
     * 响应头参数过滤器
     *
     * @param responseHeaders
     * @return
     */
    public FilterDefinition addResponseHeader(List<ResponseHeader> responseHeaders) {
        FilterDefinition addResponseHeader = new FilterDefinition();
        Map<String, String> addResponseHeaderMap = new HashMap<>(responseHeaders.size() * 2);
        for (ResponseHeader responseHeader : responseHeaders) {
            addResponseHeaderMap.put("name", responseHeader.getName());
            addResponseHeaderMap.put("value", responseHeader.getValue());
        }
        addResponseHeader.setArgs(addResponseHeaderMap);
        addResponseHeader.setName("AddResponseHeader");
        return addResponseHeader;
    }

    /**
     * 响应头参数过滤器
     *
     * @param responseHeaders
     * @return
     */
    public FilterDefinition setResponseHeader(List<ResponseHeader> responseHeaders) {
        FilterDefinition setResponseHeader = new FilterDefinition();
        Map<String, String> setResponseHeaderMap = new HashMap<>(responseHeaders.size() * 2);
        for (ResponseHeader responseHeader : responseHeaders) {
            setResponseHeaderMap.put("name", responseHeader.getName());
            setResponseHeaderMap.put("value", responseHeader.getValue());
        }
        setResponseHeader.setArgs(setResponseHeaderMap);
        setResponseHeader.setName("SetResponseHeader");
        return setResponseHeader;
    }

    /**
     * 响应头参数过滤器
     *
     * @param responseHeaders
     * @return
     */
    public FilterDefinition removeResponseHeader(List<ResponseHeader> responseHeaders) {
        FilterDefinition removeResponseHeader = new FilterDefinition();
        Map<String, String> removeResponseHeaderMap = new HashMap<>(responseHeaders.size() * 2);
        for (ResponseHeader responseHeader : responseHeaders) {
            removeResponseHeaderMap.put("name", responseHeader.getName());
        }
        removeResponseHeader.setArgs(removeResponseHeaderMap);
        removeResponseHeader.setName("RemoveResponseHeader");
        return removeResponseHeader;
    }

    /**
     * 重写过滤器，将路径中的gw删除，暂时无用
     *
     * @param route
     * @return
     */
    public FilterDefinition rewritePath(Route route) {
        FilterDefinition rewritePath = new FilterDefinition();
        Map<String, String> rewritePathMap = new HashMap<>(4);
        rewritePathMap.put(RewritePathGatewayFilterFactory.REGEXP_KEY, ISystemConst.ROUTE_PREFIX + "/?(?<segment>.*)");
        rewritePathMap.put(RewritePathGatewayFilterFactory.REPLACEMENT_KEY, "/$\\{segment}");
        rewritePath.setArgs(rewritePathMap);
        rewritePath.setName("RewritePath");
        return rewritePath;
    }

    /**
     * 设置路径过滤器
     *
     * @param route
     * @return
     */
    public FilterDefinition setPath(Route route) {
        FilterDefinition setPath = new FilterDefinition();
        Map<String, String> setPathMap = new HashMap<>(2);
        setPathMap.put(SetPathGatewayFilterFactory.TEMPLATE_KEY, route.getSetPath());
        setPath.setArgs(setPathMap);
        setPath.setName("SetPath");
        return setPath;
    }

    /**
     * 重写响应头中的地址信息
     *
     * @param route
     * @return
     */
    public FilterDefinition rewriteLocationResponseHeader(Route route) {
        FilterDefinition rewriteLocationResponse = new FilterDefinition();
        Map<String, String> rewriteLocationResponseMap = new HashMap<>(2);
        rewriteLocationResponseMap.put("stripVersionMode", RewriteLocationResponseHeaderGatewayFilterFactory.StripVersion.AS_IN_REQUEST.toString());
        rewriteLocationResponseMap.put("hostValue", "Location");
        rewriteLocationResponse.setName("RewriteLocationResponseHeader");
        return rewriteLocationResponse;
    }


}
