package kz.arta.synergy.workflow.mobile.android.common.components;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * Created by neox on 30.05.14.
 * Кастомное ImageView. Отличие от обычного в том, что при тапе этот ImageView затемнается.
 */
public class CustomImageView extends ImageView {

    private boolean mIsSelected;
    private boolean enabled = true;

    public CustomImageView(Context context) {
        super(context);
        init();
        this.setClickable(true);
    }

    public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        this.setClickable(true);
    }

    public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
        this.setClickable(true);
    }

    private void init() {
        mIsSelected = false;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    @Override
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
        if (this.enabled) {
            clearColorFilter();
        } else {
            setColorFilter(0x99000000);
        }
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        if (!enabled) {
            return false;
        }
        if (event.getAction() == MotionEvent.ACTION_DOWN && !mIsSelected) {
            setColorFilter(0x99000000);
            mIsSelected = true;
        } else if (event.getAction() == MotionEvent.ACTION_UP && mIsSelected) {
            clearColorFilter();
            mIsSelected = false;
        }
        return super.onTouchEvent(event);
    }
}
