package com.bytedance.android.live.core.monitor;

import X.2K7;
import X.2Pr;
import X.2te;
import X.2th;
import X.2us;
import X.7he;
import X.C09630aP;
import X.C10000b0;
import X.C10510bp;
import X.C10520bq;
import X.C10540bs;
import X.C10550bt;
import X.C10560bu;
import X.C10590bx;
import X.C10620c0;
import X.C46221sK;
import X.C46231sL;
import X.Crj;
import X.LsD;
import X.M12;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import com.bytedance.covode.number.Covode;
import com.bytedance.keva.Keva;
import com.google.gson.Gson;
import com.google.gson.a.c;
import com.ss.avframework.livestreamv2.utils.JsonUtils;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import kotlin.h.b.n;
import org.json.JSONObject;

public class PersistenceReporter implements C10590bx {
    public static final ConcurrentHashMap<String, C10590bx> LJIIIZ = new ConcurrentHashMap<>();
    public static final Keva LJIIJ = Keva.getRepo("PersistenceReporter-config");
    public static final Crj LJIIJJI = 7he.LIZ(C46221sK.LIZ);
    public static final Companion LJIIL = new Companion((byte) 0);
    public long LIZ;
    public long LIZIZ;
    public long LIZJ;
    public long LIZLLL;
    public long LJ;
    public long LJFF;
    public long LJI;
    public final Keva LJII;
    public final HandlerThread LJIIIIZZ;
    public final Crj LJIILIIL;
    public final String LJIILJJIL;

    public static final class Companion {
        static {
            Covode.recordClassIndex(5558);
        }

        public final long LIZ() {
            return ((Number) PersistenceReporter.LJIIJJI.getValue()).longValue();
        }

        public static final class InnerRecord {
            @c(LIZ = "create_time")
            public long createTimeStamp = System.currentTimeMillis();
            @c(LIZ = "discard_next_boot")
            public boolean discardWhenNextBoot;
            @c(LIZ = "start_id")
            public long startID = PersistenceReporter.LJIIL.LIZ();
            @c(LIZ = "status")
            public int status = 1;
            @c(LIZ = "value")
            public String value = "{}";

            static {
                Covode.recordClassIndex(5559);
            }

            public final long getCreateTimeStamp() {
                return this.createTimeStamp;
            }

            public final boolean getDiscardWhenNextBoot() {
                return this.discardWhenNextBoot;
            }

            public final long getStartID() {
                return this.startID;
            }

            public final int getStatus() {
                return this.status;
            }

            public final String getValue() {
                return this.value;
            }

            public final void setCreateTimeStamp(long j) {
                this.createTimeStamp = j;
            }

            public final void setDiscardWhenNextBoot(boolean z) {
                this.discardWhenNextBoot = z;
            }

            public final void setStartID(long j) {
                this.startID = j;
            }

            public final void setStatus(int i) {
                this.status = i;
            }

            public final void setValue(String str) {
                M12.LIZ(str);
                this.value = str;
            }
        }

        public Companion() {
        }

        public /* synthetic */ Companion(byte b) {
            this();
        }
    }

    private final Handler LIZJ() {
        return (Handler) this.LJIILIIL.getValue();
    }

    public static boolean LIZLLL() {
        try {
            return 2K7.LIZ.LIZJ();
        } catch (Exception unused) {
            return false;
        }
    }

    private final void LJ() {
        LIZJ().sendEmptyMessage(10005);
    }

    public final void LIZIZ() {
        LIZJ().sendEmptyMessageDelayed(10000, this.LIZ);
    }

    static {
        Covode.recordClassIndex(5556);
    }

