import json


def address(request):
    """
    Create Json response with address data
    :param request: POST request from "Address" dialogflow intent
    :return: Json response that contains spoken and display prompt and also basic card about company address
    """
    speech_text_pl = "Oto adres Best Transport Polska S.A. W Czym mogę Ci jeszcze pomóc"
    display_text_pl = "Oto adres Best Transport Polska S.A. W Czym mogę Ci jeszcze pomóc"
    basic_card_pl = {
        "basicCard": {
            "title": "Adres",
            "formattedText": "___ul.___ Piotrkowska 1  \n90-000 Łódź  \n"
                             "___Otwarte:___  \npon.- pt. 7.00 - 19.00  \nsob. 7.00 - 16.00",
            "image": {
                "url": "https://s23.flog.pl/media/foto/11989150_magiczna-ul-piotrkowska-w-lodzi-.jpg",
                "accessibilityText": "Adres"
            },
            "imageDisplayOptions": "CROPPED"
        }
    }
    suggestions_pl = [{"title": "Oferty"}, {"title": "Zlecenia"}, {"title": "Zapytania"}, {"title": "Konto"},
                      {"title": "Inne"}]

    speech_text_en = "Here is the address of Best Transport Poland S.A. What can I help you with?"
    display_text_en = "Here is the address of Best Transport Poland S.A. What can I help you with?"
    basic_card_en = {
        "basicCard": {
            "title": "Address",
            "formattedText": "__st.__ Piotrkowska 1  \n90-000 Lodz  \n"
                             "__Open:__  \nmon.- fri. 7.00 - 19.00  \nsat. 7.00 - 16.00",
            "image": {
                "url": "https://s23.flog.pl/media/foto/11989150_magiczna-ul-piotrkowska-w-lodzi-.jpg",
                "accessibilityText": "Address"
            },
            "imageDisplayOptions": "CROPPED"
        }
    }
    suggestions_en = [{"title": "Offers"}, {"title": "Orders"}, {"title": "Inquiries"}, {"title": "Account"},
                      {"title": "Others"}]

    with open('api/response.json') as json_file:
        address = json.load(json_file)

    part_to_modify = address['payload']['google']['richResponse']

    if request.data['queryResult']['languageCode'] == 'pl':
        part_to_modify['items'][0]['simpleResponse']['textToSpeech'] = speech_text_pl
        part_to_modify['items'][0]['simpleResponse']['displayText'] = display_text_pl
        part_to_modify['items'].append(basic_card_pl)
        part_to_modify['suggestions'] = suggestions_pl
    else:
        part_to_modify['items'][0]['simpleResponse']['textToSpeech'] = speech_text_en
        part_to_modify['items'][0]['simpleResponse']['displayText'] = display_text_en
        part_to_modify['items'].append(basic_card_en)
        part_to_modify['suggestions'] = suggestions_en

    address['payload']['google']['richResponse'] = part_to_modify
    return address