import boto3
import os
import sys

sys.path.append(os.getcwd())
sys.path.append('/home/dtomlinson/projects/slack-bot/traffic-scraper/prd')
from pullTrafficInfo import getTrafficInfo
import base64

# import emoji
# import hashlib
from datetime import datetime

motorway = 'A3'
currentTime = datetime.now().strftime('%H:%M:%S')

session = boto3.Session(profile_name='plex-aws')
sqs = session.client('sqs')

# queue = sqs.create_queue(QueueName='test', Attributes={'DelaySeconds': '5'})

inst = (
    getTrafficInfo.getTrafficURL(motorway)
    .findIncidents()
    .getIncidentInformation()
    .generateOutput()
)

# encoded = base64.b64encode(inst.output[0].encode())
# decoded = base64.b64decode(encoded).decode()

queueURL = 'https://sqs.eu-west-1.amazonaws.com/745437999005/slack-bot.fifo'

# message = sqs.send_message(QueueUrl=queueURL,
#                            MessageBofdy='string',
#                            MessageGroupId='slack-bot-motorway')

# response = sqs.receive_message(QueueUrl=queueURL,
#                                MaxNumberOfMessages=5)
# if 'Messages' in response:
#     for message in response['Messages']:
#         print(message['Body'])
# else:
#     print('Queue is empty')

# for item in inst.output:
#     print(item)
#     encoded = (base64.b64encode(item.encode())).decode()
#     dedupId = hashlib.md5((item + currentTime).encode()).hexdigest()
#     msg = sqs.send_message(QueueUrl=queueURL,
#                            MessageBody=encoded,
#                            MessageGroupId=f'slack-bot-{motorway}',
#                            MessageDeduplicationId=dedupId)
#     if msg is not None:
#         print(msg["MessageId"])
#     # print(encoded)
#     # print(encoded.encode())
#     # print(base64.b64decode(encoded.encode()).decode())

queueAttributes = sqs.get_queue_attributes(
    QueueUrl=queueURL, AttributeNames=['ApproximateNumberOfMessages']
)


response = sqs.receive_message(QueueUrl=queueURL, MaxNumberOfMessages=10)
if 'Messages' in response:
    for message in response['Messages']:
        # print(message['Body'])
        # break
        decoded = base64.b64decode(message['Body'].encode())
        print(decoded.decode())
else:
    print('Queue is empty')
