package io.scrollback.neighborhoods;

import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.util.HashMap;
import java.util.Map;

public class AlertDialogModule extends ReactContextBaseJavaModule {

    final int DIALOG_OK = 0;
    final int DIALOG_CANCEL = 1;

    private Map<Integer, AlertDialog> mAlertDialogs = new HashMap<>();
    private Map<Integer, Callback> mAlertCallbacks = new HashMap<>();

    ReactApplicationContext mReactContext;
    Context mActiviyContext;

    public AlertDialogModule(ReactApplicationContext reactContext, Context activityContext) {
        super(reactContext);

        mReactContext = reactContext;
        mActiviyContext = activityContext;
    }

    @Override
    public String getName() {
        return "AlertDialogModule";
    }

    @Override
    public Map<String, Object> getConstants() {
        final Map<String, Object> constants = new HashMap<>();

        constants.put("DIALOG_OK", DIALOG_OK);
        constants.put("DIALOG_CANCEL", DIALOG_CANCEL);

        return constants;
    }

    private void sendEvent(int id, int value) {
        WritableMap params = Arguments.createMap();

        params.putInt("id", id);
        params.putInt("value", value);

        mReactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit("alertDialogEvent", params);
    }

    @ReactMethod
    public void build(
            final int id,
            @Nullable final String title, @Nullable final String message,
            @Nullable final String ok, @Nullable final String cancel) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(mActiviyContext);

        if (title != null) {
            builder.setTitle(title);
        }

        if (message != null) {
            builder.setMessage(message);
        }

        builder.setCancelable(false);

        if (ok != null) {
            builder.setPositiveButton(ok, null);
        }

        if (cancel != null) {
            builder.setNegativeButton(cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface d, int i) {
                            sendEvent(id, DIALOG_CANCEL);
                        }
                    });
        }

        final AlertDialog dialog = builder.create();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface d) {
                if (ok != null) {
                    dialog
                            .getButton(AlertDialog.BUTTON_POSITIVE)
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    sendEvent(id, DIALOG_OK);
                                }
                            });
                }
            }
        });

        mAlertDialogs.put(id, dialog);
    }

    @ReactMethod
    public void show(final int id) {
        AlertDialog dialog = mAlertDialogs.get(id);

        if (dialog != null) {
            dialog.show();
        }
    }

    @ReactMethod
    public void dismiss(final int id) {
        if (mAlertDialogs.containsKey(id)) {
            AlertDialog dialog = mAlertDialogs.get(id);

            dialog.dismiss();
            mAlertDialogs.remove(id);
        }
    }
}

@@ -3565,32 +3565,81 @@
 final int id) %7B%0A
+        if (mAlertDialogs.containsKey(id)) %7B%0A    
         AlertDia
@@ -3679,38 +3679,8 @@
 );%0A%0A
-        if (dialog != null) %7B%0A
     

Check if dialog exists in HashMap