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.Destination;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.jms.MessageConsumer;
import jakarta.jms.MessageListener;
import jakarta.jms.MessageProducer;
import jakarta.jms.Session;
import jakarta.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * Chat Client using publish/subscribe on JMS provider Topic; represents user
 * name as String property of message
 *
 * @author Sandro Leuchter
 *
 */
public class ChatClientUserProperty implements MessageListener {
	private Connection connection;
	private Session session;
	private MessageProducer producer;
	private MessageConsumer consumer;

	/**
	 * constructor, establishes and starts connection to JMS provider specified in
	 * JNDI (via jndi.properties), afterwards producer and consumer are ready
	 *
	 * @param sendDest    Destination for producer
	 * @param receiveDest Destination for consumer
	 *
	 * @throws NamingException JNDI exceptions
	 * @throws JMSException    JMS exceptions
	 * @see jakarta.jms.Destination
	 */
	public ChatClientUserProperty(String sendDest, String receiveDest) 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);
		Destination destOut = (Destination) ctx.lookup(sendDest);
		Destination destIn = (Destination) ctx.lookup(receiveDest);
		this.producer = this.session.createProducer(destOut);
		this.consumer = this.session.createConsumer(destIn);
		this.consumer.setMessageListener(this);
		this.connection.start();
	}

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

	/**
	 * main routine and starting point of program
	 *
	 * @param args[0] user name
	 */
	public static void main(String[] args) {
		ChatClientUserProperty node = null;
		try {
			node = new ChatClientUserProperty(Conf.TOPIC, Conf.TOPIC);
			BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
			String line;
			while (true) {
				line = input.readLine();
				TextMessage msg = node.session.createTextMessage(line);
				msg.setStringProperty("user", args[0]);
				node.producer.send(msg);
			}
		} 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);
			}
		}
	}
}
