"""
Glossolalia Integration for OPAL
"""
from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder
import json
import requests

INTEGRATING  = settings.INTEGRATING
NAME         = settings.GLOSSOLALIA_NAME
ENDPOINT     = settings.GLOSSOLALIA_URL + 'api/v0.1/accept/'
OUR_ENDPOINT = settings.DEFAULT_DOMAIN + 'ddd/'

def _send_upstream_message(event, payload):
    """
    Send a message upstream
    """
    try:
        payload['endpoint'] = OUR_ENDPOINT
        r = requests.post(
            ENDPOINT,
            data={
                'servicetype': 'OPAL',
                'event'      : event,
                'name':      NAME,
                'data'       : payload
            }
        )
        print 'status', r.status_code
        print 'text', r.text
    except requests.ConnectionError:
        print 'Glossolalia Connection Error :('
        return
    return

def admit(episode):
    """
    We have admitted a patient - pass on the message to whatever
    upstream services are listening
    """
    if not INTEGRATING:
        return
    print 'Sending upstream Admission'
    payload = {
        'episode': json.dumps(episode, cls=DjangoJSONEncoder),
    }
    _send_upstream_message('admit', payload)
    return

def discharge(episode):
    """
    We have discharged a patient - pass on the message to whatever
    upstream services are listening
    """
    if not INTEGRATING:
        return
    print 'Sending upstream discharge'
    payload = {
        'episode': json.dumps(episode, cls=DjangoJSONEncoder),
    }
    _send_upstream_message('discharge', payload)
    return

def transfer(episode):
    """
    We have transferred a patient - pass on the message to whatever
    upstream services are listening
    """
    if not INTEGRATING:
        return
    raise NotImplementedError('Need to sort out transfers Larry :(')
    return

def change(pre, post):
    """
    We have made a change to an episode - pass on the message
    to whatever upstream services are listening.
    """
    if not INTEGRATING:
        return
    print 'Sending upstream change'
    payload = {
        'pre': json.dumps(pre, cls=DjangoJSONEncoder), 
        'post': json.dumps(post, cls=DjangoJSONEncoder),
        }
    _send_upstream_message('change', payload)
    return

@@ -460,266 +460,229 @@
 ad%5B'
-endpoint'%5D = OUR_ENDPOINT%0A        r = requests.post(%0A            ENDPOINT,%0A            data=%7B%0A                'servicetype': 'OPAL',%0A                'event'      : event,%0A                'name':      NAME,%0A                'data'       : payload%0A            %7D
+servicetype'%5D = 'OPAL'%0A        payload%5B'event'%5D = event,%0A        payload%5B'name'%5D = NAME,%0A        print %22Payload is:%22, json.dumps(payload, indent=2)%0A        r = requests.post(%0A            ENDPOINT,%0A            data=payload
 %0A   
@@ -1099,39 +1099,36 @@
 ad = %7B%0A        '
-episode
+data
 ': json.dumps(ep
@@ -1117,39 +1117,133 @@
 ta': json.dumps(
-episode
+%0A            %7B%0A                'episode': episode,%0A                'endpoint': OUR_ENDPOINT%0A        %7D
 , cls=DjangoJSON
@@ -1242,33 +1242,32 @@
 angoJSONEncoder)
-,
 %0A    %7D%0A    _send
@@ -1560,23 +1560,20 @@
        '
-episode
+data
 ': json.
@@ -1578,23 +1578,96 @@
 n.dumps(
-episode
+%7B%0A            'episode': episode,%0A            'endpoint': OUR_ENDPOINT%0A        %7D
 , cls=Dj
@@ -1678,25 +1678,24 @@
 JSONEncoder)
-,
 %0A    %7D%0A    _
@@ -2245,32 +2245,39 @@
 %0A    payload = %7B
+'data':
 %0A        'pre': 
@@ -2273,14 +2273,14 @@
     
-'pre':
+      
  jso
@@ -2291,46 +2291,102 @@
 mps(
-pre, cls=DjangoJSONEncoder), %0A
+%7B%0A                   'endpoint': OUR_ENDPOINT,%0A                   'pre': pre, %0A       
         
 'pos
@@ -2385,16 +2385,20 @@
     
+    
 'post': 
 json
@@ -2397,23 +2397,30 @@
 t': 
-json.dumps(post
+post,%0A               %7D
 , cl
@@ -2439,22 +2439,17 @@
 Encoder)
-,%0A    
+%0A
     %7D%0A  

Update serialisation of data to upstream so that Glossolalia can get at it easily.