/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package com.facebook.fresco.vito.draweesupport;

import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import androidx.annotation.VisibleForTesting;
import com.facebook.drawee.backends.pipeline.info.ImageOrigin;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.fresco.ui.common.DimensionsInfo;
import com.facebook.fresco.ui.common.OnDrawControllerListener;
import com.facebook.fresco.vito.listener.ImageListener;
import com.facebook.imagepipeline.image.ImageInfo;
import javax.annotation.Nullable;

public class ControllerListenerWrapper implements ImageListener {

  /**
   * Create a new controller listener wrapper or return null if the listener is null.
   *
   * @param controllerListener the controller listener to wrap
   * @return the wrapped controller listener or null if no wrapping required
   */
  @Nullable
  public static ControllerListenerWrapper create(
      @Nullable ControllerListener<ImageInfo> controllerListener) {
    return controllerListener == null ? null : new ControllerListenerWrapper(controllerListener);
  }

  private final ControllerListener<ImageInfo> mControllerListener;

  @VisibleForTesting
  ControllerListenerWrapper(ControllerListener<ImageInfo> controllerListener) {
    mControllerListener = controllerListener;
  }

  @Override
  public void onSubmit(long id, Object callerContext) {
    mControllerListener.onSubmit(toStringId(id), callerContext);
  }

  @Override
  public void onPlaceholderSet(long id, @Nullable Drawable placeholder) {
    // Not present in old API
  }

  @Override
  public void onFinalImageSet(
      long id,
      @ImageOrigin int imageOrigin,
      @Nullable ImageInfo imageInfo,
      @Nullable Drawable drawable) {
    Animatable animatable = drawable instanceof Animatable ? (Animatable) drawable : null;
    mControllerListener.onFinalImageSet(toStringId(id), imageInfo, animatable);
  }

  @Override
  public void onIntermediateImageSet(long id, @Nullable ImageInfo imageInfo) {
    mControllerListener.onIntermediateImageSet(toStringId(id), imageInfo);
  }

  @Override
  public void onIntermediateImageFailed(long id, Throwable throwable) {
    mControllerListener.onIntermediateImageFailed(toStringId(id), throwable);
  }

  @Override
  public void onFailure(long id, @Nullable Drawable error, Throwable throwable) {
    mControllerListener.onFailure(toStringId(id), throwable);
  }

  @Override
  public void onRelease(long id) {
    mControllerListener.onRelease(toStringId(id));
  }

  private static String toStringId(long id) {
    return "v" + id;
  }

  @Override
  public void onImageDrawn(String id, ImageInfo imageInfo, DimensionsInfo dimensionsInfo) {
    if (mControllerListener instanceof OnDrawControllerListener) {
      ((OnDrawControllerListener) mControllerListener).onImageDrawn(id, imageInfo, dimensionsInfo);
    }
  }
}

@@ -231,16 +231,87 @@
 pport;%0A%0A
+import static com.facebook.infer.annotation.Assertions.nullsafeFIXME;%0A%0A
 import a
@@ -776,24 +776,71 @@
 .ImageInfo;%0A
+import com.facebook.infer.annotation.Nullsafe;%0A
 import javax
@@ -862,16 +862,47 @@
 lable;%0A%0A
+@Nullsafe(Nullsafe.Mode.LOCAL)%0A
 public c
@@ -1699,16 +1699,26 @@
 long id,
+ @Nullable
  Object 
@@ -1767,16 +1767,25 @@
 nSubmit(
+%0A        
 toString
@@ -1792,29 +1792,89 @@
 Id(id), 
-callerContext
+nullsafeFIXME(callerContext, %22Legacy ControllerListener is not nullsafe%22)
 );%0A  %7D%0A%0A
@@ -2569,16 +2569,26 @@
 long id,
+ @Nullable
  Throwab
@@ -2653,16 +2653,25 @@
 eFailed(
+%0A        
 toString
@@ -2674,33 +2674,93 @@
 ringId(id), 
-throwable
+nullsafeFIXME(throwable, %22Legacy ControllerListener is not nullsafe%22)
 );%0A  %7D%0A%0A  @O
@@ -2825,16 +2825,26 @@
 e error,
+ @Nullable
  Throwab
@@ -2893,16 +2893,25 @@
 Failure(
+%0A        
 toString
@@ -2918,25 +2918,85 @@
 Id(id), 
-throwable
+nullsafeFIXME(throwable, %22Legacy ControllerListener is not nullsafe%22)
 );%0A  %7D%0A%0A

Make ControllerListenerWrapper nullsafe and ignore ControllerListener issues