package gr.scify.icsee.controllers;

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

import com.android.volley.Request;
import com.android.volley.toolbox.JsonObjectRequest;

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

import gr.scify.icsee.ICSeeApplication;
import gr.scify.icsee.data.LoginRepository;
import gr.scify.icsee.interfaces.IAnalyticsLogger;

public class GeneralAnalyticsLogger implements IAnalyticsLogger {

    @Override
    public void logEvent(Context c, String eventName, String eventValue, Bundle additionalPayload) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name", eventName);
            jsonObject.put("source", "icsee_mobile");
            jsonObject.put("action", eventName);
            jsonObject.put("value", eventValue);
            jsonObject.put("payload", additionalPayload);
            jsonObject.put("token", LoginRepository.getInstance().getStoredAuthToken(c));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        // error
        JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, "https://memoristudio.scify.org/api/analytics/store/icsee", jsonObject,
                response -> {
                },
                Throwable::printStackTrace
        );
        ICSeeApplication.queue.add(postRequest);
    }
}

@@ -234,24 +234,47 @@
 SONObject;%0A%0A
+import java.util.Set;%0A%0A
 import gr.sc
@@ -297,24 +297,24 @@
 pplication;%0A
-
 import gr.sc
@@ -880,16 +880,29 @@
 yload%22, 
+bundleToJSON(
 addition
@@ -911,16 +911,17 @@
 Payload)
+)
 ;%0A      
@@ -1358,16 +1358,16 @@
      );%0A
-
         
@@ -1412,11 +1412,452 @@
 );%0A    %7D
+%0A%0A    protected JSONObject bundleToJSON(Bundle bundle) %7B%0A        JSONObject json = new JSONObject();%0A        Set%3CString%3E keys = bundle.keySet();%0A        for (String key : keys) %7B%0A            try %7B%0A                // json.put(key, bundle.get(key)); see edit below%0A                json.put(key, bundle.get(key));%0A            %7D catch (JSONException e) %7B%0A                //Handle exception here%0A            %7D%0A        %7D%0A        return json;%0A    %7D
 %0A%7D%0A

Convert bundle to JSON