package com.chariotsolutions.nfc.plugin;

import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.tech.Ndef;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Util {

    static JSONObject ndefToJSON(Ndef ndef) {
        JSONObject json = new JSONObject();
        try {
            json.put("type", translateType(ndef.getType()));
            json.put("maxSize", ndef.getMaxSize());
            json.put("isWritable", ndef.isWritable());
            json.put("canMakeReadOnly", ndef.canMakeReadOnly());
            json.put("ndefMessage", messageToJSON(ndef.getCachedNdefMessage()));
        } catch (JSONException e) {
            //Not sure why this would happen, documentation is unclear.
            // TODO fix this message crap
            Log.e("NdefPlugin", "Failed to convert ndef record into json: " + ndef.toString(), e);
        }
        return json;
    }

    static String translateType(String type) {
        String translation = "";
        if (type.equals(Ndef.NFC_FORUM_TYPE_1)) {
            translation = "NFC Forum Type 1";
        } else if (type.equals(Ndef.NFC_FORUM_TYPE_2)) {
            translation = "NFC Forum Type 2";
        } else if (type.equals(Ndef.NFC_FORUM_TYPE_3)) {
            translation = "NFC Forum Type 3";
        } else if (type.equals(Ndef.NFC_FORUM_TYPE_4)) {
            translation = "NFC Forum Type 4";
        } else {
            translation = type;
        }
        return translation;
    }

    static NdefRecord[] jsonToNdefRecords(String ndefMessageAsJSON) throws JSONException {
        JSONArray jsonRecords = new JSONArray(ndefMessageAsJSON);
        NdefRecord[] records = new NdefRecord[jsonRecords.length()];
        for (int i = 0; i < jsonRecords.length(); i++) {
            JSONObject record = jsonRecords.getJSONObject(i);
            byte tnf = (byte) record.getInt("tnf");
            byte[] type = jsonToByteArray(record.getJSONArray("type"));
            byte[] id = jsonToByteArray(record.getJSONArray("id"));
            byte[] payload = jsonToByteArray(record.getJSONArray("payload"));
            records[i] = new NdefRecord(tnf, type, id, payload);
        }
        return records;
    }

    static JSONArray byteArrayToJSON(byte[] bytes) {
        JSONArray json = new JSONArray();
        for (byte aByte : bytes) {
            json.put(aByte);
        }
        return json;
    }

    static byte[] jsonToByteArray(JSONArray json) throws JSONException {
        byte[] b = new byte[json.length()];
        for (int i = 0; i < json.length(); i++) {
            b[i] = (byte) json.getInt(i);
        }
        return b;
    }

    static JSONArray messageToJSON(NdefMessage message) {
        if (message == null) {
            return null;
        }

        List<JSONObject> list = new ArrayList<JSONObject>();

        for (NdefRecord ndefRecord : message.getRecords()) {
            list.add(recordToJSON(ndefRecord));
        }

        return new JSONArray(list);
    }

    static JSONObject recordToJSON(NdefRecord record) {
        JSONObject json = new JSONObject();
        try {
            json.put("tnf", record.getTnf());
            json.put("type", byteArrayToJSON(record.getType()));
            json.put("id", byteArrayToJSON(record.getId()));
            json.put("payload", byteArrayToJSON(record.getPayload()));
        } catch (JSONException e) {
            //Not sure why this would happen, documentation is unclear.
            Log.e("NdefPlugin", "Failed to convert ndef record into json: " + record.toString(), e);
        }
        return json;
    }

}

@@ -313,16 +313,61 @@
 Util %7B%0A%0A
+    static final String TAG = %22NdefPlugin%22;%0A%0A
     stat
@@ -440,32 +440,65 @@
 w JSONObject();%0A
+%0A        if (ndef != null) %7B%0A    
         try %7B%0A  
@@ -499,32 +499,36 @@
 y %7B%0A            
+    
 json.put(%22type%22,
@@ -552,32 +552,36 @@
 ef.getType()));%0A
+    
             json
@@ -620,32 +620,36 @@
 ));%0A            
+    
 json.put(%22isWrit
@@ -679,32 +679,36 @@
 ));%0A            
+   
+ 
 json.put(%22canMak
@@ -740,24 +740,28 @@
 eadOnly());%0A
+    
             
@@ -829,32 +829,36 @@
 ge()));%0A        
+   
+ 
 %7D catch (JSONExc
@@ -885,140 +885,21 @@
     
-//Not sure why this would happen, documentation is unclear.%0A            // TODO fix this message crap%0A            Log.e(%22NdefPlugin%22
+    Log.e(TAG
 , %22F
@@ -912,39 +912,32 @@
 to convert ndef 
-record 
 into json: %22 + n
@@ -948,32 +948,46 @@
 toString(), e);%0A
+            %7D%0A
         %7D%0A      
@@ -1085,13 +1085,8 @@
 tion
- = %22%22
 ;%0A  
@@ -3577,28 +3577,19 @@
   Log.e(
-%22NdefPlugin%22
+TAG
 , %22Faile

Handle null ndef gracefully