package ru.micord.ervu.account_applications.kafka;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import ru.micord.ervu.account_applications.model.RecruitmentData;
import ru.micord.ervu.account_applications.model.RecruitmentIpData;
import ru.micord.ervu.account_applications.model.RecruitmentSolutionData;
import ru.micord.ervu.account_applications.model.RoleData;
import ru.micord.ervu.account_applications.model.SolutionData;
import ru.micord.ervu.account_applications.model.SolutionRoleData;
import ru.micord.ervu.account_applications.service.ErvuDirectoriesService;

/**
 * @author Eduard Tihomirov
 */
@Component
public class ErvuDirectoriesListener {

  @Autowired
  private ErvuDirectoriesService ervuDirectoriesService;

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

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

  @KafkaListener(id = "${kafka.solution.group.id}", topics = "${kafka.solution.reconciliation}")
  public void listenKafkaSolution(String kafkaMessage) {
    ervuDirectoriesService.processUpsertMessage(kafkaMessage, SolutionData.class);
  }

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

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

  @KafkaListener(id = "${kafka.domain.deactivated.group.id}", topics = "${kafka.domain.deactivated}")
  public void listenKafkaDomainDeactivated(String kafkaMessage) {
    ervuDirectoriesService.processStatusChange(kafkaMessage, RecruitmentData.class, false);
  }

  @KafkaListener(id = "${kafka.domain.activated.group.id}", topics = "${kafka.domain.activated}")
  public void listenKafkaDomainActivated(String kafkaMessage) {
    ervuDirectoriesService.processStatusChange(kafkaMessage, RecruitmentData.class, true);
  }

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

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

  @KafkaListener(id = "${kafka.role.deactivated.group.id}", topics = "${kafka.role.deactivated}")
  public void listenKafkaRoleDeactivated(String kafkaMessage) {
    ervuDirectoriesService.processStatusChange(kafkaMessage, RoleData.class, false);
  }

  @KafkaListener(id = "${kafka.role.activated.group.id}", topics = "${kafka.role.activated}")
  public void listenKafkaRoleActivated(String kafkaMessage) {
    ervuDirectoriesService.processStatusChange(kafkaMessage, RoleData.class, true);
  }

  @KafkaListener(id = "${kafka.solution.updated.group.id}", topics = "${kafka.solution.updated}")
  public void listenKafkaSolutionUpdated(String kafkaMessage) {
    ervuDirectoriesService.processUpsertMessage(kafkaMessage, SolutionData.class);
  }

  @KafkaListener(id = "${kafka.solution.created.group.id}", topics = "${kafka.solution.created}")
  public void listenKafkaSolutionCreated(String kafkaMessage) {
    ervuDirectoriesService.processUpsertMessage(kafkaMessage, SolutionData.class);
  }

  @KafkaListener(id = "${kafka.solution.deactivated.group.id}", topics = "${kafka.solution.deactivated}")
  public void listenKafkaSolutionDeactivated(String kafkaMessage) {
    ervuDirectoriesService.processStatusChange(kafkaMessage, SolutionData.class, false);
  }

  @KafkaListener(id = "${kafka.solution.activated.group.id}", topics = "${kafka.solution.activated}")
  public void listenKafkaSolutionActivated(String kafkaMessage) {
    ervuDirectoriesService.processStatusChange(kafkaMessage, SolutionData.class, true);
  }

  @KafkaListener(id = "${kafka.solution_role.created.group.id}", topics = "${kafka.solution_role.created}")
  public void listenKafkaSolutionRoleCreated(String kafkaMessage) {
    ervuDirectoriesService.processUpsertMessage(kafkaMessage, SolutionRoleData.class);
  }

  @KafkaListener(id = "${kafka.solution_role.deleted.group.id}", topics = "${kafka.solution_role.deleted}")
  public void listenKafkaSolutionRoleDeleted(String kafkaMessage) {
    ervuDirectoriesService.processDeleteLink(kafkaMessage, SolutionRoleData.class);
  }

  @KafkaListener(id = "${kafka.domain_solution.created.group.id}", topics = "${kafka.domain_solution.created}")
  public void listenKafkaDomainSolutionCreated(String kafkaMessage) {
    ervuDirectoriesService.processUpsertMessage(kafkaMessage, RecruitmentSolutionData.class);
  }

  @KafkaListener(id = "${kafka.domain_solution.deleted.group.id}", topics = "${kafka.domain_solution.deleted}")
  public void listenKafkaDomainSolutionDeleted(String kafkaMessage) {
    ervuDirectoriesService.processDeleteLink(kafkaMessage, RecruitmentSolutionData.class);
  }

  @KafkaListener(id = "${kafka.domain_ip.created.group.id}", topics = "${kafka.domain_ip.created}")
  public void listenKafkaDomainIpCreated(String kafkaMessage) {
    ervuDirectoriesService.processUpsertMessage(kafkaMessage, RecruitmentIpData.class);
  }

  @KafkaListener(id = "${kafka.domain_ip.deleted.group.id}", topics = "${kafka.domain_ip.deleted}")
  public void listenKafkaDomainIpDeleted(String kafkaMessage) {
    ervuDirectoriesService.processDeleteLink(kafkaMessage, RecruitmentIpData.class);
  }
}
