"""
Requests/functions used to interact with slack
"""
import os

import flask

import main


blueprint = flask.Blueprint('slack', __name__)  # pylint: disable=invalid-name


@blueprint.route('/', methods=['POST'])
def handle_slack_request():
    import logging
    logging.error(flask.request.form['text'])
    command_text = flask.request.form['text']
    identifiers = command_text.split()
    available = [entry['identifier'] for entry in main.list_restaurants()]
    regions = ('ki', 'bmc', 'uu', 'uppsala', 'solna')

    text = ''
    for identifier in identifiers:
        if identifier not in available and identifier.lower() not in regions:
            text = f'*Available restaurants:*\n'
            for entry in main.list_restaurants():
                text += f'- {entry["name"]}: `{entry["identifier"]}`\n'
            break
        elif identifier.lower() in regions:
            if identifier.lower() in ('solna', 'ki'):
                new_ids = [entry['identifier'] for entry in main.list_restaurants() if entry['campus'] == 'Solna']
            else:
                new_ids = [entry['identifier'] for entry in main.list_restaurants() if entry['campus'] == 'Uppsala']
            for ident in new_ids:
                restaurant_data = dict(main.get_restaurant(ident))
                text += f'*{restaurant_data["title"]}*\n'
                for dish in restaurant_data['menu']:
                    text += f'- {dish}\n'
        else:
            restaurant_data = dict(main.get_restaurant(identifier))
            text += f'*{restaurant_data["title"]}*\n'
            for dish in restaurant_data['menu']:
                text += f'- {dish}\n'
    
    response = {"blocks": [{"type": "section",
			    "text": {
				"type": "mrkdwn",
				"text": text}}]}
    return flask.jsonify(response)


def get_token(token_name: str) -> str:
    try:
        return os.environ[token]
    except KeyError:
        return None

@@ -240,73 +240,8 @@
 ():%0A
-    import logging%0A    logging.error(flask.request.form%5B'text'%5D)%0A
     
@@ -282,16 +282,16 @@
 'text'%5D%0A
+
     iden
@@ -522,20 +522,16 @@
 ntifier 
-not 
 in avail
@@ -538,47 +538,77 @@
 able
- and identifier.lower() not in regions:
+:%0A            restaurant_data = dict(main.get_restaurant(identifier))
 %0A   
@@ -625,179 +625,131 @@
 ext 
++
 = f'*
-Available restaurants:*%5Cn'%0A            for entry in main.list_restaurants():%0A                text += f'- %7Bentry%5B%22name%22%5D%7D: %60%7Bentry%5B%22identifier%22%5D%7D%60%5Cn'%0A            break
+%7Brestaurant_data%5B%22title%22%5D%7D*%5Cn'%0A            for dish in restaurant_data%5B'menu'%5D:%0A                text += f'- %7Bdish%7D%5Cn'
 %0A   
@@ -1377,51 +1377,20 @@
     
-restaurant_data = dict(main.get_restaurant(
+text = list_
 iden
@@ -1387,33 +1387,34 @@
  list_identifier
-)
+s(
 )%0A            te
@@ -1415,107 +1415,38 @@
     
-text += f'*%7Brestaurant_data%5B%22title%22%5D%7D*%5Cn'%0A            for dish in restaurant_data%5B'menu'%5D:%0A        
+break%0A    if not identifiers:%0A
     
@@ -1454,37 +1454,37 @@
    text 
-+
 = 
-f'- %7Bdish%7D%5Cn'%0A    
+list_identifiers()%0A
 %0A    res
@@ -1722,16 +1722,16 @@
 yError:%0A
-
         
@@ -1742,8 +1742,274 @@
 rn None%0A
+%0A%0Adef list_identifiers() -%3E str:%0A    text = f'*Available restaurants:*%5Cn'%0A    for entry in main.list_restaurants():%0A        text += f'- %7Bentry%5B%22name%22%5D%7D: %60%7Bentry%5B%22identifier%22%5D%7D%60%5Cn'%0A    text += '- Solna: %60solna%60%5Cn'%0A    text += '- Uppsala: %60uppsala%60%5Cn'%0A    return text%0A

Fix listing of identifiers