package com.platform.config;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.Filter;

import com.platform.listener.VideoFileProperties;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.alibaba.druid.util.StringUtils;
import com.platform.modules.sys.oauth2.OAuth2Filter;
import com.platform.modules.sys.oauth2.OAuth2Realm;

/**
 * Shiro配置
 */
@Configuration
@Import(VideoFileProperties.class)
public class ShiroConfig {


    @Bean("sessionManager")
    public SessionManager sessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionValidationSchedulerEnabled(true);
        sessionManager.setSessionIdCookieEnabled(true);
        return sessionManager;
    }

    @Bean("securityManager")
    public SecurityManager securityManager(OAuth2Realm oAuth2Realm, SessionManager sessionManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(oAuth2Realm);
        securityManager.setSessionManager(sessionManager);

        return securityManager;
    }

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager, VideoFileProperties videoFileProperties) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);

        //oauth过滤
        Map<String, Filter> filters = new HashMap<>();
        filters.put("oauth2", new OAuth2Filter());
        shiroFilter.setFilters(filters);

        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/webjars/**", "anon");
        filterMap.put("/druid/**", "anon");
        filterMap.put("/app/**", "anon");
        filterMap.put("/test", "anon");
        filterMap.put("/test/**", "anon");
        filterMap.put("/sys/login", "anon");
        filterMap.put("/video/**", "anon");
        filterMap.put(path(videoFileProperties.getBaseSavePath()), "anon");
        filterMap.put("/swagger/**", "anon");
        filterMap.put("/v2/api-docs", "anon");
        filterMap.put("/v2/api-docs-ext", "anon");
        filterMap.put("/swagger-ui.html", "anon");
        filterMap.put("/swagger-resources/**", "anon");
        filterMap.put("/doc.html", "anon");
        filterMap.put("/captcha.jpg", "anon");
        filterMap.put("/media/**", "anon");
        filterMap.put("/page/**", "anon");
        filterMap.put("/generate/live/**", "anon");
//        filterMap.put("/api/cloud/clips/**", "anon");
        filterMap.put("/plm/material/**", "anon");
        filterMap.put("/liveWs/**", "anon");
        filterMap.put("/socketjs/**", "anon");
        filterMap.put("/common/upload_media", "anon");
//        filterMap.put("/caster/**", "anon");
        filterMap.put("/transcoding/token", "anon");
        filterMap.put("/transcoding/notify", "anon");
        filterMap.put("/live/record/notify", "anon");
        filterMap.put("/live/notify/*", "anon");
        filterMap.put("/foreign/token", "anon");
        filterMap.put("/foreign/live/**", "anon");
        filterMap.put("/homepage/**", "anon");
//        filterMap.put("/api/clips/**", "anon");
        filterMap.put("/clips/**", "anon");
        filterMap.put("/trtc/**", "anon");
        filterMap.put("/websocket/**", "anon");
        filterMap.put("/**", "oauth2");
        shiroFilter.setFilterChainDefinitionMap(filterMap);

        return shiroFilter;
    }

    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true);
        return proxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

    private String path(String path) {
        if (StringUtils.isEmpty(path)) {
            return null;
        }
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        if (!path.endsWith("/")) {
            path = path + "/**";
        } else {
            path = path + "**";
        }
        return path;
    }

}
