package tbone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amazon.speech.speechlet.IntentRequest;
import com.amazon.speech.speechlet.LaunchRequest;
import com.amazon.speech.speechlet.Session;
import com.amazon.speech.speechlet.SessionEndedRequest;
import com.amazon.speech.speechlet.SessionStartedRequest;
import com.amazon.speech.speechlet.Speechlet;
import com.amazon.speech.speechlet.SpeechletException;
import com.amazon.speech.speechlet.SpeechletResponse;
import com.amazon.speech.ui.PlainTextOutputSpeech;
import com.amazon.speech.ui.Reprompt;
import com.amazon.speech.ui.SsmlOutputSpeech;

import static com.amazon.speech.speechlet.SpeechletResponse.newAskResponse;
import static com.amazon.speech.speechlet.SpeechletResponse.newTellResponse;

public class TboneSpeechlet implements Speechlet {
    @Override
    public void onSessionStarted(SessionStartedRequest request, Session session) throws SpeechletException {
        log.info(
            "onSessionStarted requestId={}, userId={}, sessionId={}",
            request.getRequestId(),
            session.getUser().getUserId(),
            session.getSessionId());
    }

    @Override
    public SpeechletResponse onLaunch(LaunchRequest request, Session session) throws SpeechletException {
        log.info(
            "onLaunch requestId={}, userId={}, sessionId={}",
            request.getRequestId(),
            session.getUser().getUserId(),
            session.getSessionId());

        String hint = "Try asking me to play a Wah Wah";


        PlainTextOutputSpeech promptOutputSpeech = new PlainTextOutputSpeech();
        promptOutputSpeech.setText("Welcome to the Sad T-Bone. " + hint);

        Reprompt reprompt = new Reprompt();
        PlainTextOutputSpeech hintOutputSpeech = new PlainTextOutputSpeech();
        hintOutputSpeech.setText(hint);

        return newAskResponse(promptOutputSpeech, reprompt);
    }

    @Override
    public SpeechletResponse onIntent(IntentRequest request, Session session) throws SpeechletException {

        log.info(
            "onIntent intent={}, requestId={}, userId={}, sessionId={}",
            request.getIntent().getName(),
            request.getRequestId(),
            session.getUser().getUserId(),
            session.getSessionId());

        switch (request.getIntent().getName()) {
            case "PlayWaWaIntent":
                return handlePlayWaWaIntent();
            case "AMAZON.HelpIntent":
                return handleHelpIntent();
            case "AMAZON.StopIntent":
                return handleStopIntent();
            default:
                throw new SpeechletException("Invalid Intent");
        }
    }

    private SpeechletResponse handleStopIntent() {
        PlainTextOutputSpeech outputSpeech = new PlainTextOutputSpeech();
        outputSpeech.setText("OK");
        return newTellResponse(outputSpeech);
    }

    private SpeechletResponse handleHelpIntent() {
        PlainTextOutputSpeech outputSpeech = new PlainTextOutputSpeech();
        outputSpeech.setText("Try asking me to play a fail sound");
        return newTellResponse(outputSpeech);
    }

    private SpeechletResponse handlePlayWaWaIntent() {
        SsmlOutputSpeech outputSpeech = new SsmlOutputSpeech();
        outputSpeech.setSsml(
            "<speak>" +
            "  <audio src=\"https://s3.amazonaws.com/tbone-skill/audio/trombone.mp3\" />" +
            "</speak>");

        return newTellResponse(outputSpeech);
    }

    @Override
    public void onSessionEnded(SessionEndedRequest request, Session session) throws SpeechletException {

    }

    private static final Logger log = LoggerFactory.getLogger(TboneSpeechlet.class);
}

@@ -1530,17 +1530,16 @@
  Wah%22;%0A%0A
-%0A
         
@@ -3052,17 +3052,25 @@
 etText(%22
-T
+You can t
 ry askin
@@ -3094,32 +3094,33 @@
 il sound%22);%0A
+%0A
         
 return newTe
@@ -3103,38 +3103,304 @@
 );%0A%0A        
-return newTell
+PlainTextOutputSpeech repromptOutputSpeech = new PlainTextOutputSpeech();%0A        repromptOutputSpeech.setText(%22Try asking me to play a fail sound%22);%0A        Reprompt reprompt = new Reprompt();%0A        reprompt.setOutputSpeech(repromptOutputSpeech);%0A        %0A        return newAsk
 Response(out
@@ -3400,32 +3400,42 @@
 nse(outputSpeech
+, reprompt
 );%0A    %7D%0A%0A    pr

handle "help" better