package by.chemerisuk.cordova.firebase;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import by.chemerisuk.cordova.support.CordovaMethod;
import by.chemerisuk.cordova.support.ReflectiveCordovaPlugin;

import com.google.firebase.analytics.FirebaseAnalytics;

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

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

import java.util.Iterator;


public class FirebaseAnalyticsPlugin extends ReflectiveCordovaPlugin {
    private static final String TAG = "FirebaseAnalyticsPlugin";

    private FirebaseAnalytics firebaseAnalytics;

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

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

        this.firebaseAnalytics = FirebaseAnalytics.getInstance(context);
    }

    @CordovaMethod
    private void logEvent(String name, JSONObject params, CallbackContext callbackContext) throws JSONException {
        Bundle bundle = new Bundle();
        Iterator iter = params.keys();

        while (iter.hasNext()) {
            String key = (String) iter.next();
            Object value = params.get(key);

            if (value instanceof Integer || value instanceof Double) {
                bundle.putFloat(key, ((Number) value).floatValue());
            } else {
                bundle.putString(key, value.toString());
            }
        }

        this.firebaseAnalytics.logEvent(name, bundle);

        callbackContext.success();
    }

    @CordovaMethod
    private void setUserId(String userId, CallbackContext callbackContext) {
        this.firebaseAnalytics.setUserId(userId);

        callbackContext.success();
    }

    @CordovaMethod
    private void setUserProperty(String name, String value, CallbackContext callbackContext) {
        this.firebaseAnalytics.setUserProperty(name, value);

        callbackContext.success();
    }
    
    @CordovaMethod
    private void resetAnalyticsData(CallbackContext callbackContext) {
        this.firebaseAnalytics.resetAnalyticsData();

        callbackContext.success();
    }

    @CordovaMethod
    private void setEnabled(boolean enabled, CallbackContext callbackContext) {
        this.firebaseAnalytics.setAnalyticsCollectionEnabled(enabled);

        callbackContext.success();
    }

    @CordovaMethod(ui = true)
    private void setCurrentScreen(String screenName, CallbackContext callbackContext) {
        firebaseAnalytics.setCurrentScreen(
            cordova.getActivity(),
            screenName,
            null
        );

        callbackContext.success();
    }
}

@@ -1137,21 +1137,27 @@
 Iterator
+%3CString%3E
  it
-er
  = param
@@ -1184,18 +1184,16 @@
 hile (it
-er
 .hasNext
@@ -1227,21 +1227,10 @@
 y = 
-(String) iter
+it
 .nex
@@ -1316,19 +1316,95 @@
 eof 
-Integer %7C%7C 
+String) %7B%0A                bundle.putString(key, (String)value);%0A            %7D else if (
 valu
@@ -1420,14 +1420,15 @@
 eof 
-Double
+Integer
 ) %7B%0A
@@ -1457,20 +1457,18 @@
 .put
-Floa
+In
 t(key, (
 (Num
@@ -1467,37 +1467,22 @@
 y, (
-(Numb
+Integ
 er)
- 
 value)
-.floatValue())
 ;%0A  
@@ -1497,16 +1497,147 @@
   %7D else
+ if (value instanceof Double) %7B%0A                bundle.putDouble(key, (Double)value);%0A            %7D else if (value instanceof Long)
  %7B%0A     
@@ -1657,28 +1657,26 @@
 ndle.put
-Stri
+Lo
 ng(key, 
 value.to
@@ -1671,24 +1671,140 @@
 ey, 
-value.toString()
+(Long)value);%0A            %7D else %7B%0A                Log.w(TAG, %22Value for key %22 + key + %22 not one of (String, Integer, Double, Long)%22
 );%0A 
@@ -2331,20 +2331,16 @@
 ;%0A    %7D%0A
-    
 %0A    @Co

improve logEvent