package com.bytedance.sdk.openadsdk.hi.fm;

import android.os.Handler;
import android.os.SystemClock;
import com.bytedance.sdk.component.utils.wey;
import com.bytedance.sdk.openadsdk.core.model.lse;
import com.bytedance.sdk.openadsdk.utils.bx;
import com.bytedance.sdk.openadsdk.utils.xgn;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
public class ro {
    private final lse ro;
    private int fm = lb.ro;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private final AtomicBoolean f7417lb = new AtomicBoolean(false);
    private final AtomicBoolean yz = new AtomicBoolean(true);
    private final AtomicInteger jnr = new AtomicInteger(0);
    private long ajl = 0;
    private long wsy = 0;
    private final Runnable wu = new Runnable() { // from class: com.bytedance.sdk.openadsdk.hi.fm.ro.3
        @Override // java.lang.Runnable
        public void run() {
            ro.this.yz();
        }
    };

    public ro(lse lseVar) {
        this.ro = lseVar;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void ajl() {
        if (this.jnr.get() != 1 || this.ajl == 0) {
            return;
        }
        Handler handlerFm = fm.fm();
        if (handlerFm != null) {
            handlerFm.removeCallbacks(this.wu);
        }
        this.wsy = (SystemClock.elapsedRealtime() - this.ajl) + this.wsy;
        this.ajl = 0L;
        this.jnr.set(2);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void jnr() {
        if (this.jnr.get() == 2) {
            this.ajl = SystemClock.elapsedRealtime();
            Handler handlerFm = fm.fm();
            if (handlerFm != null) {
                handlerFm.postDelayed(this.wu, ((long) this.fm) - this.wsy);
            }
            this.jnr.set(1);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void yz() {
        if (this.f7417lb.compareAndSet(false, true)) {
            ro();
            final long jElapsedRealtime = (SystemClock.elapsedRealtime() - this.ajl) + this.wsy;
            this.wsy = jElapsedRealtime;
            xgn.ro(new com.bytedance.sdk.component.wu.ro.lb("ev_tracker") { // from class: com.bytedance.sdk.openadsdk.hi.fm.ro.1
                @Override // java.lang.Runnable
                public void run() {
                    String strFm = bx.fm(ro.this.ro);
                    JSONObject jSONObject = new JSONObject();
                    try {
                        jSONObject.put("ev_wait_time_server", ro.this.ro.osn() * 1000);
                        jSONObject.put("ev_wait_time_client", jElapsedRealtime);
                    } catch (JSONException e10) {
                        wey.lb("EvTracker", e10.getMessage());
                    }
                    com.bytedance.sdk.openadsdk.yz.lb.ro(ro.this.ro, strFm, ro.this.ro.pis(), jSONObject);
                }
            });
        }
    }

    public void fm() {
        if (this.jnr.get() != 0) {
            return;
        }
        this.fm = this.ro.osn() * 1000;
        this.jnr.set(1);
        this.ajl = SystemClock.elapsedRealtime();
        Handler handlerFm = fm.fm();
        if (handlerFm != null) {
            handlerFm.postDelayed(this.wu, this.fm);
        }
    }

    public boolean lb() {
        return this.jnr.get() == 3;
    }

    public void ro() {
        this.jnr.set(3);
        Handler handlerFm = fm.fm();
        if (handlerFm != null) {
            handlerFm.removeCallbacks(this.wu);
        }
    }

    public void fm(final int i10) {
        Handler handlerFm;
        if (this.jnr.get() == 3 || (handlerFm = fm.fm()) == null) {
            return;
        }
        handlerFm.post(new Runnable() { // from class: com.bytedance.sdk.openadsdk.hi.fm.ro.2
            @Override // java.lang.Runnable
            public void run() {
                if (i10 == 4 && ro.this.yz.get()) {
                    ro.this.jnr();
                    return;
                }
                int i11 = i10;
                if (i11 == 8) {
                    ro.this.ajl();
                } else if (i11 == 5) {
                    ro.this.yz();
                }
            }
        });
    }

    public void fm(boolean z10) {
        this.yz.set(z10);
    }
}
