package com.tsi.comm.server.config;

import com.tsi.app.common.xnet.NetUtils;
import lombok.Data;
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
@Data
@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;

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

    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.equals("")) {
            this.cvimServers = this.bootstrapServers;
        }
        if (this.nodeServers.equals("")) {
            this.nodeServers = this.bootstrapServers;
        }
        if (this.testServers.equals("")) {
            this.testServers = this.bootstrapServers;
        }

        log.info("[{}] --------------------", this.getClass().getSimpleName());
        log.info("[{}]     multiConnect: {}", this.getClass().getSimpleName(), this.multiConnect);
        log.info("[{}] bootstrapServers: {}", this.getClass().getSimpleName(), this.bootstrapServers);
        log.info("[{}]            props: {}", this.getClass().getSimpleName(), this.props.toArray());
        log.info("[{}]      cvimServers: {}", this.getClass().getSimpleName(), this.cvimServers);
        log.info("[{}]      nodeServers: {}", this.getClass().getSimpleName(), this.nodeServers);
        log.info("[{}]      testServers: {}", this.getClass().getSimpleName(), this.testServers);
        log.info("[{}]       enableCvim: {}", this.getClass().getSimpleName(), this.enableCvim);
        log.info("[{}]       enableNode: {}", this.getClass().getSimpleName(), this.enableNode);
        log.info("[{}]       enableTest: {}", this.getClass().getSimpleName(), 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;
    }
}
