// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.firebase.messaging;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.google.firebase.messaging.RemoteMessage;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

public class FlutterFirebaseMessagingReceiver extends BroadcastReceiver {
    private static final String TAG = "FLTFireMsgReceiver";
    static HashMap<String, RemoteMessage> notifications = new HashMap<>();

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "broadcast received for message");
        if (ContextHolder.getApplicationContext() == null) {
            ContextHolder.setApplicationContext(context.getApplicationContext());
        }

        if (intent.getExtras() == null) {
            Log.d(
                    TAG,
                    "broadcast received but intent contained no extras to process RemoteMessage. Operation cancelled.");
            return;
        }

        RemoteMessage remoteMessage = new RemoteMessage(intent.getExtras());

        // Store the RemoteMessage if the message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            notifications.put(remoteMessage.getMessageId(), remoteMessage);
            FlutterFirebaseMessagingStore.getInstance().storeFirebaseMessage(remoteMessage);
        }

        //  |-> ---------------------
        //      App in Foreground
        //   ------------------------
        if (FlutterFirebaseMessagingUtils.isApplicationForeground(context)) {
            Intent onMessageIntent = new Intent(FlutterFirebaseMessagingUtils.ACTION_REMOTE_MESSAGE);
            onMessageIntent.putExtra(FlutterFirebaseMessagingUtils.EXTRA_REMOTE_MESSAGE, remoteMessage);
            LocalBroadcastManager.getInstance(context).sendBroadcast(onMessageIntent);
            return;
        }

        //  |-> ---------------------
        //    App in Background/Quit
        //   ------------------------


        if (remoteMessage.getData().containsKey("is_call")) {
            Intent intent12 = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            context.startActivity(intent12);
            try {
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        Intent onMessageIntent = new Intent(FlutterFirebaseMessagingUtils.ACTION_REMOTE_MESSAGE);
                        onMessageIntent.putExtra(FlutterFirebaseMessagingUtils.EXTRA_REMOTE_MESSAGE, remoteMessage);
                        LocalBroadcastManager.getInstance(context).sendBroadcast(onMessageIntent);
                    }
                }, 5000);
            } catch (Exception e) {
                Log.e("AppCallingException", e.getMessage());
            }
            // super.onMessageReceived(remoteMessage);
        } //else
        //    super.onMessageReceived(remoteMessage);
//
//        if (remoteMessage.getData().containsKey("is_call")) {
//            Log.e("AppCalling", "started...");
//            Intent intent12 = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
//            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//            context.startActivity(intent12);
//            try {
//                Log.e("AppCalling", "going to sleep...");
//                TimeUnit.SECONDS.sleep(10);
//                Log.e("AppCalling", "sendig to broadcast receiver...");
//                Log.e("AppCalling:DAta", remoteMessage.getData().containsKey("is_call") + "");
//                Intent onBackgroundMessageIntent =
//                        new Intent(context, FlutterFirebaseMessagingBackgroundService.class);
//                onBackgroundMessageIntent.putExtra(
//                        FlutterFirebaseMessagingUtils.EXTRA_REMOTE_MESSAGE, remoteMessage);
//                FlutterFirebaseMessagingBackgroundService.enqueueMessageProcessing(
//                        context, onBackgroundMessageIntent);
//                //return;
//            } catch (Exception e) {
//                Log.e("AppCallingException", e.getMessage());
//            }
//
//        }


//        Intent onBackgroundMessageIntent =
//                new Intent(context, FlutterFirebaseMessagingBackgroundService.class);
//        onBackgroundMessageIntent.putExtra(
//                FlutterFirebaseMessagingUtils.EXTRA_REMOTE_MESSAGE, remoteMessage);
//        FlutterFirebaseMessagingBackgroundService.enqueueMessageProcessing(
//                context, onBackgroundMessageIntent);
    }
}
