package com.caliverse.admin.global.configuration;

import javax.sql.DataSource;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
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 org.springframework.transaction.PlatformTransactionManager;
import com.caliverse.admin.domain.batch.CustomProcessor;
import com.caliverse.admin.domain.batch.CustomReader;
import com.caliverse.admin.domain.batch.CustomWriter;

@Configuration
//public class TestBatchConfiguration extends DefaultBatchConfiguration {
public class TestBatchConfiguration {
//	@Autowired
//    @Qualifier("dataSource-normal")
//    private DataSource dataSource;
//
//	@Autowired
//	@Qualifier("batchTransactionManager")
//	private PlatformTransactionManager transactionManger;
//
//	@Override
//	public DataSource getDataSource() {
//		return dataSource;
//	}
//
//	@Override
//	public PlatformTransactionManager getTransactionManager() {
//		return transactionManger;
//
//	}
//
//
//	@Bean
//	public Job createJob(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
//		return new JobBuilder("testJob", jobRepository)
//			.flow(createStep(jobRepository, transactionManager)).end().build();
//	}
//
//	@Bean
//	Step createStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
//		return new StepBuilder("testStep", jobRepository)
//			.<String, String> chunk(3, transactionManager)
//			.allowStartIfComplete(true)
//			.reader(new CustomReader())
//			.processor(new CustomProcessor())
//			.writer(new CustomWriter())
//			.build();
//	}


	// @Bean
    // public org.springframework.batch.core.Job testJob(JobRepository jobRepository,PlatformTransactionManager transactionManager) throws DuplicateJobException {
    //    org.springframework.batch.core.Job job = new JobBuilder("testJob",jobRepository)
    //            .start(testStep(jobRepository,transactionManager))
    //            .build();
    //    return job;
    // }

    // public Step testStep(JobRepository jobRepository,PlatformTransactionManager transactionManager){
    //     Step step = new StepBuilder("testStep",jobRepository)
    //             .tasklet(testTasklet(),transactionManager)
    //             .build();
    //     return step;
    // }

    // public Tasklet testTasklet(){
    //     return ((contribution, chunkContext) -> {
    //           System.out.println("***** hello batch! *****");
    //           // 원하는 비지니스 로직 작성    
    //           return RepeatStatus.FINISHED;
    //     });
    // }


    // @Bean
    // public JobRepository jobRepository() {
    //     JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    //     factory.setDataSource(dataSource);
    //     factory.setTransactionManager(transactionManager);
    //     try {
	// 		return factory.getObject();
	// 	} catch (Exception e) {
	// 		// TODO Auto-generated catch block
	// 		e.printStackTrace();
	// 	}
	// 	return null;
    // }

	// @Bean
    // public JobLauncher jobLauncher() {
    //     TaskExecutorJobLauncher jobLauncher = new TaskExecutorJobLauncher();
    //     jobLauncher.setJobRepository(jobRepository());
    //     jobLauncher.setTaskExecutor(new SyncTaskExecutor()); // Optional: You can use a different TaskExecutor
    //     return jobLauncher;
    // }

    // @Bean
    // public JobLauncher jobLauncher() {
    //     JobLauncherFactoryBean factory = new JobLauncherFactoryBean();
    //     factory.setJobRepository(jobRepository());
    //     return factory.getObject();
    // }

	// tag::readerwriterprocessor[]
	// @Bean
	// public FlatFileItemReader<Person> reader() {
	// 	return new FlatFileItemReaderBuilder<Person>()
	// 		.name("personItemReader")
	// 		.resource(new ClassPathResource("sample-data.csv"))
	// 		.delimited()
	// 		.names("firstName", "lastName")
	// 		.targetType(Person.class)
	// 		.build();
	// }

	// @Bean
	// public PersonItemProcessor processor() {
	// 	return new PersonItemProcessor();
	// }

	// @Bean
	// public JdbcBatchItemWriter<Person> writer(DataSource dataSource) {
	// 	return new JdbcBatchItemWriterBuilder<Person>()
	// 		.sql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)")
	// 		.dataSource(dataSource)
	// 		.beanMapped()
	// 		.build();
	// }
	// // end::readerwriterprocessor[]

	// // tag::jobstep[]
	// @Bean
	// public Job importUserJob(JobRepository jobRepository,Step step1, JobCompletionNotificationListener listener) {
	// 	return new JobBuilder("importUserJob", jobRepository)
	// 		.listener(listener)
	// 		.start(step1)
	// 		.build();
	// }

	// @Bean
	// public Step step1(JobRepository jobRepository, DataSourceTransactionManager transactionManager,
	// 				  FlatFileItemReader<Person> reader, PersonItemProcessor processor, JdbcBatchItemWriter<Person> writer) {
	// 	return new StepBuilder("step1", jobRepository)
	// 		.<Person, Person> chunk(3, transactionManager)
	// 		.reader(reader)
	// 		.processor(processor)
	// 		.writer(writer)
	// 		.build();

	// end::jobstep[]

}