package com.bytedance.sdk.component.adexpress.YFl.Sg;

import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;
import android.util.Log;
import android.util.LruCache;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
/* loaded from: classes7.dex */
public class vc {
    private static volatile vc Sg = null;
    public static int YFl = 20;
    private volatile ConcurrentHashMap<String, com.bytedance.sdk.component.adexpress.YFl.tN.tN> wN;
    private final Object AlY = new Object();
    private AtomicBoolean vc = new AtomicBoolean(false);
    private LruCache<String, com.bytedance.sdk.component.adexpress.YFl.tN.Sg> DSW = new LruCache<String, com.bytedance.sdk.component.adexpress.YFl.tN.Sg>(YFl) { // from class: com.bytedance.sdk.component.adexpress.YFl.Sg.vc.1
        /* JADX INFO: Access modifiers changed from: protected */
        @Override // android.util.LruCache
        /* renamed from: YFl */
        public int sizeOf(String str, com.bytedance.sdk.component.adexpress.YFl.tN.Sg sg) {
            return 1;
        }
    };
    private Set<String> tN = Collections.synchronizedSet(new HashSet());

    public static void YFl(int i) {
        YFl = i;
    }

    public static String tN() {
        return "CREATE TABLE IF NOT EXISTS template_diff_new (_id INTEGER PRIMARY KEY AUTOINCREMENT,rit TEXT ,id TEXT UNIQUE,md5 TEXT ,url TEXT , data TEXT , version TEXT , update_time TEXT)";
    }

    public static vc YFl() {
        if (Sg == null) {
            synchronized (vc.class) {
                if (Sg == null) {
                    Sg = new vc();
                }
            }
        }
        return Sg;
    }

