package com.blanyal.remindme;

//package com.example.application.alarmmanagerproject;
import androidx.core.app.NotificationCompat;

import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Build;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;




public class NotificationHelper extends ContextWrapper {
    public static final String channelID = "channelID";
    public static final String channelName = "Channel Name";
    String mTitle;
    PendingIntent mClick;
    private NotificationManager mManager;

    public NotificationHelper(Context base,Intent intent) {
        super(base);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannel();
        }
        int mReceivedID = Integer.parseInt(intent.getStringExtra(ReminderEditActivity.EXTRA_REMINDER_ID));
//
        //Get notification title from Reminder Database
        ReminderDatabase rb = new ReminderDatabase(base);
        Reminder reminder = rb.getReminder(mReceivedID);
        mTitle = reminder.getTitle();

        // Create intent to open ReminderEditActivity on notification click
        Intent editIntent = new Intent(base, ReminderEditActivity.class);
        editIntent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(mReceivedID));
        mClick = PendingIntent.getActivity(base, mReceivedID, editIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createChannel() {
        NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);

        getManager().createNotificationChannel(channel);
    }

    public NotificationManager getManager() {
        if (mManager == null) {
            mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }

        return mManager;
    }

    public NotificationCompat.Builder getChannelNotification() {
        return new NotificationCompat.Builder(getApplicationContext(), channelID)


                .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher))
                  .setSmallIcon(R.drawable.ic_alarm_on_white_24dp)
                  .setContentTitle(getApplicationContext().getResources().getString(R.string.app_name))
                  .setTicker(mTitle)
                  .setContentText(mTitle)
                  .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM))
                  .setContentIntent(mClick)
                  .setAutoCancel(true)
                  .setOnlyAlertOnce(true);
    }
}
