import boto3
import base64
import hashlib
from datetime import datetime
import os
import sys

sys.path.append(os.getcwd())
from pullTrafficInfo import getTrafficInfo


class sendToSQS(object):
    """docstring for sendToSQS"""

    def __init__(self, session, queueURL):
        super(sendToSQS, self).__init__()
        self.session = session
        self.sqs = session.client('sqs')
        self.queueURL = queueURL

    @classmethod
    def createSession(cls, profileName, queueURL):
        session = boto3.Session(profile_name=profileName)
        return cls(session, queueURL)

    def sendMessage(self, message, messageGroupId, b64=True, dedup=False):
        currentTime = datetime.now().strftime('%H:%M:%S.%f')
        if b64:
            message = (base64.b64encode(message.encode())).decode()
        if not dedup:
            dedupId = hashlib.md5((message + currentTime).encode()).hexdigest()
            msg = self.sqs.send_message(
                QueueUrl=self.queueURL,
                MessageBody=message,
                MessageGroupId=messageGroupId,
                MessageDeduplicationId=dedupId,
            )
        else:
            msg = self.sqs.send_message(
                QueueUrl=self.queueURL,
                MessageBody=message,
                MessageGroupId=messageGroupId,
            )
        if msg is not None:
            print(msg['MessageId'])


# inst = sendToSQS.createSession(
#     profileName='plex-aws',
#     queueURL='https://sqs.eu-west-1.amazonaws.com'
#     '/745437999005/slack-bot.fifo',
# )

# instM = (
#     getTrafficInfo.getTrafficURL('M62')
#     .findIncidents()
#     .getIncidentInformation()
#     .generateOutput()
# )

# for _ in range(0, 5):
#     for item in instM.output:
#         inst.sendMessage(message=item, messageGroupId='slack-bot-M62')
