package com.codepath.apps.restclienttemplate.models;

import org.json.JSONException;
import org.json.JSONObject;

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

/**
 * Created by tejen on 6/6/17.
 */

public class Tweet {

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

    // deserialize 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");
        try {
            tweet.createdAt = Tweet.parseTwitterDate(jsonObject.getString("created_at"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        tweet.user = User.fromJSON(jsonObject.getJSONObject("user"));

        return tweet;
    }

    public static Date parseTwitterDate(String date) throws ParseException
    {
        final String twitterFormat = "EEE MMM dd HH:mm:ss ZZZZZ yyyy";
        SimpleDateFormat sf = new SimpleDateFormat(twitterFormat, Locale.ENGLISH);
        sf.setLenient(true);
        return sf.parse(date);
    }

    public String getRelativeTimeAgo() {
        Date system_date = createdAt;
        Date user_date = new Date();
        double diff = Math.floor((user_date.getTime() - system_date.getTime()) / 1000);
        if (diff <= 1) {return "just now";}
        if (diff < 20) {return diff + "s";}
        if (diff < 40) {return "30s";}
        if (diff < 60) {return "45s";}
        if (diff <= 90) {return "1m";}
        if (diff <= 3540) {return Math.round(diff / 60) + "m";}
        if (diff <= 5400) {return "1h";}
        if (diff <= 86400) {return Math.round(diff / 3600) + "h";}
        if (diff <= 129600) {return "1d";}
        if (diff < 604800) {return Math.round(diff / 86400) + "d";}
        if (diff <= 777600) {return "1w";}
        return Math.round(diff / 604800) + "w";
    }

}

@@ -47,16 +47,73 @@
 odels;%0A%0A
+import android.os.Parcel;%0Aimport android.os.Parcelable;%0A%0A
 import o
@@ -131,24 +131,24 @@
 NException;%0A
-
 import org.j
@@ -340,16 +340,38 @@
 s Tweet 
+implements Parcelable 
 %7B%0A%0A    /
@@ -492,16 +492,16 @@
 r user;%0A
-
     publ
@@ -520,16 +520,251 @@
 tedAt;%0A%0A
+    private Tweet(Parcel in) %7B%0A        body = in.readString();%0A        uid= in.readLong();%0A        user = in.readParcelable(getClass().getClassLoader());%0A        createdAt = new Date(in.readLong());%0A    %7D%0A%0A    public Tweet() %7B%0A%0A    %7D%0A%0A
     // d
@@ -2406,10 +2406,642 @@
 %0A    %7D%0A%0A
+    @Override%0A    public int describeContents() %7B%0A        return 0;%0A    %7D%0A%0A    @Override%0A    public void writeToParcel(Parcel out, int flags) %7B%0A        out.writeString(body);%0A        out.writeLong(uid);%0A        out.writeParcelable(user, flags);%0A        out.writeLong(createdAt.getTime());%0A    %7D%0A%0A    public static final Parcelable.Creator%3CTweet%3E CREATOR%0A            = new Parcelable.Creator%3CTweet%3E() %7B%0A%0A        @Override%0A        public Tweet createFromParcel(Parcel in) %7B%0A            return new Tweet(in);%0A        %7D%0A%0A        @Override%0A        public Tweet%5B%5D newArray(int size) %7B%0A            return new Tweet%5Bsize%5D;%0A        %7D%0A    %7D;%0A
 %7D%0A

make Tweet data model Parcelable