import json


def offers(request):
    """
    Create Json response with offers menu
    :param request: POST request from "Offers" dialogflow intent
    :return: Json response that contains spoken and display prompt and also list as Dialogflow conversation item
    """
    speech_text_pl = "Która opcja Cię interesuje?"
    display_text_pl = "Która opcja Cię interesuje?"
    list_pl = {
        "intent": "actions.intent.OPTION",
        "data": {
            "@type": "type.googleapis.com/google.actions.v2.OptionValueSpec",
            "listSelect": {
                "items": [
                    {
                        "optionInfo": {
                            "key": "Przeglądaj oferty",
                            "synonyms": [
                                "Przejrzyj oferty"
                            ]
                        },
                        "title": "Przeglądaj oferty"
                    },
                    {
                        "optionInfo": {
                            "key": "Wyszukaj oferty",
                            "synonyms": [
                                "Znajdź oferty",
                                "Znajdź ofertę"
                            ]
                        },
                        "title": "Wyszukaj oferty"
                    },
                    {
                        "optionInfo": {
                            "key": "Wyszukaj ofertę po id",
                            "synonyms": [
                                "Znajdź ofertę po id"
                            ]
                        },
                        "title": "Wyszukaj ofertę po id"
                    },
                    {
                        "optionInfo": {
                            "key": "Do kiedy jest ważna oferta",
                            "synonyms": [
                                "Ważnosć oferty",
                                "Do kiedy oferta będzie aktualna",
                            ]
                        },
                        "title": "Do kiedy jest ważna oferta"
                    }
                ]
            }
        }
    }
    suggestions_pl = [{"title": "Oferty"}, {"title": "Zlecenia"}, {"title": "Zapytania"}, {"title": "Konto"},
                      {"title": "Inne"}]

    speech_text_en = "Which option are you interested in?"
    display_text_en = "Which option are you interested in?"
    list_en = {
        "intent": "actions.intent.OPTION",
        "data": {
            "@type": "type.googleapis.com/google.actions.v2.OptionValueSpec",
            "listSelect": {
                "items": [
                    {
                        "optionInfo": {
                            "key": "Browse offers",
                            "synonyms": [
                                "View offers",
                                "Display offers"
                            ]
                        },
                        "title": "Browse offers"
                    },
                    {
                        "optionInfo": {
                            "key": "Search offers",
                            "synonyms": [
                                "Search active offers"
                            ]
                        },
                        "title": "Search offers"
                    },
                    {
                        "optionInfo": {
                            "key": "Search offer after id",
                            "synonyms": [
                                "Search offer according to id"
                            ]
                        },
                        "title": "Search offer after id"
                    },
                    {
                        "optionInfo": {
                            "key": "Until when is the offer valid",
                            "synonyms": [
                                "Offer valid",
                                "Until when is the offer valid?",
                            ]
                        },
                        "title": "Until when is the offer valid"
                    }
                ]
            }
        }
    }
    suggestions_en = [{"title": "Offers"}, {"title": "Orders"}, {"title": "Inquiries"}, {"title": "Account"},
                      {"title": "Others"}]

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

    part_to_modify = offers['payload']['google']

    if request.data['queryResult']['languageCode'] == 'pl':
        part_to_modify['richResponse']['items'][0]['simpleResponse']['textToSpeech'] = speech_text_pl
        part_to_modify['richResponse']['items'][0]['simpleResponse']['displayText'] = display_text_pl
        part_to_modify['systemIntent'] = list_pl
        part_to_modify['richResponse']['suggestions'] = suggestions_pl
    elif request.data['queryResult']['languageCode'] == 'en':
        part_to_modify['richResponse']['items'][0]['simpleResponse']['textToSpeech'] = speech_text_en
        part_to_modify['richResponse']['items'][0]['simpleResponse']['displayText'] = display_text_en
        part_to_modify['systemIntent'] = list_en
        part_to_modify['richResponse']['suggestions'] = suggestions_en

    offers['payload']['google'] = part_to_modify
    return offers