package xyz.stream.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.r;
import java.io.File;
import java.util.ArrayList;
import xyz.stream.view.databinding.LayoutPlaceHolderBinding;
import xyz.stream.view.glide.ViewGlideUrl;

/* JADX INFO: loaded from: classes3.dex */
public class CustomImageView extends FrameLayout {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public RoundImageView f36234a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public LayoutPlaceHolderBinding f36235b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public q5.f f36236c;

    public interface OnImageLoadListener {
        void onImageFailed();
    }

    public interface RawImageLoadListener {
        void onImageFailed();

        void onImageReady();
    }

    public static class Request {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final String f36239a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public int f36240b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public int f36241c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public int f36242d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public ImageView.ScaleType f36243e = ImageView.ScaleType.FIT_XY;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public int f36244f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        public boolean f36245g;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        public boolean f36246h;

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        public boolean f36247i;

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        public OnImageLoadListener f36248j;

        /* JADX INFO: renamed from: k, reason: collision with root package name */
        public RawImageLoadListener f36249k;

        public Request(String str, boolean z10) {
            this.f36239a = str;
            this.f36245g = !z10;
            this.f36246h = !z10;
            this.f36247i = !z10;
        }

        public static Request image(String str) {
            return new Request(str, false);
        }

        public static Request raw(String str) {
            return new Request(str, true);
        }

        public Request enableThumbnail(boolean z10) {
            this.f36247i = z10;
            return this;
        }

        public Request listener(RawImageLoadListener rawImageLoadListener) {
            this.f36249k = rawImageLoadListener;
            return this;
        }

        public Request normalizeUrl(boolean z10) {
            this.f36246h = z10;
            return this;
        }

        public Request onImageLoadListener(OnImageLoadListener onImageLoadListener) {
            this.f36248j = onImageLoadListener;
            return this;
        }

        public Request placeholder(int i10) {
            this.f36244f = i10;
            return this;
        }

        public Request radius(int i10) {
            this.f36240b = i10;
            return this;
        }

        public Request scaleType(ImageView.ScaleType scaleType) {
            if (scaleType != null) {
                this.f36243e = scaleType;
            }
            return this;
        }

        public Request showDefaultImage(boolean z10) {
            this.f36245g = z10;
            return this;
        }

        public Request size(int i10, int i11) {
            this.f36241c = i10;
            this.f36242d = i11;
            return this;
        }
    }

