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 categoryLink = "http://ea2ac45.ngrok.com/categories";
    private final String intNews = "http://ea2ac45.ngrok.com/intNews";
    private final String intNewsPics = "http://ea2ac45.ngrok.com/intNewsPics";
    private HttpClient httpClient;
    private HttpGet request;
    private HttpResponse httpResponse;
    private BufferedReader bufferedReader;
    private ArrayList<String> titles;
    private ArrayList<Integer> article_count;

    public ParseCategories() {
        article_count = new ArrayList<Integer>();
        titles = new ArrayList<String>();
        httpClient = new DefaultHttpClient();
    }

    /**
     * Get titles in categories
     * @return ArrayList<String>
     */
    public ArrayList<String> getTitles () {
        try {
            request = new HttpGet(new URI(categoryLink));
            httpResponse = httpClient.execute(request);
            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++) {
                titles.add(jsonArray.getJSONObject(i).getString("title"));
            }
        }catch (IOException e){
            onError(e);
        } catch (JSONException e) {
            onError(e);
        } catch (URISyntaxException e) {
            onError(e);
        }
        return titles;
    }

    public ArrayList<Integer> getArticleCount () {
        try {
            request = new HttpGet(new URI(intNews));
            httpResponse = httpClient.execute(request);
            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);
        } catch (URISyntaxException e) {
            onError(e);
        }
        return article_count;
    }


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

}

@@ -474,16 +474,42 @@
 ayList;%0A
+import java.util.Objects;%0A
 %0A%0A/**%0A *
@@ -875,16 +875,20 @@
  intNews
+Link
  = %22http
@@ -954,16 +954,20 @@
 NewsPics
+Link
  = %22http
@@ -1230,16 +1230,198 @@
 e_count;
+%0A    private ArrayList%3CString%3E intNewsContent;%0A    private ArrayList%3CInteger%3E intNewsid;%0A    private ArrayList%3CString%3E intNewstitle;%0A    private ArrayList%3CArrayList%3CString%3E%3E intNews;
 %0A%0A    pu
@@ -1533,24 +1533,78 @@
 %3CString%3E();%0A
+        intNews = new ArrayList%3CArrayList%3CString%3E%3E();%0A
         http
@@ -2598,23 +2598,28 @@
 new URI(
-intNews
+categoryLink
 ));%0A    
@@ -3240,32 +3240,32 @@
 r(e);%0A        %7D%0A
-
         return a
@@ -3285,16 +3285,1191 @@
 %0A    %7D%0A%0A
+    public ArrayList%3CArrayList%3CString%3E%3E getIntNews () %7B%0A        try %7B%0A            intNewsid = new ArrayList%3CInteger%3E();%0A            intNewstitle = new ArrayList%3CString%3E();%0A            intNewsContent = new ArrayList%3CString%3E();%0A            request = new HttpGet(new URI(intNewsLink));%0A            httpResponse = httpClient.execute(request);%0A            bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));%0A            String tempStr = bufferedReader.readLine();%0A            JSONArray jsonArray = new JSONArray(tempStr);%0A            for (int i = 0; i %3C jsonArray.length(); i++) %7B%0A//                intNewsid.add((jsonArray.getJSONObject(i).getInt(%22id%22)));%0A                intNewstitle.add(jsonArray.getJSONObject(i).getString(%22title%22));%0A                intNewsContent.add(jsonArray.getJSONObject(i).getString(%22content%22));%0A            %7D%0A        %7D catch (IOException e) %7B%0A            onError(e);%0A        %7D catch (JSONException e) %7B%0A            onError(e);%0A        %7D catch (URISyntaxException e) %7B%0A            onError(e);%0A        %7D%0A        intNews.add(intNewstitle);%0A        intNews.add(intNewsContent);%0A        return intNews;%0A    %7D%0A%0A
 %0A    pri

update API