/*

Copyright (C) 2016 The Android Open Source Project

Modifications copyright (C) 2018 Yanko Georgiev

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   */

package com.batyanko.strokeratecoach.Utils;

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.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;

import com.batyanko.strokeratecoach.R;
import com.batyanko.strokeratecoach.WaveActivity;

public class NotificationUtils {
    private static final int WORKOUT_NOTIFICATION_ID = 8210;
    /**
     * This pending intent id is used to uniquely reference the pending intent
     */
    private static final int WORKOUT_PENDING_INTENT_ID = 4921;
    /**
     * This notification channel id is used to link notifications to this channel
     */
    private static final String WORKOUT_NOTIFICATION_CHANNEL_ID = "workout-notification-channel";


    public static void clearAllNotifications(Context context) {
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager == null) {
            return;
        }notificationManager.cancelAll();
    }

    public static void showWorkoutNotification(Context context) {
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager == null) {
            return;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(
                    WORKOUT_NOTIFICATION_CHANNEL_ID,
                    context.getString(R.string.main_notification_channel_name),
                    NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(mChannel);
        }
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,WORKOUT_NOTIFICATION_CHANNEL_ID)
                .setColor(ContextCompat.getColor(context, R.color.blueAppColor))
                .setSmallIcon(R.drawable.ic_icon2_transparent)
                .setLargeIcon(largeIcon(context))
                .setContentTitle(context.getString(R.string.app_name))
                .setContentText(context.getString(R.string.workout_in_progress))
//                .setStyle(new NotificationCompat.BigTextStyle().bigText(context.getString(R.string.charging_reminder_notification_body)))
                .setDefaults(Notification.DEFAULT_LIGHTS)
                .setContentIntent(contentIntent(context))
//                .addAction(stopWorkoutAction(context))
                .setOngoing(true)
                .setAutoCancel(false);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
        }
        notificationManager.notify(WORKOUT_NOTIFICATION_ID, notificationBuilder.build());
    }


    private static PendingIntent contentIntent(Context context) {
        Intent startActivityIntent = new Intent(context, WaveActivity.class);
        return PendingIntent.getActivity(
                context,
                WORKOUT_PENDING_INTENT_ID,
                startActivityIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    }

    //TODO add stop action
/*    private static NotificationCompat.Action stopWorkoutAction(Context context) {
        Intent incrementWaterCountIntent = new Intent(context, WaterReminderIntentService.class);
        incrementWaterCountIntent.setAction(ReminderTasks.ACTION_INCREMENT_WATER_COUNT);
        PendingIntent incrementWaterPendingIntent = PendingIntent.getService(
                context,
                ACTION_DRINK_PENDING_INTENT_ID,
                incrementWaterCountIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationCompat.Action drinkWaterAction = new NotificationCompat.Action(R.drawable.ic_local_drink_black_24px,
                "I did it!",
                incrementWaterPendingIntent);
        return drinkWaterAction;
    }*/

    private static Bitmap largeIcon(Context context) {
        Resources res = context.getResources();
        Bitmap largeIcon = BitmapFactory.decodeResource(res, R.mipmap.ic_launcher);
        return largeIcon;
    }
}

@@ -3999,32 +3999,98 @@
 ctivity.class);%0A
+        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.M) %7B%0A    
         return P
@@ -4123,32 +4123,36 @@
                 
+  
+  
 context,%0A       
@@ -4136,32 +4136,36 @@
        context,%0A
+    
                 
@@ -4207,16 +4207,20 @@
         
+    
 startAct
@@ -4240,32 +4240,36 @@
                 
+ 
+   
 PendingIntent.FL
@@ -4281,26 +4281,303 @@
 DATE_CURRENT
-);
+ %7C PendingIntent.FLAG_IMMUTABLE);%0A        %7D else %7B%0A            return PendingIntent.getActivity(%0A                    context,%0A                    WORKOUT_PENDING_INTENT_ID,%0A                    startActivityIntent,%0A                    PendingIntent.FLAG_UPDATE_CURRENT);%0A        %7D
 %0A    %7D%0A%0A    

Set FLAG_IMMUTABLE to PendingIntent to avoid crash on API 31+