package com.qmth.teachcloud.cas.filter;

import com.qmth.teachcloud.cas.util.sxufe.Constants;
import com.qmth.teachcloud.exchange.common.contant.SystemConstant;
import edu.yale.its.tp.cas.client.filter.CASFilter;
import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.jasig.cas.client.session.SingleSignOutFilter;
import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;
import org.jasig.cas.client.util.HttpServletRequestWrapperFilter;
import org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyWebApplicationInitializer implements ServletContextInitializer {
    private static final String SXUFE_URL = "/api/open/sxufe/authentication";
    private static final String GDPU_URL = "/api/open/gdpu/authentication";
    private static final String SXMU_URL = "/api/open/sxmu/authentication";
//    private static final String SXMU_URL = "/*";

    @Override
    public void onStartup(ServletContext servletContext) {
        WebApplicationContext cxt = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        servletContext.setRequestCharacterEncoding(SystemConstant.CHARSET_NAME);
        /**
         * 山西财经filter
         */
        servletContext.addListener(new SingleSignOutHttpSessionListener());
        FilterRegistration.Dynamic caslogoutFilter = servletContext.addFilter("caslogoutFilter", SingleSignOutFilter.class);
        caslogoutFilter.addMappingForUrlPatterns(null, false, SXUFE_URL);

        FilterRegistration.Dynamic ssoFilter = servletContext.addFilter("SSO Filter", CASFilter.class);

        Map<String, String> map = new HashMap<>();
        map.put("edu.yale.its.tp.cas.client.filter.loginUrl", Constants.CAS_LOGIN_URL);
        map.put("edu.yale.its.tp.cas.client.filter.validateUrl", Constants.CAS_VALIDATE_URL);
        //接入的应用系统IP:Port，需要根据实际路径修改

        String serverName = cxt.getEnvironment().getProperty("cas.filter.sxufeServerName");
        map.put("edu.yale.its.tp.cas.client.filter.serverName", serverName);
        map.put("edu.yale.its.tp.cas.client.filter.initContextClass", "net.psctech.sso.filter.LoginFilter");
        ssoFilter.setInitParameters(map);

        ssoFilter.addMappingForUrlPatterns(null, false, SXUFE_URL);

        /**
         * 广药filter
         */
        servletContext.addListener(new org.jasig.cas.client.session.SingleSignOutHttpSessionListener());
        String gdpuServerName = cxt.getEnvironment().getProperty("cas.filter.gdpuServerName");
        servletContext.setInitParameter("serverName", gdpuServerName);

        FilterRegistration.Dynamic caslogoutGdpuFilter = servletContext.addFilter("Single Sign Out Filter", org.ly.uap.client.session.SingleSignOutFilter.class);
        caslogoutGdpuFilter.addMappingForUrlPatterns(null, false, GDPU_URL);

        FilterRegistration.Dynamic authenticationFilter = servletContext.addFilter("Authentication Filter", org.ly.uap.client.authentication.AuthenticationFilter.class);
        authenticationFilter.setInitParameter("casServerLoginUrl", "https://cas.gdpu.edu.cn/lyuapServer/login");
        authenticationFilter.addMappingForUrlPatterns(null, false, GDPU_URL);

        FilterRegistration.Dynamic validationFilter = servletContext.addFilter("Validation Filter", org.ly.uap.client.validation.Cas20ProxyReceivingTicketValidationFilter.class);
        validationFilter.setInitParameter("casServerUrlPrefix", "https://cas.gdpu.edu.cn/lyuapServer");
        validationFilter.addMappingForUrlPatterns(null, false, GDPU_URL);

        FilterRegistration.Dynamic wrapperFilter = servletContext.addFilter("HttpServletRequest Wrapper Filter", org.ly.uap.client.util.HttpServletRequestWrapperFilter.class);
        wrapperFilter.addMappingForUrlPatterns(null, false, GDPU_URL);

        FilterRegistration.Dynamic transNameFilter = servletContext.addFilter("TransNameFilter", org.ly.uap.client.util.TransNameFilter.class);
        transNameFilter.addMappingForUrlPatterns(null, false, GDPU_URL);

        /**
         * 山西医科大学ilter
         */
        servletContext.addListener(new SingleSignOutHttpSessionListener());
        String sxmuServerName = cxt.getEnvironment().getProperty("cas.filter.sxmuServerName");

        FilterRegistration.Dynamic sxmuCaslogoutGdpuFilter = servletContext.addFilter("CAS Single Sign Out Filter", SingleSignOutFilter.class);
        sxmuCaslogoutGdpuFilter.addMappingForUrlPatterns(null, false, SXMU_URL);

        FilterRegistration.Dynamic sxmuAuthenticationFilter = servletContext.addFilter("CASFilter", AuthenticationFilter.class);
        sxmuAuthenticationFilter.setInitParameter("casServerLoginUrl", "https://authserver.sxmu.edu.cn/authserver/login");
        sxmuAuthenticationFilter.setInitParameter("serverName", sxmuServerName);
        sxmuAuthenticationFilter.addMappingForUrlPatterns(null, false, SXMU_URL);

        FilterRegistration.Dynamic sxmuValidationFilter = servletContext.addFilter("CAS Validation Filter", Cas20ProxyReceivingTicketValidationFilter.class);
        sxmuValidationFilter.setInitParameter("casServerUrlPrefix", "https://authserver.sxmu.edu.cn/authserver");
        sxmuValidationFilter.setInitParameter("serverName", sxmuServerName);
        sxmuValidationFilter.setInitParameter("encoding", SystemConstant.CHARSET_NAME);
        sxmuValidationFilter.addMappingForUrlPatterns(null, false, SXMU_URL);

        FilterRegistration.Dynamic sxmuWrapperFilter = servletContext.addFilter("CAS HttpServletRequest Wrapper Filter", HttpServletRequestWrapperFilter.class);
        sxmuWrapperFilter.addMappingForUrlPatterns(null, false, SXMU_URL);
        //接入的应用系统IP:Port，需要根据实际路径修改
    }
}
