package com.KFCBETA.hjeaimreus.chikan;

import android.util.Log;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;


/**
 * To parse the side menu items from mySQL database,
 * return a array list in order to input into constructor
 * of navigation drawer.
 * Created by hrw on 14/8/20.
 */
public class ParseCategories {
    private final static String TAG = "ParseCategories";
    private final String link = "http://ea2ac45.ngrok.com/navigationitem";
    private HttpClient httpClient;
    private HttpGet request;
    private HttpResponse httpResponse;
    private BufferedReader bufferedReader;
    private ArrayList<String> titles;
    private ArrayList<Integer> article_count;

    ParseCategories() {
        try {
            httpClient = new DefaultHttpClient();
            request = new HttpGet(new URI(link));
            httpResponse = httpClient.execute(request);
        } catch (IOException e) {
            onError(e);
        } catch (URISyntaxException e) {
            onError(e);
        }
    }

    /**
     * Get titles in categories
     * @return ArrayList<String>
     */
    public ArrayList<String> getTitles () {
        try {
            titles = new ArrayList<String>();
            bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            String tempStr = bufferedReader.readLine();
            Log.w(TAG,tempStr);
            JSONArray jsonArray = new JSONArray(tempStr);
            for (int i = 0; i < jsonArray.length(); i++) {
                titles.add(jsonArray.getJSONObject(i).getString("title"));
            }
        }catch (IOException e){
            onError(e);
        } catch (JSONException e) {
            onError(e);
        }
        return titles;
    }

    public ArrayList<Integer> getArticleCount () {
        article_count = new ArrayList<Integer>();
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            String tempStr = bufferedReader.readLine();
            JSONArray jsonArray = new JSONArray(tempStr);
            for (int i = 0; i < jsonArray.length(); i++) {
                article_count.add(jsonArray.getJSONObject(i).getInt("article_count"));
            }
        } catch (IOException e) {
            onError(e);
        } catch (JSONException e) {
            onError(e);
        }
        return article_count;
    }


    private void onError(Exception e){
        Log.w(TAG,e.toString());
    }

}

@@ -763,17 +763,25 @@
  String 
-l
+categoryL
 ink = %22h
@@ -808,22 +808,89 @@
 com/
-navigationitem
+categories%22;%0A    private final String intNews = %22http://ea2ac45.ngrok.com/intNews
 %22;%0A 
@@ -1155,60 +1155,46 @@
     
-try %7B%0A            httpClient = new DefaultHttpClient
+article_count = new ArrayList%3CInteger%3E
 ();%0A
@@ -1205,238 +1205,87 @@
     
-    requ
+titl
 es
-t
  = new 
-HttpGet(new URI(link));%0A            httpResponse = httpClient.execute(request);%0A        %7D catch (IOException e) %7B%0A            onError(e);%0A        %7D catch (URISyntaxException e) %7B%0A            onError(e);%0A        %7D
+ArrayList%3CString%3E();%0A        httpClient = new DefaultHttpClient();
 %0A   
@@ -1443,39 +1443,107 @@
     
-titl
+requ
 es
+t
  = new 
-ArrayList%3CString%3E(
+HttpGet(new URI(categoryLink));%0A            httpResponse = httpClient.execute(request
 );%0A 
@@ -1712,40 +1712,8 @@
 ();%0A
-            Log.w(TAG,tempStr);%0A
     
@@ -2022,32 +2022,97 @@
     onError(e);%0A
+        %7D catch (URISyntaxException e) %7B%0A            onError(e);%0A
         %7D%0A      
@@ -2198,63 +2198,122 @@
     
-article_count = new ArrayList%3CInteger%3E();%0A        try %7B
+try %7B%0A            request = new HttpGet(new URI(intNews));%0A            httpResponse = httpClient.execute(request);
 %0A   
@@ -2804,32 +2804,97 @@
     onError(e);%0A
+        %7D catch (URISyntaxException e) %7B%0A            onError(e);%0A
         %7D%0A      

update http url used in api