package ru.micord.ervu.audit.config;

import java.util.HashMap;
import java.util.Map;

import org.apache.kafka.clients.CommonClientConfigs;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.config.SaslConfigs;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.config.TopicBuilder;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaAdmin;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;

/**
 * @author Adel Kalimullin
 */
@Configuration
@Conditional(AuditEnabledCondition.class)
public class AuditKafkaConfig {
  @Value("${audit.kafka.bootstrap.servers}")
  private String bootstrapServers;
  @Value("${audit.kafka.security.protocol}")
  private String securityProtocol;
  @Value("${audit.kafka.login.module}")
  private String loginModule;
  @Value("${audit.kafka.username}")
  private String username;
  @Value("${audit.kafka.password}")
  private String password;
  @Value("${audit.kafka.sasl.mechanism}")
  private String saslMechanism;
  @Value("${audit.kafka.authorization.topic:ervu.lkrp.auth.events}")
  private String authorizationTopic;
  @Value("${audit.kafka.action.topic:ervu.lkrp.action.events}")
  private String actionTopic;
  @Value("${audit.kafka.file.download.topic:ervu.lkrp.import.file}")
  private String fileDownloadTopic;


  @Bean("auditProducerFactory")
  public ProducerFactory<String, String> producerFactory() {
    Map<String, Object> configProps = new HashMap<>();
    configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
    configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProps.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, securityProtocol);
    configProps.put(SaslConfigs.SASL_JAAS_CONFIG, loginModule + " required username=\""
                                                  + username + "\" password=\"" + password + "\";");
    configProps.put(SaslConfigs.SASL_MECHANISM, saslMechanism);
    return new DefaultKafkaProducerFactory<>(configProps);
  }

  @Bean("auditTemplate")
  public KafkaTemplate<String, String> kafkaTemplate() {
    return new KafkaTemplate<>(producerFactory());
  }

  @Bean
  public KafkaAdmin auditKafkaAdmin() {
    Map<String, Object> configs = new HashMap<>();
    configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
    configs.put(AdminClientConfig.SECURITY_PROTOCOL_CONFIG, securityProtocol);
    configs.put(SaslConfigs.SASL_JAAS_CONFIG, loginModule + " required username=\""
                                              + username + "\" password=\"" + password + "\";");
    configs.put(SaslConfigs.SASL_MECHANISM, saslMechanism);
    return new KafkaAdmin(configs);
  }

  @Bean
  public NewTopic auditAuthTopic() {
    return TopicBuilder.name(authorizationTopic).build();
  }

  @Bean
  public NewTopic auditActionTopic() {
    return TopicBuilder.name(actionTopic).build();
  }

  @Bean
  public NewTopic auditDownloadTopic() {
    return TopicBuilder.name(fileDownloadTopic).build();
  }
}
