package com.lion.lionwebsite.Interceptor;

import com.lion.lionwebsite.Dao.normal.UserMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;


@Component
public class TaskHandlerInterceptor implements HandlerInterceptor {

    @Resource
    UserMapper userMapper;

    String[] AuthCodes;

    public TaskHandlerInterceptor(UserMapper userMapper) {
        this.userMapper = userMapper;
        AuthCodes = userMapper.selectAllAuthCode();
    }

    @Override
    public boolean preHandle(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler){
        String auth = request.getParameter("AuthCode");

        if(auth != null)
            for(String AuthCode: AuthCodes)
                if(auth.equals(AuthCode))
                    return true;

        return false;
    }

    public void updateAuthCodes(){
        AuthCodes = userMapper.selectEnableAuthCode();
    }
}
