package com.notipon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.content.Context;
import android.util.Log;

import com.notipon.Deal;
import com.notipon.Filter;

public class DealFetcher {
    private static final String EXAMPLE_JSON_FILE = "example_deals.json";
    private static final String TAG = "DealFetcher";
    private Context context;

    public DealFetcher(Context context) {
        this.context = context;
    }

    public ArrayList<Deal> getDeals() {
        return getJsonExampleDeals();
    }

    public ArrayList<Deal> getDeals(Filter filter) {
        return filter.apply(getDeals());
    }

    private ArrayList<Deal> getJsonExampleDeals() {
        return Deal.parseJsonDeals(loadExampleJson());
    }

    private String loadExampleJson() {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(context.getAssets().open(EXAMPLE_JSON_FILE)));
            StringBuilder builder = new StringBuilder();
            String line;

            while ((line = in.readLine()) != null) {
                builder.append(line);
            }
            in.close();

            return builder.toString();
        } catch (IOException e) {
            Log.e(TAG, "Failed to load example Json");
        }

        return "";
    }
}
@@ -100,22 +100,110 @@
 utStream
-Reader
+;%0Aimport java.io.InputStreamReader;%0Aimport java.net.MalformedURLException;%0Aimport java.net.URL
 ;%0Aimport
@@ -518,16 +518,105 @@
 context;
+%0A    private static final String grouponKey = %228a42c4f943043ffe9cb59defb3f51bc30e4c59a3%22;
 %0A%0A    pu
@@ -981,32 +981,36 @@
 eJson());%0A    %7D%0A
+    
 %0A    private Str
@@ -1021,42 +1021,69 @@
 load
-ExampleJson() %7B%0A        try %7B%0A    
+StreamAsString(InputStream binaryInput) throws IOException %7B%0A
     
@@ -1151,59 +1151,23 @@
 der(
-context.getAssets().open(EXAMPLE_JSON_FILE
+binaryInput
 ))
-)
 ;%0A
-    
     
@@ -1211,28 +1211,24 @@
 gBuilder();%0A
-    
         Stri
@@ -1245,20 +1245,16 @@
         
-    
 while ((
@@ -1298,20 +1298,16 @@
         
-    
 builder.
@@ -1332,25 +1332,18 @@
     
-    %7D%0A   
+%7D%0A
         
  in.
@@ -1338,17 +1338,16 @@
         
- 
 in.close
@@ -1351,20 +1351,16 @@
 ose();%0A%0A
-    
         
@@ -1394,121 +1394,911 @@
     
-    %7D catch (IOException e) %7B%0A            Log.e(TAG, %22Failed to load example Json%22);%0A        %7D%0A%0A        return %22%22
+%7D%0A%0A    private String loadExampleJson() %7B%0A        try %7B%0A            return loadStreamAsString(context.getAssets().open(EXAMPLE_JSON_FILE));%0A        %7D catch (IOException e) %7B%0A            Log.e(TAG, %22Failed to load example Json%22);%0A        %7D%0A%0A        return %22%22;%0A    %7D%0A    %0A    private URL buildDealRequestUrl() throws MalformedURLException %7B%0A%09return new URL(%22http://api.groupon.com/v2/deals/?client_id=%22 + grouponKey);%0A    %7D%0A    %0A    private String fetchCurrentDeals() %7B%0A        try %7B%0A            URL dealUrl = buildDealRequestUrl();%0A            return loadStreamAsString(dealUrl.openStream());%0A        %7D%0A        catch (MalformedURLException e) %7B%0A            Log.e(TAG, %22Bad deal URL:%22, e);%0A        %7D%0A        catch (IOException e) %7B%0A            Log.e(TAG, %22Failed to load URL:%22, e);%0A        %7D%0A%09%0A        Log.e(TAG, %22Error in fetching current deals, falling back to example Json.%22);%0A%09return loadExampleJson()
 ;%0A  

Load from server prototype