package com.creationgroundmedia.twitternator.models;

import android.support.annotation.NonNull;
import android.text.format.DateUtils;

import com.raizlabs.android.dbflow.annotation.Column;
import com.raizlabs.android.dbflow.annotation.PrimaryKey;
import com.raizlabs.android.dbflow.annotation.Table;
import com.raizlabs.android.dbflow.structure.BaseModel;

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

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

/**
 * Created by geo on 10/28/16.
 */

@Table(database = TwitterDb.class)
public class Tweet extends BaseModel {
    @Column
    String createdAt;

    @Column
    @PrimaryKey
    long id;

    @Column
    String body;

    @Column
    String userName;

    @Column
    long userId;

    @Column
    String userScreenName;

    @Column
    String userProfileImageUrl;

    @Column
    String collection;

    public Tweet() {
    }

    public static Tweet fromJson(@NonNull JSONObject jsonTweet) {
        Tweet tweet = new Tweet();
        try {
            tweet.setBody(jsonTweet.getString("text"));
            tweet.setId(jsonTweet.getLong("id"));
            tweet.setCreatedAt(jsonTweet.getString("created_at"));
            JSONObject jsonUser = jsonTweet.getJSONObject("user");
            if (jsonUser != null) {
                tweet.setUserName(jsonUser.getString("name"));
                tweet.setUserId(jsonUser.getLong("id"));
                tweet.setUserScreenName("@" + jsonUser.getString("screen_name"));
                tweet.setUserProfileImageUrl(
                        jsonUser.getString("profile_image_url").replace("normal", "bigger"));
            }
            tweet.save();
        } catch (JSONException e) {
            e.printStackTrace();
        };

        return tweet;
    }

    public static Tweet fromJson(@NonNull JSONObject jsonTweet, String collection) {
        Tweet tweet = fromJson(jsonTweet);
        if (tweet != null) {
            tweet.collection = collection;
        }
        return tweet;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public String getUserScreenName() {
        return userScreenName;
    }

    public void setUserScreenName(String userScreenName) {
        this.userScreenName = userScreenName;
    }

    public String getUserProfileImageUrl() {
        return userProfileImageUrl;
    }

    public void setUserProfileImageUrl(String userProfileImageUrl) {
        this.userProfileImageUrl = userProfileImageUrl;
    }

    public String getCollection() {
        return collection;
    }

    public void setCollection(String collection) {
        this.collection = collection;
    }

    // getRelativeTimeAgo("Mon Apr 01 21:16:23 +0000 2014");
    // from https://gist.github.com/nesquena/f786232f5ef72f6e10a7
    public String getCreatedAtRelativeTimeAgo() {
        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(getCreatedAt()).getTime();
            relativeDate = DateUtils.getRelativeTimeSpanString(dateMillis,
                    System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS).toString();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return relativeDate;
    }

    public static ArrayList<Tweet> fromJsonArray(JSONArray jsonTweets, String collection) {
        ArrayList<Tweet> tweets = new ArrayList<>();

        for (int i = 0; i < jsonTweets.length(); i++) {
            Tweet tweet = null;
            try {
                tweet = Tweet.fromJson(jsonTweets.getJSONObject(i), collection);
            } catch (JSONException e) {
                e.printStackTrace();
                continue;
            }
            if (tweet != null) {
                tweets.add(tweet);
            }
        }
        return tweets;
    }
}

@@ -43,24 +43,80 @@
 or.models;%0A%0A
+import android.os.Parcel;%0Aimport android.os.Parcelable;%0A
 import andro
@@ -620,48 +620,8 @@
 e;%0A%0A
-/**%0A * Created by geo on 10/28/16.%0A */%0A%0A
 @Tab
@@ -688,16 +688,38 @@
 seModel 
+implements Parcelable 
 %7B%0A    @C
@@ -4745,11 +4745,1199 @@
 s;%0A    %7D
+%0A%0A    @Override%0A    public int describeContents() %7B%0A        return 0;%0A    %7D%0A%0A    @Override%0A    public void writeToParcel(Parcel dest, int flags) %7B%0A        dest.writeString(this.createdAt);%0A        dest.writeLong(this.id);%0A        dest.writeString(this.body);%0A        dest.writeString(this.userName);%0A        dest.writeLong(this.userId);%0A        dest.writeString(this.userScreenName);%0A        dest.writeString(this.userProfileImageUrl);%0A        dest.writeString(this.collection);%0A    %7D%0A%0A    protected Tweet(Parcel in) %7B%0A        this.createdAt = in.readString();%0A        this.id = in.readLong();%0A        this.body = in.readString();%0A        this.userName = in.readString();%0A        this.userId = in.readLong();%0A        this.userScreenName = in.readString();%0A        this.userProfileImageUrl = in.readString();%0A        this.collection = in.readString();%0A    %7D%0A%0A    public static final Parcelable.Creator%3CTweet%3E CREATOR = new Parcelable.Creator%3CTweet%3E() %7B%0A        @Override%0A        public Tweet createFromParcel(Parcel source) %7B%0A            return new Tweet(source);%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

Implement Parcelable