package com.clipsub.rnbottomsheet;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.telecom.Call;

import com.cocosw.bottomsheet.BottomSheet;
import com.facebook.react.bridge.ActivityEventListener;
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.ReadableArray;
import com.facebook.react.bridge.ReadableMap;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class RNBottomSheet extends ReactContextBaseJavaModule {

    private Callback shareSuccessCallback;
    private Callback shareFailureCallback;

    public RNBottomSheet(ReactApplicationContext reactContext) {
        super(reactContext);
    }

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

    @ReactMethod
    public void showBottomSheetWithOptions(ReadableMap options, final Callback onSelect) {
        ReadableArray optionArray = options.getArray("options");
        final Integer cancelButtonIndex = options.getInt("cancelButtonIndex");
        String title;
        boolean dark = false;
        BottomSheet.Builder builder;

        // Title.
        try {
            title = options.getString("title");
            builder = new BottomSheet.Builder(this.getCurrentActivity()).title(title);
        } catch (Exception e) {
            builder = new BottomSheet.Builder(this.getCurrentActivity());
        }

        // Dark theme.
        try {
          dark = options.getBoolean("dark");
          if (dark) {
            builder.darkTheme();
          }
        } catch (Exception e) {
          // Code...
        }

        // Options.
        Integer size = optionArray.size();
        for (int i = 0; i < size; i++) {
            builder.sheet(i, optionArray.getString(i));
        }

        builder.listener(new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == cancelButtonIndex) {
                    dialog.dismiss();
                } else {
                    onSelect.invoke(which);
                }
            }
        });

        builder.build().show();
    }

    @ReactMethod
    public void showShareBottomSheetWithOptions(ReadableMap options, Callback failureCallback, Callback successCallback) {
        String url = options.getString("url");
        String message = options.getString("message");
        String subject = options.getString("subject");

        List<String> items = new ArrayList<>();
        if (message != null && !message.isEmpty()) {
            items.add(message);
        }

        final Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        Uri uri = Uri.parse(url);
        if (uri != null) {
            if (uri.getScheme() != null && "data".equals(uri.getScheme().toLowerCase())) {
                shareIntent.setType("*/*");
                shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            } else {
                shareIntent.setType("text/plain");
                shareIntent.putExtra(Intent.EXTRA_EMAIL, url);
                shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
                shareIntent.putExtra(Intent.EXTRA_TEXT, message);
            }
        }

        this.shareSuccessCallback = successCallback;
        this.shareFailureCallback = failureCallback;

        if (shareIntent.resolveActivity(this.getCurrentActivity().getPackageManager()) != null) {
            this.getCurrentActivity().startActivity(Intent.createChooser(shareIntent, "Share To"));
        } else {
            failureCallback.invoke(new Exception("The app you want to share is not installed."));
        }
    }

}

@@ -781,16 +781,61 @@
 dule %7B%0A%0A
+    private static boolean isOpened = false;%0A
     priv
@@ -1201,24 +1201,109 @@
 onSelect) %7B%0A
+        if (RNBottomSheet.isOpened) return;%0A%0A        RNBottomSheet.isOpened = true;%0A%0A
         Read
@@ -2546,24 +2546,260 @@
        %7D);%0A%0A
+        builder.setOnDismissListener(new DialogInterface.OnDismissListener() %7B%0A            @Override%0A            public void onDismiss(DialogInterface dialog) %7B%0A                RNBottomSheet.isOpened = false;%0A            %7D%0A        %7D);%0A%0A
         buil

Allow only one active bottomsheet