/*
 * 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.core.impl.debug;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import com.facebook.common.internal.Supplier;
import com.facebook.fresco.vito.core.FrescoDrawable2;
import com.facebook.infer.annotation.OkToExtend;

@OkToExtend
public abstract class BaseDebugOverlayFactory2 implements DebugOverlayFactory2 {

  private static final int NUMBER_OF_DEBUG_OVERLAY_WRAPPER_LAYERS = 2;
  private static final int DEBUG_OVERLAY_DRAWABLE_INDEX = 1;

  private final Supplier<Boolean> mDebugOverlayEnabled;

  public BaseDebugOverlayFactory2(Supplier<Boolean> debugOverlayEnabled) {
    mDebugOverlayEnabled = debugOverlayEnabled;
  }

  @Override
  public void update(FrescoDrawable2 drawable) {
    if (!mDebugOverlayEnabled.get()) {
      return;
    }
    DebugOverlayDrawable overlay = extractOrCreate(drawable);
    overlay.reset();
    setData(overlay, drawable);
  }

  protected abstract void setData(DebugOverlayDrawable overlay, FrescoDrawable2 drawable);

  private DebugOverlayDrawable extractOrCreate(FrescoDrawable2 drawable) {
    Drawable existingOverlay = drawable.getOverlayDrawable();
    DebugOverlayDrawable overlay = null;
    if (existingOverlay instanceof LayerDrawable) {
      LayerDrawable layers = (LayerDrawable) existingOverlay;
      if (layers.getNumberOfLayers() == NUMBER_OF_DEBUG_OVERLAY_WRAPPER_LAYERS
          && layers.getDrawable(DEBUG_OVERLAY_DRAWABLE_INDEX) instanceof DebugOverlayDrawable) {
        overlay = (DebugOverlayDrawable) layers.getDrawable(DEBUG_OVERLAY_DRAWABLE_INDEX);
      }
    }
    if (overlay == null) {
      overlay = new DebugOverlayDrawable("V2");
      drawable.setOverlayDrawable(new LayerDrawable(new Drawable[] {existingOverlay, overlay}));
      drawable.showOverlayImmediately();
    }
    return overlay;
  }
}

@@ -572,141 +572,8 @@
  %7B%0A%0A
-  private static final int NUMBER_OF_DEBUG_OVERLAY_WRAPPER_LAYERS = 2;%0A  private static final int DEBUG_OVERLAY_DRAWABLE_INDEX = 1;%0A%0A
   pr
@@ -1230,48 +1230,126 @@
     
-DebugOverlayDrawable overlay = null;%0A   
+if (existingOverlay instanceof DebugOverlayDrawable) %7B%0A      return (DebugOverlayDrawable) existingOverlay;%0A    %7D else
  if 
@@ -1376,29 +1376,36 @@
 tanceof 
-Layer
+DebugOverlay
 Drawable
 ) %7B%0A    
@@ -1388,32 +1388,39 @@
 gOverlayDrawable
+Wrapper
 ) %7B%0A      LayerD
@@ -1417,45 +1417,67 @@
     
-LayerDrawable lay
+DebugOverlayDrawableWrapper wrapp
 er
-s
  = (
-Layer
+DebugOverlay
 Drawable
 ) ex
@@ -1464,32 +1464,39 @@
 gOverlayDrawable
+Wrapper
 ) existingOverla
@@ -1508,329 +1508,535 @@
     
-if (lay
+return wrapp
 er
-s
 .get
-NumberOfLayers() == NUMBER_OF_DEBUG_OVERLAY_WRAPPER_LAYERS
+DebugOverlayDrawable();
 %0A    
+%7D%0A%0A
     
-  && layers.getDrawable(DEBUG_OVERLAY_DRAWABLE_INDEX) instanceof DebugOverlayDrawable) %7B%0A        overlay = (DebugOverlayDrawable) layers.getDrawable(DEBUG_OVERLAY_DRAWABLE_INDEX);%0A      %7D%0A    %7D%0A    if (overlay == null) %7B%0A      overlay = new
+DebugOverlayDrawable debugOverlay = new DebugOverlayDrawable(%22V2%22);%0A    if (existingOverlay != null) %7B%0A      drawable.setOverlayDrawable(new DebugOverlayDrawableWrapper(existingOverlay, debugOverlay));%0A    %7D else %7B%0A      drawable.setOverlayDrawable(debugOverlay);%0A    %7D%0A    drawable.showOverlayImmediately();%0A    return debugOverlay;%0A  %7D%0A%0A  private class DebugOverlayDrawableWrapper extends LayerDrawable %7B%0A    private DebugOverlayDrawable mDebugOverlayDrawable;%0A%0A    public
  Deb
@@ -2056,34 +2056,42 @@
 able
-(%22V2%22);
+Wrapper(
 %0A      
-d
+  D
 rawable
-.set
+ existing
 Over
@@ -2105,138 +2105,292 @@
 able
-(new LayerDrawable(new Drawable%5B%5D %7BexistingOverlay, overlay%7D));%0A      drawable.showOverlayImmediately();%0A    %7D%0A    return overlay;
+, DebugOverlayDrawable debugOverlayDrawable) %7B%0A      super(new Drawable%5B%5D %7BexistingOverlayDrawable, debugOverlayDrawable%7D);%0A      mDebugOverlayDrawable = debugOverlayDrawable;%0A    %7D%0A%0A    public DebugOverlayDrawable getDebugOverlayDrawable() %7B%0A      return mDebugOverlayDrawable;%0A    %7D
 %0A  %7D

Fix debug overlay for Vito2