package com.appvuze.android.plugin;

import com.appvuze.android.sdk.appVuze;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class CDVAppVuze extends CordovaPlugin
{
    private static final String ACTION_INITIALIZE_SDK = "initializeSdk";
    private static final String ACTION_BEGIN_HELP_SESSION = "beginHelpSession";
    private static final String ACTION_SET_NETWORK_CONNECTIVITY_REQUIRED_MESSAGE = "setNetworkConnectivityRequiredMessage";
    private static final String ACTION_SET_IN_APP_SUPPORT_SERVER_UNAVAILABLE_MESSAGE = "setInAppSupportServiceUnavailableMessage";
    private static final String ACTION_SET_INCOMING_SUPPORT_CALL_MESSAGE = "setIncomingSupportCallMessage";
    private static final String ACTION_SET_CONFIRM_DISCONNECT_FROM_SUPPORT_MESSAGE = "setConfirmDisconnectFromSupportMessage";
    private static final String ACTION_SET_ENDED_SUPPORT_CALL_MESSAGE = "setEndedSupportCallMessage";
    private static final String ACTION_SET_WELCOME_MESSAGE = "setWelcomeMessage";
    private static final String ACTION_SET_RECONNECT_TO_HELP_SESSION_MESSAGE = "setReconnectToHelpSessionMessage";
    private static final String ACTION_SET_APPLICATION_UPDATE_REQUIRED_MESSAGE = "setApplicationUpdateRequiredMessage";
    // If you add a line below this, please update the array below!

    // Keep this table in sync with action strings above!
    private static final String [] ALL_ACTIONS = {
        ACTION_INITIALIZE_SDK,
        ACTION_BEGIN_HELP_SESSION,
        ACTION_SET_NETWORK_CONNECTIVITY_REQUIRED_MESSAGE,
        ACTION_SET_IN_APP_SUPPORT_SERVER_UNAVAILABLE_MESSAGE,
        ACTION_SET_INCOMING_SUPPORT_CALL_MESSAGE,
        ACTION_SET_CONFIRM_DISCONNECT_FROM_SUPPORT_MESSAGE,
        ACTION_SET_ENDED_SUPPORT_CALL_MESSAGE,
        ACTION_SET_WELCOME_MESSAGE,
        ACTION_SET_RECONNECT_TO_HELP_SESSION_MESSAGE,
        ACTION_SET_APPLICATION_UPDATE_REQUIRED_MESSAGE,
    };

    @Override
    public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
        for (String supportedAction : ALL_ACTIONS) {
            if (action.equals(supportedAction)) {
                cordova.getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        try {
                            executeOnUiThread(action, args, callbackContext);
                            callbackContext.success();
                        } catch (JSONException e) {}
                    }
                });
            }
        }

        return false;
    }

    private void executeOnUiThread(String action, JSONArray args, CallbackContext callbackContext) throws JSONException
    {
        if (action.equals(ACTION_INITIALIZE_SDK))
        {
            // Replace YOUR_APPLICATION_KEY with a key obtained from https://appvuze.io dashboard
            // Remember to enclose application key string in ""
            appVuze.initializeSdk(cordova.getActivity(), YOUR_APPLICATION_KEY);
        }
        else if (action.equals(ACTION_BEGIN_HELP_SESSION))
        {
            String personName = args.length() > 0 ? args.getString(0) : null;
            JSONObject customData = args.length() > 1 ? args.getJSONObject(1) : null;
            Map<String, Object> customDataMap = null;

            if (customData != null) {
                Iterator<String> keys = customData.keys();
                customDataMap = new HashMap<String, Object>();

                while (keys.hasNext()) {
                    String key = keys.next();
                    Object value = customData.get(key);
                    customDataMap.put(key, value);
                }
            }

            appVuze.beginHelpSession(cordova.getActivity(), personName, customDataMap);
        }
        else if (action.equals(ACTION_SET_NETWORK_CONNECTIVITY_REQUIRED_MESSAGE))
        {
            String setNetworkConnectivityRequiredMessage = args.getString(0);
            appVuze.setNetworkConnectivityRequiredMessage(setNetworkConnectivityRequiredMessage);
        }
        else if (action.equals(ACTION_SET_IN_APP_SUPPORT_SERVER_UNAVAILABLE_MESSAGE))
        {
            String setInAppSupportServiceUnavailableMessage = args.getString(0);
            appVuze.setInAppSupportServiceUnavailableMessage(setInAppSupportServiceUnavailableMessage);
        }
        else if (action.equals(ACTION_SET_INCOMING_SUPPORT_CALL_MESSAGE))
        {
            String setIncomingSupportCallMessage = args.getString(0);
            appVuze.setIncomingSupportCallMessage(setIncomingSupportCallMessage);
        }
        else if (action.equals(ACTION_SET_CONFIRM_DISCONNECT_FROM_SUPPORT_MESSAGE))
        {
            String setConfirmDisconnectFromSupportMessage = args.getString(0);
            appVuze.setConfirmDisconnectFromSupportMessage(setConfirmDisconnectFromSupportMessage);
        }
        else if (action.equals(ACTION_SET_ENDED_SUPPORT_CALL_MESSAGE))
        {
            String setEndedSupportCallMessage = args.getString(0);
            appVuze.setEndedSupportCallMessage(setEndedSupportCallMessage);
        }
        else if (action.equals(ACTION_SET_WELCOME_MESSAGE))
        {
            String setWelcomeMessage = args.getString(0);
            appVuze.setWelcomeMessage(setWelcomeMessage);
        }
        else if (action.equals(ACTION_SET_RECONNECT_TO_HELP_SESSION_MESSAGE))
        {
            String setReconnectToHelpSessionMessage = args.getString(0);
            appVuze.setReconnectToHelpSessionMessage(setReconnectToHelpSessionMessage);
        }
        else if (action.equals(ACTION_SET_APPLICATION_UPDATE_REQUIRED_MESSAGE))
        {
            String setApplicationUpdateRequiredMessage = args.getString(0);
            appVuze.setApplicationUpdateRequiredMessage(setApplicationUpdateRequiredMessage);
        }
    }
}
