package ervu_business_metrics.kafka.listener;

import ervu_business_metrics.config.KafkaEnabledCondition;
import ervu_business_metrics.model.idm.AccountData;
import ervu_business_metrics.model.idm.DomainData;
import ervu_business_metrics.model.idm.RoleData;
import ervu_business_metrics.service.IdmDirectoriesService;
import org.springframework.context.annotation.Conditional;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

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

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

  @KafkaListener(id = "${kafka.domain.group.id}", topics = "${kafka.domain.reconciliation}")
  public void listenKafkaDomain(String kafkaMessage) {
      idmDirectoriesService.processUpsertMessage(kafkaMessage, DomainData.class);
  }

  @KafkaListener(id = "${kafka.role.group.id}", topics = "${kafka.role.reconciliation}")
  public void listenKafkaRole(String kafkaMessage) {
    idmDirectoriesService.processUpsertMessage(kafkaMessage, RoleData.class);
  }

  @KafkaListener(id = "${kafka.account.group.id}", topics = "${kafka.account.reconciliation}")
  public void listenKafkaAccount(String kafkaMessage) {
    idmDirectoriesService.processUpsertMessage(kafkaMessage, AccountData.class);
  }

  @KafkaListener(id = "${kafka.domain.updated.group.id}", topics = "${kafka.domain.updated}")
  public void listenKafkaDomainUpdated(String kafkaMessage) {
    idmDirectoriesService.processUpsertMessage(kafkaMessage, DomainData.class);
  }

  @KafkaListener(id = "${kafka.domain.created.group.id}", topics = "${kafka.domain.created}")
  public void listenKafkaDomainCreated(String kafkaMessage) {
    idmDirectoriesService.processUpsertMessage(kafkaMessage, DomainData.class);
  }

  @KafkaListener(id = "${kafka.account.updated.group.id}", topics = "${kafka.account.updated}")
  public void listenKafkaAccountUpdated(String kafkaMessage) {
    idmDirectoriesService.processUpsertMessage(kafkaMessage, AccountData.class);
  }

  @KafkaListener(id = "${kafka.account.created.group.id}", topics = "${kafka.account.created}")
  public void listenKafkaAccountCreated(String kafkaMessage) {
    idmDirectoriesService.processUpsertMessage(kafkaMessage, AccountData.class);
  }

  @KafkaListener(id = "${kafka.domain.deleted.group.id}", topics = "${kafka.domain.deleted}")
  public void listenKafkaDomainDeleted(String kafkaMessage) {
    idmDirectoriesService.processDeleteMessage(kafkaMessage, DomainData.class);
  }

  @KafkaListener(id = "${kafka.account.deleted.group.id}", topics = "${kafka.account.deleted}")
  public void listenKafkaAccountDeleted(String kafkaMessage) {
    idmDirectoriesService.processDeleteMessage(kafkaMessage, AccountData.class);
  }
}
