package ru.micord.ervu.account_applications.kafka;
import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import ru.micord.ervu.account_applications.service.ErvuDirectoriesService;

/**
 * @author Eduard Tihomirov
 */
@Component
@DependsOn("ervuDirectoriesListener")
public class KafkaConsumerInitializer {
  @Value("${load.directories:true}")
  private Boolean loadDirectories;

  private final ErvuDirectoriesService ervuDirectoriesService;

  public KafkaConsumerInitializer(ErvuDirectoriesService ervuDirectoriesService) {
    this.ervuDirectoriesService = ervuDirectoriesService;
  }

  @PostConstruct
  public void init() {
    if (loadDirectories) {
      new Thread(this::runWithSleep).start();
    }
  }

  private void runWithSleep() {
    try {
      Thread.sleep(10000);
    }
    catch (InterruptedException e) {
      throw new RuntimeException(e);
    }
    ervuDirectoriesService.updateDirectories();
  }
}
