package com.drumonii.loltrollbuild.batch;

import com.drumonii.loltrollbuild.model.Version;
import com.drumonii.loltrollbuild.riot.service.VersionsService;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.drumonii.loltrollbuild.batch.scheduling.RetrievalJobsScheduling.LATEST_PATCH_KEY;

/**
 * {@link Job} configuration for running all retrieval jobs sequentially starting with versions.
 */
@Configuration
public class AllRetrievalsJobConfig {

	@Autowired
	private JobBuilderFactory jobBuilderFactory;

	@Autowired
	private StepBuilderFactory stepBuilderFactory;

	@Autowired
	private VersionsService versionsService;

	@Autowired
	@Qualifier("versionsRetrievalJob")
	private Job versionsRetrievalJob;

	@Autowired
	@Qualifier("mapsRetrievalJob")
	private Job mapsRetrievalJob;

	@Autowired
	@Qualifier("summonerSpellsRetrievalJob")
	private Job summonerSpellsRetrievalJob;

	@Autowired
	@Qualifier("championsRetrievalJob")
	private Job championsRetrievalJob;

	@Autowired
	@Qualifier("itemsRetrievalJob")
	private Job itemsRetrievalJob;

	@Bean
	public Job allRetrievalsJob() {
		return jobBuilderFactory.get("allRetrievalsJob")
				.incrementer(parameters -> {
					Version latestVersion = versionsService.getLatestVersion();
					String patch;
					if (latestVersion == null) {
						patch = new Version("0.0.0").getPatch();
					} else {
						patch = latestVersion.getPatch();
					}
					return new JobParametersBuilder()
							.addString(LATEST_PATCH_KEY, patch)
							.toJobParameters();
				})
				.start(versionsRetrievalJobStep())
				.next(mapsRetrievalJobStep())
				.next(summonerSpellsRetrievalJobStep())
				.next(championsRetrievalJobStep())
				.next(itemsRetrievalJobStep())
				.build();
	}

	@Bean
	public Step versionsRetrievalJobStep() {
		return stepBuilderFactory.get("versionsRetrievalJobStep")
				.job(versionsRetrievalJob)
				.build();
	}

	@Bean
	public Step mapsRetrievalJobStep() {
		return stepBuilderFactory.get("mapsRetrievalJobStep")
				.job(mapsRetrievalJob)
				.build();
	}

	@Bean
	public Step summonerSpellsRetrievalJobStep() {
		return stepBuilderFactory.get("summonerSpellsRetrievalJobStep")
				.job(summonerSpellsRetrievalJob)
				.build();
	}

	@Bean
	public Step championsRetrievalJobStep() {
		return stepBuilderFactory.get("championsRetrievalJobStep")
				.job(championsRetrievalJob)
				.build();
	}

	@Bean
	public Step itemsRetrievalJobStep() {
		return stepBuilderFactory.get("itemsRetrievalJobStep")
				.job(itemsRetrievalJob)
				.build();
	}

}

@@ -191,115 +191,9 @@
 ore.
-Job;%0Aimport org.springframework.batch.core.JobParametersBuilder;%0Aimport org.springframework.batch.core.Step
+*
 ;%0Aim
@@ -1562,364 +1562,51 @@
 ter(
-parameters -%3E %7B%0A%09%09%09%09%09Version latestVersion = versionsService.getLatestVersion();%0A%09%09%09%09%09String patch;%0A%09%09%09%09%09if (latestVersion == null) %7B%0A%09%09%09%09%09%09patch = new Version(%220.0.0%22).getPatch();%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09patch = latestVersion.getPatch();%0A%09%09%09%09%09%7D%0A%09%09%09%09%09return new JobParametersBuilder()%0A%09%09%09%09%09%09%09.addString(LATEST_PATCH_KEY, patch)%0A%09%09%09%09%09%09%09.toJobPara
+new AllRetrievalsJobParametersIncre
 me
+n
 ter
-s
 ()
-;%0A%09%09%09%09%7D
 )%0A%09%09
@@ -2576,32 +2576,32 @@
 msRetrievalJob)%0A
-
 %09%09%09%09.build();%0A%09%7D
@@ -2602,10 +2602,497 @@
 ();%0A%09%7D%0A%0A
+%09private class AllRetrievalsJobParametersIncrementer implements JobParametersIncrementer %7B%0A%0A%09%09@Override%0A%09%09public JobParameters getNext(JobParameters parameters) %7B%0A%09%09%09Version latestVersion = versionsService.getLatestVersion();%0A%09%09%09String patch;%0A%09%09%09if (latestVersion == null) %7B%0A%09%09%09%09patch = new Version(%220.0.0%22).getPatch();%0A%09%09%09%7D else %7B%0A%09%09%09%09patch = latestVersion.getPatch();%0A%09%09%09%7D%0A%09%09%09return new JobParametersBuilder()%0A%09%09%09%09%09.addString(LATEST_PATCH_KEY, patch)%0A%09%09%09%09%09.toJobParameters();%0A%09%09%7D%0A%0A%09%7D%0A%0A
 %7D%0A

Replace JobParametersIncrementer lambda with class