/*
 * Copyright (c) 2015 Zhang Hai <Dreaming.in.Code.ZH@Gmail.com>
 * All Rights Reserved.
 */

package info.dourok.lruimage.progress;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;


public abstract class ProgressDrawableBase extends Drawable
        implements TintableDrawable {

    public static final int LEVEL_MAX = 10000;
    protected boolean mAutoMirrored;
    protected int mAlpha = 0xFF;
    protected ColorFilter mColorFilter;
    protected ColorStateList mTintList;
    protected PorterDuff.Mode mTintMode = PorterDuff.Mode.SRC_IN;
    protected PorterDuffColorFilter mTintFilter;

    private Paint mPaint;

    public ProgressDrawableBase(Context context) {
        setAutoMirrored(true);
        try {
            int colorControlActivated = ThemeUtils.getColorFromAttrRes(android.R.attr.colorControlActivated,
                    context);
            setTint(colorControlActivated);
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
        // setTint() has been overridden for compatibility; DrawableCompat won't work because
        // wrapped Drawable won't be Animatable.

    }


    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isAutoMirrored() {
        return mAutoMirrored;
    }

    /**
     * f
     * {@inheritDoc}
     */
    @Override
    public void setAutoMirrored(boolean mirrored) {
        if (mAutoMirrored != mirrored) {
            mAutoMirrored = mirrored;
            invalidateSelf();
        }
    }

    @Override
    public int getAlpha() {
        return mAlpha;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setAlpha(int alpha) {
        if (mAlpha != alpha) {
            mAlpha = alpha;
            invalidateSelf();
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ColorFilter getColorFilter() {
        return mColorFilter;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        mColorFilter = colorFilter;
        invalidateSelf();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setTint(@ColorInt int tintColor) {
        setTintList(ColorStateList.valueOf(tintColor));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setTintList(@Nullable ColorStateList tint) {
        mTintList = tint;
        mTintFilter = makeTintFilter(mTintList, mTintMode);
        invalidateSelf();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setTintMode(@NonNull PorterDuff.Mode tintMode) {
        mTintMode = tintMode;
        mTintFilter = makeTintFilter(mTintList, mTintMode);
        invalidateSelf();
    }

    private PorterDuffColorFilter makeTintFilter(ColorStateList tint, PorterDuff.Mode tintMode) {

        if (tint == null || tintMode == null) {
            return null;
        }

        int color = tint.getColorForState(getState(), Color.TRANSPARENT);
        // They made PorterDuffColorFilter.setColor() and setMode() @hide.
        return new PorterDuffColorFilter(color, tintMode);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getOpacity() {
        // Be safe.
        return PixelFormat.TRANSLUCENT;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void draw(Canvas canvas) {

        Rect bounds = getBounds();
        if (bounds.width() == 0 || bounds.height() == 0) {
            return;
        }

        if (mPaint == null) {
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setColor(Color.BLACK);
            onPreparePaint(mPaint);
        }
        mPaint.setAlpha(mAlpha);
        ColorFilter colorFilter = mColorFilter != null ? mColorFilter : mTintFilter;
        mPaint.setColorFilter(colorFilter);

        int saveCount = canvas.save();

        canvas.translate(bounds.left, bounds.top);
        if (needMirroring()) {
            canvas.translate(bounds.width(), 0);
            canvas.scale(-1, 1);
        }

        onDraw(canvas, bounds.width(), bounds.height(), mPaint);

        canvas.restoreToCount(saveCount);
    }

    //TODO support RTL
    private boolean needMirroring() {
//        return mAutoMirrored
//                && DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
        return false;
    }

    protected abstract void onPreparePaint(Paint paint);

    protected abstract void onDraw(Canvas canvas, int width, int height, Paint paint);


}
@@ -585,16 +585,42 @@
 wable;%0D%0A
+import android.os.Build;%0D%0A
 import a
@@ -747,16 +747,50 @@
 ble;%0D%0A%0D%0A
+import info.dourok.lruimage.R;%0D%0A%0D%0A
 %0D%0Apublic
@@ -1350,178 +1350,44 @@
     
-int colorControlActivated = ThemeUtils.getColorFromAttrRes(android.R.attr.colorControlActivated,%0D%0A                    context);%0D%0A            setTint(colorControlActivated
+setTint(getDefaultTintColor(context)
 );%0D%0A
@@ -1407,26 +1407,8 @@
 ch (
-Resources.NotFound
 Exce
@@ -1609,16 +1609,500 @@
 table.%0D%0A
+    %7D%0D%0A%0D%0A    private int getDefaultTintColor(Context context) %7B%0D%0A        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.LOLLIPOP) %7B%0D%0A            return ThemeUtils.getColorFromAttrRes(android.R.attr.colorControlActivated,%0D%0A                    context);%0D%0A        %7D else %7B%0D%0A            return ThemeUtils.getColorFromAttrRes(context.getResources()%0D%0A                    .getIdentifier(%22colorControlActivated%22, %22attr%22, context.getPackageName())%0D%0A                    , context);%0D%0A        %7D
 %0D%0A    %7D%0D

Add compatible method to get colorControlActivated without v7 dependent