/*
 * 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.listener;

import android.graphics.drawable.Drawable;
import com.facebook.drawee.backends.pipeline.info.ImageOrigin;
import com.facebook.fresco.ui.common.DimensionsInfo;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.infer.annotation.Nullsafe;
import javax.annotation.Nullable;

@Nullsafe(Nullsafe.Mode.STRICT)
public class ForwardingImageListener implements ImageListener {

  @Nullable
  public static ImageListener create(@Nullable ImageListener a, @Nullable ImageListener b) {
    if (a == null) {
      return b;
    }
    if (b == null) {
      return a;
    }
    return new ForwardingImageListener(a, b);
  }

  @Nullable
  public static ImageListener create(
      @Nullable ImageListener a, @Nullable ImageListener b, @Nullable ImageListener c) {
    if (a == null) {
      return create(b, c);
    }
    if (b == null) {
      return create(a, c);
    }
    if (c == null) {
      return create(a, b);
    }
    return new ForwardingImageListener(a, b, c);
  }

  private final ImageListener[] mListeners;

  public ForwardingImageListener(ImageListener... listeners) {
    mListeners = listeners;
  }

  @Override
  public void onSubmit(long id, @Nullable Object callerContext) {
    for (int i = 0; i < mListeners.length; i++) {
      if (mListeners[i] != null) {
        mListeners[i].onSubmit(id, callerContext);
      }
    }
  }

  @Override
  public void onPlaceholderSet(long id, @Nullable Drawable placeholder) {
    for (int i = 0; i < mListeners.length; i++) {
      if (mListeners[i] != null) {
        mListeners[i].onPlaceholderSet(id, placeholder);
      }
    }
  }

  @Override
  public void onFinalImageSet(
      long id,
      @ImageOrigin int imageOrigin,
      @Nullable ImageInfo imageInfo,
      @Nullable Drawable drawable) {
    for (int i = 0; i < mListeners.length; i++) {
      if (mListeners[i] != null) {
        mListeners[i].onFinalImageSet(id, imageOrigin, imageInfo, drawable);
      }
    }
  }

  @Override
  public void onIntermediateImageSet(long id, @Nullable ImageInfo imageInfo) {
    for (int i = 0; i < mListeners.length; i++) {
      if (mListeners[i] != null) {
        mListeners[i].onIntermediateImageSet(id, imageInfo);
      }
    }
  }

  @Override
  public void onIntermediateImageFailed(long id, @Nullable Throwable throwable) {
    for (int i = 0; i < mListeners.length; i++) {
      if (mListeners[i] != null) {
        mListeners[i].onIntermediateImageFailed(id, throwable);
      }
    }
  }

  @Override
  public void onFailure(long id, @Nullable Drawable error, @Nullable Throwable throwable) {
    for (int i = 0; i < mListeners.length; i++) {
      if (mListeners[i] != null) {
        mListeners[i].onFailure(id, error, throwable);
      }
    }
  }

  @Override
  public void onRelease(long id) {
    for (int i = 0; i < mListeners.length; i++) {
      if (mListeners[i] != null) {
        mListeners[i].onRelease(id);
      }
    }
  }

  @Override
  public void onImageDrawn(String id, ImageInfo imageInfo, DimensionsInfo dimensionsInfo) {
    for (int i = 0; i < mListeners.length; i++) {
      if (mListeners[i] != null) {
        mListeners[i].onImageDrawn(id, imageInfo, dimensionsInfo);
      }
    }
  }
}

@@ -3424,11 +3424,982 @@
    %7D%0A  %7D
+%0A%0A  @Override%0A  public boolean equals(@Nullable Object o) %7B%0A    // If the object is compared with itself then return true%0A    if (o == this) %7B%0A      return true;%0A    %7D%0A%0A    // Check if o is an instance of ForwardingImageListener or not%0A    if (!(o instanceof ForwardingImageListener)) %7B%0A      return false;%0A    %7D%0A%0A    ForwardingImageListener forwardingImageListener = (ForwardingImageListener) o;%0A    ImageListener%5B%5D listeners = forwardingImageListener.mListeners;%0A    if (mListeners.length != listeners.length) %7B%0A      return false;%0A    %7D%0A    for (int i = 0; i %3C mListeners.length; i++) %7B%0A      if (!(mListeners%5Bi%5D.equals(listeners%5Bi%5D))) %7B%0A        return false;%0A      %7D%0A    %7D%0A    return true;%0A  %7D%0A%0A  @Override%0A  public int hashCode() %7B%0A    int result = mListeners%5B0%5D != null ? mListeners%5B0%5D.hashCode() : 0;%0A    for (int i = 1; i %3C mListeners.length; i++) %7B%0A      result = 31 * result + (mListeners%5Bi%5D != null ? mListeners%5Bi%5D.hashCode() : 0);%0A    %7D%0A    return result;%0A  %7D
 %0A%7D%0A

Add equals() function to ForwardingImageListener