package com.ningdatech.pmapi.user.security.auth;

import cn.hutool.core.collection.CollectionUtil;
import com.ningdatech.basic.factory.PropertySourceFactory;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author LiuXinXin
 * @Date 2020/8/1 6:06 下午
 * @Version 1.0
 **/
@Configuration
@PropertySource(value = "classpath:security/auth-${spring.profiles.active}.yml"
        , encoding = "utf-8", factory = PropertySourceFactory.class)
@ConfigurationProperties(prefix = "security.auth")
@Component
@Data
public class AuthProperties {

    private String authRequireUrl;

    private String invalidSessionUrl;

    private String passwordLoginUrl;

    /**
     * 代登陆接口
     */
    private String agentLoginUrl;

    /**
     * 跳转登陆接口(目前只有省局跳转)
     */
    private String commonLoginUrl;

    private String logoutUrl;

    private List<String> ignoreAuthUrls;

    private List<String> ignoreCsrfUrls;

    private Map<String, List<String>> roleMap = new HashMap<>();

    public String[] getIgnoreAuthUrlsArray() {
        String[] stringArray = new String[ignoreAuthUrls.size()];
        return ignoreAuthUrls.toArray(stringArray);
    }

    public String[] getIgnoreCsrfUrlsArray() {
        String[] stringArray = new String[ignoreCsrfUrls.size()];
        return ignoreCsrfUrls.toArray(stringArray);
    }

    public Map<String, String[]> getRoleArrayMap() {
        Map<String, String[]> roleArrayMap = new HashMap<>();
        if (Objects.nonNull(roleMap)) {
            Set<String> keySet = roleMap.keySet();
            for (String key : keySet) {
                List<String> urls = roleMap.get(key);
                if (CollectionUtil.isNotEmpty(urls)) {
                    String[] stringArray = new String[urls.size()];
                    roleArrayMap.put(key, urls.toArray(stringArray));
                }
            }
        }
        return roleArrayMap;
    }
}
