package net.heartshield.prevent;


import android.content.Context;
import android.content.res.TypedArray;
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 ToggleImageButton extends android.support.v7.widget.AppCompatImageButton {
    private static final String TAG = "ToggleImageButton";

    private int mSrcDisabled;
    private int mSrcEnabled;
    private boolean mPressed;

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

    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.ToggleImageButton,
                0, 0);
        mSrcDisabled = a.getResourceId(R.styleable.ToggleImageButton_srcDisabled, 0);
        mSrcEnabled = a.getResourceId(R.styleable.ToggleImageButton_srcEnabled, 0);
        if(mSrcDisabled == 0 || mSrcEnabled == 0)
            throw new IllegalArgumentException("must define srcDisabled and srcEnabled image resources");

        mPressed = false;
        setImageResource(mSrcDisabled);


        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");

                        Log.i(TAG, "setting toggle button to state=" + Boolean.toString(!mPressed));
                        setPressed(!mPressed);

                        // 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()???
            }
        });
    }

    public void setPressed(boolean pressed) {
        setImageResource(pressed ? mSrcEnabled : mSrcDisabled);
        postInvalidate();
        mPressed = pressed;
    }
    public boolean getPressed() { return mPressed; }
}