    public CustomImageView(Context context) {
        super(context);
        b(context);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void setError(Request request) {
        this.f36235b.shimmerViewContainer.a();
        if (request.f36244f == 0) {
            removeView(this.f36234a);
        } else {
            this.f36234a.setScaleType(ImageView.ScaleType.FIT_XY);
            this.f36234a.setImageResource(request.f36244f);
        }
    }

    public final void b(Context context) {
        RoundImageView roundImageView = new RoundImageView(context);
        this.f36234a = roundImageView;
        roundImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        addView(this.f36234a, new FrameLayout.LayoutParams(-1, -1));
        this.f36235b = LayoutPlaceHolderBinding.inflate(LayoutInflater.from(context), this, false);
        File file = new File(ViewRuntime.requireContext(context).getCacheDir(), "default_pic.png");
        if (file.exists()) {
            this.f36235b.imageLogo.setImageDrawable(Drawable.createFromPath(file.getAbsolutePath()));
        }
        this.f36236c = (q5.f) ((q5.f) ((q5.f) new q5.f().q(false)).e(com.bumptech.glide.load.engine.o.f5754d)).f();
    }

    public void clearImage() {
        com.bumptech.glide.c.e(this.f36234a).f(this.f36234a);
        removeAllViews();
        this.f36234a.setImageDrawable(null);
        addView(this.f36234a, new FrameLayout.LayoutParams(-1, -1));
    }

    public RoundImageView getImageView() {
        return this.f36234a;
    }

    public void load(final Request request) {
        if (request == null) {
            clearImage();
            return;
        }
        removeAllViews();
        com.bumptech.glide.c.e(this.f36234a).f(this.f36234a);
        int width = request.f36241c;
        int height = request.f36242d;
        q5.f fVar = (q5.f) this.f36236c.clone();
        if (width <= 0) {
            width = this.f36234a.getWidth() > 0 ? this.f36234a.getWidth() : getWidth() > 0 ? getWidth() : 0;
        }
        if (height <= 0) {
            height = this.f36234a.getHeight() > 0 ? this.f36234a.getHeight() : Math.max(getHeight(), 0);
        }
        if (width > 0 && height > 0) {
            fVar = (q5.f) fVar.i(width, height);
        }
        if (request.f36245g) {
            addView(this.f36235b.getRoot());
            this.f36235b.getRoot().setRadius(request.f36240b);
        }
        addView(this.f36234a);
        String strConcat = request.f36239a;
        if (strConcat == null || strConcat.isEmpty()) {
            if (request.f36245g && this.f36235b.getRoot().getParent() == null) {
                addView(this.f36235b.getRoot());
            }
            if (request.f36245g) {
                int i10 = request.f36241c;
                int iDp2px = i10 > 0 ? i10 - UiUtils.dp2px(12.0f) : -2;
                int i11 = request.f36242d;
                this.f36235b.getRoot().setLayoutParams(new FrameLayout.LayoutParams(iDp2px, i11 > 0 ? i11 - UiUtils.dp2px(12.0f) : -2));
            }
            setError(request);
            OnImageLoadListener onImageLoadListener = request.f36248j;
            if (onImageLoadListener != null) {
                onImageLoadListener.onImageFailed();
            }
            RawImageLoadListener rawImageLoadListener = request.f36249k;
            if (rawImageLoadListener != null) {
                rawImageLoadListener.onImageFailed();
                return;
            }
            return;
        }
        this.f36234a.setImageDrawable(null);
        this.f36234a.setAdjustViewBounds(true);
        this.f36234a.setScaleType(request.f36243e);
        this.f36234a.setRadius(request.f36240b);
        if (strConcat.startsWith("/data/user/0") || strConcat.startsWith("/storage/")) {
            com.bumptech.glide.c.e(this.f36234a).load("file://".concat(strConcat)).a(fVar).y(this.f36234a);
            removeView(this.f36235b.getRoot());
            RawImageLoadListener rawImageLoadListener2 = request.f36249k;
            if (rawImageLoadListener2 != null) {
                rawImageLoadListener2.onImageReady();
                return;
            }
            return;
        }
        if (request.f36246h && !strConcat.isEmpty()) {
            strConcat = strConcat.startsWith("/") ? ViewConstants.TMDB_IMAGE_URL_ORIGINAL.concat(strConcat) : strConcat.replace("/w500/", "/original/").replace("/w780/", "/original/");
        }
        ViewGlideUrl viewGlideUrl = new ViewGlideUrl(strConcat);
        q5.e eVar = new q5.e() { // from class: xyz.stream.view.CustomImageView.1
            @Override // q5.e
            public boolean onLoadFailed(@Nullable GlideException glideException, Object obj, com.bumptech.glide.request.target.j jVar, boolean z10) {
                if (glideException != null) {
                    ArrayList arrayList = new ArrayList();
                    GlideException.a(glideException, arrayList);
                    int size = arrayList.size();
                    int i12 = 0;
                    while (i12 < size) {
                        int i13 = i12 + 1;
                        i12 = i13;
                    }
                }
                CustomImageView customImageView = CustomImageView.this;
                Request request2 = request;
                customImageView.setError(request2);
                OnImageLoadListener onImageLoadListener2 = request2.f36248j;
                if (onImageLoadListener2 != null) {
                    onImageLoadListener2.onImageFailed();
                }
                RawImageLoadListener rawImageLoadListener3 = request2.f36249k;
                if (rawImageLoadListener3 != null) {
                    rawImageLoadListener3.onImageFailed();
                }
                return false;
            }

            @Override // q5.e
            public boolean onResourceReady(Drawable drawable, Object obj, com.bumptech.glide.request.target.j jVar, DataSource dataSource, boolean z10) {
                CustomImageView customImageView = CustomImageView.this;
                customImageView.removeView(customImageView.f36235b.getRoot());
                customImageView.getClass();
                RawImageLoadListener rawImageLoadListener3 = request.f36249k;
                if (rawImageLoadListener3 == null) {
                    return false;
                }
                rawImageLoadListener3.onImageReady();
                return false;
            }
        };
        String strReplace = request.f36247i ? strConcat.replace("/original/", "/w92/") : "";
        try {
            r rVarV = com.bumptech.glide.c.e(this.f36234a).load((Object) viewGlideUrl).a(fVar);
            rVarV.getClass();
            r rVarListener = ((r) rVarV.m(l5.l.f30160b, Boolean.TRUE)).listener(eVar);
            if (request.f36247i && strReplace.contains("/w92/")) {
                rVarListener = rVarListener.thumbnail((r) ((r) ((r) ((r) com.bumptech.glide.c.e(this.f36234a).load((Object) new ViewGlideUrl(strReplace)).a(new q5.f()).f()).e(com.bumptech.glide.load.engine.o.f5753c)).i(UiUtils.dp2px(92.0f), UiUtils.dp2px(92.0f))).j());
            }
            rVarListener.y(this.f36234a);
        } catch (Exception unused) {
            setError(request);
            OnImageLoadListener onImageLoadListener2 = request.f36248j;
            if (onImageLoadListener2 != null) {
                onImageLoadListener2.onImageFailed();
            }
            RawImageLoadListener rawImageLoadListener3 = request.f36249k;
            if (rawImageLoadListener3 != null) {
                rawImageLoadListener3.onImageFailed();
            }
        }
    }

    public void setImage(String str) {
        load(Request.image(str));
    }

    public void setShimmerImage() {
        removeAllViews();
        addView(this.f36235b.getRoot());
    }

    public CustomImageView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        b(context);
    }

    public CustomImageView(Context context, AttributeSet attributeSet, int i10) {
        super(context, attributeSet, i10);
        b(context);
    }
}
