package com.bytedance.sdk.openadsdk.utils;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import java.util.concurrent.atomic.AtomicBoolean;

/* JADX INFO: loaded from: classes2.dex */
public class ef implements onz, Runnable {
    private boolean ajl;

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    private fhx f7536ef;
    private long jnr;
    private final Activity ro;
    private final fm wsy;
    private boolean wu;
    private int yz = 5000;
    private final AtomicBoolean vt = new AtomicBoolean(false);

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private final Handler f7537lb = new Handler(Looper.getMainLooper());
    private View fm = jnr();

    public interface fm {
        View fm();

        void ro();
    }

    private ef(Activity activity, fm fmVar) {
        this.wsy = fmVar;
        this.ro = activity;
    }

    private void ajl() {
        this.wu = false;
        this.jnr = SystemClock.elapsedRealtime();
        Handler handler = this.f7537lb;
        if (handler != null) {
            handler.postDelayed(this, this.yz);
        }
    }

    private View jnr() {
        fm fmVar = this.wsy;
        if (fmVar != null) {
            return fmVar.fm();
        }
        return null;
    }

    private void wsy() {
        Activity activity;
        View childAt;
        if (this.ajl || (activity = this.ro) == null || activity.isFinishing() || this.ro.isDestroyed()) {
            return;
        }
        if (this.fm == null) {
            this.fm = jnr();
        }
        View view = this.fm;
        if (view != null) {
            if (ro(view)) {
                fm(this.fm);
                View view2 = this.fm;
                if ((view2 instanceof ViewGroup) && ((ViewGroup) view2).getChildCount() > 0 && (childAt = ((ViewGroup) this.fm).getChildAt(0)) != null && ro(childAt)) {
                    fm(childAt);
                }
                fm fmVar = this.wsy;
                if (fmVar != null) {
                    fmVar.ro();
                }
                fhx fhxVar = this.f7536ef;
                if (fhxVar != null) {
                    fhxVar.fm();
                }
            }
            wu();
        }
        this.ajl = true;
    }

    private void wu() {
        ViewParent parent = this.fm.getParent();
        if (parent instanceof ViewGroup) {
            if (((ViewGroup) parent).indexOfChild(this.fm) != r0.getChildCount() - 1) {
                this.fm.bringToFront();
            }
        }
    }

    @Override // com.bytedance.sdk.openadsdk.utils.onz
    public void fm(int i10) {
        this.yz = i10;
    }

    @Override // com.bytedance.sdk.openadsdk.utils.onz
    public void lb() {
        if (this.ajl) {
            return;
        }
        this.ajl = true;
        Handler handler = this.f7537lb;
        if (handler != null) {
            try {
                handler.removeCallbacks(this);
            } catch (Throwable th) {
                com.bytedance.sdk.component.utils.wey.lb(th.getMessage(), new Object[0]);
            }
        }
    }

    @Override // com.bytedance.sdk.openadsdk.utils.onz
    public void ro() {
        if (this.jnr <= 0 || this.ajl) {
            return;
        }
        if (!this.wu) {
            this.yz = (int) (((long) this.yz) - (SystemClock.elapsedRealtime() - this.jnr));
        }
        this.wu = true;
        if (this.yz <= 0) {
            wsy();
            return;
        }
        Handler handler = this.f7537lb;
        if (handler != null) {
            try {
                handler.removeCallbacks(this);
            } catch (Throwable th) {
                com.bytedance.sdk.component.utils.wey.lb(th.getMessage(), new Object[0]);
            }
        }
    }

    @Override // java.lang.Runnable
    public void run() {
        wsy();
    }

    @Override // com.bytedance.sdk.openadsdk.utils.onz
    public void yz() {
        this.vt.set(false);
        this.wu = false;
        this.ajl = false;
        this.yz = 0;
        Handler handler = this.f7537lb;
        if (handler != null) {
            try {
                handler.removeCallbacks(this);
            } catch (Throwable th) {
                com.bytedance.sdk.component.utils.wey.lb(th.getMessage(), new Object[0]);
            }
        }
    }

    @Override // com.bytedance.sdk.openadsdk.utils.onz
    public void fm(fhx fhxVar) {
        this.f7536ef = fhxVar;
    }

    public static onz fm(Activity activity, fm fmVar) {
        return new ef(activity, fmVar);
    }

    @Override // com.bytedance.sdk.openadsdk.utils.onz
    public void fm(long j10) {
        this.vt.get();
        if (this.vt.compareAndSet(false, true)) {
            if (j10 < 0) {
                j10 = 0;
            }
            this.yz = (int) (((long) this.yz) + j10);
            ajl();
        }
    }

    private boolean ro(View view) {
        if (view == null) {
            return true;
        }
        return !view.isShown() || ((double) view.getAlpha()) <= 0.9d;
    }

    @Override // com.bytedance.sdk.openadsdk.utils.onz
    public void fm(com.bytedance.sdk.openadsdk.component.reward.fm.ro roVar, long j10) {
        if (roVar == null) {
            return;
        }
        if (j10 < 0) {
            j10 = 0;
        }
        int iFm = com.bytedance.sdk.openadsdk.hlt.jnr.fm(roVar);
        if (iFm >= 0) {
            fm(iFm);
            fm(j10);
        }
    }

    @Override // com.bytedance.sdk.openadsdk.utils.onz
    public void fm() {
        if (this.jnr == 0 || !this.wu) {
            return;
        }
        ajl();
    }

    private void fm(View view) {
        view.setVisibility(0);
        view.setAlpha(1.0f);
    }
}
