//
//  RNNetmeraPushBroadcastReceiver.java
//  Netmera SDK
//
//  Created by Huseyin Kocoglu on 22.04.2019.
//  Copyright © 2019 Netmera. All rights reserved.
//

package com.reactlibrary;

import android.content.Context;
import android.os.Bundle;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.netmera.NetmeraPushBroadcastReceiver;
import com.netmera.NetmeraPushObject;

public class RNNetmeraPushBroadcastReceiver extends NetmeraPushBroadcastReceiver {

    @Override
    protected void onPushRegister(Context context, String gcmSenderId, String pushToken) {
        Bundle bundle = new Bundle();
        bundle.putString("gcmSenderId", gcmSenderId);
        bundle.putString("pushToken", pushToken);
        sendEvent("onPushRegister", bundle);
    }

    @Override
    protected void onPushReceive(Context context, Bundle bundle, NetmeraPushObject netmeraPushObject) {
        sendEvent("onPushReceive", bundle);
    }

    @Override
    protected void onPushOpen(Context context, Bundle bundle, NetmeraPushObject netmeraPushObject) {
        sendEvent("onPushOpen", bundle);
    }

    @Override
    protected void onPushDismiss(Context context, Bundle bundle, NetmeraPushObject netmeraPushObject) {
        sendEvent("onPushDismiss", bundle);
    }

    @Override
    protected void onPushButtonClicked(Context context, Bundle bundle, NetmeraPushObject netmeraPushObject) {
        sendEvent("onPushButtonClicked", bundle);
    }

    private void sendEvent(String eventName, Bundle bundle) {
        WritableMap map = bundle != null ? Arguments.fromBundle(bundle) : null;
        try {
            ReactContext reactContext = RNNetmeraModule.reactContext;
            reactContext
                    .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                    .emit(eventName, map);
        } catch (Exception e) {
        }
    }
}