package ru.micord.ervu.kafka.service.impl;

import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.header.internals.RecordHeader;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.kafka.requestreply.ReplyingKafkaTemplate;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.stereotype.Service;

/**
 * @author Eduard Tihomirov
 */
@Service
@Qualifier("person")
public class PersonReplyingKafkaService extends BaseReplyingKafkaService<String, String> {

  private final ReplyingKafkaTemplate<String, String, String> replyingKafkaTemplate;

  public PersonReplyingKafkaService(@Qualifier("person")
      ReplyingKafkaTemplate<String, String, String> replyingKafkaTemplate) {
    this.replyingKafkaTemplate = replyingKafkaTemplate;
  }

  @Override
  protected ReplyingKafkaTemplate<String, String, String> getTemplate() {
    return replyingKafkaTemplate;
  }

  @Override
  protected ProducerRecord<String, String> getProducerRecord(String requestTopic, String replyTopic,
                                                             String requestMessage) {
    ProducerRecord<String, String> record = new ProducerRecord<>(requestTopic, requestMessage);
    record.headers().add(new RecordHeader(KafkaHeaders.REPLY_TOPIC, replyTopic.getBytes()));
    return record;
  }
}
