package com.zanxiang.game.module.sdk.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.Metric;
import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.PartitionInfo;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.errors.ProducerFencedException;
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.Configuration;

import java.net.InetAddress;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Future;

/**
 * @author : lingfeng
 * @time : 2022-09-28
 * @description : kafka配置
 */
@Slf4j
@Configuration
public class KafkaConfig {

    @Value("${spring.kafka.game-sdk.bootstrap-servers}")
    private String gameSdkKafkaSevers;

    @Value("${server.domain}")
    private String serverUrl;

    @Bean("gameSdkKafkaProducer")
    public Producer<String, String> gameKafkaProducer() {
        if (this.serverUrl.contains("test")) {
            return new TempKafkaProducer<>();
        }
        String clientId = "UNKNOWN";
        try {
            clientId = InetAddress.getLocalHost().getHostAddress();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        Properties props = new Properties();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, gameSdkKafkaSevers);
        props.put(ProducerConfig.CLIENT_ID_CONFIG, clientId);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        return new KafkaProducer<>(props);
    }

    public static class TempKafkaProducer<K, V> implements Producer<K, V> {

        @Override
        public void initTransactions() {

        }

        @Override
        public void beginTransaction() throws ProducerFencedException {

        }

        @Override
        public void sendOffsetsToTransaction(Map<TopicPartition, OffsetAndMetadata> map, String s) throws ProducerFencedException {

        }

        @Override
        public void commitTransaction() throws ProducerFencedException {

        }

        @Override
        public void abortTransaction() throws ProducerFencedException {

        }

        @Override
        public Future<RecordMetadata> send(ProducerRecord<K, V> producerRecord) {
            return null;
        }

        @Override
        public Future<RecordMetadata> send(ProducerRecord<K, V> producerRecord, Callback callback) {
            return null;
        }

        @Override
        public void flush() {

        }

        @Override
        public List<PartitionInfo> partitionsFor(String s) {
            return null;
        }

        @Override
        public Map<MetricName, ? extends Metric> metrics() {
            return null;
        }

        @Override
        public void close() {

        }

        @Override
        public void close(Duration duration) {

        }
    }

}
