package by.chemerisuk.cordova.firebase;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

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


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

    private FirebaseRemoteConfig firebaseRemoteConfig;

    @Override
    protected void pluginInitialize() {
        Log.d(TAG, "Starting Firebase Remote Config plugin");

        Context context = this.cordova.getActivity().getApplicationContext();

        this.firebaseRemoteConfig = FirebaseRemoteConfig.getInstance(context);
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if ("update".equals(action)) {
            update(args.getLong(0), callbackContext);
        } else if ("getBoolean".equals(action)) {
            getBoolean(args.getString(0), args.getString(1));
        } else if ("getByteArray".equals(action)) {
            getByteArray(args.getString(0), args.getString(1));
        } else if ("getDouble".equals(action)) {
            getDouble(args.getString(0), args.getString(1));
        } else if ("getLong".equals(action)) {
            getLong(args.getString(0), args.getString(1));
        } else if ("getString".equals(action)) {
            getString(args.getString(0), args.getString(1));
        } else {
            return false;
        }

        return true;
    }

    private void update(long ttlSeconds, final CallbackContext callbackContext) throws JSONException {
        Activity activity = this.cordova.getActivity();

        if (ttlSeconds == 0) {
            // App should use developer mode to fetch values from the service
            firebaseRemoteConfig.setConfigSettings(
                new FirebaseRemoteConfigSettings.Builder()
                    .setDeveloperModeEnabled(BuildConfig.DEBUG)
                    .build()
            );
        }

        firebaseRemoteConfig.fetch(ttlSeconds);
            .addOnCompleteListener(activity, new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()) {
                        firebaseRemoteConfig.activateFetched();

                        callbackContext.success();
                    } else {
                        callbackContext.error(task.getException());
                    }
                }
            });
    }

    private void getBoolean(String key, String namespace, CallbackContext callbackContext) {
        if (namespace.isEmpty()) {
            callbackContext.success(firebaseRemoteConfig.getBoolean(key))
        } else {
            callbackContext.success(firebaseRemoteConfig.getBoolean(key, namespace));
        }
    }

    private void getByteArray(String key, String namespace, CallbackContext callbackContext) {
        if (namespace.isEmpty()) {
            callbackContext.success(firebaseRemoteConfig.getByteArray(key))
        } else {
            callbackContext.success(firebaseRemoteConfig.getByteArray(key, namespace));
        }
    }

    private void getDouble(String key, String namespace, CallbackContext callbackContext) {
        if (namespace.isEmpty()) {
            callbackContext.success(firebaseRemoteConfig.getDouble(key))
        } else {
            callbackContext.success(firebaseRemoteConfig.getDouble(key, namespace));
        }
    }

    private void getLong(String key, String namespace, CallbackContext callbackContext) {
        if (namespace.isEmpty()) {
            callbackContext.success(firebaseRemoteConfig.getLong(key))
        } else {
            callbackContext.success(firebaseRemoteConfig.getLong(key, namespace));
        }
    }

    private void getString(String key, String namespace, CallbackContext callbackContext) {
        if (namespace.isEmpty()) {
            callbackContext.success(firebaseRemoteConfig.getString(key))
        } else {
            callbackContext.success(firebaseRemoteConfig.getString(key, namespace));
        }
    }
}

@@ -34,16 +34,71 @@
 ebase;%0A%0A
+import android.app.Activity;%0Aimport android.util.Log;%0A%0A
 import c
@@ -775,87 +775,8 @@
 );%0A%0A
-        Context context = this.cordova.getActivity().getApplicationContext();%0A%0A
     
@@ -844,15 +844,8 @@
 nce(
-context
 );%0A 
@@ -1172,32 +1172,49 @@
 rgs.getString(1)
+, callbackContext
 );%0A        %7D els
@@ -1305,32 +1305,49 @@
 rgs.getString(1)
+, callbackContext
 );%0A        %7D els
@@ -1432,32 +1432,49 @@
 rgs.getString(1)
+, callbackContext
 );%0A        %7D els
@@ -1555,32 +1555,49 @@
 rgs.getString(1)
+, callbackContext
 );%0A        %7D els
@@ -1690,16 +1690,33 @@
 tring(1)
+, callbackContext
 );%0A     
@@ -2221,25 +2221,12 @@
 led(
-BuildConfig.DEBUG
+true
 )%0A  
@@ -2324,17 +2324,16 @@
 Seconds)
-;
 %0A       
@@ -2472,17 +2472,8 @@
 ete(
-@NonNull 
 Task
@@ -2744,16 +2744,29 @@
 eption()
+.getMessage()
 );%0A     
@@ -3020,24 +3020,25 @@
 oolean(key))
+;
 %0A        %7D e
@@ -3347,24 +3347,25 @@
 eArray(key))
+;
 %0A        %7D e
@@ -3670,24 +3670,25 @@
 Double(key))
+;
 %0A        %7D e
@@ -3982,32 +3982,33 @@
 ig.getLong(key))
+;
 %0A        %7D else 
@@ -4308,16 +4308,17 @@
 ng(key))
+;
 %0A       

fix typos