package cgeo.geocaching.twitter;

import cgeo.geocaching.Geocache;
import cgeo.geocaching.Trackable;
import cgeo.geocaching.cgData;
import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.GeopointFormatter.Format;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.OAuth;
import cgeo.geocaching.network.Parameters;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.utils.Log;

import ch.boye.httpclientandroidlib.HttpResponse;

import org.apache.commons.lang3.StringUtils;

public final class Twitter {
    private static final String HASH_PREFIX_WITH_BLANK = " #";
    private static final int MAX_TWEET_SIZE = 140;

    public static void postTweetCache(String geocode) {
        final Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB);
        postTweet(cgeoapplication.getInstance(), getStatusMessage(cache), null);
    }

    public static void postTweetTrackable(String geocode) {
        final Trackable trackable = cgData.loadTrackable(geocode);
        postTweet(cgeoapplication.getInstance(), getStatusMessage(trackable), null);
    }

    private static void postTweet(final cgeoapplication app, final String status, final Geopoint coords) {
        if (app == null || !Settings.isUseTwitter() || !Settings.isTwitterLoginValid()) {
            return;
        }

        try {
            Parameters parameters = new Parameters("status", status);
            if (coords != null) {
                parameters.put(
                        "lat", coords.format(Format.LAT_DECDEGREE_RAW),
                        "long", coords.format(Format.LON_DECDEGREE_RAW),
                        "display_coordinates", "true");
            }

            OAuth.signOAuth("api.twitter.com", "/1.1/statuses/update.json", "POST", false, parameters, Settings.getTokenPublic(), Settings.getTokenSecret(), Settings.getKeyConsumerPublic(), Settings.getKeyConsumerSecret());
            final HttpResponse httpResponse = Network.postRequest("http://api.twitter.com/1.1/statuses/update.json", parameters);
            if (httpResponse != null) {
                if (httpResponse.getStatusLine().getStatusCode() == 200) {
                    Log.i("Tweet posted");
                } else {
                    Log.e("Tweet could not be posted. Reason: " + httpResponse.toString());
                }
            } else {
                Log.e("Tweet could not be posted. Reason: httpResponse Object is null");
            }
        } catch (Exception e) {
            Log.e("Twitter.postTweet", e);
        }
    }

    private static void appendHashTag(final StringBuilder status, final String tag) {
        if (status.length() + HASH_PREFIX_WITH_BLANK.length() + tag.length() <= MAX_TWEET_SIZE) {
            final String tagWithPrefix = HASH_PREFIX_WITH_BLANK + tag;
            if (status.indexOf(tagWithPrefix, 0) == -1) {
                status.append(tagWithPrefix);
            }
        }
    }

    private static String getStatusMessage(Geocache cache) {
        String name = cache.getName();
        if (name.length() > 100) {
            name = name.substring(0, 100) + '…';
            }
        final String url = StringUtils.defaultString(cache.getUrl());
        return fillTemplate(Settings.getCacheTwitterMessage(), name, url);
    }

    private static String getStatusMessage(Trackable trackable) {
        String name = trackable.getName();
        if (name.length() > 82) {
            name = name.substring(0, 81) + '…';
        }
        String url = StringUtils.defaultString(trackable.getUrl());
        String status = Settings.getTrackableTwitterMessage();
        return fillTemplate(status, name, url);
    }

    private static String fillTemplate(String template, String name, final String url) {
        String result = StringUtils.replace(template, "[NAME]", name);
        result = StringUtils.replace(result, "[URL]", url);
        StringBuilder builder = new StringBuilder(result);
        appendHashTag(builder, "cgeo");
        appendHashTag(builder, "geocaching");
        return builder.toString();
    }
}

@@ -3047,32 +3047,24 @@
 %0A    %7D%0A%0A    
-private 
 static Strin
@@ -3389,32 +3389,24 @@
 %0A    %7D%0A%0A    
-private 
 static Strin

Fix test compile issue