package com.tofly.ghrq_route.common.util.Notification;

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.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.core.app.NotificationManagerCompat;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.common.util.dialog.DialogUtil;

public class NotificationUtil {

    public static boolean isNotificationEnabled(Context context) {
        if (context == null)
            context = BaseApp.getInstance().getApplicationContext();
        return NotificationManagerCompat.from(context).areNotificationsEnabled();
    }

    public static void gotoSystemSetting() {
        Context context = BaseApp.getInstance().getApplicationContext();
        String packageName = context.getPackageName();
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        localIntent.setData(Uri.fromParts("package", packageName, null));
        context.startActivity(localIntent);
    }

    public static void showPermissionDialog(Context context) {
        String title = "通知权限";
        String message = "检测到您没有打开通知权限，是否去打开";
        DialogUtil.showAlertWithConfirmCancelButton(context, title, message, true, (dialogInterface, i) -> {
            gotoSystemSetting();
        });
    }

    public static Notification createNotification(Context context, String channelName, PendingIntent pendIntent,
                                                                 String title, String message) {
        Notification.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager =
                    (NotificationManager) BaseApp.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(context.getPackageName(), channelName,
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            channel.enableVibration(true);
            Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            channel.setSound(uri, null);
            notificationManager.createNotificationChannel(channel);
            builder = new Notification.Builder(context, channel.getId());
        }else {
            builder = new Notification.Builder(context);
        }
        builder.setContentIntent(pendIntent);
        builder.setSmallIcon(R.mipmap.logo);
        builder.setContentTitle(title);
        builder.setContentText(message);
        Notification notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notification.defaults = Notification.DEFAULT_ALL;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.vibrate = new long[]{0};
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;

        return notification;
    }
}
