#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 for amimoto_alexa
"""


from helpers import *
from debugger import *


def dispatch_question(intent, session):
    """Dispatch questions and return answer.
    """
    session_attributes = build_session_attributes(session)
    if session_attributes['state'] in ['started']:
        session_attributes['state'] = 'on_question'

    if intent['name'] == 'WhatIsIntent':
        card_title = "WhatIs"
    elif intent['name'] == 'CanIUseIntent':
        card_title = "CanIUse"
    else:
        card_title = "Null"

    text_data = load_text_from_yaml(card_title)
    debug_logger(text_data)
    question = intent['slots']['AskedQuestion']['value'].lower()
    # todo: stock question to session_attributes
    if question in text_data.keys():
        session_attributes['accepted_questions'].append(question)
        speech_output = text_data[question] + '. Do you have any other questions?'
    else:
        session_attributes['rejected_questions'].append(question)
        speech_output = "Pardon?" \
            'Please ask to me by saying, What is WordPress?, or Can I use free trial?'

    should_end_session = False
    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, None, should_end_session))


def dispatch_yes_intent(intent, session):
    """Dispatch yes intent and return message
    """

    card_title = "Yes"
    session_attributes = build_session_attributes(session)
#    text_data = load_text_from_yaml(card_title)
    debug_logger(session)

    if session_attributes['state'] in ['on_question']:
        speech_output = 'OK. Please ask to me by saying, What is WordPress?, or Can I use free trial?'
    else:
        speech_output = 'Pardon?' \
            'Please ask to me by saying, What is WordPress?, or Can I use free trial?'

    should_end_session = False
    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, None, should_end_session))


def dispatch_no_intent(intent, session):
    """Dispatch no intent and tweet. Then end session.
    """

    card_title = "No"
    session_attributes = build_session_attributes(session)
#    text_data = load_text_from_yaml(card_title)
    debug_logger(session)

    if session_attributes['state'] in ['on_question']:
        session_attributes['state'] = 'finalizing'
        speech_output = 'Thank you for trying the, A MI MO TO Ninja. ' \
                        'Please tell us your thoughts by saying, my impression is, "I love WordPress!"'
        should_end_session = False
    else:
        speech_output = "Thank you for trying the, A MI MO TO Ninja. " \
                        "Have a nice day! "
        should_end_session = True

    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, None, should_end_session))

@@ -831,32 +831,57 @@
 stions'%5D.append(
+':'.join(intent%5B'name'%5D, 
 question)%0A      
@@ -865,32 +865,33 @@
 ame'%5D, question)
+)
 %0A        speech_
@@ -1024,16 +1024,41 @@
 .append(
+':'.join(intent%5B'name'%5D, 
 question
@@ -1054,24 +1054,25 @@
 %5D, question)
+)
 %0A        spe
@@ -1092,16 +1092,48 @@
 %22Pardon?
+ please check list of questions.
 %22 %5C%0A    
@@ -1133,33 +1133,37 @@
  %5C%0A            '
-P
+So, p
 lease ask to me 
@@ -1212,32 +1212,87 @@
 e free trial?'%0A%0A
+    reprompt_text = 'Do you have any other questions?'%0A
     should_end_s
@@ -1405,36 +1405,45 @@
  speech_output, 
-None
+reprompt_text
 , should_end_ses

set reprompt text to question