package pl.freki.reactnativesmsandroid;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.util.ArrayList;
import java.util.UUID;

class ReactNativeSmsAndroidModule extends ReactContextBaseJavaModule {
    private final ReactApplicationContext reactContext;
    private String MESSAGE_SENT = "MESSAGE_SENT";
    private String MESSAGE_DELIVERED = "MESSAGE_DELIVERED";
    private String MESSAGE_SEND_FAILURE = "MESSAGE_SEND_FAILURE";

    ReactNativeSmsAndroidModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
    }

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

    private void sendDeliveryEvent(String messageId) {
        reactContext
            .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
            .emit(MESSAGE_DELIVERED, messageId);
    }

    @ReactMethod
    public void send(String phoneNumber, String message, final String messageId, final Promise promise) {
        if (phoneNumber == null || phoneNumber.isEmpty() || message == null ||
                message.isEmpty() || messageId == null || message.isEmpty()) {
            promise.reject(new Error("phoneNumber, message and messageId must not be empty"));

            return;
        }

        try {
            SmsManager smsManager = SmsManager.getDefault();
            ArrayList<String> messageParts = smsManager.divideMessage(message);
            final Integer messagePartsSize = messageParts.size();
            String intentId = UUID.randomUUID().toString();
            final PendingIntent messageSentIntent = PendingIntent.getBroadcast(reactContext, 0, new Intent(intentId + MESSAGE_SENT), 0);
            final PendingIntent messageDeliveredIntent = PendingIntent.getBroadcast(reactContext, 0, new Intent(intentId + MESSAGE_DELIVERED), 0);
            ArrayList<PendingIntent> messageSentIntents = new ArrayList<>(messagePartsSize);
            ArrayList<PendingIntent> messageDeliveredIntents = new ArrayList<>(messagePartsSize);

            for (int i = 0; i < messagePartsSize; i++) {
                messageSentIntents.add(messageSentIntent);
                messageDeliveredIntents.add(messageDeliveredIntent);
            }

            reactContext.registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if (getResultCode() == Activity.RESULT_OK) {
                        promise.resolve(MESSAGE_SENT);
                    } else {
                        promise.reject(new Error(MESSAGE_SEND_FAILURE));
                    }
                }
            }, new IntentFilter(intentId + MESSAGE_SENT));

            reactContext.registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    sendDeliveryEvent(messageId);
                }
            }, new IntentFilter(intentId + MESSAGE_DELIVERED));

            smsManager.sendMultipartTextMessage(phoneNumber, null, messageParts, messageSentIntents, messageDeliveredIntents);
        } catch (Exception e) {
            promise.reject(e);
        }
    }
}

@@ -904,16 +904,132 @@
 AILURE%22;
+%0A    private String PARTS_CONFIRMED = ReactNativeSmsAndroidModule.class.getPackage().toString() + %22.PartsConfirmed%22;
 %0A%0A    Re
@@ -2820,32 +2820,134 @@
 %0A            %7D%0A%0A
+            System.out.println(%22Sending message with %22 + messagePartsSize + %22 and id %22 + messageId);%0A%0A
             reac
@@ -3120,522 +3120,1726 @@
     
-if (getResultCode() == Activity.RESULT_OK) %7B%0A                        promise.resolve(MESSAGE_SENT);%0A                    %7D else %7B%0A                        promise.reject(new Error(MESSAGE_SEND_FAILURE));%0A                    %7D%0A                %7D%0A            %7D, new IntentFilter(intentId + MESSAGE_SENT));%0A%0A            reactContext.registerReceiver(new BroadcastReceiver() %7B%0A                @Override%0A                public void onReceive(Context context, Intent intent) %7B%0A                    sendDeliveryEvent(messageI
+System.out.println(%22received sent intent, message id %22 + messageId + %22 result %22 + getResultCode() + %22 partsConfirmed %22 + intent.getIntExtra(PARTS_CONFIRMED, 0));%0A%0A                    if (getResultCode() == Activity.RESULT_OK) %7B%0A                        Integer partsConfirmed = intent.getIntExtra(PARTS_CONFIRMED, 0);%0A%0A                        partsConfirmed++;%0A%0A                        if (partsConfirmed.equals(messagePartsSize)) %7B%0A                            promise.resolve(MESSAGE_SENT);%0A                            messageSentIntent.cancel();%0A%0A                            return;%0A                        %7D%0A%0A                        intent.putExtra(PARTS_CONFIRMED, partsConfirmed);%0A                    %7D else %7B%0A                        messageSentIntent.cancel();%0A                        promise.reject(new Error(MESSAGE_SEND_FAILURE));%0A                    %7D%0A                %7D%0A            %7D, new IntentFilter(intentId + MESSAGE_SENT));%0A%0A            reactContext.registerReceiver(new BroadcastReceiver() %7B%0A                @Override%0A                public void onReceive(Context context, Intent intent) %7B%0A                    System.out.println(%22received delivered intent, message id %22 + messageId + %22 result %22 + getResultCode() + %22 partsConfirmed %22 + intent.getIntExtra(PARTS_CONFIRMED, 0));%0A%0A                    Integer partsConfirmed = intent.getIntExtra(PARTS_CONFIRMED, 0);%0A%0A                    partsConfirmed++;%0A%0A                    if (partsConfirmed.equals(messagePartsSize)) %7B%0A                        sendDeliveryEvent(messageId);%0A                        messageDeliveredIntent.cancel();%0A%0A                        return;%0A                    %7D%0A%0A                    intent.putExtra(PARTS_CONFIRMED, partsConfirme
 d);%0A

revert previous commit, added debug helpers