package com.nyubustracker.helpers;

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

import com.nyubustracker.activities.MainActivity;

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

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class Downloader extends AsyncTask<String, Void, JSONObject> {
    final DownloaderHelper helper;
    public static final String CREATED_FILES_DIR = "NYUCachedFiles";
    static Context context;

    public static Context getContext() {
        return context;
    }

    public Downloader(DownloaderHelper helper, Context mContext) {
        this.helper = helper;
        context = mContext;
        Log.v("Context Tag", (context == null) + " that context is null");
    }

    @Override
    public JSONObject doInBackground(String... urls) {
        try {
            if (MainActivity.LOCAL_LOGV) Log.v(MainActivity.REFACTOR_LOG_TAG, "First url: " + urls[0]);
            return new JSONObject(downloadUrl(urls[0]));
        } catch (IOException e) {
            //Log.e("JSON", "DownloadURL IO error.");
            e.printStackTrace();
        } catch (JSONException e) {
            //Log.e("JSON", "DownloadURL JSON error.");
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(JSONObject result) {
        try {
            helper.parse(result);
            if (MainActivity.LOCAL_LOGV) Log.v(MainActivity.REFACTOR_LOG_TAG, "helper class: " + helper.getClass() + " (" + MainActivity.downloadsOnTheWire + ")");
            if (!helper.getClass().toString().contains("BusDownloaderHelper")) MainActivity.pieceDownloadsTogether(context);
        } catch (JSONException e) {
            Log.d(MainActivity.REFACTOR_LOG_TAG, "JSON Exception while parsing in onPostExecute.");
            e.printStackTrace();
        } catch (IOException e) {
            Log.d(MainActivity.REFACTOR_LOG_TAG, "IO Exception while parsing in onPostExecute.");
        }
    }

    // Given a URL, establishes an HttpUrlConnection and retrieves
    // the web page content as a InputStream, which it returns as
    // a string.
    private String downloadUrl(String myUrl) throws IOException {
        InputStream is = null;

        try {
            URL url = new URL(myUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.setRequestProperty("X-Mashape-Authorization", "0gpwrDtINCQRxnhWEyJpEgdfYdQjZYSp");
            // Starts the QUERY
            conn.connect();
            //int response = conn.getResponseCode();
            //Log.d("JSON", "The response is: " + response);
            is = conn.getInputStream();

            // Convert the InputStream into a string
            return readIt(is);

            // Makes sure that the InputStream is closed after the app is
            // finished using it.
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

    // Reads an InputStream and converts it to a String.
    private String readIt(InputStream stream) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "iso-8859-1"), 128);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    }

    public static void cache(String fileName, JSONObject jsonObject) throws IOException {
        File path = new File(context.getFilesDir(), CREATED_FILES_DIR);
        path.mkdir();
        File file = new File(path, fileName);
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
        bufferedWriter.write(jsonObject.toString());
        bufferedWriter.close();
    }

    public static String makeQuery(String param, String value, String charset) {
        try {
            return String.format(param + "=" + URLEncoder.encode(value, charset));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }
}

@@ -3990,32 +3990,106 @@
 s IOException %7B%0A
+        if (jsonObject != null && !jsonObject.toString().isEmpty()) %7B%0A    
         File pat
@@ -4144,16 +4144,20 @@
 S_DIR);%0A
+    
         
@@ -4174,24 +4174,28 @@
 ();%0A        
+    
 File file = 
@@ -4220,32 +4220,36 @@
 eName);%0A        
+   
+ 
 BufferedWriter b
@@ -4306,32 +4306,36 @@
 file));%0A        
+    
 bufferedWriter.w
@@ -4363,32 +4363,36 @@
 ing());%0A        
+ 
+   
 bufferedWriter.c
@@ -4395,24 +4395,34 @@
 er.close();%0A
+        %7D%0A
     %7D%0A%0A    p

Fix null pointer when opened offline.