"""
read file with URLs and store in DB
"""
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import logging
import json
from proton import Message, Event
from proton.handlers import MessagingHandler
from proton.reactor import Container

parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('-f', '--links', help='file with links')
parser.add_argument('--verbose', '-v', action='count', default=0)
args = parser.parse_args()


def read_links_file(links_file):
    with open(links_file, 'r') as input_file:
        lines = input_file.readlines()
    return lines


class AddLinkMessage(MessagingHandler):
    def __init__(self, server, url, log):
        super(AddLinkMessage, self).__init__()
        log.info("create AddLinkMessage")
        self.server = server
        self.address = "KontorMediaFile::add_link_file"
        self.url = url
        self.log = log

    def on_start(self, event: Event):
        self.log.info("Connection...")
        conn = event.container.connect(self.server, user="artemis", password="artemis")
        event.container.create_sender(conn, self.address)

    def on_connection_opened(self, event: Event) -> None:
        self.log.debug("connection open")

    def on_connection_error(self, event: Event) -> None:
        self.log.info(f"error: {event}")

    def on_disconnected(self, event: Event) -> None:
        self.log.debug(f"disconnected: {repr(event)}")


    def on_sendable(self, event: Event):
        self.log.info("send message")
        message = Message(body=self.url, address=self.address, content_type="application/json", durable=True)
        delivery = event.sender.send(message)
        self.log.info(f"Delivery {delivery} sent")

    def on_accepted(self, event: Event) -> None:
        self.log.info(f"accepted Delivery: {event.delivery.remote_state}")
        event.connection.close()


    def on_rejected(self, event: Event) -> None:
        self.log.info(f"rejected Delivery: {event.delivery}")

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO, format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s')
    logging.info('kontor.read_list started')
    #conn = stomp.Connection([('127.0.0.1', '61616')])
    #conn.connect('artemis', 'artemis', wait=True)
    if args.links:
        logging.info("read links from file")
        links = read_links_file(args.links)
        for link in links:
            data_dict = {'url': link.strip()}
            data = json.dumps(data_dict)
            logging.info("send link message")
            handler = AddLinkMessage("amqp://127.0.0.1:5672", data, logging)
            container = Container(handler)
            container.container_id = "process_add_links"
            container.run()
            # conn.send(body=data, destination='KontorMediaFile::add_link_file', headers={'content-type': 'application/json'})
    #conn.disconnect()
    logging.info('kontor.read_list finished')

