package pq;

import android.content.ContentValues;
import android.database.Cursor;
import com.google.android.gms.measurement.api.AppMeasurementSdk;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import rq.a;
import sq.a;

/* loaded from: classes3.dex */
public class e extends pq.a {

    /* renamed from: b, reason: collision with root package name */
    private final c f33053b;

    /* loaded from: classes3.dex */
    public static class a {

        /* renamed from: a, reason: collision with root package name */
        private final Class f33054a;

        /* renamed from: b, reason: collision with root package name */
        private final e f33055b;

        /* renamed from: c, reason: collision with root package name */
        private String f33056c;

        /* renamed from: d, reason: collision with root package name */
        private String[] f33057d;

        /* renamed from: e, reason: collision with root package name */
        private String f33058e;

        /* renamed from: f, reason: collision with root package name */
        private String f33059f;

        /* renamed from: g, reason: collision with root package name */
        private String f33060g;

        /* renamed from: h, reason: collision with root package name */
        private String[] f33061h;

        /* renamed from: i, reason: collision with root package name */
        private String f33062i = null;

        /* renamed from: j, reason: collision with root package name */
        private String f33063j = null;

        /* renamed from: k, reason: collision with root package name */
        private boolean f33064k = false;

        a(Class cls, e eVar) {
            this.f33054a = cls;
            this.f33055b = eVar;
        }

        public a a(long j10) {
            this.f33056c = "_id = ?";
            this.f33057d = new String[]{String.valueOf(j10)};
            c(1);
            return this;
        }

        public Object b() {
            return e().e();
        }

        public a c(int i10) {
            if (i10 < 1) {
                throw new IllegalArgumentException("Limit must be greater or equal to 1");
            }
            this.f33062i = String.valueOf(i10);
            return this;
        }

        public List d() {
            return e().i();
        }

        public g e() {
            String str;
            String str2 = this.f33062i;
            if (str2 == null || (str = this.f33063j) == null) {
                String str3 = this.f33063j;
                if (str3 != null) {
                    this.f33062i = String.format("%s,%d", str3, Long.MAX_VALUE);
                }
            } else {
                this.f33062i = String.format("%s,%s", str, str2);
            }
            return this.f33055b.k(this.f33054a, this.f33061h, this.f33056c, this.f33057d, this.f33059f, this.f33060g, this.f33058e, this.f33062i, this.f33064k);
        }

        public a f(String str, String... strArr) {
            this.f33056c = str;
            this.f33057d = strArr;
            return this;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public e(b bVar, c cVar) {
        super(bVar);
        this.f33053b = cVar;
    }

    private boolean g(c cVar, String str, List list) {
        qq.d dVar;
        Cursor d10 = cVar.d("select name, sql from sqlite_master where type = 'index' and tbl_name = '" + str + "' and name like '_cb%'", null);
        HashMap hashMap = new HashMap();
        while (d10.moveToNext()) {
            hashMap.put(d10.getString(0), d10.getString(1));
        }
        d10.close();
        a.C0846a c0846a = new a.C0846a();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            a.C0824a c0824a = (a.C0824a) it.next();
            if (c0824a.f35087b != a.b.JOIN && (dVar = c0824a.f35088c) != null) {
                c0846a.c(str, c0824a.f35086a, dVar);
            }
        }
        Map f10 = c0846a.f();
        Set keySet = hashMap.keySet();
        Set keySet2 = f10.keySet();
        HashSet hashSet = new HashSet(keySet);
        hashSet.removeAll(keySet2);
        Iterator it2 = hashSet.iterator();
        boolean z10 = false;
        while (it2.hasNext()) {
            cVar.execSQL("drop index if exists " + ((String) it2.next()));
            z10 |= true;
        }
        HashSet hashSet2 = new HashSet(keySet2);
        hashSet2.removeAll(keySet);
        Iterator it3 = hashSet2.iterator();
        while (it3.hasNext()) {
            cVar.execSQL(((sq.a) f10.get((String) it3.next())).a(str));
            z10 |= true;
        }
        HashSet<String> hashSet3 = new HashSet(keySet2);
        hashSet3.retainAll(keySet);
        for (String str2 : hashSet3) {
            String str3 = (String) hashMap.get(str2);
            String b10 = ((sq.a) f10.get(str2)).b(str, false);
            if (!str3.equalsIgnoreCase(b10)) {
                cVar.execSQL("drop index if exists " + str2);
                cVar.execSQL(b10);
                z10 |= true;
            }
        }
        return z10;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public g k(Class cls, String[] strArr, String str, String[] strArr2, String str2, String str3, String str4, String str5, boolean z10) {
        rq.a a10 = a(cls);
        return new g(this.f33053b.c(z10, l(a10.b()), strArr, str, strArr2, str2, str3, str4, str5), a10);
    }

    private String l(String str) {
        return "'" + str + "'";
    }

    boolean c(c cVar, String str, List list) {
        StringBuilder sb2 = new StringBuilder("create table '");
        sb2.append(str);
        sb2.append("' (_id integer primary key autoincrement");
        a.C0846a c0846a = new a.C0846a();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            a.C0824a c0824a = (a.C0824a) it.next();
            if (c0824a.f35087b != a.b.JOIN) {
                String str2 = c0824a.f35086a;
                if (!str2.equals("_id")) {
                    sb2.append(", '");
                    sb2.append(str2);
                    sb2.append("'");
                    sb2.append(" ");
                    sb2.append(c0824a.f35087b.toString());
                }
                qq.d dVar = c0824a.f35088c;
                if (dVar != null) {
                    c0846a.c(str, str2, dVar);
                }
            }
        }
        sb2.append(");");
        cVar.execSQL(sb2.toString());
        Iterator it2 = c0846a.e().iterator();
        while (it2.hasNext()) {
            cVar.execSQL(((sq.a) it2.next()).a(str));
        }
        return true;
    }

