import stomp
import json
import time
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from config import get_logger

parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('--verbose', '-v', action='count', default=0)
parser.add_argument('--config', '-c', default='kontor-docker')
args = parser.parse_args()

class MyListener(stomp.ConnectionListener):
    def __init__(self, log):
        self.log = log

    def on_error(self, frame):
        self.log.info(f"received an error {frame.body}")

    def on_message(self, frame):
        self.log.info(f"received a message '{frame.body}'")
        data = json.loads(frame.body)
        url = data['url']
        self.log.info(f"found link: {url}")


if __name__ == '__main__':
    log = get_logger(args.verbose, args.config)
    log.info("kontor.read_queue started")
    host = [('127.0.0.1', 61616)]
    conn = stomp.Connection(host_and_ports=host)
    conn.set_listener('', MyListener(log))
    conn.connect(username='artemis', passcode='artemis', wait=True)
    conn.subscribe(destination='KontorMediaFile::add_link_file', id=1, ack='auto', headers={})
    time.sleep(5)
    conn.disconnect()
    log.info("kontor.read_queue finished")

