package com.tsi.comm.server.config;

import com.tsi.comm.server.xnet.NetUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "application.kafka.producer")
public class TsiKafkaProducerConfig {

    public static final String CVIM_RAW_TOPIC = "cvim-raw";
    public static final String TEST_TOPIC = "topic-for-ssd-test";
    public static final String CVIM_PING = "cvim_ping";

    private String consumerGroupId = "tsi-comm-server";
    private String consumerAckConfig = "1";

    private boolean multiConnect = false;
    private String bootstrapServers;

    private List<Map<String, String>> props = new ArrayList<>();

    private String cvimServers = "";
    private String nodeServers = "";
    private String testServers = "";

    private boolean enableCvim = true;
    private boolean enableNode = true;
    private boolean enableTest = true;

    @PostConstruct
    private void init() {

        if (this.cvimServers.isEmpty()) {
            this.cvimServers = this.bootstrapServers;
        }
        if (this.nodeServers.isEmpty()) {
            this.nodeServers = this.bootstrapServers;
        }
        if (this.testServers.isEmpty()) {
            this.testServers = this.bootstrapServers;
        }

        log.info("[TsiKafkaProducerConfig] --------------------");
        log.info("[TsiKafkaProducerConfig]     multiConnect: {}", this.multiConnect);
        log.info("[TsiKafkaProducerConfig] bootstrapServers: {}", this.bootstrapServers);
        log.info("[TsiKafkaProducerConfig]            props: {}", this.props.toArray());
        log.info("[TsiKafkaProducerConfig]      cvimServers: {}", this.cvimServers);
        log.info("[TsiKafkaProducerConfig]      nodeServers: {}", this.nodeServers);
        log.info("[TsiKafkaProducerConfig]      testServers: {}", this.testServers);
        log.info("[TsiKafkaProducerConfig]       enableCvim: {}", this.enableCvim);
        log.info("[TsiKafkaProducerConfig]       enableNode: {}", this.enableNode);
        log.info("[TsiKafkaProducerConfig]       enableTest: {}", this.enableTest);
    }

    public String getGroupId() {
        return this.consumerGroupId + "-" + NetUtils.getHostName();
    }

    public Map<String, Object> getConsumerPropertiesMap() {
        Map<String, Object> properties = new HashMap<>();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, this.bootstrapServers);
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, getGroupId());
        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true");
        properties.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, 1);
        properties.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "10000");
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
        properties.put(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG, "100");
        properties.put(ConsumerConfig.CHECK_CRCS_CONFIG, false);
        properties.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 1);
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, org.apache.kafka.common.serialization.StringDeserializer.class);
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, org.apache.kafka.common.serialization.LongDeserializer.class);

        return properties;
    }
}