    public void d() {
        Iterator it = this.f33048a.d().iterator();
        while (it.hasNext()) {
            rq.a b10 = this.f33048a.b((Class) it.next());
            c(this.f33053b, b10.b(), b10.e());
        }
    }

    public int e(Class cls, String str, String... strArr) {
        return this.f33053b.f(l(a(cls).b()), str, strArr);
    }

    public boolean f(Object obj) {
        Class<?> cls = obj.getClass();
        Long c10 = a(cls).c(obj);
        return c10 != null && e(cls, "_id = ?", String.valueOf(c10)) > 0;
    }

    public long h(Class cls, ContentValues contentValues) {
        rq.a a10 = a(cls);
        Long asLong = contentValues.getAsLong("_id");
        if (asLong == null) {
            return Long.valueOf(this.f33053b.e(l(a10.b()), "_id", contentValues)).longValue();
        }
        this.f33053b.a(l(a10.b()), "_id", contentValues);
        return asLong.longValue();
    }

    public long i(Object obj) {
        rq.a a10 = a(obj.getClass());
        ContentValues contentValues = new ContentValues();
        a10.a(obj, contentValues);
        Long asLong = contentValues.getAsLong("_id");
        long h10 = h(obj.getClass(), contentValues);
        if (asLong == null) {
            a10.f(Long.valueOf(h10), obj);
        }
        return asLong == null ? h10 : asLong.longValue();
    }

    public a j(Class cls) {
        return new a(cls, this);
    }

    public int m(Class cls, ContentValues contentValues) {
        rq.a a10 = a(cls);
        return contentValues.containsKey("_id") ? this.f33053b.b(l(a10.b()), contentValues, "_id = ?", new String[]{contentValues.getAsString("_id")}) : this.f33053b.b(l(a10.b()), contentValues, null, null);
    }

    boolean n(c cVar, String str, Cursor cursor, List list) {
        boolean z10;
        Locale locale = Locale.US;
        HashMap hashMap = new HashMap(list.size());
        Iterator it = list.iterator();
        while (it.hasNext()) {
            a.C0824a c0824a = (a.C0824a) it.next();
            if (c0824a.f35087b != a.b.JOIN) {
                hashMap.put(c0824a.f35086a.toLowerCase(locale), c0824a);
            }
        }
        int columnIndex = cursor.getColumnIndex(AppMeasurementSdk.ConditionalUserProperty.NAME);
        while (cursor.moveToNext()) {
            hashMap.remove(cursor.getString(columnIndex).toLowerCase(locale));
        }
        if (hashMap.isEmpty()) {
            z10 = false;
        } else {
            for (a.C0824a c0824a2 : hashMap.values()) {
                cVar.execSQL("alter table '" + str + "' add column '" + c0824a2.f35086a + "' " + c0824a2.f35087b.toString());
            }
            z10 = true;
        }
        return g(cVar, str, list) | z10;
    }

    boolean o(c cVar, String str, List list) {
        Cursor d10 = cVar.d("pragma table_info('" + str + "')", null);
        try {
            return d10.getCount() == 0 ? c(cVar, str, list) : n(cVar, str, d10, list);
        } finally {
            d10.close();
        }
    }

    public void p() {
        Iterator it = this.f33048a.d().iterator();
        while (it.hasNext()) {
            rq.a b10 = this.f33048a.b((Class) it.next());
            o(this.f33053b, b10.b(), b10.e());
        }
    }
}
