package io.kristal.actionpicker;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;

import org.cobaltians.cobalt.plugin.CobaltAbstractPlugin;
import org.cobaltians.cobalt.Cobalt;
import org.cobaltians.cobalt.fragments.CobaltFragment;
import org.cobaltians.cobalt.plugin.CobaltPluginWebContainer;

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

import java.util.ArrayList;

public class ActionPicker extends CobaltAbstractPlugin {

    private static final String TAG = CobaltAbstractPlugin.class.getSimpleName();

    private static ActionPicker sInstance;
    private String mPluginName;

    public static CobaltAbstractPlugin getInstance(CobaltPluginWebContainer webContainer) {
        if (sInstance == null) {
            sInstance = new ActionPicker();
        }

        return sInstance;
    }

    @Override
    public void onMessage(CobaltPluginWebContainer webContainer, JSONObject message) {
        try {
            String action = message.getString("action");
            mPluginName = message.getString(Cobalt.kJSPluginName);

            if ("getAction".equals(action)) {
                JSONObject data = message.getJSONObject(Cobalt.kJSData);
                JSONArray actionsJSON = data.getJSONArray("actions");
                int actionsLength = actionsJSON.length();
                String callback = data.getString(Cobalt.kJSCallback);

                ArrayList<String> actions = new ArrayList<>(actionsLength);
                for (int i = 0; i < actionsLength; i++){
                    actions.add(actionsJSON.getString(i));
                }

                showUIPicker(actions, callback, webContainer);
            }
            else if (Cobalt.DEBUG) {
                Log.w(TAG, "onMessage: action '" + action + "' or plugin name not recognized");
            }
        }
        catch(JSONException exception) {
            if (Cobalt.DEBUG) {
                Log.e(TAG, "onMessage: wrong format, possible issues: \n" +
                        "\t- missing 'action' field or not a string,\n" +
                        "\t- missing 'data' field or not a object,\n" +
                        "\t- missing 'data.actions' field or not an array,\n" +
                        "\t- missing 'callback' field or not a string.\n");
            }
            exception.printStackTrace();
        }
    }

    private void showUIPicker(ArrayList<String> actions, final String callback, CobaltPluginWebContainer webContainer) {
        CharSequence[] items = actions.toArray(new CharSequence[actions.size()]);
        final CobaltFragment fragment = webContainer.getFragment();

        new AlertDialog.Builder(webContainer.getActivity())
                .setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        try {
                            JSONObject data = new JSONObject();
                            data.put("index", i);
                            data.put("callback", callback);
                            fragment.sendPlugin(mPluginName, data);
                        }
                        catch (JSONException exception) {
                            exception.printStackTrace();
                        }
                    }
                })
                .setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        try {
                            JSONObject data = new JSONObject();
                            data.put("index", -1);
                            data.put("callback", callback);
                            fragment.sendPlugin(mPluginName, data);
                        }
                        catch (JSONException exception) {
                            exception.printStackTrace();
                        }
                    }
                })
                .show();
    }
}

@@ -641,40 +641,8 @@
 nce;
-%0A    private String mPluginName;
 %0A%0A  
@@ -949,16 +949,40 @@
  message
+, String callbackChannel
 ) %7B%0A    
@@ -1051,75 +1051,8 @@
 n%22);
-%0A            mPluginName = message.getString(Cobalt.kJSPluginName);
 %0A%0A  
@@ -1299,78 +1299,8 @@
 h();
-%0A                String callback = data.getString(Cobalt.kJSCallback);
 %0A%0A  
@@ -1554,16 +1554,23 @@
 callback
+Channel
 , webCon
@@ -2350,16 +2350,23 @@
 callback
+Channel
 , Cobalt
@@ -2972,108 +2972,55 @@
     
-data.put(%22callback%22, callback);%0A                            fragment.sendPlugin(mPluginName, data
+Cobalt.publishMessage(data, callbackChannel
 );%0A
+%0A
     
@@ -3584,105 +3584,51 @@
     
-data.put(%22callback%22, callback);%0A                            fragment.sendPlugin(mPluginName, data
+Cobalt.publishMessage(data, callbackChannel
 );%0A 

Update ActionPicker.java