    private vc() {
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public com.bytedance.sdk.component.adexpress.YFl.tN.Sg YFl(String str) {
        com.bytedance.sdk.component.adexpress.YFl.tN.Sg sg;
        com.bytedance.sdk.component.adexpress.YFl.tN.Sg YFl2;
        if (TextUtils.isEmpty(str) || com.bytedance.sdk.component.adexpress.YFl.YFl.YFl.YFl().Sg() == null) {
            return null;
        }
        synchronized (this.AlY) {
            sg = this.DSW.get(String.valueOf(str));
        }
        if (sg != null) {
            return sg;
        }
        Cursor YFl3 = com.bytedance.sdk.component.adexpress.YFl.YFl.YFl.YFl().Sg().YFl("template_diff_new", null, "id=?", new String[]{str}, null, null, null);
        if (YFl3 != null) {
            try {
                if (YFl3.moveToFirst()) {
                    do {
                        String string = YFl3.getString(YFl3.getColumnIndex("rit"));
                        String string2 = YFl3.getString(YFl3.getColumnIndex("id"));
                        String string3 = YFl3.getString(YFl3.getColumnIndex("md5"));
                        String string4 = YFl3.getString(YFl3.getColumnIndex("url"));
                        String string5 = YFl3.getString(YFl3.getColumnIndex("data"));
                        String string6 = YFl3.getString(YFl3.getColumnIndex("version"));
                        YFl2 = new com.bytedance.sdk.component.adexpress.YFl.tN.Sg().YFl(string).Sg(string2).tN(string3).AlY(string4).wN(string5).vc(string6).YFl(Long.valueOf(YFl3.getLong(YFl3.getColumnIndex("update_time"))));
                        synchronized (this.AlY) {
                            this.DSW.put(string2, YFl2);
                        }
                        this.tN.add(string2);
                    } while (YFl3.moveToNext());
                    return YFl2;
                }
            } finally {
                try {
                } finally {
                }
            }
        }
        return null;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Set<String> Sg(String str) {
        if (!TextUtils.isEmpty(str) && com.bytedance.sdk.component.adexpress.YFl.YFl.YFl.YFl().Sg() != null) {
            HashSet hashSet = new HashSet();
            Cursor YFl2 = com.bytedance.sdk.component.adexpress.YFl.YFl.YFl.YFl().Sg().YFl("template_diff_new", null, "rit=?", new String[]{str}, null, null, null);
            try {
                if (YFl2 != null) {
                    try {
                        if (YFl2.moveToFirst()) {
                            do {
                                hashSet.add(YFl2.getString(YFl2.getColumnIndex("id")));
                            } while (YFl2.moveToNext());
                            return hashSet;
                        }
                    } catch (Exception e) {
                        Log.e("TmplDbHelper", "", e);
                    }
                }
            } finally {
                YFl2.close();
            }
        }
        return null;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* JADX WARN: Code restructure failed: missing block: B:32:0x0100, code lost:
        if (r3 == null) goto L7;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public java.util.List<com.bytedance.sdk.component.adexpress.YFl.tN.Sg> Sg() {
        /*
            Method dump skipped, instructions count: 267
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: com.bytedance.sdk.component.adexpress.YFl.Sg.vc.Sg():java.util.List");
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void YFl(com.bytedance.sdk.component.adexpress.YFl.tN.Sg sg, boolean z) {
        if (sg == null || com.bytedance.sdk.component.adexpress.YFl.YFl.YFl.YFl().Sg() == null || TextUtils.isEmpty(sg.Sg())) {
            return;
        }
        Cursor YFl2 = com.bytedance.sdk.component.adexpress.YFl.YFl.YFl.YFl().Sg().YFl("template_diff_new", null, "id=?", new String[]{sg.Sg()}, null, null, null);
        boolean z2 = YFl2 != null && YFl2.getCount() > 0;
        if (YFl2 != null) {
            try {
                r2 = YFl2.moveToFirst() ? YFl2.getString(YFl2.getColumnIndex("rit")) : null;
                YFl2.close();
            } catch (Throwable unused) {
            }
        }
        ContentValues contentValues = new ContentValues();
        contentValues.put("rit", sg.YFl());
        contentValues.put("id", sg.Sg());
        contentValues.put("md5", sg.tN());
        contentValues.put("url", sg.AlY());
        contentValues.put("data", sg.wN());
        contentValues.put("version", sg.vc());
        contentValues.put("update_time", sg.DSW());
        if (z2) {
            com.bytedance.sdk.component.adexpress.YFl.YFl.YFl.YFl().Sg().YFl("template_diff_new", contentValues, "id=?", new String[]{sg.Sg()});
        } else {
            com.bytedance.sdk.component.adexpress.YFl.YFl.YFl.YFl().Sg().YFl("template_diff_new", contentValues);
        }
        synchronized (this.AlY) {
            this.DSW.put(sg.Sg(), sg);
        }
        this.tN.add(sg.Sg());
        if (z) {
            return;
        }
        try {
            if (com.bytedance.sdk.component.adexpress.YFl.YFl.YFl.YFl().vc() == null) {
                return;
            }
            if (this.wN == null) {
                this.wN = new ConcurrentHashMap<>();
            }
            com.bytedance.sdk.component.adexpress.YFl.tN.tN tNVar = new com.bytedance.sdk.component.adexpress.YFl.tN.tN(sg.YFl(), sg.Sg(), sg.tN());
            this.wN.put(sg.Sg(), tNVar);
            if (r2 != null) {
                com.bytedance.sdk.component.adexpress.YFl.YFl.YFl.YFl().vc();
                tNVar.Sg();
            }
            com.bytedance.sdk.component.adexpress.YFl.YFl.YFl.YFl().vc();
            sg.YFl();
        } catch (Throwable unused2) {
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void YFl(Set<String> set) {
        if (set == null || set.isEmpty() || com.bytedance.sdk.component.adexpress.YFl.YFl.YFl.YFl().Sg() == null) {
            return;
        }
        String[] strArr = (String[]) set.toArray(new String[set.size()]);
        if (strArr.length > 0) {
            for (int i = 0; i < strArr.length; i++) {
                AlY(strArr[i]);
                com.bytedance.sdk.component.adexpress.YFl.YFl.YFl.YFl().Sg().YFl("template_diff_new", "id=?", new String[]{strArr[i]});
                tN(strArr[i]);
            }
        }
    }

    void tN(String str) {
        com.bytedance.sdk.component.adexpress.YFl.tN.tN tNVar;
        try {
            if (this.wN == null || this.wN.isEmpty() || (tNVar = this.wN.get(str)) == null) {
                return;
            }
            if (!TextUtils.isEmpty(tNVar.YFl()) && com.bytedance.sdk.component.adexpress.YFl.YFl.YFl.YFl().vc() != null) {
                com.bytedance.sdk.component.adexpress.YFl.YFl.YFl.YFl().vc();
            }
            this.wN.remove(str);
        } catch (Throwable unused) {
        }
    }

    private void AlY(String str) {
        LruCache<String, com.bytedance.sdk.component.adexpress.YFl.tN.Sg> lruCache;
        if (TextUtils.isEmpty(str) || (lruCache = this.DSW) == null || lruCache.size() <= 0) {
            return;
        }
        synchronized (this.AlY) {
            this.DSW.remove(str);
        }
    }
}
