package net.heartshield.prevent;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;

/**
 * An ImageButton that always renders as a square.
 *
 * @author David Madl (git@abanbytes.eu)
 * @date 2016-04-01
 */
public class SquareImageButton extends android.support.v7.widget.AppCompatImageButton {
    private static final String TAG = "SquareImageButton";

    private int mHighlightColor;
    private int mRegularColor;
    private boolean mSquare;

    public SquareImageButton(Context context) {
        super(context);
    }
    public SquareImageButton(Context context, AttributeSet attrs) {
        super(context, attrs); init(context, attrs);
    }
    public SquareImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        mHighlightColor = ContextCompat.getColor(context, R.color.square_image_background_highlight);

        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.SquareImageButton,
                0, 0);
        mSquare = a.getBoolean(R.styleable.SquareImageButton_square, true);

        int regularColorFallback = ContextCompat.getColor(context, R.color.square_image_background_regular);

        int color = regularColorFallback;
        Drawable background = this.getBackground();
        if (background instanceof ColorDrawable)
            color = ((ColorDrawable) background).getColor();

        mRegularColor = color;

        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        Log.i(TAG, "ACTION_DOWN");
                        ImageButton view = (ImageButton ) v;
                        //view.setBackgroundColor(0x80800000);
                        view.setBackgroundColor(mHighlightColor);

                        // ContextCompat.getColor(context, R.color.my_color)
                        //view.getBackground().setColorFilter(0x77800000, PorterDuff.Mode.SRC_ATOP);
                        v.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                        Log.i(TAG, "ACTION_UP");

                        // Your action here on button click
                        callOnClick(); // either this, or return false (which has other side effects)

                        // fall through to clear color filter


                    case MotionEvent.ACTION_CANCEL: {
                        ImageButton view = (ImageButton) v;
                        view.setBackgroundColor(mRegularColor);
                        //view.getBackground().clearColorFilter();
                        view.invalidate();
                        break;
                    }
                }
                return true;
                //return false; // make sure onClick() gets fired as well // avoids ACTION_UP for not-listened onClick()???
            }
        });
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(!mSquare) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }

        //final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        final int widthSpecSize =  MeasureSpec.getSize(widthMeasureSpec);
        //final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        final int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);

        if(widthSpecSize < heightSpecSize)
            super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        else
            super.onMeasure(heightMeasureSpec, heightMeasureSpec);
    }
}
