package com.interplug.qcast.batch;

import java.util.Date;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class JobLauncherController {
  private final Map<String, Job> jobs; // 여러 Job을 주입받도록 변경

  private final JobLauncher jobLauncher;

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

  /**
   * 특정 Job을 매핑으로 실행하는 메소드
   *
   * @param jobName
   * @return
   * @throws JobInstanceAlreadyCompleteException
   * @throws JobExecutionAlreadyRunningException
   * @throws JobParametersInvalidException
   * @throws JobRestartException
   */
  @GetMapping("/batch/job/{jobName}") // Path Variable로 jobName을 받음
  public String launchJob(@PathVariable String jobName)
      throws JobInstanceAlreadyCompleteException,
          JobExecutionAlreadyRunningException,
          JobParametersInvalidException,
          JobRestartException {

    Job job = jobs.get(jobName);
    if (job == null) {
      return "Job " + jobName + " not found";
    }

    JobParameters jobParameters =
        new JobParametersBuilder()
            .addString("jobName", jobName)
            .addDate("time", new Date())
            .toJobParameters();

    jobLauncher.run(job, jobParameters);

    return "Job " + jobName + " started";
  }

  /**
   * Q.CAST 판매점 / 사용자 / 즐겨찾기 / 노출 아이템 동기화 배치
   *
   * @return
   * @throws JobInstanceAlreadyCompleteException
   * @throws JobExecutionAlreadyRunningException
   * @throws JobParametersInvalidException
   * @throws JobRestartException
   */
  // @Scheduled(cron = "*/5 * * * * *")
  @Scheduled(cron = "0 55 23 * * *")
  public String storeAdditionalInfoJob()
      throws JobInstanceAlreadyCompleteException,
          JobExecutionAlreadyRunningException,
          JobParametersInvalidException,
          JobRestartException {

    String jobName = "storeAdditionalJob";
    Job job = jobs.get(jobName);
    if (job == null) {
      return "Job " + jobName + " not found";
    }
    if ("Y".equals(System.getProperty("spring.profiles.scheduler"))) {
      JobParameters jobParameters =
          new JobParametersBuilder().addDate("time", new Date()).toJobParameters();

      jobLauncher.run(job, jobParameters);
    }
    return "OK";
  }

  /**
   * 아이템 동기화 배치
   *
   * @return
   * @throws JobInstanceAlreadyCompleteException
   * @throws JobExecutionAlreadyRunningException
   * @throws JobParametersInvalidException
   * @throws JobRestartException
   */
  @Scheduled(cron = "0 30 02 * * *")
  public String materialJob()
      throws JobInstanceAlreadyCompleteException,
          JobExecutionAlreadyRunningException,
          JobParametersInvalidException,
          JobRestartException {

    String jobName = "materialJob";
    Job job = jobs.get(jobName);
    if (job == null) {
      return "Job " + jobName + " not found";
    }

    if ("Y".equals(System.getProperty("spring.profiles.scheduler"))) {
      JobParameters jobParameters =
          new JobParametersBuilder().addDate("time", new Date()).toJobParameters();

      jobLauncher.run(job, jobParameters);
    }
    return "OK";
  }

  /**
   * BOM 아이템 동기화 배치
   *
   * @return
   * @throws JobInstanceAlreadyCompleteException
   * @throws JobExecutionAlreadyRunningException
   * @throws JobParametersInvalidException
   * @throws JobRestartException
   */
  @Scheduled(cron = "0 40 02 * * *")
  public String bomJob()
      throws JobInstanceAlreadyCompleteException,
          JobExecutionAlreadyRunningException,
          JobParametersInvalidException,
          JobRestartException {

    String jobName = "bomJob";
    Job job = jobs.get(jobName);
    if (job == null) {
      return "Job " + jobName + " not found";
    }

    if ("Y".equals(System.getProperty("spring.profiles.scheduler"))) {
      JobParameters jobParameters =
          new JobParametersBuilder().addDate("time", new Date()).toJobParameters();

      jobLauncher.run(job, jobParameters);
    }

    return "OK";
  }

  /**
   * 영업사원 동기화 배치
   *
   * @return
   * @throws JobInstanceAlreadyCompleteException
   * @throws JobExecutionAlreadyRunningException
   * @throws JobParametersInvalidException
   * @throws JobRestartException
   */
  @Scheduled(cron = "0 40 03 * * *")
  public String businessChargerJob()
      throws JobInstanceAlreadyCompleteException,
          JobExecutionAlreadyRunningException,
          JobParametersInvalidException,
          JobRestartException {

    String jobName = "businessChargerJob";
    Job job = jobs.get(jobName);
    if (job == null) {
      return "Job " + jobName + " not found";
    }

    if ("Y".equals(System.getProperty("spring.profiles.scheduler"))) {
      JobParameters jobParameters =
          new JobParametersBuilder().addDate("time", new Date()).toJobParameters();

      jobLauncher.run(job, jobParameters);
    }

    return "OK";
  }

  /**
   * 관리자 유저 동기화 배치
   *
   * @return
   * @throws JobInstanceAlreadyCompleteException
   * @throws JobExecutionAlreadyRunningException
   * @throws JobParametersInvalidException
   * @throws JobRestartException
   */
  @Scheduled(cron = "0 0 01 * * *")
  public String adminUserJob()
      throws JobInstanceAlreadyCompleteException,
          JobExecutionAlreadyRunningException,
          JobParametersInvalidException,
          JobRestartException {

    String jobName = "adminUserJob";
    Job job = jobs.get(jobName);
    if (job == null) {
      return "Job " + jobName + " not found";
    }

    if ("Y".equals(System.getProperty("spring.profiles.scheduler"))) {
      JobParameters jobParameters =
          new JobParametersBuilder().addDate("time", new Date()).toJobParameters();

      jobLauncher.run(job, jobParameters);
    }

    return "OK";
  }

  /**
   * 가격 동기화 배치
   *
   * @return
   * @throws JobInstanceAlreadyCompleteException
   * @throws JobExecutionAlreadyRunningException
   * @throws JobParametersInvalidException
   * @throws JobRestartException
   */
  @Scheduled(cron = "0 0 0 * * *")
  public String priceJob()
      throws JobInstanceAlreadyCompleteException,
          JobExecutionAlreadyRunningException,
          JobParametersInvalidException,
          JobRestartException {

    String jobName = "priceJob";
    Job job = jobs.get(jobName);
    if (job == null) {
      return "Job " + jobName + " not found";
    }

    if ("Y".equals(System.getProperty("spring.profiles.scheduler"))) {
      JobParameters jobParameters =
          new JobParametersBuilder().addDate("time", new Date()).toJobParameters();

      jobLauncher.run(job, jobParameters);
    }

    return "OK";
  }

  /**
   * 공통코드 M_COMM_H, M_COMM_L 동기화 배치
   *
   * @return
   * @throws JobInstanceAlreadyCompleteException
   * @throws JobExecutionAlreadyRunningException
   * @throws JobParametersInvalidException
   * @throws JobRestartException
   */
  @Scheduled(cron = "0 10 03 * * *")
  public String commonCodeJob()
      throws JobInstanceAlreadyCompleteException,
          JobExecutionAlreadyRunningException,
          JobParametersInvalidException,
          JobRestartException {

    String jobName = "commonCodeJob";
    Job job = jobs.get(jobName);
    if (job == null) {
      return "Job " + jobName + " not found";
    }

    if ("Y".equals(System.getProperty("spring.profiles.scheduler"))) {
      JobParameters jobParameters =
          new JobParametersBuilder().addDate("time", new Date()).toJobParameters();

      jobLauncher.run(job, jobParameters);
    }
    return "OK";
  }

  /**
   * Q.CAST 견적특이사항 / 아이템 표시, 미표시 동기화 배치
   *
   * @return
   * @throws JobInstanceAlreadyCompleteException
   * @throws JobExecutionAlreadyRunningException
   * @throws JobParametersInvalidException
   * @throws JobRestartException
   */
  @Scheduled(cron = "0 30 23 * * *")
  public String specialNoteDispItemAdditionalInfoJob()
      throws JobInstanceAlreadyCompleteException,
          JobExecutionAlreadyRunningException,
          JobParametersInvalidException,
          JobRestartException {
    String jobName = "specialNoteDispItemAdditionalJob";
    Job job = jobs.get(jobName);
    if (job == null) {
      return "Job " + jobName + " not found";
    }

    if ("Y".equals(System.getProperty("spring.profiles.scheduler"))) {
      JobParameters jobParameters =
          new JobParametersBuilder().addDate("time", new Date()).toJobParameters();

      jobLauncher.run(job, jobParameters);
    }

    return "OK";
  }

  /**
   * Plan Confrim 동기화 배치
   *
   * @return
   * @throws JobInstanceAlreadyCompleteException
   * @throws JobExecutionAlreadyRunningException
   * @throws JobParametersInvalidException
   * @throws JobRestartException
   */
  @Scheduled(cron = "1 0 0 * * *")
  public String planConfirmJob()
      throws JobInstanceAlreadyCompleteException,
          JobExecutionAlreadyRunningException,
          JobParametersInvalidException,
          JobRestartException {

    String jobName = "planConfirmJob";
    Job job = jobs.get(jobName);
    if (job == null) {
      return "Job " + jobName + " not found";
    }

    if ("Y".equals(System.getProperty("spring.profiles.scheduler"))) {
      JobParameters jobParameters =
          new JobParametersBuilder().addDate("time", new Date()).toJobParameters();

      jobLauncher.run(job, jobParameters);
    }

    return "OK";
  }

  /**
   * 견적서 전송 동기화 배치
   *
   * @return
   * @throws JobInstanceAlreadyCompleteException
   * @throws JobExecutionAlreadyRunningException
   * @throws JobParametersInvalidException
   * @throws JobRestartException
   */
  @Scheduled(cron = "1 20 0 * * *")
  public String estimateSyncJob()
      throws JobInstanceAlreadyCompleteException,
          JobExecutionAlreadyRunningException,
          JobParametersInvalidException,
          JobRestartException {

    String jobName = "estimateSyncJob";
    Job job = jobs.get(jobName);
    if (job == null) {
      return "Job " + jobName + " not found";
    }

    if ("Y".equals(System.getProperty("spring.profiles.scheduler"))) {
      JobParameters jobParameters =
          new JobParametersBuilder().addDate("time", new Date()).toJobParameters();

      jobLauncher.run(job, jobParameters);
    }

    return "OK";
  }
}
