package cz.moneta.demo;

import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.WMQConstants;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;

import javax.jms.JMSContext;
import java.util.Properties;

public class MessagingConnectionApp {

    public static KafkaProducer<String, String> createKafkaConnection(String bootstrapServers,
                                                                      String apiKey,
                                                                      String apiSecret) {
        Properties kafkaProps = new Properties();
        kafkaProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        kafkaProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        kafkaProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        kafkaProps.put("security.protocol", "SASL_SSL");
        kafkaProps.put("sasl.mechanism", "PLAIN");
        kafkaProps.put("sasl.jaas.config",
                String.format("org.apache.kafka.common.security.plain.PlainLoginModule required username=\"%s\" password=\"%s\";",
                        apiKey, apiSecret));

        return new KafkaProducer<>(kafkaProps);
    }

    public static JMSContext createMqConnection(String host,
                                                int port,
                                                String channel,
                                                String queueManager,
                                                String user,
                                                String password) throws Exception {
        JmsFactoryFactory factoryFactory = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
        JmsConnectionFactory connectionFactory = factoryFactory.createConnectionFactory();

        connectionFactory.setStringProperty(WMQConstants.WMQ_HOST_NAME, host);
        connectionFactory.setIntProperty(WMQConstants.WMQ_PORT, port);
        connectionFactory.setStringProperty(WMQConstants.WMQ_CHANNEL, channel);
        connectionFactory.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, queueManager);
        connectionFactory.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
        connectionFactory.setStringProperty(WMQConstants.USERID, user);
        connectionFactory.setStringProperty(WMQConstants.PASSWORD, password);

        return connectionFactory.createContext(user, password, JMSContext.AUTO_ACKNOWLEDGE);
    }

    public static void main(String[] args) throws Exception {
        String kafkaBootstrap = System.getProperty("kafka.bootstrap", "localhost:9092");
        String kafkaApiKey = System.getProperty("kafka.apiKey", "api-key");
        String kafkaApiSecret = System.getProperty("kafka.apiSecret", "api-secret");

        String mqHost = System.getProperty("mq.host", "localhost");
        int mqPort = Integer.parseInt(System.getProperty("mq.port", "1414"));
        String mqChannel = System.getProperty("mq.channel", "DEV.APP.SVRCONN");
        String mqQueueManager = System.getProperty("mq.queueManager", "QM1");
        String mqUser = System.getProperty("mq.user", "app");
        String mqPassword = System.getProperty("mq.password", "pass");

        try (KafkaProducer<String, String> kafkaProducer = createKafkaConnection(kafkaBootstrap, kafkaApiKey, kafkaApiSecret);
             JMSContext mqContext = createMqConnection(mqHost, mqPort, mqChannel, mqQueueManager, mqUser, mqPassword)) {
            System.out.println("Kafka connection created: " + (kafkaProducer != null));
            System.out.println("IBM MQ connection created: " + (mqContext != null));
        }
    }
}
