package com.casic.dcms.utils;

import android.annotation.SuppressLint;
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.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;

import com.casic.dcms.R;

public class NotificationHelper {
    private Context mContext;
    @SuppressLint("StaticFieldLeak")
    private static NotificationHelper notificationHelper;
    private NotificationManager notificationManager;

    private NotificationHelper(Context context) {
        this.mContext = context;
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    /**
     * 双重锁单例
     */
    public static NotificationHelper getInstance(Context context) {
        if (notificationHelper == null) {
            synchronized (NotificationHelper.class) {
                if (notificationHelper == null) {
                    notificationHelper = new NotificationHelper(context);
                }
            }
        }
        return notificationHelper;
    }

    private Notification.Builder createBuilder() {
        Notification.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String name = mContext.getResources().getString(R.string.app_name);
            //创建渠道
            String id = name + "_DefaultChannel";
            NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
//            mChannel.setSound(Uri.parse(""), Notification.AUDIO_ATTRIBUTES_DEFAULT);
            mChannel.setShowBadge(true);
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300});
            mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);//设置锁屏可见
            notificationManager.createNotificationChannel(mChannel);
            builder = new Notification.Builder(mContext, id);
        } else {
            builder = new Notification.Builder(mContext);
        }
        return builder;
    }

    /**
     * 创建常驻状态栏通知
     */
    public Notification createKeepAlive() {
        Notification.Builder builder = createBuilder();
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.normal);
        builder.setContentTitle("智慧城管")
                .setContentText("智慧城管运行中")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.app_logo)
                .setLargeIcon(bitmap);
        Notification notification = builder.build();
        notification.flags = Notification.FLAG_NO_CLEAR;
        return notification;
    }

    /**
     * 创建有动作的默认通知
     *
     * @param title       通知标题
     * @param message     通知内容
     * @param targetClass 点击通知之后跳转的页面
     */
    public Notification createDefault(String title, String message, String caseId, Class<?> targetClass) {
        Notification.Builder builder = createBuilder();
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.normal);
        Intent intent = new Intent(mContext, targetClass);
        intent.putExtra("id", caseId);
        builder.setContentTitle(title)
                .setContentText(message)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.normal_small)
                .setLargeIcon(bitmap)
                .setDefaults(Notification.DEFAULT_ALL)
                .setPriority(Notification.PRIORITY_MAX) //设置通知的优先级最高
                .setContentIntent(PendingIntent.getActivity(mContext, 0, intent, 0))
                .setAutoCancel(true);
        Notification notification = builder.build();
        notification.visibility = Notification.VISIBILITY_PUBLIC;//设置锁屏可见
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        return notification;
    }

    //显示通知
    public void push(int notificationId, Notification notification) {
        if (notificationManager != null) {
            notificationManager.notify(notificationId, notification);
        }
    }

    //取消通知
    public void cancel(int notificationId) {
        if (notificationManager != null) {
            notificationManager.cancel(notificationId);
        }
    }
}
