package com.nikolak.weatherapp.ForecastIO;

import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ForecastAPI {

    private static String API_BASE = "https://api.forecast.io";
    private static String API_KEY = "f1a122b49ed8d229604f818cd13ae21d";

    private String getAPIresponse(String apiURL) {
        DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
        HttpGet httpget = new HttpGet(apiURL);
        httpget.setHeader("Content-type", "application/json");

        InputStream inputStream = null;
        String result = null;
        try {
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            inputStream = entity.getContent();
            // json is UTF-8 by default
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            result = sb.toString();
            return result;
        } catch (Exception e) {
            Log.e("URL Exception", e.toString());
            return null;
        } finally {
            try {
                if (inputStream != null) inputStream.close();
            } catch (IOException e) {
                Log.e("URL Get", e.toString());
            }
        }
    }

    public Current getCurrent(String lat, String lon) {
        String forecastURL = API_BASE + "/forecast/" + API_KEY + "/" + lat + "," + lon+"?units=auto";
        Log.d("forecastURL", forecastURL);
        if (lat == null || lon == null) {
            return null;
        }

        String apiResp = getAPIresponse(forecastURL);
        JSONObject jObject;
        try {
            jObject = new JSONObject(apiResp);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }

        JSONObject currentObject;
        String forecastUnits = "us";
        try {
            currentObject = jObject.getJSONObject("currently");
            //forecastUnits = currentObject.getJSONArray("flags").getString("units");
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
        Log.d("currentObject", currentObject.toString());

        Current currentForecast = new Current();

        currentForecast.setUnits(forecastUnits);
        try {
            currentForecast.fromJson(currentObject);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }

        return currentForecast;
    }
}

@@ -1870,25 +1870,28 @@
 lic 
-Current getCurren
+JSONObject getDefaul
 t(St
@@ -2031,24 +2031,27 @@
  Log.d(%22
-forecast
+getDefault 
 URL%22, fo
@@ -2213,17 +2213,23 @@
 NObject 
-j
+APIJson
 Object;%0A
@@ -2254,17 +2254,23 @@
         
-j
+APIJson
 Object =
@@ -2295,16 +2295,50 @@
 iResp);%0A
+            return APIJsonObject;%0A
         
@@ -2426,32 +2426,34 @@
 ull;%0A        %7D%0A%0A
+//
         JSONObje
@@ -2462,32 +2462,34 @@
  currentObject;%0A
+//
         String f
@@ -2509,16 +2509,18 @@
 = %22us%22;%0A
+//
         
@@ -2517,32 +2517,34 @@
 //        try %7B%0A
+//
             curr
@@ -2587,24 +2587,26 @@
 urrently%22);%0A
+//
             
@@ -2675,24 +2675,26 @@
 g(%22units%22);%0A
+//
         %7D ca
@@ -2709,32 +2709,34 @@
 NException e) %7B%0A
+//
             e.pr
@@ -2744,32 +2744,34 @@
 ntStackTrace();%0A
+//
             retu
@@ -2771,32 +2771,34 @@
    return null;%0A
+//
         %7D%0A      
@@ -2783,32 +2783,34 @@
 ll;%0A//        %7D%0A
+//
         Log.d(%22c
@@ -2851,17 +2851,21 @@
 ing());%0A
-%0A
+//%0A//
         
@@ -2901,25 +2901,29 @@
  Current();%0A
-%0A
+//%0A//
         curr
@@ -2959,16 +2959,18 @@
 Units);%0A
+//
         
@@ -2967,32 +2967,34 @@
 //        try %7B%0A
+//
             curr
@@ -3026,24 +3026,26 @@
 entObject);%0A
+//
         %7D ca
@@ -3060,32 +3060,34 @@
 NException e) %7B%0A
+//
             e.pr
@@ -3095,32 +3095,34 @@
 ntStackTrace();%0A
+//
             retu
@@ -3122,32 +3122,34 @@
    return null;%0A
+//
         %7D%0A%0A     
@@ -3134,33 +3134,37 @@
 ll;%0A//        %7D%0A
-%0A
+//%0A//
         return c

Return jsonobject instead of class instance