import zmq

from messagebus import MessageBus
from zeromqmessagehandler._PublishEventWorkerThread import PublishEventWorkerThread
from zeromqthreadpool import ZeroMqPushPullThreadPool, ThreadPoolParameters


class PublisherMessageBus(MessageBus):
    def __init__(self, context, pub_address):
        # type: (zmq.Context) -> None
        self.context = context
        self.pub_address = pub_address

        self.push_address = "inproc://publisherPullAddress"
        self.thread_pool = ZeroMqPushPullThreadPool(
            self.context,
            self.push_address,
            ThreadPoolParameters(5, 50, 5, 2000, 2000),
            "inproc://publisherWorkerAddress",
            PublishEventWorkerThread,
            pub_address)

    def publish_event(self, event):
        data = event.subject.encode("utf-8") + b" " + event.event_type.encode("utf-8") + b"\0" + event.data

        pub_socket = self.context.socket(zmq.PUSH)
        pub_socket.connect(self.push_address)
        pub_socket.send(data)
        pub_socket.close()

    def send_message(self, component, message):
        pass

    def subscribe(self, subject):
        pass

    def reply_message(self, message, reply_message):
        pass

    def reply_event(self, event, reply_message):
        pass
