package ru.micord.ervu.eks;

import javax.sql.DataSource;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import ru.cg.webbpm.modules.webkit.DispatcherConfig;

@Configuration
@ComponentScan(basePackages = {
    "service",
    "dao",
    "bpmn",
    "i18n",
    "errorhandling",
    "database",
    "security",
    "component.addresses",
    "gen",
    "ru.cg",
    "ru.micord",
    "com.example.ervu_eks_5"
})
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableScheduling()
@EnableSchedulerLock(defaultLockAtMostFor = "PT4H")
@SpringBootApplication(exclude = {
    HibernateJpaAutoConfiguration.class,
    TransactionAutoConfiguration.class,
})
public class App extends DispatcherConfig {

  @Value("${config.data.executor.socket.timeout:10}")
  private int socketTimeout;
  @Value("${config.data.executor.connection.timeout:10}")
  private int connectionTimeout;

  public static void main(String[] args) {
    SpringApplication.run(App.class, args);
  }

  @Bean
  public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
  }

  @Bean
  public LockProvider lockProvider(DataSource dataSource) {
    return new JdbcTemplateLockProvider(dataSource);
  }

  @Bean
  public RestTemplate restTemplate() {
    RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(socketTimeout * 1000)
            .setConnectionRequestTimeout(connectionTimeout * 1000)
            .setConnectTimeout(connectionTimeout * 1000)
            .build();

    CloseableHttpClient httpClient = HttpClients.custom()
            .setDefaultRequestConfig(requestConfig)
            .build();

    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);

    return new RestTemplate(factory);
  }
}
