package com.github.snowdream.android.apps.jokes;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hui.yang on 2014/12/7.
 */
public class JokeManager {

    public static void getJokes(final CallBack callBack){
        List<Joke> list = null;
        AsyncHttpClient client = new AsyncHttpClient();
        client.get("https://raw.githubusercontent.com/snowdream/snowdream-jokes-android/master/docs/test/jokes.json", new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                List<Joke> list =  parseJokes(response);
                if (callBack != null){
                    callBack.callback(list);
                }
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {
            }

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

    private static List<Joke> parseJokes(JSONObject obj){
        List<Joke> list = null;

        if (obj == null){
            return list;
        }

        try {
            list = new ArrayList<Joke>();
            JSONArray array = obj.getJSONArray("jokes");
            int length = array.length();
            for(int i = 0; i < length; i++){
                JSONObject oj = array.getJSONObject(i);

                if (oj == null){
                    continue;
                }
                Joke joke = new Joke();
                joke.setContent(oj.getString("content"));
                joke.setAuthor(oj.getString("author"));
                joke.setCreatetime(oj.getString("createtime"));
                joke.setUp(oj.getInt("up"));
                joke.setDown(oj.getInt("down"));

                JSONArray images = obj.getJSONArray("images");
                int size = images.length();
                List<String> imgs = new ArrayList<String>();
                for(int j = 0; j < size; j++){
                    imgs.add(images.getString(j));
                }
                joke.setImages(imgs);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        return list;
    }
}

@@ -444,16 +444,17 @@
 allBack)
+ 
 %7B%0A      
@@ -831,17 +831,16 @@
 %3E list =
- 
  parseJo
@@ -887,24 +887,25 @@
 ack != null)
+ 
 %7B%0A          
@@ -1405,16 +1405,17 @@
 ect obj)
+ 
 %7B%0A      
@@ -1457,32 +1457,33 @@
 if (obj == null)
+ 
 %7B%0A            re
@@ -1669,24 +1669,25 @@
          for
+ 
 (int i = 0; 
@@ -1702,16 +1702,17 @@
 th; i++)
+ 
 %7B%0A      
@@ -1793,16 +1793,17 @@
 == null)
+ 
 %7B%0A      
@@ -1915,26 +1915,26 @@
 tContent(oj.
-ge
+op
 tString(%22con
@@ -1972,26 +1972,26 @@
 etAuthor(oj.
-ge
+op
 tString(%22aut
@@ -2036,18 +2036,18 @@
 time(oj.
-ge
+op
 tString(
@@ -2088,26 +2088,26 @@
 ke.setUp(oj.
-ge
+op
 tInt(%22up%22));
@@ -2139,18 +2139,18 @@
 Down(oj.
-ge
+op
 tInt(%22do
@@ -2189,29 +2189,28 @@
 y images = o
-b
 j.
-ge
+op
 tJSONArray(%22
@@ -2215,24 +2215,66 @@
 (%22images%22);%0A
+                if (images != null) %7B%0A    
             
@@ -2317,24 +2317,28 @@
             
+   
+ 
 List%3CString%3E
@@ -2386,19 +2386,24 @@
         
+    
 for
+ 
 (int j =
@@ -2420,16 +2420,17 @@
 ze; j++)
+ 
 %7B%0A      
@@ -2443,16 +2443,20 @@
         
+  
+  
 imgs.add
@@ -2486,34 +2486,41 @@
                 
-%7D%0A
+    %7D%0A   
                 
@@ -2511,32 +2511,33 @@
                 
+ 
 joke.setImages(i
@@ -2542,16 +2542,67 @@
 (imgs);%0A
+                %7D%0A%0A                list.add(joke);%0A
         

debug ok