package com.apibootstraper.core;

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

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

import com.apibootstraper.mobile.http.HTTPClient;
import com.apibootstraper.mobile.http.HTTPResponse;
import com.apibootstraper.mobile.http.JsonHttpResponseHandler;
import com.apibootstraper.mobile.util.DateUtils;

public class User implements Serializable {

    /** serialVersionUID */
    private static final long serialVersionUID = -4670218264175191002L;
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'", Locale.ENGLISH);

    private String uuid;

    private Date createdAt;

    private Date updatedAt;

    /**
     * 
     * @param o
     * @throws JSONException 
     * @throws ParseException 
     */
    public User(JSONObject o) throws JSONException, ParseException {
        this.uuid = o.getString("uuid");

        this.createdAt = (Date) dateFormat.parse(o.getString("created_at"));
        this.updatedAt = (Date) dateFormat.parse(o.getString("updated_at"));
    }

    /**
     * @return id
     */
    public String getUUID() {
        return uuid;
    }

    /**
     * @param id
     * @return this user
     */
    public User setUUID(String uuid) {
        this.uuid = uuid;
        return this;
    }

    /**
     * @return createdAt
     */
    public Date getCreatedAt() {
        return DateUtils.clone(createdAt);
    }

    /**
     * @param createdAt
     * @return this User
     */
    public User setCreatedAt(Date createdAt) {
        this.createdAt = DateUtils.clone(createdAt);
        return this;
    }

    /**
     * @return updatedAt
     */
    public Date getUpdatedAt() {
        return DateUtils.clone(updatedAt);
    }

    /**
     * @param updatedAt
     * @return this user
     */
    public User setUpdatedAt(Date updatedAt) {
        this.updatedAt = DateUtils.clone(updatedAt);
        return this;
    }


    /**
     * Find a user by UUID
     * 
     * @apiRoute /user/{uuid}
     * 
     * @param uuid
     * @param responseHandler
     */
    public static void findByUUID(String uuid, final HTTPResponse<User> response) {
        HTTPClient.getInstance().get(String.format("user/%s", uuid), null, new JsonHttpResponseHandler<User>(response) {

            @Override
            public void onSuccess(JSONObject json) {
                try {
                    User user = new User(json.getJSONObject("response").getJSONObject("user"));
                    response.onSuccess(user);

                } catch(Exception e) {
                    onFailure(e, json);
                }
            }
        });
    }
}

@@ -124,16 +124,44 @@
 Format;%0A
+import java.util.ArrayList;%0A
 import j
@@ -197,24 +197,51 @@
 il.Locale;%0A%0A
+import org.json.JSONArray;%0A
 import org.j
@@ -501,16 +501,61 @@
 teUtils;
+%0Aimport com.loopj.android.http.RequestParams;
 %0A%0Apublic
@@ -2150,16 +2150,1110 @@
     %7D%0A%0A%0A
+    /**%0A     * Search a user%0A     * %0A     * @apiRoute /user/search?q=%7Bquery%7D%0A     * %0A     * @param query%0A     * @param responseHandler%0A     */%0A    public static void search(String query, final HTTPResponse%3CArrayList%3CUser%3E%3E response) %7B%0A%0A        RequestParams params = new RequestParams();%0A        params.put(%22q%22, query);%0A%0A        HTTPClient.getInstance().get(%22user/my%22, params, new JsonHttpResponseHandler%3CArrayList%3CUser%3E%3E(response) %7B%0A%0A            @Override%0A            public void onSuccess(int statusCode, JSONObject json) %7B%0A                try %7B%0A                    ArrayList%3CUser%3E users = new ArrayList%3CUser%3E();%0A%0A                    JSONArray array = json.getJSONObject(%22response%22).getJSONArray(%22users%22);%0A                    for(int i = 0 ; i %3C array.length(); i++)%7B%0A                        User user = new User(array.getJSONObject(i));%0A                        users.add(user);%0A                    %7D%0A%0A                    response.onSuccess(statusCode, users);%0A%0A                %7D catch(Exception e) %7B%0A                    onFailure(e, json);%0A                %7D%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     /**%0A

Add search method into User model