package ink.wgink.common.manager.access;

import ink.wgink.common.dao.access.IWhiteListDao;
import ink.wgink.properties.BaseProperties;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.AntPathMatcher;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName: WhiteListManager
 * @Description: 白名单管理
 * @Author: wanggeng
 * @Date: 2022/7/27 11:18
 * @Version: 1.0
 */
public class WhiteListManager {

    private static final Logger LOG = LoggerFactory.getLogger(WhiteListManager.class);
    private static WhiteListManager whiteListManager = WhiteListManagerBuilder.whiteListManager;
    private BaseProperties baseProperties;
    private IWhiteListDao whiteListDao;
    private Set<String> whiteListSet = new HashSet<>();
    private AntPathMatcher antPathMatcher = new AntPathMatcher();

    private WhiteListManager() {

    }

    public static WhiteListManager getInstance() {
        return whiteListManager;
    }

    public Set<String> getWhiteListSet() {
        return whiteListSet;
    }

    /**
     * 是否匹配
     *
     * @param path
     * @return
     */
    public boolean isMatch(String path) {
        for (String whiteList : whiteListSet) {
            if (antPathMatcher.match(whiteList, path)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 刷新白名单
     */
    public void refreshWhiteList() {
        whiteListSet.clear();
        addBaseList();
        // 加载数据库配置
        whiteListSet.addAll(whiteListDao.list());
    }

    private void addBaseList() {
        whiteListSet.add(baseProperties.getLoginUrl());
        whiteListSet.add(baseProperties.getLoginProcess());
        whiteListSet.add(baseProperties.getLoginFailure());
        whiteListSet.add("/oauth/**");
        whiteListSet.add("/oauth2_client/**");
        whiteListSet.add("/app/**");
        whiteListSet.add("/approute/**");
        whiteListSet.add("/wechat/**");
        whiteListSet.add("/wechat-miniapp/**");
        whiteListSet.add("/api/sms/getverificationcode/*");
        whiteListSet.add("/api/user/getsignintype/**");
        whiteListSet.add("/assets/**");
        whiteListSet.add("/route/file/**");
        whiteListSet.add("/files/**");

        String assetsMatchers = baseProperties.getAssetsMatchers();
        // 资源
        if (!StringUtils.isBlank(assetsMatchers)) {
            String[] assetsMatchersArray = baseProperties.getAssetsMatchers().split(",");
            for (String assetsMatcher : assetsMatchersArray) {
                whiteListSet.add(assetsMatcher);
            }
        }
    }

    public void setBaseProperties(BaseProperties baseProperties) {
        this.baseProperties = baseProperties;
    }

    public void setWhiteListDao(IWhiteListDao whiteListDao) {
        this.whiteListDao = whiteListDao;
    }

    private static class WhiteListManagerBuilder {
        public static WhiteListManager whiteListManager = new WhiteListManager();
    }
}
