package com.crypho.plugins;

//import android.util.Log;
import android.util.Base64;

import android.content.Context;
import android.content.Intent;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.ConfigXmlParser;
import org.apache.cordova.CordovaActivity;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONException;

public class SecureStorage extends CordovaPlugin {
    private static final String TAG = "SecureStorage";

    private String encryptionKey;
    private String serviceName;

    private SecurePreferences preferences;

    @Override
    public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
        if ("init".equals(action)) {
            try {
                serviceName = args.getString(0);
                encryptionKey = args.getString(1);

                preferences = new SecurePreferences(getContext(), encryptionKey, serviceName);

                callbackContext.success();
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        }
        if ("set".equals(action)) {
            final String key = args.getString(0);
            final String value = args.getString(1);
            cordova.getThreadPool().execute(new Runnable() {
                public void run() {
                    try {
                        SecurePreferences.Editor editor = preferences.edit();
                        editor.putString(key, value);
                        editor.commit();

                        callbackContext.success();
                    } catch (Exception e) {
                        callbackContext.error(e.getMessage());
                    }
                }
            });
            return true;
        }
        if ("get".equals(action)) {
            final String key = args.getString(0);
            cordova.getThreadPool().execute(new Runnable() {
                public void run() {
                    try {
                        String value = preferences.getString(key, "");

                        if("".equals(value)) {
                            callbackContext.error("Not Found");
                        }
                        else{
                            callbackContext.success(value);    
                        }
                    } catch (Exception e) {
                        callbackContext.error(e.getMessage());
                    }
                }
            });
            return true;
        }
        if ("remove".equals(action)) {
            final String key = args.getString(0);
            cordova.getThreadPool().execute(new Runnable() {
                public void run() {
                    try {
                        SecurePreferences.Editor editor = preferences.edit();
                        editor.putString(key, "");
                        editor.commit();

                        callbackContext.success();
                    } catch (Exception e) {
                        callbackContext.error(e.getMessage());
                    }
                }
            });
            return true;
        }
        return false;
    }

    private Context getContext(){
        return cordova.getActivity().getApplicationContext();
    }
}

@@ -2141,180 +2141,8 @@
 %22);%0A
-%0A                        if(%22%22.equals(value)) %7B%0A                            callbackContext.error(%22Not Found%22);%0A                        %7D%0A                        else%7B%0A    
     
@@ -2201,34 +2201,8 @@
    %0A
-                        %7D%0A
     

revert last change. On object not found, returns empty string ("")