package com.fisheradelakin.bitwear;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.TextView;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;


public class MainActivity extends ActionBarActivity {

    //public static final String URL = "https://api.bitcoinaverage.com/ticker/global/USD/";
    TextView mPriceText;
    private ProgressDialog pDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(isNetworkAvailable()) {
            //JSONParse parse = new JSONParse();
            //parse.execute();

            dialog();
            new GetPrice().execute();
        }
    }



    protected void dialog() {
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Getting price ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();

        boolean isAvailable = false;
        if(networkInfo != null && networkInfo.isConnected()) {
            isAvailable = true;
        }

        return isAvailable;
    }

    private class GetPrice extends AsyncTask<String, String, String> {
        protected void doInBackground() {
            try {
                URL url = new URL("https://api.bitcoinaverage.com/ticker/global/USD/24h_avg");
                URLConnection yc = url.openConnection();
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(
                                yc.getInputStream()));
                String inputLine;
                StringBuilder builder = new StringBuilder();
                while ((inputLine = in.readLine()) != null)
                    builder.append(inputLine.trim());
                in.close();
                String price = builder.toString();
                pDialog.dismiss();
                mPriceText = (TextView) findViewById(R.id.priceTextView);
                mPriceText.setText(price);
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    }

    /* private class JSONParse extends AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Getting price ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        @Override
        protected JSONObject doInBackground(String... args) {
            JSONParser jParser = new JSONParser();
            // Getting JSON from URL
            JSONObject json = jParser.getJsonFromUrl(URL);
            return json;
        }
        @Override
        protected void onPostExecute(JSONObject json) {
            pDialog.dismiss();
            try {
                // jokes = json.getJSONArray(TAG_JOKE);
                // getting json from url
                //JSONObject c = json.getJSONObject(json);
                // store json item
                //JSONObject c = new JSONObject(json);
                String price = json.getString("24h_avg");
                // set json data in textview
                mPriceText = (TextView) findViewById(R.id.priceTextView);
                mPriceText.setText(price.replace("n", ""));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } */
}

@@ -339,154 +339,8 @@
 t;%0A%0A
-import java.io.BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStreamReader;%0Aimport java.net.URL;%0Aimport java.net.URLConnection;%0A%0A
 %0Apub
@@ -395,18 +395,16 @@
  %7B%0A%0A    
-//
 public s
@@ -509,44 +509,8 @@
 ext;
-%0A    private ProgressDialog pDialog;
 %0A%0A  
@@ -714,26 +714,24 @@
             
-//
 JSONParse pa
@@ -765,18 +765,16 @@
         
-//
 parse.ex
@@ -786,325 +786,17 @@
 ();%0A
-%0A            dialog();%0A            new GetPrice().execute();%0A        %7D%0A    %7D%0A%0A%0A%0A    protected void dialog() %7B%0A        pDialog = new ProgressDialog(MainActivity.this);%0A        pDialog.setMessage(%22Getting price ...%22);%0A        pDialog.setIndeterminate(false);%0A        pDialog.setCancelable(true);%0A        pDialog.show();
+        %7D
 %0A   
@@ -1202,997 +1202,8 @@
 %0A   
- private class GetPrice extends AsyncTask%3CString, String, String%3E %7B%0A        protected void doInBackground() %7B%0A            try %7B%0A                URL url = new URL(%22https://api.bitcoinaverage.com/ticker/global/USD/24h_avg%22);%0A                URLConnection yc = url.openConnection();%0A                BufferedReader in = new BufferedReader(%0A                        new InputStreamReader(%0A                                yc.getInputStream()));%0A                String inputLine;%0A                StringBuilder builder = new StringBuilder();%0A                while ((inputLine = in.readLine()) != null)%0A                    builder.append(inputLine.trim());%0A                in.close();%0A                String price = builder.toString();%0A                pDialog.dismiss();%0A                mPriceText = (TextView) findViewById(R.id.priceTextView);%0A                mPriceText.setText(price);%0A            %7D catch(IOException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A    %7D%0A%0A    /*
  pri
@@ -2615,10 +2615,7 @@
    %7D
- */
 %0A%7D%0A

revert back to sort of working state