package com.andela.currencycalculator.model.jsonparser;


import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

/**
 * Created by Spykins on 18/02/2016.
 */
public class CurrencyJsonParser {
    HashMap<String, Double> allDataFromJson;

    private String jsonUrl =  "https://openexchangerates.org/api/latest.json?app_id=125d7e1c1f664d0488a4262f599038ae";

    public CurrencyJsonParser() {


    }

    public void loadRate()   {
        FetchExchangeFromJson exchangeRateTask = new FetchExchangeFromJson();
        exchangeRateTask.execute();
    }

    public class FetchExchangeFromJson extends AsyncTask<String, Void, String[]>{


        @Override
        protected String[] doInBackground(String... params) {

            URL url;
            BufferedReader reader = null;
            //StringBuilder stringBuilder;

            try
            {
                // create the HttpURLConnection
                Uri jsonUri = Uri.parse(jsonUrl);
                url = new URL(jsonUri.toString());
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                // just want to do an HTTP GET here
                connection.setRequestMethod("GET");

                // uncomment this if you want to write output to this url
                //connection.setDoOutput(true);

                // give it 15 seconds to respond
                //connection.setReadTimeout(15*1000);
                connection.connect();
                StringBuffer buffer = new StringBuffer();

                // read the output from the server
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                //stringBuilder = new StringBuilder();
                String jsonFile = "";

                String line = "";
                while ((line = reader.readLine()) != null)
                {
                    manipulateStringData(line);
                    buffer.append(line + "\n");
                }
                //return stringBuilder.toString();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                //throw e;
            }
            finally
            {
                // close the reader; this can throw an exception too, so
                // wrap it in another try/catch block.
                if (reader != null)
                {
                    try
                    {
                        reader.close();
                    }
                    catch (IOException ioe)
                    {
                        ioe.printStackTrace();
                    }
                }
            }

            return null;
        }



    }

    private void manipulateStringData(String line){

        if(allDataFromJson != null && line.split(":")[0].trim().length() > 1){
            String currencyName = line.split(":")[0].trim().replace("\"", "");
            Double currencyExchangeRate = Double.parseDouble(line.split(":")[1].trim().replace(",",""));
            //currency = currency.substring(currency.indexOf("\""));
            Log.d("waleola", currencyName + " = " + currencyExchangeRate);


        }
        if(line.split(":")[0].trim().equals("\"rates\"")){
            allDataFromJson = new HashMap<>();
        }

    }



}



@@ -429,16 +429,56 @@
 romJson;
+%0A    public static String BASE_CURRENCY;
 %0A%0A    pr
@@ -1699,66 +1699,8 @@
 t();
-%0A                StringBuffer buffer = new StringBuffer();
 %0A%0A  
@@ -2102,56 +2102,8 @@
 e);%0A
-                    buffer.append(line + %22%5Cn%22);%0A
     
@@ -3174,117 +3174,41 @@
     
-//currency = currency.substring(currency.indexOf(%22%5C%22%22));%0A            Log.d(%22waleola%22, currencyName + %22 = %22 + 
+allDataFromJson.put(currencyName,
 curr
@@ -3227,17 +3227,16 @@
 Rate);%0A%0A
-%0A
         
@@ -3350,24 +3350,263 @@
 %0A        %7D%0A%0A
+        if(line.split(%22:%22)%5B0%5D.trim().equals(%22%5C%22base%5C%22%22))%7B%0A            CurrencyJsonParser.BASE_CURRENCY = line.split(%22:%22)%5B1%5D.trim();%0A        %7D%0A%0A    %7D%0A%0A    public HashMap%3CString, Double%3E getAllDataFromJson()%7B%0A        return allDataFromJson;%0A
     %7D%0A%0A%0A%0A%7D%0A%0A

Add base currency to the class