package de.wackernagel.android.sidekick.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RelativeLayout;

import de.wackernagel.android.sidekick.R;

public class ForegroundRelativeLayout extends RelativeLayout {

    private Drawable foregroundDrawable;
    private int foregroundGravity = Gravity.FILL;

    private final Rect mSelfBounds = new Rect();
    private final Rect mOverlayBounds = new Rect();

    private boolean foregroundBoundsChanged = false;

    public ForegroundRelativeLayout(Context context) {
        this(context, null);
    }

    public ForegroundRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ForegroundRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super( context, attrs, defStyle );
        init(context, attrs, defStyle);
    }

    private void init( @NonNull final Context context, @Nullable final AttributeSet attrs, int defStyle ) {
        if( attrs != null ) {
            int[] definedAttr = new int[]{ android.R.attr.foreground, android.R.attr.foregroundGravity };
            TypedArray a = context.obtainStyledAttributes(attrs, definedAttr, defStyle, 0);
            foregroundGravity = a.getInt( R.styleable.ForegroundRelativeLayout_android_foregroundGravity, foregroundGravity);

            final Drawable drawable = a.getDrawable(R.styleable.ForegroundRelativeLayout_android_foreground);
            if( drawable != null ) {
                setForeground(drawable);
            }
            a.recycle();
        }
    }

    public int getForegroundGravity() {
        return foregroundGravity;
    }

    public void setForegroundGravity(int foregroundGravity) {
        if (this.foregroundGravity != foregroundGravity) {
            if ((foregroundGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
                foregroundGravity |= Gravity.START;
            }

            if ((foregroundGravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
                foregroundGravity |= Gravity.TOP;
            }

            this.foregroundGravity = foregroundGravity;


            if (this.foregroundGravity == Gravity.FILL && foregroundDrawable != null) {
                Rect padding = new Rect();
                foregroundDrawable.getPadding(padding);
            }

            requestLayout();
        }
    }

    @Override
    protected boolean verifyDrawable(Drawable who) {
        return super.verifyDrawable(who) || (who == foregroundDrawable);
    }

    @Override
    public void jumpDrawablesToCurrentState() {
        super.jumpDrawablesToCurrentState();
        if (foregroundDrawable != null) DrawableCompat.jumpToCurrentState(foregroundDrawable);
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        if (foregroundDrawable != null && foregroundDrawable.isStateful()) {
            foregroundDrawable.setState(getDrawableState());
        }
    }

    public void setForeground(Drawable drawable) {
        if (foregroundDrawable != drawable) {
            if (foregroundDrawable != null) {
                foregroundDrawable.setCallback(null);
                unscheduleDrawable(foregroundDrawable);
            }

            foregroundDrawable = drawable;

            if (drawable != null) {
                setWillNotDraw(false);
                drawable.setCallback(this);
                if (drawable.isStateful()) {
                    drawable.setState(getDrawableState());
                }
                if (foregroundGravity == Gravity.FILL) {
                    Rect padding = new Rect();
                    drawable.getPadding(padding);
                }
            } else {
                setWillNotDraw(true);
            }
            requestLayout();
            invalidate();
        }
    }

    public Drawable getForeground() {
        return foregroundDrawable;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        foregroundBoundsChanged = changed;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        foregroundBoundsChanged = true;
    }

    @Override
    public void draw( @NonNull final Canvas canvas) {
        super.draw(canvas);

        if (foregroundDrawable != null) {
            final Drawable foreground = foregroundDrawable;

            if (foregroundBoundsChanged) {
                foregroundBoundsChanged = false;
                final Rect selfBounds = mSelfBounds;
                final Rect overlayBounds = mOverlayBounds;

                final int w = getRight() - getLeft();
                final int h = getBottom() - getTop();
                selfBounds.set(0, 0, w, h);

                Gravity.apply(foregroundGravity, foreground.getIntrinsicWidth(), foreground.getIntrinsicHeight(), selfBounds, overlayBounds);
                foreground.setBounds( overlayBounds );
            }

            foreground.draw(canvas);
        }
    }
}
@@ -472,51 +472,8 @@
 t;%0A%0A
-import de.wackernagel.android.sidekick.R;%0A%0A
 publ
@@ -1319,37 +1319,30 @@
     
-int%5B%5D defined
+final int%5B%5D my
 Attr
+s
  = 
-new int%5B%5D
 %7B an
@@ -1414,16 +1414,22 @@
         
+final 
 TypedArr
@@ -1470,26 +1470,23 @@
 tes(
+ 
 attrs, 
-defined
+my
 Attr
+s
 , de
@@ -1494,19 +1494,21 @@
 Style, 0
+ 
 );%0A
+%0A
         
@@ -1515,123 +1515,41 @@
     
-foregroundGravity = a.getInt( R.styleable.ForegroundRelativeLayout_android_foregroundGravity, foregroundGravity
+setForeground(a.getDrawable(0)
 );%0A
-%0A
     
@@ -1560,197 +1560,65 @@
     
-final Drawable drawable = a.getDrawable(R.styleable.ForegroundRelativeLayout_android_foreground);%0A            if( drawable != null ) %7B%0A                setForeground(drawable);%0A            %7D
+setForegroundGravity( a.getInt( 1, foregroundGravity) );%0A
 %0A   

fix bug in ForegroundRelativeLayout