package com.base.track.schedule;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class AsyncScheduleConfiguration {

    @Value("${async.core.eph}")
    private int ephCore;
    @Value("${async.max.eph}")
    private int ephMax;
    @Value("${async.core.subpoint}")
    private int subPointCore;
    @Value("${async.max.subpoint}")
    private int subPointMax;
    @Value("${async.core.trbut}")
    private int trbutCore;
    @Value("${async.max.trbut}")
    private int trbutMax;
    @Value("${async.core.relay}")
    private int relayCore;
    @Value("${async.max.relay}")
    private int relayMax;
    @Value("${async.core.orbitnum}")
    private int orbitNumCore;
    @Value("${async.max.orbitnum}")
    private int orbitNumMax;
    @Value("${async.core.astronomy}")
    private int astronomyCore;
    @Value("${async.max.astronomy}")
    private int astronomyMax;

    @Bean("ephTaskExecutor")
    public Executor ephTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(ephCore);
        executor.setMaxPoolSize(ephMax);
        executor.setQueueCapacity(10000);
        executor.setAwaitTerminationSeconds(10000);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setThreadNamePrefix("Eph-Async-");
        executor.initialize();
        return executor;
    }

    @Bean("subPointTaskExecutor")
    public Executor subPointTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(subPointCore);
        executor.setMaxPoolSize(subPointMax);
        executor.setQueueCapacity(10000);
        executor.setAwaitTerminationSeconds(10000);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setThreadNamePrefix("Subpoint-Async-");
        executor.initialize();
        return executor;
    }

    @Bean("orbitNumExecutor")
    public Executor orbitNumExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(orbitNumCore);
        executor.setMaxPoolSize(orbitNumMax);
        executor.setQueueCapacity(10000);
        executor.setAwaitTerminationSeconds(10000);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setThreadNamePrefix("OrbitNum-Async-");
        executor.initialize();
        return executor;
    }

    @Bean("astronomyExecutor")
    public Executor astronomyExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(astronomyCore);
        executor.setMaxPoolSize(astronomyMax);
        executor.setQueueCapacity(10000);
        executor.setAwaitTerminationSeconds(10000);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setThreadNamePrefix("Astronomy-Async-");
        executor.initialize();
        return executor;
    }

    @Bean("trbutTaskExecutor")
    public Executor trbutTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(trbutCore);
        executor.setMaxPoolSize(trbutMax);
        executor.setQueueCapacity(10000);
        executor.setAwaitTerminationSeconds(10000);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setThreadNamePrefix("Trbut-Async-");
        executor.initialize();
        return executor;
    }

    @Bean("relayTaskExecutor")
    public Executor relayTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(relayCore);
        executor.setMaxPoolSize(relayMax);
        executor.setQueueCapacity(10000);
        executor.setAwaitTerminationSeconds(10000);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setThreadNamePrefix("Relay-Async-");
        executor.initialize();
        return executor;
    }

}
