package com.community.huiju.filter;

import com.alibaba.fastjson.JSONObject;
import com.community.commom.constant.Constant;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.constant.Header;
import com.community.huiju.constant.RequestURI;
import com.community.huiju.constant.Status;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截Token
 * @author weiximei
 */
@Component
@Slf4j
public class DomainZuulPostFilter extends ZuulFilter {

    private AntPathMatcher matcher = new AntPathMatcher();

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        if (request.getMethod().equals("OPTIONS")) {
            return false;
        }
        return true;
    }

    @Override
    public Object run() throws ZuulException {

        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletResponse response = ctx.getResponse();
        response.setContentType("application/json;charset=UTF-8");
        HttpServletRequest request = ctx.getRequest();
        String token = request.getHeader(Header.REQUEST_X_AUTH_TOKEN.getValue());
        // String loginType = request.getHeader(Header.LOGIN_TYPE.getValue());
        // 根据URL判断是访问哪一个端
        String requestURI = request.getRequestURI();
        ResponseBean responseBean = new ResponseBean();

        if (StringUtils.isBlank(token)) {

        }else {
            log.info("获取的token: {}", token);

            if (matcher.match(RequestURI.APP_API.getUrl(), requestURI)) {
                UserElement appElement = (UserElement) request.getSession().getAttribute(Constant.APP_USER_SESSION);
                if (null == appElement) {
                    responseBean.addError(Status.RESPONSE_STATUS_803.getValue(),Status.RESPONSE_STATUS_803.getComment());
                    ctx.setSendZuulResponse(false);
                    ctx.setResponseBody(JSONObject.toJSONString(responseBean));
                }

            } else if (matcher.match(RequestURI.OPERATE_API.getUrl(), requestURI) || matcher.match(RequestURI.PROPERTY_API.getUrl(), requestURI)) {
                UserElement webOperateElement = (UserElement) request.getSession().getAttribute(Constant.WEB_USER_SESSION);
                if (null == webOperateElement) {
                    responseBean.addError(Status.RESPONSE_STATUS_803.getValue(),Status.RESPONSE_STATUS_803.getComment());
                    ctx.setSendZuulResponse(false);
                    ctx.setResponseBody(JSONObject.toJSONString(responseBean));
                }
            }

        }

        return null;
    }
}
