package src;

import twitter4j.*;
import twitter4j.auth.AccessToken;

import java.io.IOException;
import java.net.URL;
import java.util.Arrays;

import com.amazon.speech.slu.Intent;
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.SimpleCard;

public class EchoTwitterHandler {
        APIKeys Keys;
        String consumerKey;
        String consumerSecret;
        String accessToken;
        String accessTokenSecret;
        TwitterFactory twitterFactory;
        Twitter twitter;

        public EchoTwitterHandler() {
                Keys = new APIKeys();
                //Your Twitter App's Consumer Key
                consumerKey = Keys.getconsumerKey();

                //Your Twitter App's Consumer Secret
                consumerSecret = Keys.getconsumerSecret();

                //Your Twitter Access Token
                accessToken = Keys.getaccessToken();

                //Your Twitter Access Token Secret
                accessTokenSecret = Keys.getaccessTokenSecret();

                //Instantiate a re-usable and thread-safe factory
                twitterFactory = new TwitterFactory();

                //Instantiate a new Twitter instance
                twitter = twitterFactory.getInstance();

                //setup OAuth Consumer Credentials
                twitter.setOAuthConsumer(consumerKey, consumerSecret);

                //setup OAuth Access Token
                twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret));
        }

        public SpeechletResponse getTweetResponse(Intent intent, Session session)
                throws SpeechletException, TwitterException {
                //Instantiate and initialize a new twitter status update

                String tweet = intent.getSlot("Tweet").getValue();
                tweet = tweetParse(tweet);
                if (tweet.length() > 140) {
                        PlainTextOutputSpeech speech = new PlainTextOutputSpeech();
                        speech.setText("I'm Sorry, your tweet is over one forty characters. Please tweet a shorter message.");
                        return SpeechletResponse.newTellResponse(speech);
                }
                StatusUpdate statusUpdate = new StatusUpdate(
                        tweet);

                //tweet or update status
                Status status = twitter.updateStatus(statusUpdate);

                PlainTextOutputSpeech speech = new PlainTextOutputSpeech();
                speech.setText("I have tweeted " + tweet);
                return SpeechletResponse.newTellResponse(speech);
        }

        /**
         * Creates a {@code SpeechletResponse} for the help intent.
         *
         * @return SpeechletResponse spoken and visual response for the given intent
         */
        public SpeechletResponse getHelpResponse() {
                String speechText = "You can ask me to tweet somthing for you";

                // Create the plain text output.
                PlainTextOutputSpeech speech = new PlainTextOutputSpeech();
                speech.setText(speechText);
        
                return SpeechletResponse.newTellResponse(speech);

        }

        public String tweetParse(String tweet) {
            String[] arr = tweet.split(" ");
            for (int i = 0; i < arr.length - 1; i++) {
                if (arr[i].equals("hashtag")) {
                    arr[i] = "";
                    arr[i+1] = "#" + arr[i+1];
                }
            }
            for (int i = 0; i < arr.size() - 2; i++) {
                if (arr[i].equals("hash") & arr[i+1].equals("tag")) {
                    arr[i] = "";
                    arr[i+1] = "";
                    arr[i+2] = "#" + arr[i+1];
                }
            }
            tweet = String.join(" ", arr);
            return tweet;
        }

}

@@ -3990,14 +3990,14 @@
 arr.
-size()
+length
  - 2
@@ -4178,33 +4178,33 @@
 %5D = %22#%22 + arr%5Bi+
-1
+2
 %5D;%0A             
@@ -4238,63 +4238,219 @@
     
-tweet = String.join(%22 %22, arr);%0A            return tweet
+for (int i = 0; i %3C arr.length; i++) %7B%0A                    if (!arr%5Bi%5D.equals(%22%22)) %7B%0A                            arr%5Bi%5D = arr%5Bi%5D + %22 %22;%0A                    %7D%0A            %7D%0A            return String.join(%22%22, arr)
 ;%0A  

fix stuff