package com.yutou.bilibili.Tools;

import com.alibaba.fastjson.JSONArray;
import com.yutou.bilibili.Services.IUserService;
import com.yutou.bilibili.mybatis.dao.PermissionDao;
import com.yutou.bilibili.mybatis.model.Permission;
import com.yutou.bilibili.mybatis.model.PermissionExample;
import com.yutou.bilibili.mybatis.model.UUser;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Component
@WebFilter
public class APIFilter implements Filter {
    @Resource
    IUserService service;
    @Resource
    PermissionDao permissionDao;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String token = request.getParameter("token");
        if (StringUtils.isEmpty(token)) {
            Cookie cookie = AppTools.getCookie(request, "login");
            if (cookie != null) {
                token = cookie.getValue();
            }
        }
        String tmp = request.getRequestURI();
        if (tmp.contains("/user/login.do")
                || tmp.contains("/user/reg.do")
                || tmp.contains("/user/logout.do")
                || tmp.contains("/user/get/test.do")
                || tmp.contains("/system/public/reg.do")
                || tmp.contains("/favicon.ico")
        ) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            if (StringUtils.isEmpty(token)) {
                response.sendRedirect("/");
                return;
            }
            UUser user = service.getUserToUid(Integer.parseInt(RedisTools.get(token)));
            if (user == null) {
                response.sendRedirect("/");
            } else {
                String url = null;
                try {
                    url = tmp.split(tmp.split("/")[tmp.split("/").length - 1])[0];
                } catch (Exception e) {
                    com.yutou.bilibili.Tools.Log.e(e);
                    com.yutou.bilibili.Tools.Log.i("无权限请求:" + tmp);
                    return;
                }
                JSONArray powers = JSONArray.parseArray(user.getPower());
                if (powers.toJavaList(Integer.class).contains(-1)) {
                    filterChain.doFilter(servletRequest, servletResponse);
                } else {
                    PermissionExample pExample = new PermissionExample();
                    pExample.createCriteria().andUrlEqualTo(url);
                    List<Permission> permissions = permissionDao.selectByExample(pExample);
                    if (permissions != null && permissions.size() > 0) {
                        if (powers.toJavaList(Integer.class).contains(permissions.get(0).getId())) {
                            filterChain.doFilter(servletRequest, servletResponse);
                        }
                    }
                }
            }
        }
    }


}
