package org.ju2ender.gitlab.config;

import org.ju2ender.gitlab.aspects.RequestAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.resource.VersionResourceResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.servlet.ServletContext;

@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy
@ComponentScan(
    value = {
        "org.ju2ender.gitlab",
        "org.ju2ender.x"
    }
)
// The type WebMvcConfigurerAdapter is deprecated
// https://stackoverflow.com/a/47553586
public class WebConfig implements WebMvcConfigurer
{
    @Autowired
    ServletContext servletContext;

    @Bean
    public ViewResolver viewResolver()
    {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();

        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);

        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
    {
        configurer.enable();
    }

    @Bean
    public RequestAspect requestAspect()
    {
        return new RequestAspect();
    }
}
