from flask import request, Response
from messaging.ReconnectingMessageManager import ReconnectingMessageManager
import json

message_sender = ReconnectingMessageManager.getInstance()
message_sender.create_message_destination('inhub', 'direct')

def receive():
    body = request.json
    
    if isBlockchainTraceValid(body):
        message = {'type': 'blockchain-transaction', 'content': body}
        message_sender.send_message('inhub', 'trace-retrieval', json.dumps(message))
        return Response(status=201)
        
    return Response(status=400)

def isBlockchainTraceValid(trace) -> bool:
    return 'TransactionId' in trace \
        and 'Timestamp' in trace \
        and 'ApplicationType' in trace \
        and 'TransactionFrom' in trace \
        and 'TransactionFromLatLng' in trace \
        and 'TransactionTo' in trace \
        and 'TransactionToLatLng' in trace \
        and 'TransferredAsset' in trace \
        and 'ResourceIds' in trace \
        and 'ResourceMd5' in trace \
        and 'ResourceState' in trace \
        and 'Metadata' in trace
