package com.centaurwarchief.smslistener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.util.Log;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

public class SmsReceiver extends BroadcastReceiver {
    private ReactApplicationContext mContext;

    private static final String EVENT = "com.centaurwarchief.smslistener:smsReceived";

    public SmsReceiver() {
        super();
    }

    public SmsReceiver(ReactApplicationContext context) {
        mContext = context;
    }

    private void receiveMessage(SmsMessage message) {
        if (mContext == null) {
            return;
        }

        if (! mContext.hasActiveCatalystInstance()) {
            return;
        }

        Log.d(
            SmsListenerPackage.TAG,
            String.format("%s: %s", message.getOriginatingAddress(), message.getMessageBody())
        );

        WritableNativeMap receivedMessage = new WritableNativeMap();

        receivedMessage.putString("originatingAddress", message.getOriginatingAddress());
        receivedMessage.putString("body", message.getMessageBody());

        mContext
            .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
            .emit(EVENT, receivedMessage);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            for (SmsMessage message : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
                receiveMessage(message);
            }

            return;
        }

        try {
            final Bundle bundle = intent.getExtras();

            if (bundle == null || ! bundle.containsKey("pdus")) {
                return;
            }

            final Object[] pdus = (Object[]) bundle.get("pdus");

            for (Object pdu : pdus) {
                receiveMessage(SmsMessage.createFromPdu((byte[]) pdu));
            }
        } catch (Exception e) {
            Log.e(SmsListenerPackage.TAG, e.getMessage());
        }
    }
}

@@ -847,16 +847,29 @@
  message
+, String body
 ) %7B%0A    
@@ -1372,38 +1372,143 @@
 %22body%22, 
-message.getMessageBody
+body.length() %3E 0 ? body : message.getMessageBody());%0A        receivedMessage.putDouble(%22timestamp%22, message.getTimestampMillis
 ());%0A%0A  
@@ -1647,24 +1647,546 @@
 ge);%0A    %7D%0A%0A
+    private void receiveMultipartMessage(SmsMessage%5B%5D messages) %7B%0A        SmsMessage sms = messages%5B0%5D;%0A        String body;%0A%0A        if (messages.length == 1 %7C%7C sms.isReplace()) %7B%0A            body = sms.getDisplayMessageBody();%0A        %7D else %7B%0A            StringBuilder bodyText = new StringBuilder();%0A%0A            for (SmsMessage message : messages) %7B%0A                bodyText.append(message.getMessageBody());%0A            %7D%0A%0A            body = bodyText.toString();%0A        %7D%0A%0A        receiveMessage(sms, body);%0A    %7D%0A%0A
     @Overrid
@@ -2330,34 +2330,32 @@
     
-for (SmsMessage m
+receiveMultipartM
 essage
- : 
+(
 Tele
@@ -2406,65 +2406,9 @@
 nt))
- %7B%0A                receiveMessage(message);%0A            %7D
+;
 %0A%0A  
@@ -2679,43 +2679,128 @@
 %22);%0A
-%0A            for (Object pdu : pdus
+            final SmsMessage%5B%5D messages = new SmsMessage%5Bpdus.length%5D;%0A%0A            for (int i = 0; i %3C pdus.length; i++
 ) %7B%0A
@@ -2815,31 +2815,30 @@
         
-receiveMessage(
+messages%5Bi%5D = 
 SmsMessa
@@ -2866,17 +2866,20 @@
 e%5B%5D) pdu
-)
+s%5Bi%5D
 );%0A     
@@ -2882,24 +2882,72 @@
            %7D
+%0A%0A            receiveMultipartMessage(messages);
 %0A        %7D c

handle multipart messages