package com.bytedance.sdk.openadsdk.qhl;

import android.os.SystemClock;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bytedance.sdk.component.utils.wey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
public class fm implements Comparable<fm> {
    private int jnr;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private int f7524lb;
    private long onz;
    private final String ro;
    private int wsy;
    private final ArrayList<Long> fm = new ArrayList<>();
    private final ArrayList<Long> yz = new ArrayList<>();
    private final ArrayList<Long> ajl = new ArrayList<>();
    private final ArrayList<Long> wu = new ArrayList<>();

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    private final HashMap<String, ro> f7523ef = new HashMap<>();
    private int vt = 0;
    private int ku = 0;
    private final HashMap<String, ro> duv = new HashMap<>();
    private int dsz = 0;
    private final ArrayList<String> fhx = new ArrayList<>();

    public fm(String str) {
        this.ro = str;
    }

    public void fm(@NonNull String str, @Nullable String str2) {
        ro roVar;
        ro roVar2;
        ro roVar3;
        ro roVar4;
        ro roVar5;
        ro roVar6;
        str.getClass();
        switch (str) {
            case "landingContinue":
                if (!TextUtils.isEmpty(str2) && (roVar = this.duv.get(str2)) != null) {
                    roVar.yz(SystemClock.elapsedRealtime());
                    break;
                }
                break;
            case "landingPause":
                if (!TextUtils.isEmpty(str2) && (roVar2 = this.duv.get(str2)) != null) {
                    roVar2.lb(SystemClock.elapsedRealtime());
                    break;
                }
                break;
            case "landingStart":
                if (!TextUtils.isEmpty(str2) && this.duv.get(str2) == null) {
                    ro roVar7 = new ro();
                    this.duv.put(str2, roVar7);
                    roVar7.fm(SystemClock.elapsedRealtime());
                    break;
                }
                break;
            case "feed_over":
            case "feed_break":
            case "videoForceBreak":
            case "play_error":
                if (!TextUtils.isEmpty(str2) && (roVar3 = this.f7523ef.get(str2)) != null && roVar3.fm() != ro.jnr) {
                    roVar3.ro(SystemClock.elapsedRealtime());
                    if (com.bytedance.sdk.openadsdk.ajl.fm.fm().fhx()) {
                        this.vt = (int) (roVar3.fm(this.onz, SystemClock.elapsedRealtime()) + ((long) this.vt));
                    }
                    break;
                }
                break;
            case "feed_play":
                this.ajl.add(Long.valueOf(SystemClock.elapsedRealtime()));
                if (com.bytedance.sdk.openadsdk.ajl.fm.fm().wu()) {
                    this.wsy++;
                }
                if (!TextUtils.isEmpty(str2) && this.f7523ef.get(str2) == null) {
                    ro roVar8 = new ro();
                    this.f7523ef.put(str2, roVar8);
                    roVar8.fm(SystemClock.elapsedRealtime());
                    break;
                }
                break;
            case "show":
                this.fm.add(Long.valueOf(SystemClock.elapsedRealtime()));
                if (com.bytedance.sdk.openadsdk.ajl.fm.fm().ajl()) {
                    this.f7524lb++;
                    break;
                }
                break;
            case "click":
                if (!this.fhx.contains(str2)) {
                    if (this.fhx.size() > 50) {
                        this.fhx.subList(0, 25).clear();
                    }
                    this.fhx.add(str2);
                    this.yz.add(Long.valueOf(SystemClock.elapsedRealtime()));
                    if (com.bytedance.sdk.openadsdk.ajl.fm.fm().wsy()) {
                        this.jnr++;
                    }
                    break;
                }
                break;
            case "feed_continue":
                if (!TextUtils.isEmpty(str2) && (roVar4 = this.f7523ef.get(str2)) != null) {
                    roVar4.yz(SystemClock.elapsedRealtime());
                    break;
                }
                break;
            case "feed_pause":
                if (!TextUtils.isEmpty(str2) && (roVar5 = this.f7523ef.get(str2)) != null) {
                    roVar5.lb(SystemClock.elapsedRealtime());
                    break;
                }
                break;
            case "landingFinish":
                if (!TextUtils.isEmpty(str2) && (roVar6 = this.duv.get(str2)) != null && roVar6.fm() != ro.jnr) {
                    roVar6.ro(SystemClock.elapsedRealtime());
                    if (com.bytedance.sdk.openadsdk.ajl.fm.fm().onz()) {
                        this.dsz = (int) (roVar6.fm(this.onz, SystemClock.elapsedRealtime()) + ((long) this.dsz));
                    }
                    break;
                }
                break;
            case "videoPercent30":
                if (com.bytedance.sdk.openadsdk.ajl.fm.fm().qhl()) {
                    this.ku++;
                    break;
                }
                break;
            case "dislike":
                this.wu.add(Long.valueOf(SystemClock.elapsedRealtime()));
                break;
        }
    }

    public String ro() {
        return this.ro;
    }

