package com.codepath.apps.restclienttemplate.models;

import com.codepath.apps.restclienttemplate.helpers.TwitterApp;
import com.codepath.apps.restclienttemplate.helpers.TwitterClient;
import com.loopj.android.http.JsonHttpResponseHandler;

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

import cz.msebera.android.httpclient.Header;

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

public class User {

    // list the attributes
    public String name;
    public long uid;
    public String screenName;
    public String profileImageUrl;

    // deserialize JSON
    public static User fromJSON(JSONObject jsonObject) throws JSONException {
        User user = new User();

        // extract and fill the values
        user.name = jsonObject.getString("name");
        user.uid = jsonObject.getLong("id");
        user.screenName = jsonObject.getString("screen_name");
        user.profileImageUrl = jsonObject.getString("profile_image_url");

        return user;
    }

    public interface UserCallbackInterface {
        void onUserAvailable(User currentUser);
    }

    public static void getCurrentUser(final UserCallbackInterface handler) {
        TwitterClient client = TwitterApp.getRestClient();
        client.verifyCredentials(new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                try {
                    handler.onUserAvailable(User.fromJSON(response));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable, errorResponse);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
                super.onFailure(statusCode, headers, throwable, errorResponse);
            }
        });
    }

}

@@ -47,16 +47,73 @@
 odels;%0A%0A
+import android.os.Parcel;%0Aimport android.os.Parcelable;%0A%0A
 import c
@@ -168,16 +168,16 @@
 terApp;%0A
-
 import c
@@ -482,16 +482,38 @@
 ss User 
+implements Parcelable 
 %7B%0A%0A    /
@@ -645,16 +645,223 @@
 geUrl;%0A%0A
+    public User() %7B%0A%0A    %7D%0A%0A    private User(Parcel in) %7B%0A        name = in.readString();%0A        uid = in.readLong();%0A        screenName = in.readString();%0A        profileImageUrl = in.readString();%0A    %7D%0A%0A
     // d
@@ -1255,24 +1255,24 @@
 age_url%22);%0A%0A
-
         retu
@@ -1279,32 +1279,652 @@
 rn user;%0A    %7D%0A%0A
+    @Override%0A    public int describeContents() %7B%0A        return 0;%0A    %7D%0A%0A    public static final Parcelable.Creator%3CUser%3E CREATOR%0A            = new Parcelable.Creator%3CUser%3E() %7B%0A%0A        @Override%0A        public User createFromParcel(Parcel in) %7B%0A            return new User(in);%0A        %7D%0A%0A        @Override%0A        public User%5B%5D newArray(int size) %7B%0A            return new User%5Bsize%5D;%0A        %7D%0A    %7D;%0A%0A    @Override%0A    public void writeToParcel(Parcel out, int flags) %7B%0A        out.writeString(name);%0A        out.writeLong(uid);%0A        out.writeString(screenName);%0A        out.writeString(profileImageUrl);%0A    %7D%0A%0A
     public inter

make User data model Parcelable