package epson.scan.activity;

import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.widget.Button;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class CustomButtonWrapper {
    private final Button mButton;
    private final int mDrawableId;

    public CustomButtonWrapper(@NonNull Button button, @DrawableRes int i) {
        mButton = button;
        mButton.setText("");
        mDrawableId = i;
        setEnabled(mButton.isEnabled());
    }

    public void setEnabled(boolean z) {
        setScanButtonState(mButton, mDrawableId, 0, z);
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        mButton.setOnClickListener(onClickListener);
    }

    public void setVisibility(int i) {
        mButton.setVisibility(i);
    }

    public static void setScanButtonState(@NonNull Button button, @DrawableRes int i, int i2, boolean z) {
        Drawable localGetDrawable = localGetDrawable(button.getResources(), i);
        if (localGetDrawable != null) {
            if (z) {
                localGetDrawable.setColorFilter(-1, PorterDuff.Mode.MULTIPLY);
            } else {
                localGetDrawable.setColorFilter(Color.parseColor("#cccccc"), PorterDuff.Mode.SRC_IN);
            }
            button.setCompoundDrawablesWithIntrinsicBounds((Drawable) null, (Drawable) null, (Drawable) null, localGetDrawable);
            button.setText("");
            button.setEnabled(z);
        }
    }

    @Nullable
    public static Drawable localGetDrawable(Resources resources, @DrawableRes int i) {
        if (resources == null) {
            return null;
        }
        if (Build.VERSION.SDK_INT >= 21) {
            return resources.getDrawable(i, (Resources.Theme) null);
        }
        return resources.getDrawable(i);
    }
}
