package com.yfd.platform.config;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import com.yfd.platform.modules.basedata.service.ISubstationDeviceService;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Order;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.Map;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${file-space.system}")
    private String systempath;

    @Value("${httpserver.patrolserver.snapfilepath}")
    private String snapFilePath;

    @Value("${httpserver.patrolserver.tempfilepath}")
    private String tempFilePath;

    @Value("${httpserver.patrolserver.alarmfilepath}")
    private String alarmFilePath;

    @Resource
    private ISubstationDeviceService substationDeviceService;

    @Bean
    public Map<String, List<Map<String, Object>>> typeList() {
        return substationDeviceService.getDeviceType();
    }

    @Bean
    public Cache<String, String> loginuserCache() {
        return CacheUtil.newLRUCache(200);//用户登录缓存数 缺省200
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source =
                new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOriginPattern("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        config.setMaxAge(3600L);
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

    @SneakyThrows
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 菜单图标访问路径

        // 菜单图标
        String iconUrl ="file:" + systempath+ "menuicon"+ File.separator;
        registry.addResourceHandler("/menu/**").addResourceLocations(iconUrl).setCachePeriod(0);

        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");

        // 用户图片
        String systemUrl = "file:" + systempath + "user"+ File.separator;
        registry.addResourceHandler("/avatar/**").addResourceLocations(systemUrl).setCachePeriod(0);

        // 变电站图片
        String stationUrl = "file:" + systempath + "station"+ File.separator;
        registry.addResourceHandler("/station/**").addResourceLocations(stationUrl).setCachePeriod(0);

        // 巡视设备图片
        String patrolDeviceUrl ="file:" + systempath+ "patrolDevice"+ File.separator;
        registry.addResourceHandler("/patrolDevice/**").addResourceLocations(patrolDeviceUrl).setCachePeriod(0);
        // 预览巡视结果路径（图片或者视频）
        String videoUrl = "file:" + snapFilePath;
        registry.addResourceHandler("/video/**").addResourceLocations(videoUrl).setCachePeriod(0);
        // 点位模板图片
        String deviceUrl = "file:" + tempFilePath;
        registry.addResourceHandler("/temp/**").addResourceLocations(deviceUrl).setCachePeriod(0);

        // 告警图片
        String alarmUrl = "file:" + alarmFilePath;
        registry.addResourceHandler("/alarm/**").addResourceLocations(alarmUrl).setCachePeriod(0);
    }

}
