package com.zxhl.cms.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.os.Build;
import android.text.TextUtils;

import com.zxhl.cms.R;
import com.zxhl.cms.common.NotifyInfo;

/**
 * @Description: java类作用描述
 * @Author: lidandan
 * @CreateDate: 2022/1/25 2:47 下午
 */
public class NotificationUtil {

    public static final String LOCAL_PUSH_PARAM_DATA = "localPushParams";

    public static void notifyNotification(Context context, int nid, NotifyInfo notifyInfo) {
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        //准备intent
        try {
            Class clazz =
                    Class.forName("com.zhangxin.starlight.activity.MainActivity");

//                val clazz = Class.forName("jd.overseas.market.local_push.app.SecondActivity")

            Intent intent = new Intent(context, clazz);

            if (notifyInfo.getParam() != null && !notifyInfo.getParam().trim().isEmpty()) {
                intent.putExtra(LOCAL_PUSH_PARAM_DATA, notifyInfo.getParam());
            }

            Notification notification = null;
            PendingIntent pi =
                    PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                AudioAttributes audioAttributes = new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                        .build();
                String id = "my_local_push_channel_02";
                int importance = NotificationManager.IMPORTANCE_HIGH;
                CharSequence name = "notice";
                NotificationChannel mChannel = new NotificationChannel(id, name, importance);
                mChannel.enableLights(true);
                mChannel.setLightColor(Color.GREEN);
                mChannel.enableVibration(true);
                mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
//                mChannel.setSound(soundUri, audioAttributes);
                manager.createNotificationChannel(mChannel);
                Notification.Builder builder = new Notification.Builder(context, id);

                builder.setAutoCancel(true)
                        .setContentIntent(pi)
                        .setOngoing(false)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(
                                BitmapFactory.decodeResource(
                                        context.getResources(),
                                        R.mipmap.ic_launcher
                                )
                        )
                        .setWhen(System.currentTimeMillis());
                if (notifyInfo.getTitle() != null && !notifyInfo.getTitle().trim().isEmpty()) {
                    builder.setContentTitle(notifyInfo.getTitle());
                }
                if (notifyInfo.getSubText() != null && !notifyInfo.getSubText().trim().isEmpty()) {
                    builder.setSubText(notifyInfo.getSubText());
                }
                if (!TextUtils.isEmpty(notifyInfo.getContent())) {
                    builder.setContentText(notifyInfo.getContent());
                }
                notification = builder.build();
            } else if (Build.VERSION.SDK_INT >= 23) {
                Notification.Builder builder = new Notification.Builder(context);
                builder.setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(
                                BitmapFactory.decodeResource(
                                        context.getResources(),
                                        R.mipmap.ic_launcher
                                )
                        )
                        .setContentIntent(pi)
                        .setAutoCancel(true)
                        .setOngoing(false)
                        .setWhen(System.currentTimeMillis());
                if (notifyInfo.getSubText() != null && !notifyInfo.getSubText().trim().isEmpty()) {
                    builder.setSubText(notifyInfo.getSubText());
                }
                if (notifyInfo.getTitle() != null && !notifyInfo.getTitle().trim().isEmpty()) {
                    builder.setContentTitle(notifyInfo.getTitle());
                }
                if (!TextUtils.isEmpty(notifyInfo.getContent())) {
                    builder.setContentText(notifyInfo.getContent());
                }
                notification = builder.build();
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN &&
                    Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1
            ) {
                Notification.Builder builder = new Notification.Builder(context);
                builder.setAutoCancel(true)
                        .setContentIntent(pi)
                        .setOngoing(false)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(
                                BitmapFactory.decodeResource(
                                        context.getResources(),
                                        R.mipmap.ic_launcher
                                )
                        )
                        .setWhen(System.currentTimeMillis())
                        .setAutoCancel(true);
                if (notifyInfo.getSubText() != null && !notifyInfo.getSubText().trim().isEmpty()) {
                    builder.setSubText(notifyInfo.getSubText());
                }
                if (notifyInfo.getTitle() != null && !notifyInfo.getTitle().trim().isEmpty()) {
                    builder.setContentTitle(notifyInfo.getTitle());
                }
                if (!TextUtils.isEmpty(notifyInfo.getContent())) {
                    builder.setContentText(notifyInfo.getContent());
                }
                notification = builder.build();
            }
            if (notification != null) {
                manager.notify(nid, notification);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
