package net.heartshield.prevent;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * A Button that changes color when pressed.
 *
 * @author David Madl (git@abanbytes.eu)
 * @date 2016-04-01
 */
public class FancyButton extends AppCompatButton {
    private int mHighlightColor;
    private int mRegularColor;
    private int mDisabledColor;
    private boolean mEnabled;

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

    public void setEnabled(boolean enabled) {
        mEnabled = enabled;
        if(mEnabled)
            setBackgroundColor(mRegularColor);
        else
            setBackgroundColor(mDisabledColor);
        postInvalidate();
    }

    private void init(Context context, AttributeSet attrs) {
        mEnabled = true;
        mHighlightColor = ContextCompat.getColor(context, R.color.square_image_background_highlight);

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

        mDisabledColor = ContextCompat.getColor(context, R.color.square_image_background_disabled);

        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: {
                        AppCompatButton view = (AppCompatButton) v;
                        //view.setBackgroundColor(0x80800000);
                        view.setBackgroundColor(mEnabled ? mHighlightColor : mDisabledColor);

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

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

                        // fall through to clear color filter


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