package com.interplug.qcast.batch.master;

import com.fasterxml.jackson.core.type.TypeReference;
import com.interplug.qcast.biz.displayItem.DisplayItemService;
import com.interplug.qcast.biz.displayItem.dto.BomSyncResponse;
import com.interplug.qcast.util.InterfaceQsp;
import java.util.Collections;
import java.util.List;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.StepScope;
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.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.transaction.PlatformTransactionManager;

/** Bom Item 마스터 동기화 배치 */
@Configuration
public class BomConfiguration implements JobExecutionListener {
  private final DisplayItemService displayItemService;

  private final InterfaceQsp interfaceQsp;

  List<BomSyncResponse> bomSyncList;

  @Value("${qsp.master-bom-batch-url}")
  private String qspInterfaceUrl;

  public BomConfiguration(DisplayItemService displayItemService, InterfaceQsp interfaceQsp) {
    this.displayItemService = displayItemService;
    this.interfaceQsp = interfaceQsp;
  }

  @Bean
  public Job bomJob(JobRepository jobRepository, Step bomStep) {
    return new JobBuilder("bomJob", jobRepository).start(bomStep).build();
  }

  @Bean
  public Step bomStep(JobRepository jobRepository, PlatformTransactionManager transactionManager)
      throws Exception {
    return new StepBuilder("bomStep", jobRepository)
        .<BomSyncResponse, BomSyncResponse>chunk(100, transactionManager)
        .reader(bomReader())
        .processor(bomProcessor())
        .writer(bomWriter())
        .build();
  }

  @Bean
  @StepScope
  public ListItemReader<BomSyncResponse> bomReader() throws Exception {
    this.bomSyncList =
        interfaceQsp.callApiData(
            HttpMethod.GET, qspInterfaceUrl, null, new TypeReference<List<BomSyncResponse>>() {});
    return (bomSyncList != null)
        ? new ListItemReader<>(bomSyncList)
        : new ListItemReader<>(Collections.emptyList());
  }

  @Bean
  public ItemProcessor<BomSyncResponse, BomSyncResponse> bomProcessor() {
    return item -> item;
  }

  @Bean
  public ItemWriter<BomSyncResponse> bomWriter() {
    return items -> {
      displayItemService.setBomSyncSave((List<BomSyncResponse>) items.getItems());
    };
  }
}
