package com.stanfy.images.decorator;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

/**
 * Draws the specified image on top of input one.
 * @author Roman Mazur (Stanfy - http://www.stanfy.com)
 */
public class ComposerDecorator extends ImageDecoratorAdapter {

  /** 'Left' option. */
  public static final int LEFT = 1;
  /** 'Top' option. */
  public static final int TOP = 2;
  /** 'Right' option. */
  public static final int RIGHT = 4;
  /** 'Bottom' option. */
  public static final int BOTTOM = 8;
  /** 'Center vertical' option. */
  public static final int CENTER_VERTICAL = 16;
  /** 'Center horizontal' option. */
  public static final int CENTER_HORIZONTAL = 32;
  /** 'Center' option. */
  public static final int CENTER = CENTER_VERTICAL | CENTER_HORIZONTAL;

  /** Justify flags. */
  private final int justify;

  /** Drawable. */
  private final Drawable drawable;
  /** Drawable size. */
  private final int dWidth, dHeight;
  /** Drawable bounds. */
  private final Rect bounds = new Rect();

  public ComposerDecorator(final Drawable drawable) {
    this(drawable, CENTER);
  }

  public ComposerDecorator(final Drawable drawable, final int justify) {
    this.justify = justify;
    this.drawable = drawable;
    this.dWidth = drawable.getIntrinsicWidth();
    this.dHeight = drawable.getIntrinsicHeight();
  }

  @Override
  public boolean dependsOnDrawableState() { return drawable.isStateful(); }

  @Override
  public Bitmap processBitmap(final Bitmap bitmap, final Canvas bitmapVanvas) {
    final Drawable d = this.drawable;
    d.setBounds(bounds);
    d.draw(bitmapVanvas);
    return bitmap;
  }

  @Override
  public void setup(final int width, final int height, final int[] state, final int level) {
    final int flags = this.justify;
    final Rect bounds = this.bounds;
    final int dWidth = this.dWidth, dHeight = this.dHeight;

    final Drawable d = this.drawable;
    d.setState(state);
    d.setLevel(level);

    // horizontal
    if ((flags & LEFT) != 0) {
      bounds.left = 0;
      bounds.right = dWidth;
    } else if ((flags & RIGHT) != 0) {
      bounds.left = width - dWidth;
      bounds.right = width;
    } else if ((flags & CENTER_HORIZONTAL) != 0) {
      final int p = (width - dWidth) / 2;
      bounds.left = p;
      bounds.right = width - p;
    }

    // vertical
    if ((flags & TOP) != 0) {
      bounds.top = 0;
      bounds.bottom = dHeight;
    } else if ((flags & BOTTOM) != 0) {
      bounds.top = height - dHeight;
      bounds.bottom = height;
    } else if ((flags & CENTER_VERTICAL) != 0) {
      final int p = (height - dHeight) / 2;
      bounds.top = p;
      bounds.bottom = height - p;
    }
  }

}

@@ -772,86 +772,363 @@
 ** '
-Center' option. */%0A  public static final int CENTER = CENTER_VERTICAL %7C CENTER
+Stretch vertical' option. */%0A  public static final int STRETCH_VERTICAL = 64;%0A  /** 'Stretch horizontal' option. */%0A  public static final int STRETCH_HORIZONTAL = 128;%0A  /** 'Center' option. */%0A  public static final int CENTER = CENTER_VERTICAL %7C CENTER_HORIZONTAL;%0A  /** 'Stretch' option. */%0A  public static final int STRETCH = STRETCH_VERTICAL %7C STRETCH
 _HOR
@@ -2168,30 +2168,24 @@
 bounds;%0A    
-final 
 int dWidth =
@@ -2221,16 +2221,101 @@
 dHeight;
+%0A    if (dWidth %3C= 0) %7B dWidth = width; %7D%0A    if (dHeight %3C= 0) %7B dHeight = height; %7D
 %0A%0A    fi
@@ -2750,16 +2750,119 @@
 p;%0A    %7D
+ else if ((flags & STRETCH_HORIZONTAL) != 0) %7B%0A      bounds.left = 0;%0A      bounds.right = width;%0A    %7D
 %0A%0A    //
@@ -3206,24 +3206,126 @@
 height - p;%0A
+    %7D else if ((flags & STRETCH_VERTICAL) != 0) %7B%0A      bounds.top = 0;%0A      bounds.bottom = height;%0A
     %7D%0A  %7D%0A%0A%7D

extend composer decorator