package ervu_business_metrics.kafka;

import javax.annotation.PostConstruct;

import ervu_business_metrics.config.KafkaEnabledCondition;
import ervu_business_metrics.service.IdmDirectoriesService;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

/**
 * @author Adel Kalimullin
 */
@Component
@DependsOn("idmDirectoriesListener")
@Conditional(KafkaEnabledCondition.class)
public class KafkaConsumerInitializer {
  private final IdmDirectoriesService idmDirectoriesService;

  public KafkaConsumerInitializer(IdmDirectoriesService idmDirectoriesService) {
    this.idmDirectoriesService = idmDirectoriesService;
  }

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

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