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;

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

        return sInstance;
    }

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

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

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

                showUIPicker(actions, callbackChannel, 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 callbackChannel, 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);
                            Cobalt.publishMessage(data, callbackChannel);

                        }
                        catch (JSONException exception) {
                            exception.printStackTrace();
                        }
                    }
                })
                .setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        try {
                            JSONObject data = new JSONObject();
                            data.put("index", -1);
                            Cobalt.publishMessage(data, callbackChannel);
                        }
                        catch (JSONException exception) {
                            exception.printStackTrace();
                        }
                    }
                })
                .show();
    }
}

@@ -99,16 +99,103 @@
 erface;%0A
+import android.support.annotation.NonNull;%0Aimport android.support.annotation.Nullable;%0A
 import a
@@ -781,46 +781,13 @@
 nce(
-CobaltPluginWebContainer webContainer)
+)%0A   
  %7B%0A 
@@ -815,16 +815,24 @@
 == null)
+%0A       
  %7B%0A     
@@ -872,33 +872,32 @@
 er();%0A        %7D%0A
-%0A
         return s
@@ -908,25 +908,29 @@
 ance;%0A    %7D%0A
+    
 %0A
-
     @Overrid
@@ -957,16 +957,25 @@
 Message(
+@NonNull 
 CobaltPl
@@ -1009,27 +1009,80 @@
 er, 
-JSONObject message,
+@NonNull String action,%0A            @Nullable JSONObject data, @Nullable
  Str
@@ -1101,16 +1101,20 @@
 Channel)
+%0A   
  %7B%0A     
@@ -1111,34 +1111,32 @@
    %7B%0A        try
- %7B
 %0A            Str
@@ -1132,57 +1132,9 @@
     
-    String action = message.getString(%22action%22);%0A
+%7B
 %0A   
@@ -1172,19 +1172,16 @@
 (action)
-) %7B
 %0A       
@@ -1193,64 +1193,38 @@
     
-JSONObject
+&&
  data 
+!
 = 
-message.getJSONObject(Cobalt.kJSData);
+null)%0A            %7B
 %0A   

Update onMessage method