package com.tenlionsoft.baselib.core.network.update.utils;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;

import com.tenlionsoft.baselib.R;
import com.tenlionsoft.baselib.core.network.update.service.DownloadService;

import androidx.core.app.NotificationCompat;

import static android.app.Notification.VISIBILITY_SECRET;

public class NotificationHelper {

    private NotificationManager manager;

    private Context mContext;

    public static final String CHANNEL_ID = "com.tengshisoft.citygovernance_channel_id";
    private static final String CHANNEL_NAME = "com.tengshisoft.citygovernance_channel_name";
    private int notification_id = (int) (Math.random() * (800 - 1) + 1);

    public NotificationHelper(Context context) {
        this.mContext = context;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            channel.canBypassDnd();
            channel.enableLights(true);
            channel.setLockscreenVisibility(VISIBILITY_SECRET);
            channel.setLightColor(Color.RED);
            channel.canShowBadge();
            channel.enableVibration(true);
            channel.getAudioAttributes();
            channel.getGroup();
            channel.setBypassDnd(true);
            channel.setVibrationPattern(new long[]{100, 100, 200});
            channel.shouldShowLights();
            getManager().createNotificationChannel(channel);
        }
    }

    /**
     * 显示Notification
     */
    public void showNotification(String content, String apkUrl) {
        Intent myIntent = new Intent(mContext, DownloadService.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        myIntent.putExtra(Constants.APK_DOWNLOAD_URL, apkUrl);
        PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = getNofity(content)
            .setContentIntent(pendingIntent);
        getManager().notify(notification_id, builder.build());
    }

    /**
     * 不断调用次方法通知通知栏更新进度条
     */
    public void updateProgress(int progress) {
        String text = mContext.getString(R.string.android_auto_update_download_progress, progress);
        PendingIntent pendingintent = PendingIntent.getActivity(mContext, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = getNofity(text)
            .setProgress(100, progress, false)
            .setContentIntent(pendingintent);

        getManager().notify(notification_id, builder.build());
    }

    private NotificationCompat.Builder getNofity(String text) {
        return new NotificationCompat.Builder(mContext.getApplicationContext(), CHANNEL_ID)
            .setTicker(mContext.getString(R.string.android_auto_update_notify_ticker))
            .setContentTitle("市域治理")
            .setContentText(text)
            .setSmallIcon(R.drawable.app_logo_small)
            .setAutoCancel(true)
            .setOnlyAlertOnce(true)
            .setWhen(System.currentTimeMillis())
            .setPriority(NotificationCompat.PRIORITY_HIGH);

    }

    public void cancel() {
        getManager().cancel(notification_id);
    }


    private NotificationManager getManager() {
        if (manager == null) {
            manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return manager;
    }
}