    /* JADX WARN: Removed duplicated region for block: B:43:0x0107 A[PHI: r10 r14
      0x0107: PHI (r10v3 int) = (r10v2 int), (r10v5 int), (r10v5 int) binds: [B:34:0x00e6, B:39:0x00fa, B:41:0x00ff] A[DONT_GENERATE, DONT_INLINE]
      0x0107: PHI (r14v2 long) = (r14v1 long), (r14v4 long), (r14v4 long) binds: [B:34:0x00e6, B:39:0x00fa, B:41:0x00ff] A[DONT_GENERATE, DONT_INLINE]] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private void ro(@NonNull JSONObject jSONObject, JSONObject jSONObject2) throws JSONException {
        int i10;
        int i11;
        int i12;
        long j10;
        HashSet hashSet;
        HashSet hashSet2;
        HashSet hashSet3;
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        HashSet hashSet4 = new HashSet();
        HashSet hashSet5 = new HashSet();
        int[] iArrDuv = com.bytedance.sdk.openadsdk.ajl.fm.fm().duv();
        long j11 = 60000;
        if (iArrDuv != null) {
            int i13 = 0;
            while (i13 < iArrDuv.length) {
                int i14 = iArrDuv[i13];
                long j12 = jElapsedRealtime - (((long) i14) * j11);
                Iterator<String> it = this.duv.keySet().iterator();
                long j13 = 0;
                while (it.hasNext()) {
                    String next = it.next();
                    Iterator<String> it2 = it;
                    ro roVar = this.duv.get(next);
                    if (roVar != null) {
                        long jFm = roVar.fm(j12, jElapsedRealtime);
                        j13 += jFm;
                        if (jFm <= 0 && i13 == iArrDuv.length - 1) {
                            hashSet4.add(next);
                        }
                    }
                    it = it2;
                }
                if (j13 != 0) {
                    jSONObject.put("lp_stay_t_".concat(String.valueOf(i14)), j13);
                    hashSet2 = hashSet4;
                    hashSet3 = hashSet5;
                    long jOptInt = ((long) jSONObject2.optInt("lp_stay_t_".concat(String.valueOf(i14)))) + j13;
                    if (jOptInt != 0) {
                        jSONObject2.put("lp_stay_t_".concat(String.valueOf(i14)), jOptInt);
                    }
                } else {
                    hashSet2 = hashSet4;
                    hashSet3 = hashSet5;
                }
                i13++;
                hashSet4 = hashSet2;
                hashSet5 = hashSet3;
                j11 = 60000;
            }
        }
        HashSet hashSet6 = hashSet4;
        HashSet hashSet7 = hashSet5;
        int[] iArrDsz = com.bytedance.sdk.openadsdk.ajl.fm.fm().dsz();
        if (iArrDsz != null) {
            int i15 = 0;
            while (i15 < iArrDsz.length) {
                int i16 = iArrDsz[i15];
                long j14 = jElapsedRealtime - (((long) i16) * 60000);
                Iterator<String> it3 = this.f7523ef.keySet().iterator();
                int i17 = 0;
                long j15 = 0;
                while (it3.hasNext()) {
                    String next2 = it3.next();
                    Iterator<String> it4 = it3;
                    ro roVar2 = this.f7523ef.get(next2);
                    if (roVar2 != null) {
                        long jFm2 = roVar2.fm(j14, jElapsedRealtime);
                        j15 += jFm2;
                        if (jFm2 > 20000) {
                            i17++;
                        }
                        if (jFm2 > 0 || i15 != iArrDsz.length - 1) {
                            hashSet = hashSet7;
                        } else {
                            hashSet = hashSet7;
                            hashSet.add(next2);
                        }
                    }
                    hashSet7 = hashSet;
                    it3 = it4;
                }
                HashSet hashSet8 = hashSet7;
                if (j15 != 0) {
                    jSONObject.put("v_stay_t_".concat(String.valueOf(i16)), j15);
                    j10 = jElapsedRealtime;
                    long jOptInt2 = ((long) jSONObject2.optInt("v_stay_t_".concat(String.valueOf(i16)))) + j15;
                    if (jOptInt2 != 0) {
                        jSONObject2.put("v_stay_t_".concat(String.valueOf(i16)), jOptInt2);
                    }
                } else {
                    j10 = jElapsedRealtime;
                }
                if (i17 != 0) {
                    jSONObject.put("v_20s_play_c_".concat(String.valueOf(i16)), i17);
                    int iOptInt = jSONObject2.optInt("v_20s_play_c_".concat(String.valueOf(i16))) + i17;
                    if (iOptInt != 0) {
                        jSONObject2.put("v_20s_play_c_".concat(String.valueOf(i16)), iOptInt);
                    }
                }
                i15++;
                hashSet7 = hashSet8;
                jElapsedRealtime = j10;
            }
        }
        HashSet hashSet9 = hashSet7;
        if (!hashSet6.isEmpty()) {
            Iterator it5 = hashSet6.iterator();
            while (it5.hasNext()) {
                this.duv.remove((String) it5.next());
            }
        }
        if (!hashSet9.isEmpty()) {
            Iterator it6 = hashSet9.iterator();
            while (it6.hasNext()) {
                this.f7523ef.remove((String) it6.next());
            }
        }
        if (com.bytedance.sdk.openadsdk.ajl.fm.fm().fhx() && (i12 = this.vt) != 0) {
            jSONObject.put("v_stay_t_s", i12);
            int iOptInt2 = jSONObject2.optInt("v_stay_t_s") + this.vt;
            if (iOptInt2 != 0) {
                jSONObject2.put("v_stay_t_s", iOptInt2);
            }
        }
        if (com.bytedance.sdk.openadsdk.ajl.fm.fm().onz() && (i11 = this.dsz) != 0) {
            jSONObject.put("lp_stay_t_s", i11);
            int iOptInt3 = jSONObject2.optInt("lp_stay_t_s") + this.dsz;
            if (iOptInt3 != 0) {
                jSONObject2.put("lp_stay_t_s", iOptInt3);
            }
        }
        if (!com.bytedance.sdk.openadsdk.ajl.fm.fm().qhl() || (i10 = this.ku) == 0) {
            return;
        }
        jSONObject.put("v_30p_play_c_s", i10);
        int iOptInt4 = jSONObject2.optInt("v_30p_play_c_s") + this.ku;
        if (iOptInt4 != 0) {
            jSONObject2.put("v_30p_play_c_s", iOptInt4);
        }
    }

    public JSONObject fm(JSONObject jSONObject) {
        JSONObject jSONObject2 = new JSONObject();
        try {
            fm(jSONObject2, jSONObject);
            ro(jSONObject2, jSONObject);
        } catch (Throwable th) {
            wey.lb(th.getMessage(), new Object[0]);
        }
        return jSONObject2;
    }

    private void fm(String str, JSONObject jSONObject, ArrayList<Long> arrayList, int[] iArr, long j10, JSONObject jSONObject2) throws JSONException {
        int size = arrayList.size() - 1;
        int i10 = 0;
        for (int i11 : iArr) {
            long j11 = j10 - (((long) i11) * 60000);
            while (size >= 0 && arrayList.get(size).longValue() >= j11) {
                i10++;
                size--;
            }
            if (i10 != 0) {
                jSONObject.put(str + i11, i10);
                int iOptInt = jSONObject2.optInt(str + i11) + i10;
                if (iOptInt != 0) {
                    jSONObject2.put(str + i11, iOptInt);
                }
            }
        }
        while (size >= 0) {
            arrayList.remove(0);
            size--;
        }
    }

    private void fm(@NonNull JSONObject jSONObject, JSONObject jSONObject2) throws JSONException {
        int i10;
        int i11;
        int i12;
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        fm("show_c_", jSONObject, this.fm, com.bytedance.sdk.openadsdk.ajl.fm.fm().ef(), jElapsedRealtime, jSONObject2);
        fm("click_c_", jSONObject, this.yz, com.bytedance.sdk.openadsdk.ajl.fm.fm().vt(), jElapsedRealtime, jSONObject2);
        fm("v_play_c_", jSONObject, this.ajl, com.bytedance.sdk.openadsdk.ajl.fm.fm().ku(), jElapsedRealtime, jSONObject2);
        fm("dislike_c_", jSONObject, this.wu, com.bytedance.sdk.openadsdk.ajl.fm.fm().irt(), jElapsedRealtime, jSONObject2);
        if (com.bytedance.sdk.openadsdk.ajl.fm.fm().ajl() && (i12 = this.f7524lb) != 0) {
            jSONObject.put("show_c_s", i12);
            int iOptInt = jSONObject2.optInt("show_c_s") + this.f7524lb;
            if (iOptInt != 0) {
                jSONObject2.put("show_c_s", iOptInt);
            }
        }
        if (com.bytedance.sdk.openadsdk.ajl.fm.fm().wsy() && (i11 = this.jnr) != 0) {
            jSONObject.put("click_c_s", i11);
            int iOptInt2 = jSONObject2.optInt("click_c_s") + this.jnr;
            if (iOptInt2 != 0) {
                jSONObject2.put("click_c_s", iOptInt2);
            }
        }
        if (!com.bytedance.sdk.openadsdk.ajl.fm.fm().wu() || (i10 = this.wsy) == 0) {
            return;
        }
        jSONObject.put("v_play_c_s", i10);
        int iOptInt3 = jSONObject2.optInt("v_play_c_s") + this.wsy;
        if (iOptInt3 != 0) {
            jSONObject2.put("v_play_c_s", iOptInt3);
        }
    }

    public void fm() {
        this.onz = SystemClock.elapsedRealtime();
        this.ku = 0;
        this.jnr = 0;
        this.f7524lb = 0;
        this.dsz = 0;
        this.vt = 0;
        this.wsy = 0;
    }

    @Override // java.lang.Comparable
    /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
    public int compareTo(fm fmVar) {
        return fmVar.f7524lb - this.f7524lb;
    }
}
