package cgmblelib.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.mytatvarnsdk.R;

/**
 * Created by minyuchun on 2019/6/18.
 * 通知工具
 * @author minyuchun
 */
public class NotificationTool {
    /**前台服务通知组的标识，不能为0*/
    private static final int mNotificationServiceId = 1;
    /**通知组的id*/
    private static final String mChannelId = "POCTech";
    /**通知组名称,用户可见的名称*/
    private static final String mChannelName = "Warning Channel";
    /**通知组,在Android 8 时需要添加*/
    private NotificationChannel mChannel = null;
    /**获取通知控制器*/
    private NotificationManager manager = null;

    private NotificationTool() {}

    private static class SingletonInstance {
        private static final NotificationTool INSTANCE = new NotificationTool();
    }

    public static NotificationTool getInstance() {
        return SingletonInstance.INSTANCE;
    }

    /**
     * 开启前台服务通知
     * @param service 服务
     */
    public void createServiceNotification(Service service){
        try{
            getNotificationManager(service);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                createNotificationChannel(service);
            }
            NotificationCompat.Builder builder = new NotificationCompat.Builder(service, mChannelId);
            builder
                    .setAutoCancel(true)
                    .setOngoing(true)
                    .setWhen(System.currentTimeMillis())
                    .setPriority(NotificationCompat.PRIORITY_LOW)
                    .setVisibility(NotificationCompat.VISIBILITY_SECRET);
            service.startForeground(mNotificationServiceId, builder.build());
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取通知控制器
     * @param context 上下文
     */
    private void getNotificationManager(Context context){
        if (manager == null){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                manager = context.getSystemService(NotificationManager.class);
            } else {
                manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            }
        }
    }

    /**
     * 创建通知组,Android 8(26) 才需要增加,
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void createNotificationChannel(@NonNull Context context){
        try {
            if (manager != null){
                if (mChannel == null){
                    mChannel = new NotificationChannel(mChannelId, mChannelName,NotificationManager.IMPORTANCE_HIGH);
                    //设置通知渠道的重要性，最重要级别
                    mChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
                    mChannel.setSound(null,null);
                    //不震动
                    mChannel.enableVibration(false);
                    //通知可以显示在锁定屏幕上
                    mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
                    //使用呼吸灯
                    mChannel.enableLights(true);
                    //呼吸灯颜色
                    mChannel.setLightColor(context.getColor(R.color.colorPrimary));
                    mChannel.setBypassDnd(true);
                    //不显示角标
                    mChannel.setShowBadge(false);
                    //指定用户在系统设置中看到的说明
                    mChannel.setDescription("In the process of monitoring abnormal reminder, please be sure to turn on");
                    //填充通知渠道
                    manager.createNotificationChannel(mChannel);
                }
            }
        } catch (Exception e){
            e.printStackTrace();
            mChannel = null;
        }
    }

}
