package com.cordova.plugin;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;

import org.json.JSONException;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.samsung.android.sdk.SsdkUnsupportedException;
import com.samsung.android.sdk.pass.Spass;
import com.samsung.android.sdk.pass.SpassFingerprint;
import com.samsung.android.sdk.pass.SpassInvalidStateException;

public class SamsungPassPlugin extends CordovaPlugin {

    private Spass mSpass;
    private SpassFingerprint mSpassFingerprint;
    private boolean isFeatureEnabled = false;
    private static final String TAG = "SamsungPassPlugin";

    @Override
    public void initialize(final CordovaInterface cordova, CordovaWebView webView) {

        mSpass = new Spass();

        try {
            mSpass.initialize(this.cordova.getActivity().getApplicationContext());
            Log.d(TAG, "Spass was Initialized");
        } catch (SsdkUnsupportedException e) {
            Log.d(TAG, "Spass could not initialize" + e);
        }

        isFeatureEnabled = mSpass.isFeatureEnabled(Spass.DEVICE_FINGERPRINT);
        
        if (isFeatureEnabled) {
            mSpassFingerprint = new SpassFingerprint(this.cordova.getActivity().getApplicationContext());
            Log.d(TAG, "mSpassFingerprint was Initialized");
        } else {
            Log.d(TAG, "Fingerprint Service is not supported in the device.");
        }
    }

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        
        Log.d(TAG, "Plugin Method Called: " + action);

        if (action.equals("checkSamsungPassSupport")) {
            this.checkSamsungPassSupport(args, callbackContext);
        } else if (action.equals("checkForRegisteredFingers")) {
            this.checkForRegisteredFingers(args, callbackContext);
        }
        else {
            return false;
        }

        return true;
    }

    private void checkSamsungPassSupport(JSONArray args, CallbackContext callbackContext) {
        Log.d(TAG, "checkSamsungPassSupport");

        if (isFeatureEnabled) {
            callbackContext.success();
        } else {
            callbackContext.error("Error");
        }
    }

    private void checkForRegisteredFingers(JSONArray args, CallbackContext callbackContext) {
        Log.d(TAG, "checkForRegisteredFingers");

        boolean mHasRegisteredFinger = mSpassFingerprint.hasRegisteredFinger();

        if (mHasRegisteredFinger) {
            callbackContext.success();
        } else {
            callbackContext.error("Error");
        }
    }

    private void startIdentifyWithDialog(JSONArray args, CallbackContext callbackContext) {

        mSpassFingerprint.setDialogTitle("Authentificate yourself!", 0xff0000);

        mSpassFingerprint.startIdentifyWithDialog(this.cordova.getActivity().getApplicationContext(), listener, false);

        private SpassFingerprint.IdentifyListener listener = new SpassFingerprint.IdentifyListener() {
            @Override
            public void onFinished(int eventStatus) {
                callbackContext.success(eventStatus);
            }
        };
    }
}

@@ -838,16 +838,72 @@
 Plugin%22;
+%0A    private SpassFingerprint.IdentifyListener listener;
 %0A%0A    @O
@@ -3051,33 +3051,47 @@
 0000);%0A%0A        
-m
+listener = new 
 SpassFingerprint
@@ -3091,29 +3091,24 @@
 erprint.
-start
 Identify
 WithDial
@@ -3103,189 +3103,180 @@
 tify
-WithDialog(this.cordova.getActivity().getApplicationContext(), listener, false);%0A%0A        private SpassFingerprint.IdentifyListener listener = new SpassFingerprint.IdentifyListener(
+Listener() %7B%0A            @Override%0A            public void onFinished(int eventStatus) %7B%0A                if (eventStatus == SpassFingerprint.STATUS_AUTHENTIFICATION_SUCCESS
 ) %7B%0A
@@ -3291,149 +3291,449 @@
     
+  
-@Override%0A            public void onFinished(int eventStatus) %7B%0A                callbackContext.success(eventStatus);%0A            %7D%0A        %7D
+      callbackContext.success();%0A                %7D else if (eventStatus == SpassFingerprint.STATUS_AUTHENTIFICATION_PASSWORD_SUCCESS) %7B%0A                    callbackContext.success();%0A                %7D else %7B%0A                    callbackContext.error(%22Error%22);%0A                %7D%0A                %0A            %7D%0A        %7D;%0A%0A        mSpassFingerprint.startIdentifyWithDialog(this.cordova.getActivity().getApplicationContext(), listener, false)
 ;%0A  

reorganize variable declaration