package autoChirp.tweeting;

import java.net.MalformedURLException;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.social.twitter.api.TweetData;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.social.twitter.api.impl.TwitterTemplate;
import org.springframework.stereotype.Component;

import autoChirp.DBConnector;
import autoChirp.tweetCreation.Tweet;
import autoChirp.tweetCreation.TweetFactory;

/**
 * This class executes the actual twitter status-update using Spring Social
 * Twitter API
 *
 * @author Alena Geduldig
 *
 */
@Component
public class TwitterConnection {

	@Value("${spring.social.twitter.appId}")
	private String appIDProp;

	@Value("${spring.social.twitter.appSecret}")
	private String appSecretProp;

	@Value("${autochirp.domain}")
	private String appDomainProp;

	@Value("${autochirp.parser.dateformats}")
	private String dateformatsProp;

	private static String appID;
	private static String appSecret;
	private static String appDomain;
	private static String dateformats;

	/**
	 * get appToken and appSecret
	 */
	@PostConstruct
	public void initializeConnection() {
		this.appID = appIDProp;
		this.appSecret = appSecretProp;
		this.appDomain = appDomainProp;
		this.dateformats = dateformatsProp;
	}

	/**
	 * updates the users twitter-status to the tweets content. 1. reads the
	 * tweet with the given tweetID from the database 2. checks if the related
	 * tweetGroup is still enabled and tweet wasn't tweeted already 3. reads the
	 * users oAuthToken and oAuthTokenSecret from the database 4. updates the
	 * users twitter status to the tweets tweetContent
	 *
	 * @param userID
	 *            userID
	 * @param tweetID
	 *            tweetID
	 */
	public void run(int userID, int tweetID) {
		// read tweet from DB
		Tweet toTweet = DBConnector.getTweetByID(tweetID, userID);

		// check if tweet exists
		if (toTweet == null) {
			return;
		}
		// check if tweetGroup is still enabled
		if (!DBConnector.isEnabledGroup(toTweet.groupID, userID)) {
			return;
		}
		// check if tweet was not tweeted already
		if (toTweet.tweeted) {
			return;
		}

		// read userConfig (oAuthToken, tokenSecret) from DB
		String[] userConfig = DBConnector.getUserConfig(userID);
		String token = userConfig[1];
		String tokenSecret = userConfig[2];

		// tweeting
		Twitter twitter = new TwitterTemplate(appID, appSecret, token, tokenSecret);

		// TweetData tweetData = new TweetData(toTweet.content);
		String tweet = toTweet.content;
		TweetData tweetData = new TweetData(tweet);

		// add image
		if (toTweet.imageUrl != null) {
			try {
				Resource img = new UrlResource(toTweet.imageUrl);
				tweetData = tweetData.withMedia(img);
			} catch (MalformedURLException e) {
				tweetData = new TweetData(tweet + " " + toTweet.imageUrl);
			}
		}

		// add flashcard
		if (toTweet.adjustedLength() > 140) {
			TweetFactory tf = new TweetFactory(dateformats);
			String flashcard = appDomain + "/flashcard/" + toTweet.tweetID;

			try {
				if (tweetData.hasMedia()) {
					tweetData = new TweetData(tf.trimToTweet(tweet, toTweet.imageUrl))
							.withMedia(new UrlResource(flashcard));
				} else {
					tweetData = tweetData.withMedia(new UrlResource(flashcard));
				}
			} catch (MalformedURLException e) {
				tweetData = new TweetData(tf.trimToTweet(tweet, flashcard));
			}
		}

		// add Geo-Locations
		if (toTweet.longitude != 0 || toTweet.latitude != 0) {
			System.out.println("long: " + toTweet.longitude);
			System.out.println("lat: " + toTweet.latitude);
			tweetData = tweetData.atLocation(toTweet.longitude, toTweet.latitude).displayCoordinates(true);
		}

		// update Tweet-Status in DB
		DBConnector.flagAsTweeted(tweetID, userID);

		// update Status
		twitter.timelineOperations().updateStatus(tweetData);
	}

}

@@ -3269,177 +3269,8 @@
  %7B%0D%0A
-%09%09%09%09if (tweetData.hasMedia()) %7B%0D%0A%09%09%09%09%09tweetData = new TweetData(tf.trimToTweet(tweet, toTweet.imageUrl))%0D%0A%09%09%09%09%09%09%09.withMedia(new UrlResource(flashcard));%0D%0A%09%09%09%09%7D else %7B%0D%0A%09
 %09%09%09%09
@@ -3335,15 +3335,8 @@
 );%0D%0A
-%09%09%09%09%7D%0D%0A
 %09%09%09%7D
@@ -3421,17 +3421,24 @@
 et(tweet
-,
+ + %22 %22 +
  flashca
@@ -3431,32 +3431,38 @@
  %22 %22 + flashcard
+, null
 ));%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A

Correct error handling