package org.robolectric.shadows;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import org.robolectric.Robolectric;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

@Implements(ImageView.class)
public class ShadowImageView extends ShadowView {
  private Drawable imageDrawable;
  private Bitmap imageBitmap;
  private ImageView.ScaleType scaleType;
  private Matrix matrix;
  private int imageLevel;

  @Implementation
  public void setImageBitmap(Bitmap imageBitmap) {
    setImageDrawable(new BitmapDrawable(imageBitmap));
    this.imageBitmap = imageBitmap;
  }

  @Deprecated
  public Bitmap getImageBitmap() {
    return imageBitmap;
  }

  @Implementation
  public void setImageDrawable(Drawable drawable) {
    this.imageDrawable = drawable;
  }

  @Implementation
  public void setImageResource(int resId) {
    setImageDrawable(resId != 0 ? buildDrawable(resId) : null);
  }

  public int getImageResourceId() {
    ShadowDrawable shadow = Robolectric.shadowOf(imageDrawable);
    return shadow.getCreatedFromResId();
  }

  @Implementation
  public ImageView.ScaleType getScaleType() {
    return scaleType;
  }

  @Implementation
  public void setScaleType(ImageView.ScaleType scaleType) {
    this.scaleType = scaleType;
  }

  @Implementation
  public Drawable getDrawable() {
    return imageDrawable;
  }

  @Implementation
  public void setImageMatrix(Matrix matrix) {
    this.matrix = new Matrix(matrix);
  }

  @Implementation
  public Matrix getImageMatrix() {
    return matrix;
  }

  @Implementation
  public void draw(Canvas canvas) {
    if (imageDrawable != null) {
      imageDrawable.draw(canvas);
    }
  }

  private void applyImageAttribute() {
    String source = attributeSet.getAttributeValue(ANDROID_NS, "src");
    if (source != null) {
      if (source.startsWith("@drawable/")) {
        setImageResource(attributeSet.getAttributeResourceValue(ANDROID_NS, "src", 0));
      }
    }
  }

  @Implementation
  public void setImageLevel(int imageLevel) {
    this.imageLevel = imageLevel;
  }

  /**
   * Non-Android accessor.
   *
   * @return the imageLevel set in {@code setImageLevel(int)}
   */
  public int getImageLevel() {
    return imageLevel;
  }
}

@@ -1857,296 +1857,8 @@
  %7D%0A%0A
-  private void applyImageAttribute() %7B%0A    String source = attributeSet.getAttributeValue(ANDROID_NS, %22src%22);%0A    if (source != null) %7B%0A      if (source.startsWith(%22@drawable/%22)) %7B%0A        setImageResource(attributeSet.getAttributeResourceValue(ANDROID_NS, %22src%22, 0));%0A      %7D%0A    %7D%0A  %7D%0A%0A
   @I

Kill dead code