    public final void LIZ() {
        Keva keva = this.LJII;
        n.LIZIZ(keva, "");
        Map all = keva.getAll();
        if (!all.isEmpty()) {
            2us r7 = new 2us();
            r7.element = 0;
            n.LIZIZ(all, "");
            for (Map.Entry entry : all.entrySet()) {
                if (r7.element >= 10) {
                    LIZJ().postDelayed(new C10510bp(this, r7), 5000);
                    return;
                }
                C10000b0.LJ();
                if (LIZLLL()) {
                    if (entry.getValue() instanceof String) {
                        Object key = entry.getKey();
                        n.LIZIZ(key, "");
                        Companion.InnerRecord LIZIZ2 = LIZIZ((String) key, String.valueOf(entry.getValue()));
                        if (LIZIZ2 != null) {
                            long currentTimeMillis = System.currentTimeMillis();
                            if (LIZIZ2.getCreateTimeStamp() > currentTimeMillis) {
                                this.LJII.erase((String) entry.getKey());
                            } else if (Math.abs(currentTimeMillis - LIZIZ2.getCreateTimeStamp()) > this.LIZJ) {
                                this.LJII.erase((String) entry.getKey());
                            } else if (LIZIZ2.getStartID() != LJIIL.LIZ()) {
                                if (!LIZIZ2.getDiscardWhenNextBoot()) {
                                    LIZ(LIZIZ2, false);
                                }
                                this.LJII.erase((String) entry.getKey());
                            } else if (this.LJ > 0 && Math.abs(currentTimeMillis - LIZIZ2.getCreateTimeStamp()) > this.LJ) {
                                LIZ(LIZIZ2, false);
                                r7.element++;
                                this.LJII.erase((String) entry.getKey());
                            }
                        }
                    }
                } else {
                    return;
                }
            }
        }
    }

    public final void LIZ(String str) {
        M12.LIZ(str);
        Message obtain = Message.obtain();
        obtain.what = 10002;
        obtain.obj = str;
        LIZJ().sendMessage(obtain);
    }

    public final void LIZIZ(String str) {
        M12.LIZ(str);
        LIZJ().post(new C10540bs(this, str));
    }

    public PersistenceReporter(String str) {
        this.LJIILJJIL = str;
        this.LIZ = 300000;
        this.LIZIZ = 2048;
        this.LIZJ = Long.MAX_VALUE;
        this.LJ = -1;
        this.LJFF = -1;
        this.LJI = -1;
        this.LJII = Keva.getRepo(str);
        HandlerThread handlerThread = new HandlerThread("PersistenceReporter-" + str);
        this.LJIIIIZZ = handlerThread;
        this.LJIILIIL = 7he.LIZ(new C46231sL(this));
        handlerThread.start();
        LIZIZ();
        if (this.LIZLLL >= 0) {
            LIZJ().sendEmptyMessageDelayed(10004, this.LIZLLL);
        }
        LIZJ().postDelayed(new Runnable(this) {
            public final /* synthetic */ PersistenceReporter LIZ;

            static {
                Covode.recordClassIndex(5557);
            }

            {
                this.LIZ = r1;
            }

            public final void run() {
                this.LIZ.LIZ();
            }
        }, 5000);
    }

    public /* synthetic */ PersistenceReporter(String str, byte b) {
        this(str);
    }

    public final void LIZ(String str, long j) {
        M12.LIZ(str);
        LIZIZ(str, 600000);
    }

    private final String LIZJ(String str, String str2) {
        JSONObject jSONObject = new JSONObject(str);
        JsonUtils.mergeJson(jSONObject, new JSONObject(str2));
        String jSONObject2 = jSONObject.toString();
        n.LIZIZ(jSONObject2, "");
        return jSONObject2;
    }

    public final void LIZ(String str, LsD<? super String, ? super Long, 2Pr> lsD) {
        M12.LIZ(str, lsD);
        LIZJ().post(new C10520bq(this, str, lsD));
    }

    public final Companion.InnerRecord LIZIZ(String str, String str2) {
        Object obj;
        try {
            obj = (Companion.InnerRecord) new Gson().LIZ(str2, Companion.InnerRecord.class);
            2te.constructor-impl(obj);
        } catch (Throwable th) {
            obj = 2th.LIZ(th);
            2te.constructor-impl(obj);
        }
        if (2te.isFailure-impl(obj)) {
            obj = null;
        }
        Companion.InnerRecord innerRecord = (Companion.InnerRecord) obj;
        if (innerRecord == null) {
            this.LJII.erase(str);
        }
        return innerRecord;
    }

    public final void LIZ(String str, String str2) {
        M12.LIZ(str, str2);
        LIZJ().post(new C10550bt(this, str, str2));
    }

    public final void LIZIZ(String str, long j) {
        if (j > 0) {
            Message obtain = Message.obtain();
            obtain.what = 10001;
            obtain.obj = str;
            LIZJ().sendMessageDelayed(obtain, j);
        }
    }

