package com.spoon.backgroundfileupload;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.res.Resources;
import android.widget.RemoteViews;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import net.gotev.uploadservice.UploadService;
import net.gotev.uploadservice.data.UploadInfo;
import net.gotev.uploadservice.data.UploadNotificationConfig;
import net.gotev.uploadservice.data.UploadRate;
import net.gotev.uploadservice.observer.task.AbstractSingleNotificationHandler;

import org.jetbrains.annotations.NotNull;

import java.util.Map;

public class NotificationHandler extends AbstractSingleNotificationHandler {

    private Activity mContext;
    private long uploadCount = 0;
    private String defaultTitle;
    private String defaultContent;

    public NotificationHandler(@NotNull UploadService service, Activity context, String defaultTitle, String defaultContent) {
        super(service);
        this.mContext = context;
        this.defaultTitle = defaultTitle;
        this.defaultContent = defaultContent;
    }

    @Override
    public void onStart(@NotNull UploadInfo info, int notificationId, @NotNull UploadNotificationConfig notificationConfig) {
        super.onStart(info, notificationId, notificationConfig);
        this.uploadCount = PendingUpload.count(PendingUpload.class);
    }

    @Override
    public void onCompleted(@NotNull UploadInfo info, int notificationId, @NotNull UploadNotificationConfig notificationConfig) {
        super.onCompleted(info, notificationId, notificationConfig);
        removeTask(info.getUploadId());
        this.uploadCount = PendingUpload.count(PendingUpload.class);
    }

    @Nullable
    @Override
    public NotificationCompat.Builder updateNotification(@NotNull NotificationManager notificationManager, @NotNull NotificationCompat.Builder builder, @NotNull Map<String, TaskData> tasks) {
        float speed = 0;
        int inProgress = 0;

        for (Map.Entry<String, TaskData> entry : tasks.entrySet()) {
            if (entry.getValue().getStatus() == TaskStatus.InProgress) {
                inProgress++;

                speed += convertUnitToKbps(
                        entry.getValue().getInfo().getUploadRate().getUnit().name(),
                        entry.getValue().getInfo().getUploadRate().getValue()
                );
            }
        }

        String pkg = mContext.getApplication().getPackageName();
        String layoutDef = "layout";
        String idDef = "id";

        Resources resources = mContext.getResources();

        RemoteViews notificationLayout = new RemoteViews(mContext.getPackageName(),
                resources.getIdentifier("notification_small", layoutDef, pkg));

        notificationLayout.setTextViewText(
                resources.getIdentifier("notification_title", idDef, pkg),
                inProgress == 0 ? defaultTitle : String.format("%s upload(s) remaining", uploadCount)
        );

        notificationLayout.setTextViewText(
                resources.getIdentifier("notification_content_left", idDef, pkg),
                inProgress == 0 ? defaultContent : String.format("%d in progress", inProgress)
        );

        notificationLayout.setTextViewText(
                resources.getIdentifier("notification_content_right", idDef, pkg),
                inProgress == 0 ? "" : toReadable((int) speed)
        );

        return builder
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                .setCustomContentView(notificationLayout)
                .setSmallIcon(android.R.drawable.ic_menu_upload);
    }

    private float convertUnitToKbps(String unit, int speed) {
        final String BPS = UploadRate.UploadRateUnit.BitPerSecond.name();
        final String KPS = UploadRate.UploadRateUnit.KilobitPerSecond.name();
        final String MPS = UploadRate.UploadRateUnit.MegabitPerSecond.name();

        float value = 0;

        if (unit == BPS) {
            value = speed / 1000f;
        }

        if (unit == KPS) {
            value = speed;
        }

        if (unit == MPS) {
            value = speed * 1000;
        }

        return value;
    }

    private String toReadable(int speed) {
        final String KBPS = "Kbps";
        final String MBPS = "Mbps";

        if (speed >= 1000) {
            return String.format("%d %s", speed / 1000, MBPS);
        }

        return String.format("%d %s", speed, KBPS);
    }
}

@@ -3410,14 +3410,8 @@
 ble(
-(int) 
 spee
@@ -4244,18 +4244,20 @@
 eadable(
-in
+floa
 t speed)
@@ -4251,32 +4251,66 @@
 (float speed) %7B%0A
+        final String BPS = %22bps%22;%0A
         final St
@@ -4322,17 +4322,17 @@
 KBPS = %22
-K
+k
 bps%22;%0A  
@@ -4432,24 +4432,30 @@
 mat(%22%25d %25s%22,
+ (int)
  speed / 100
@@ -4487,37 +4487,147 @@
     
-return String.format(%22%25d %25s%22,
+if (speed %3C 1) %7B%0A            return String.format(%22%25d %25s%22, (int) speed * 1000, BPS);%0A        %7D%0A%0A        return String.format(%22%25d %25s%22, (int)
  spe

Update NotificationHandler