package com.codepath.apps.restclienttemplate.models;

import android.text.format.DateUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.parceler.Parcel;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * Created by bbaraban on 6/26/17.
 */

@Parcel
public class Tweet {

    // list out the attributes
    public String body;
    public long uid; // database ID for the tweet
    public User user;
    public String createdAt;
    public String relativeTimestamp;

    public Tweet() {};

    // deserialize the JSON
    public static Tweet fromJSON(JSONObject jsonObject) throws JSONException {
        Tweet tweet = new Tweet();

        // extract the values from JSON
        tweet.body = jsonObject.getString("text");
        tweet.uid = jsonObject.getLong("id");
        tweet.createdAt = jsonObject.getString("created_at");
        tweet.getRelativeTimeAgo(tweet.createdAt);
        tweet.user = User.fromJSON(jsonObject.getJSONObject("user"));

        return tweet;
    }

    public void getRelativeTimeAgo(String rawJsonDate) {
        String twitterFormat = "EEE MMM dd HH:mm:ss ZZZZZ yyyy";
        SimpleDateFormat sf = new SimpleDateFormat(twitterFormat, Locale.ENGLISH);
        sf.setLenient(true);

        String relativeDate = "";
        try {
            long dateMillis = sf.parse(rawJsonDate).getTime();
            relativeDate = DateUtils.getRelativeTimeSpanString(dateMillis,
                    System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS).toString();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        relativeTimestamp = relativeDate;
    }
}

@@ -1639,24 +1639,379 @@
 %0A        %7D%0A%0A
+        if (relativeDate.contains(%22seconds%22)) %7B%0A            relativeDate = relativeDate.substring(0, 1) + %22s%22;%0A        %7D else if (relativeDate.contains(%22minutes%22)) %7B%0A            relativeDate = relativeDate.substring(0, 1) + %22m%22;%0A        %7D else if (relativeDate.contains(%22hour%22)) %7B%0A            relativeDate = relativeDate.substring(0, 1) + %22h%22;%0A        %7D%0A%0A
         rela

Change verbose timestamps to abbreviated timestamps on tweets