import utils.response_builder as response_builder
import resources.bible as bible


def handle_welcome():

    session_attributes = {}
    card_title = "Welcome"
    speech_output = "Christ Church Mayfair Assistant at your service. What " \
                    "would you like? "
    should_end_session = False
    reprompt_text = None
    return response_builder.build_response(
        session_attributes, response_builder.build_speechlet_response(
            card_title, 'Hello!', speech_output, reprompt_text,
            should_end_session
        )
    )


def handle_session_end_request():
    card_title = 'Goodbye'
    speech_output = 'Thanks for using Christ Church Mayfair Assistant. '
    should_end_session = True
    return response_builder.build_response({}, response_builder.build_speechlet_response(
        card_title=card_title, card_content=speech_output,
        output=speech_output, reprompt_text=None,
        should_end_session=should_end_session))


def handle_get_sermon_passage(intent, session):
    if ('value' not in intent['slots']['Date']) \
            or ('value' not in intent['slots']['Service']):
        speechlet_response = {
            'shouldEndSession': False,
            'directives': [{'type': 'Dialog.Delegate'}]
        }
        return response_builder.build_response({}, speechlet_response)

    if ('value' not in intent['slots']['ReadPassage']) \
            or ('value' not in intent['slots']['Service']):
        session_attributes = {}
        should_end_session = False

        book = 'Mark'
        start_chapter = '9'
        start_verse = '30'
        end_chapter = '9'
        end_verse = '41'  # TODO: change all this using fetched data

        card_title = book + ' ' + start_chapter + ':' + start_verse + '-' + \
            end_chapter + ':' + end_verse
        passage_text = bible.get_bible_text(book, start_chapter, start_verse,
                                            end_chapter, end_verse)

        speech_output = book + ' chapter ' + start_chapter + ' verse ' + \
            start_verse + ' to  chapter ' + end_chapter + \
            ' verse ' + end_verse + '. '
        speech_output += 'I\'ve sent this bible passage to your Alexa app. '
        speech_output += 'Would you like me to read this out?'

        new_slots = {
            'book': {
                'name': 'book',
                'value': book,
                'confirmationStatus': 'NONE'
            },
            'start_chapter': {
                'name': 'start_chapter',
                'value': start_chapter,
                'confirmationStatus': 'NONE'},
            'start_verse': {
                'name': 'start_verse',
                'value': start_verse,
                'confirmationStatus': 'NONE'
            },
            'end_chapter': {
                'name': 'end_chapter',
                'value': end_chapter,
                'confirmationStatus': 'NONE'
            },
            'end_verse': {
                'name': 'end_verse',
                'value': end_verse,
                'confirmationStatus': 'NONE'
            }
        }

        updated_intent = {
            'name': 'GetSermonPassage',
            'confirmationStatus': 'NONE', 'slots': new_slots
        }

        get_read_passage_directives = [{'type': 'Dialog.ElicitSlot',
                                        'slotToElicit': 'ReadPassage',
                                        'updatedIntent': updated_intent}]

        speechlet_response = response_builder.build_speechlet_response(
            card_title=card_title, card_content=passage_text,
            output=speech_output, reprompt_text=None,
            should_end_session=should_end_session,
            directives=get_read_passage_directives)

        return response_builder.build_response(session_attributes,
                                               speechlet_response)
    book = intent['slots']['book']
    start_chapter = intent['slots']['start_chapter']
    start_verse = intent['slots']['start_verse']
    end_chapter = intent['slots']['end_chapter']
    end_verse = intent['slots']['end_verse']
    passage_text = bible.get_bible_text(book, start_chapter, start_verse,
                                        end_chapter, end_verse)
    speechlet_response = response_builder.build_speechlet_response_no_card(
        output=passage_text, reprompt_text=None,
        should_end_session=True)
    return response_builder.build_response({}, speechlet_response)


def handle_get_next_event(intent, session):
    # TODO: implement this method

    session_attributes = {}
    reprompt_text = None

    if session.get('attributes', {}) and "favoriteColor" in session.get('attributes', {}):
        favorite_color = session['attributes']['favoriteColor']
        speech_output = "Your favorite color is " + favorite_color + \
                        ". Goodbye."
        should_end_session = True
    else:
        speech_output = "I'm not sure what your favorite color is. " \
                        "You can say, my favorite color is red."
        should_end_session = False

    # Setting reprompt_text to None signifies that we do not want to reprompt
    # the user. If the user does not respond or says something that is not
    # understood, the session will end.
    return response_builder.build_response(session_attributes, response_builder.build_speechlet_response(
        intent['name'], None, speech_output, reprompt_text, should_end_session))

@@ -3097,32 +3097,73 @@
     %7D%0A        %7D%0A
+        new_slots.update(intent%5B'slots'%5D)
 %0A        updated
@@ -3255,16 +3255,28 @@
  'NONE',
+%0A           
  'slots'

fix updated slots returned in directive by combining with previous slots