import time
import linkatos.parser as parser
import linkatos.printer as printer
import linkatos.firebase as fb
import linkatos.reaction as react


def is_empty(events):
    return ((events is None) or (len(events) == 0))


def is_url(url_message):
    return url_message['type'] == 'url'


def event_consumer(expecting_url, expecting_reaction, parsed_url_message,
                   slack_client, fb_credentials, firebase):

    # Read slack events
    events = slack_client.rtm_read()
    time.sleep(1)  # 1 second delay after reading

    if is_empty(events):
        return (expecting_url, expecting_reaction, parsed_url_message)

    for event in events:
        print(event)
        print('expecting_url: ', expecting_url)
        print('expecting_reaction: ', expecting_reaction)
        if 'type' in event:
            if expecting_url and event['type'] == 'message':
                parsed_url_message = parser.parse_url_message(event)

                if is_url(parsed_url_message):
                    printer.ask_confirmation(parsed_url_message, slack_client)
                    expecting_url = False

            if not expecting_url and event['type'] == 'reaction_added':
                reaction = parser.parse_reaction_added(event)

                if react.is_confirmation(reaction['reaction'],
                                         parsed_url_message['id'],
                                         reaction['to_id']):
                    react.handle(reaction['reaction'], parsed_url_message['url'],
                                 fb_credentials, firebase)
                    expecting_url = True

    return (expecting_url, expecting_reaction, parsed_url_message)

@@ -725,98 +725,9 @@
 rl)%0A
-        print('expecting_reaction: ', expecting_reaction)%0A        if 'type' in event:%0A    
+%0A
     
@@ -779,20 +779,16 @@
 ssage':%0A
-    
         
@@ -857,20 +857,16 @@
         
-  
-  
 if is_ur
@@ -888,20 +888,16 @@
 ssage):%0A
-    
         
@@ -971,36 +971,32 @@
                 
-  
-  
 expecting_url = 
@@ -1002,20 +1002,16 @@
  False%0A%0A
-    
         
@@ -1074,36 +1074,32 @@
 d':%0A            
-   
- 
 reaction = parse
@@ -1121,36 +1121,32 @@
 n_added(event)%0A%0A
-    
             if r
@@ -1217,36 +1217,32 @@
                 
-   
- 
 parsed_url_messa
@@ -1280,36 +1280,32 @@
                 
-    
 reaction%5B'to_id'
@@ -1308,20 +1308,16 @@
 _id'%5D):%0A
-    
         
@@ -1415,20 +1415,16 @@
         
-    
 fb_crede
@@ -1441,20 +1441,16 @@
 rebase)%0A
-    
         

Delete condition of type in event as it should always be true