package com.dieam.reactnativepushnotification.modules;

import androidx.annotation.Nullable;
import com.facebook.common.executors.CallerThreadExecutor;
import com.facebook.common.references.CloseableReference;
import com.facebook.datasource.DataSource;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.common.Priority;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import java.util.concurrent.atomic.AtomicInteger;

public class RNPushNotificationPicturesAggregator {
  interface Callback {
    public void call(Bitmap largeIconImage, Bitmap bigPictureImage);
  }

  final private int wait = 2;
  private AtomicInteger count = new AtomicInteger(0);

  private Bitmap largeIconImage;
  private Bitmap bigPictureImage;

  private ImagePipeline imagePipeline;

  private Callback callback;

  public RNPushNotificationPicturesAggregator(Callback callback) {
    this.imagePipeline = Fresco.getImagePipeline();

    this.callback = callback;
  }

  public void setBigPicture(Bitmap bitmap) {
    this.bigPictureImage = bitmap;
    this.finished();
  }

  public void setBigPictureUrl(Context context, String url) {
    if(null == url) {
      this.setBigPicture(null);
      return;
    }

    final RNPushNotificationPicturesAggregator aggregator = this;

    this.downloadRequest(context, url, new BaseBitmapDataSubscriber() {
      @Override
      public void onNewResultImpl(@Nullable Bitmap bitmap) {
        aggregator.setBigPicture(bitmap);
      }

      @Override
      public void onFailureImpl(DataSource dataSource) {
        aggregator.setBigPicture(null);
      }
    });
  }

  public void setLargeIcon(Bitmap bitmap) {
    this.largeIconImage = bitmap;
    this.finished();
  }

  public void setLargeIconUrl(Context context, String url) {
    if(null == url) {
      this.setLargeIcon(null);
      return;
    }

    final RNPushNotificationPicturesAggregator aggregator = this;

    this.downloadRequest(context, url, new BaseBitmapDataSubscriber() {
      @Override
      public void onNewResultImpl(@Nullable Bitmap bitmap) {
        aggregator.setLargeIcon(bitmap);
      }

      @Override
      public void onFailureImpl(DataSource dataSource) {
        aggregator.setLargeIcon(null);
      }
    });
  }
  
  private void downloadRequest(Context context, String url, BaseBitmapDataSubscriber subscriber) {
    ImageRequest imageRequest = ImageRequestBuilder
      .newBuilderWithSource(Uri.parse(url))
      .setRequestPriority(Priority.HIGH)
      .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.FULL_FETCH)
      .build();

    DataSource<CloseableReference<CloseableImage>> dataSource = this.imagePipeline.fetchDecodedImage(imageRequest, context);

    dataSource.subscribe(subscriber, CallerThreadExecutor.getInstance());
  }

  private void finished() {
    synchronized(this.count) {
      int val = this.count.incrementAndGet();
      
      if(val >= this.wait && this.callback != null) {
        this.callback.call(this.largeIconImage, this.bigPictureImage);
      }
    }
  }
}
@@ -651,16 +651,41 @@
 ilder;%0A%0A
+import android.util.Log;%0A
 import a
@@ -815,16 +815,105 @@
 teger;%0A%0A
+import static com.dieam.reactnativepushnotification.modules.RNPushNotification.LOG_TAG;%0A%0A
 public c
@@ -1666,32 +1666,231 @@
  return;%0A    %7D%0A%0A
+    Uri uri = null;%0A%0A    try %7B%0A      uri = Uri.parse(url);%0A    %7D catch(Exception ex) %7B%0A      Log.e(LOG_TAG, %22Failed to parse bigPictureUrl%22, ex);%0A      this.setBigPicture(null);%0A      return;%0A    %7D%0A%0A
     final RNPush
@@ -1968,33 +1968,33 @@
 uest(context, ur
-l
+i
 , new BaseBitmap
@@ -2505,32 +2505,229 @@
  return;%0A    %7D%0A%0A
+    Uri uri = null;%0A%0A    try %7B%0A      uri = Uri.parse(url);%0A    %7D catch(Exception ex) %7B%0A      Log.e(LOG_TAG, %22Failed to parse largeIconUrl%22, ex);%0A      this.setLargeIcon(null);%0A      return;%0A    %7D%0A%0A
     final RNPush
@@ -2813,17 +2813,17 @@
 text, ur
-l
+i
 , new Ba
@@ -3159,26 +3159,23 @@
 ontext, 
-String url
+Uri uri
 , BaseBi
@@ -3287,30 +3287,19 @@
 hSource(
-U
+u
 ri
-.parse(url)
 )%0A      

Check URI before load.