package vs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import jakarta.jms.Connection;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.jms.MessageConsumer;
import jakarta.jms.MessageListener;
import jakarta.jms.MessageProducer;
import jakarta.jms.Queue;
import jakarta.jms.Session;
import jakarta.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * requester for String reverter service
 *
 * @author Sandro Leuchter
 *
 */
public class Anfrager implements MessageListener {
	private Connection connection;
	private Session session;
	private MessageProducer producer;
	private MessageConsumer consumer;
	private Queue replyQueue;

	/**
	 * constructor, establishes and starts connection to JMS provider specified in
	 * JNDI (via jndi.properties), afterwards producer and a consumer to a temporary
	 * replyQueue are ready
	 *
	 * @throws NamingException JNDI exceptions
	 * @throws JMSException    JMS exceptions
	 */
	public Anfrager() throws NamingException, JMSException {
		Context ctx = new InitialContext();
		ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
		this.connection = factory.createConnection();
		this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Queue queue = (Queue) ctx.lookup(Conf.QUEUE);
		this.producer = this.session.createProducer(queue);
		this.replyQueue = this.session.createTemporaryQueue();
		this.consumer = this.session.createConsumer(this.replyQueue);
		this.consumer.setMessageListener(this);
		this.connection.start();
	}

	/**
	 * asynchronous message consumption
	 *
	 * @see jakarta.jms.MessageListener
	 */
	@Override
	public void onMessage(Message message) {
		try {
			if (message instanceof TextMessage) {
				TextMessage textMessage = (TextMessage) message;
				String messageText = textMessage.getText();
				System.out.println(messageText);
			}
		} catch (JMSException e) {
			System.err.println(e);
		}

	}

	/**
	 * creates TextMessage for JMS provider with temporary reply queue set and sends
	 * it to Destination of producer, which is configured in Conf.QUEUE
	 *
	 * @param text text to be sent
	 * @throws JMSException JMS exceptions
	 */
	public void sendMessage(String text) throws JMSException {
		TextMessage message = this.session.createTextMessage();
		message.setText(text);
		message.setJMSReplyTo(this.replyQueue);
		this.producer.send(message);
	}

	/**
	 * main routine and starting point of program
	 *
	 * @param args not used
	 */
	public static void main(String[] args) {
		Anfrager node = null;
		try {
			node = new Anfrager();
			BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
			String line;
			while (true) {
				line = input.readLine();
				node.sendMessage(line);
			}
		} catch (NamingException | JMSException | IOException e) {
			System.err.println(e);
		} finally {
			try {
				if ((node != null) && (node.producer != null)) {
					node.producer.close();
				}
				if ((node != null) && (node.consumer != null)) {
					node.consumer.close();
				}
				if ((node != null) && (node.session != null)) {
					node.session.close();
				}
				if ((node != null) && (node.connection != null)) {
					node.connection.close();
				}
			} catch (JMSException e) {
				System.err.println(e);
			}
		}
	}
}
