package com.webclip.base;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.google.gson.Gson;
import com.webclip.base.GsonUtils;
import com.webclip.base.MessageInfo;
import java.util.Map;
import java.util.Random;

public class MyFirebaseMessageingService extends FirebaseMessagingService {

    public MyFirebaseMessageingService() {
    }

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Map<String, String> serviceData = remoteMessage.getData();  //后台推送数据
        if (serviceData != null && serviceData.containsKey("message")) {
            String value = serviceData.get("message");
            Gson gson = new Gson();
            MessageInfo messageInfo = gson.fromJson(value, MessageInfo.class);
            showNotification(messageInfo);
        } else {
            //收到通知 创建notify
            if (remoteMessage.getNotification() != null) {
                showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
            }
        }
    }

    private void showNotification(MessageInfo messageInfo) {
        Intent notifyIntent = new Intent(this, IndexActivity.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
            ComponentName launchComponent = null;
            launchComponent = getApplication()
                    .getPackageManager()
                    .getLaunchIntentForPackage(getApplication().getPackageName())
                    .getComponent();
            notifyIntent.setComponent(launchComponent);
        }
        notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        notifyIntent.setAction(Intent.ACTION_VIEW);
        notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 必须
        notifyIntent.putExtra("message", messageInfo);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, new Random().nextInt(10000), notifyIntent, PendingIntent.FLAG_IMMUTABLE);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channelwinway = null;
        NotificationCompat.Builder notificationBuilder = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channelwinway = new NotificationChannel(getString(R.string.app_name), "notify", NotificationManager.IMPORTANCE_DEFAULT);
            channelwinway.enableLights(true);
            channelwinway.enableVibration(true);
            notificationManager.createNotificationChannel(channelwinway);
            notificationBuilder = new NotificationCompat.Builder(this, channelwinway.getId())
                    .setSmallIcon(R.mipmap.app_logo)
                    .setContentTitle(messageInfo.getTitle())
                    .setContentText(messageInfo.getContent())
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent);
        } else {
            notificationBuilder = new NotificationCompat.Builder(this, getString(R.string.app_name))
                    .setSmallIcon(R.mipmap.app_logo)
                    .setContentTitle(messageInfo.getTitle())
                    .setContentText(messageInfo.getContent())
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent);
        }
        notificationManager.notify(0, notificationBuilder.build());
    }

    private void showNotification(String title, String body) {
        Intent notifyIntent = new Intent(this, IndexActivity.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
            ComponentName launchComponent = null;
            launchComponent = getApplication()
                    .getPackageManager()
                    .getLaunchIntentForPackage(getApplication().getPackageName())
                    .getComponent();
            notifyIntent.setComponent(launchComponent);
        }
        notifyIntent.putExtra("message", body);
        notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        notifyIntent.setAction(Intent.ACTION_VIEW);
        notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 必须
        PendingIntent pendingIntent = PendingIntent.getActivity(this, new Random().nextInt(10000), notifyIntent, PendingIntent.FLAG_IMMUTABLE);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channelwinway = null;
        NotificationCompat.Builder notificationBuilder = null;
        MessageInfo messageInfo = GsonUtils.getObjFromJSON(body, MessageInfo.class);
        if (messageInfo != null) {
            body = messageInfo.getContent();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channelwinway = new NotificationChannel(getString(R.string.app_name), "notify", NotificationManager.IMPORTANCE_DEFAULT);
            channelwinway.enableLights(true);
            channelwinway.enableVibration(true);
            notificationManager.createNotificationChannel(channelwinway);
            notificationBuilder = new NotificationCompat.Builder(this, channelwinway.getId())
                    .setSmallIcon(R.mipmap.app_logo)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent);
        } else {
            notificationBuilder = new NotificationCompat.Builder(this, getString(R.string.app_name))
                    .setSmallIcon(R.mipmap.app_logo)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent);
        }
        notificationManager.notify(0, notificationBuilder.build());
    }
}
