package org.example.demo.praktikum3;

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.Topic;

public class Client {
    private int id;

    public Client(int id) {
        this.id = id;

        Properties props = new Properties();
        props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
        props.setProperty(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");

        try {
            InitialContext context = new InitialContext(props);
            ConnectionFactory cf = (ConnectionFactory) context.lookup("jms/RemoteConnectionFactory");
            Topic topic = (Topic) context.lookup("jms/topic/Praktikum3Topic");

            var sender = new MessageSender(topic, cf, "Sender " + this.id + ": ");
            var receiver = new MessageReceiver(topic, cf, null);
            var win = new ChatWindow(receiver, sender);

            win.setVisible(true);
            win.setTitle("Chat - Sender " + this.id);
            receiver.start();
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}
