package id.iptek.utms.agent.sample;

import java.util.concurrent.TimeUnit;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class MqttSubscribeSample {

    public static void main(String[] args) {

        final String topic = "5354b64f-c52c-45e0-8b26-49b5d5352e44";
        final String topic2 = "V1E0352316_IN";
        //V1E0352316_IN
        int qos = 2;
        //String broker = "tcp://verifone.id:1883";
        String broker       = "tcp://bit.unifiedtms.id:1883";
        String clientId = "SN02";
        MemoryPersistence persistence = new MemoryPersistence();

        try {
            final MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            //connOpts.setUserName("wira");
            //connOpts.setPassword("wira1234".toCharArray());
            connOpts.setUserName("user1");
            connOpts.setPassword("P@ssw0rd".toCharArray());
            connOpts.setAutomaticReconnect(true);
            connOpts.setKeepAliveInterval(10000);
            System.out.println("Connecting to broker: " + broker);
            sampleClient.setCallback(new MqttCallbackExtended() {
                @Override
                public void connectComplete(boolean bln, String string) {
                    System.out.println("Connect complete? " + bln + " : " + string);
                    try {
                        sampleClient.subscribe(topic);
                        sampleClient.subscribe(topic2);
                    } catch(MqttException ex) {
                        System.err.println("Error subscribe topic: " + topic);
                    }
                }

                @Override
                public void connectionLost(Throwable thrwbl) {
                    System.err.println("Connection Lost: " + thrwbl);
                }

                @Override
                public void messageArrived(String topic, MqttMessage mm) throws Exception {
                    System.out.println("<< [" + topic + "] " + new String(mm.getPayload()) + " (" + mm.getId() + " - " + mm.getQos() + ")");
                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken imdt) {
                    System.err.println("Delivery Complete: " + imdt.getMessageId());
                }

            });
            sampleClient.connect(connOpts);
            System.out.println("Connected");
            //sampleClient.subscribe(topic);

            try {
                TimeUnit.SECONDS.sleep(100);
            } catch(InterruptedException ex) {
                ex.printStackTrace();
            }

            sampleClient.disconnect();
            System.out.println("Disconnected");
            System.exit(0);
        } catch (MqttException me) {
            System.out.println("reason " + me.getReasonCode());
            System.out.println("msg " + me.getMessage());
            System.out.println("loc " + me.getLocalizedMessage());
            System.out.println("cause " + me.getCause());
            System.out.println("excep " + me);
            me.printStackTrace();
        }
    }

}
