package d0020e.basac;

import android.bluetooth.BluetoothAdapter;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Date;

/**
 * Created by weedz on 2016-02-03.
 */

/*

JSON structure:

{
    "MAC": "02:00:00:00:00:00",
    "timestamp": 1454496535538,
    "data": {
        "value_01": "value",
        "value_02": "value 02"
    }
}

 */
public class JSONData {
    private static final String TAG = "JSONData";

    private JSONObject finalJSON;

    public JSONData() {
        if (BluetoothAdapter.getDefaultAdapter() == null) {
            Log.e(TAG, "Bluetooth not available");
            return;
        }
        finalJSON = new JSONObject();
        try {
            finalJSON.put("MAC", BluetoothAdapter.getDefaultAdapter().getAddress());
            finalJSON.put("timestamp", System.currentTimeMillis());
            finalJSON.put("values", new JSONObject());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void put(String name, Object value) {
        try {
            finalJSON.getJSONObject("values").put(name, value);
        } catch (JSONException e) {
            e.printStackTrace();
            Log.d(TAG, "put() failed to put " + name + ": " + value.toString());
        }
    }

    public void remove(String name) {
        try {
            finalJSON.getJSONObject("values").remove(name);
        } catch (JSONException e) {
            e.printStackTrace();
            Log.d(TAG, "remove() failed to remove value: values." + name);
        }
    }

    public Object getDataValues(String name) {
        try {
            return finalJSON.getJSONObject("values").get(name);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Object get(String name) {
        try {
            return finalJSON.get(name);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void logJSON() {
        try {
            finalJSON.put("timestamp", System.currentTimeMillis());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.d(TAG, "JSONData: " + finalJSON.toString());
    }

    public JSONObject getJSON() {
        try {
            finalJSON.put("timestamp", System.currentTimeMillis());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return finalJSON;
    }

    public String toString() {
        return this.finalJSON.toString();
    }

}

@@ -515,24 +515,80 @@
 SONData() %7B%0A
+        finalJSON = new JSONObject();%0A        try %7B%0A    
         if (
@@ -624,17 +624,17 @@
 apter() 
-=
+!
 = null) 
@@ -643,137 +643,8 @@
     
-        Log.e(TAG, %22Bluetooth not available%22);%0A            return;%0A        %7D%0A        finalJSON = new JSONObject();%0A        try %7B%0A
     
@@ -720,24 +720,38 @@
 Address());%0A
+            %7D%0A
             

Fix crash if bluetooth adapter is unavailable