    private final void LIZ(Companion.InnerRecord innerRecord, boolean z) {
        Object obj;
        if (innerRecord.getValue().length() != 0 && !n.LIZ(innerRecord.getValue(), "{}")) {
            try {
                obj = new JSONObject(innerRecord.getValue());
                2te.constructor-impl(obj);
            } catch (Throwable th) {
                obj = 2th.LIZ(th);
                2te.constructor-impl(obj);
            }
            if (2te.isFailure-impl(obj)) {
                obj = null;
            }
            JSONObject jSONObject = (JSONObject) obj;
            if (z && jSONObject != null) {
                jSONObject.put("expire_info", new JSONObject("{expire:1,time:" + this.LJ + '}'));
            }
            if (innerRecord.getStartID() == LJIIL.LIZ() || innerRecord.getStatus() == 0) {
                if (jSONObject == null) {
                    return;
                }
            } else if (jSONObject != null) {
                jSONObject.put("app_killed", true);
            } else {
                return;
            }
            C10620c0.LIZ(this.LJIILJJIL, innerRecord.getStatus(), jSONObject);
        }
    }

    public final void LIZ(String str, boolean z) {
        if (str.length() != 0 && this.LJII.contains(str)) {
            C10000b0.LJ();
            if (LIZLLL()) {
                String string = this.LJII.getString(str, "{}");
                n.LIZIZ(string, "");
                Companion.InnerRecord LIZIZ2 = LIZIZ(str, string);
                if (LIZIZ2 != null) {
                    if (z) {
                        LIZIZ2.setStatus(1);
                    }
                    LIZ(LIZIZ2, z);
                    this.LJII.erase(str);
                    return;
                }
                return;
            }
            return;
        }
        C09630aP.LIZ(3, "PersistenceReporter", "report specific record id empty  recordId =".concat(String.valueOf(str)));
    }

    public final void LIZIZ(String str, String str2, boolean z, boolean z2, long j, boolean z3) {
        Companion.InnerRecord LIZIZ2;
        C09630aP.LIZ(3, "PersistenceReporter", "updateValue,recordId =" + str + ",isEnd =" + z + " jsonString length = " + str2.length());
        if (!this.LJII.contains(str)) {
            LIZIZ2 = new Companion.InnerRecord();
            LIZIZ(str, j);
            LJ();
        } else {
            String string = this.LJII.getString(str, "{}");
            n.LIZIZ(string, "");
            LIZIZ2 = LIZIZ(str, string);
            if (LIZIZ2 == null) {
                LIZIZ2 = new Companion.InnerRecord();
            }
        }
        if (LIZIZ2.getStatus() != 0) {
            if (z3) {
                LIZIZ2.setDiscardWhenNextBoot(true);
            }
            if (z2) {
                str2 = LIZJ(LIZIZ2.getValue(), str2);
            }
            LIZIZ2.setValue(str2);
            LIZIZ2.setStatus(z ^ true ? 1 : 0);
            this.LJII.storeString(str, new Gson().LIZIZ(LIZIZ2));
        }
    }

    public final boolean LIZ(String str, String str2, boolean z, boolean z2, long j, boolean z3) {
        String str3 = str2;
        String str4 = str;
        M12.LIZ(str4, str3);
        if (str4.length() == 0) {
            return false;
        }
        Thread currentThread = Thread.currentThread();
        n.LIZIZ(currentThread, "");
        boolean z4 = z;
        boolean z5 = z2;
        boolean z6 = z3;
        long j2 = j;
        if (currentThread.getId() != this.LJIIIIZZ.getId()) {
            StringBuilder sb = new StringBuilder("PersistenceReporter_");
            Thread currentThread2 = Thread.currentThread();
            n.LIZIZ(currentThread2, "");
            sb.append(currentThread2.getId());
            sb.append(": force async");
            String sb2 = sb.toString();
            C09630aP.LIZ(3, "PersistenceReporter", sb2);
            C10620c0.LIZ(new Throwable(), sb2);
            LIZJ().post(new C10560bu(this, str4, str3, z4, z5, j2, z6));
        } else {
            LIZIZ(str4, str3, z4, z5, j2, z6);
        }
        return true;
    }
}
