Telegram5 v10.6.2版本的 MD5 值为:423cae39e902ed9dd2cc7f4b4547b276

以下内容为反编译后的 MessagesStorage.java 源代码,内容仅作参考


package org.telegram.messenger;

import android.appwidget.AppWidgetManager;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Pair;
import android.util.SparseArray;
import android.util.SparseIntArray;
import androidx.collection.LongSparseArray;
import j$.util.function.Consumer;
import java.io.File;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import org.telegram.PhoneFormat.PhoneFormat;
import org.telegram.SQLite.SQLiteCursor;
import org.telegram.SQLite.SQLiteDatabase;
import org.telegram.SQLite.SQLiteException;
import org.telegram.SQLite.SQLitePreparedStatement;
import org.telegram.messenger.ContactsController;
import org.telegram.messenger.MessagesController;
import org.telegram.messenger.MessagesStorage;
import org.telegram.messenger.NotificationsController;
import org.telegram.messenger.TopicsController;
import org.telegram.messenger.support.LongSparseIntArray;
import org.telegram.tgnet.AbstractSerializedData;
import org.telegram.tgnet.ConnectionsManager;
import org.telegram.tgnet.NativeByteBuffer;
import org.telegram.tgnet.RequestDelegate;
import org.telegram.tgnet.TLObject;
import org.telegram.tgnet.TLRPC$BotInfo;
import org.telegram.tgnet.TLRPC$ChannelParticipant;
import org.telegram.tgnet.TLRPC$Chat;
import org.telegram.tgnet.TLRPC$ChatFull;
import org.telegram.tgnet.TLRPC$ChatParticipant;
import org.telegram.tgnet.TLRPC$ChatParticipants;
import org.telegram.tgnet.TLRPC$Dialog;
import org.telegram.tgnet.TLRPC$DialogFilter;
import org.telegram.tgnet.TLRPC$Document;
import org.telegram.tgnet.TLRPC$DraftMessage;
import org.telegram.tgnet.TLRPC$EncryptedChat;
import org.telegram.tgnet.TLRPC$InputChannel;
import org.telegram.tgnet.TLRPC$InputDialogPeer;
import org.telegram.tgnet.TLRPC$InputMedia;
import org.telegram.tgnet.TLRPC$InputPeer;
import org.telegram.tgnet.TLRPC$Message;
import org.telegram.tgnet.TLRPC$MessageAction;
import org.telegram.tgnet.TLRPC$MessageEntity;
import org.telegram.tgnet.TLRPC$MessageFwdHeader;
import org.telegram.tgnet.TLRPC$MessageMedia;
import org.telegram.tgnet.TLRPC$MessageReplies;
import org.telegram.tgnet.TLRPC$MessageReplyHeader;
import org.telegram.tgnet.TLRPC$Peer;
import org.telegram.tgnet.TLRPC$Photo;
import org.telegram.tgnet.TLRPC$PhotoSize;
import org.telegram.tgnet.TLRPC$Poll;
import org.telegram.tgnet.TLRPC$PollResults;
import org.telegram.tgnet.TLRPC$ReplyMarkup;
import org.telegram.tgnet.TLRPC$TL_channelFull;
import org.telegram.tgnet.TLRPC$TL_channels_deleteMessages;
import org.telegram.tgnet.TLRPC$TL_chatAdminRights;
import org.telegram.tgnet.TLRPC$TL_chatBannedRights;
import org.telegram.tgnet.TLRPC$TL_chatChannelParticipant;
import org.telegram.tgnet.TLRPC$TL_chatFull;
import org.telegram.tgnet.TLRPC$TL_chatParticipant;
import org.telegram.tgnet.TLRPC$TL_chatParticipantAdmin;
import org.telegram.tgnet.TLRPC$TL_chatParticipants;
import org.telegram.tgnet.TLRPC$TL_contact;
import org.telegram.tgnet.TLRPC$TL_dialog;
import org.telegram.tgnet.TLRPC$TL_dialogFilterChatlist;
import org.telegram.tgnet.TLRPC$TL_dialogFolder;
import org.telegram.tgnet.TLRPC$TL_documentEmpty;
import org.telegram.tgnet.TLRPC$TL_folder;
import org.telegram.tgnet.TLRPC$TL_folderPeer;
import org.telegram.tgnet.TLRPC$TL_forumTopic;
import org.telegram.tgnet.TLRPC$TL_inputChannel;
import org.telegram.tgnet.TLRPC$TL_inputFolderPeer;
import org.telegram.tgnet.TLRPC$TL_inputMediaGame;
import org.telegram.tgnet.TLRPC$TL_inputMessageEntityMentionName;
import org.telegram.tgnet.TLRPC$TL_message;
import org.telegram.tgnet.TLRPC$TL_messageActionChatAddUser;
import org.telegram.tgnet.TLRPC$TL_messageActionGameScore;
import org.telegram.tgnet.TLRPC$TL_messageActionGeoProximityReached;
import org.telegram.tgnet.TLRPC$TL_messageActionGiftCode;
import org.telegram.tgnet.TLRPC$TL_messageActionPaymentSent;
import org.telegram.tgnet.TLRPC$TL_messageActionPinMessage;
import org.telegram.tgnet.TLRPC$TL_messageActionTopicCreate;
import org.telegram.tgnet.TLRPC$TL_messageActionTopicEdit;
import org.telegram.tgnet.TLRPC$TL_messageEmpty;
import org.telegram.tgnet.TLRPC$TL_messageEntityCustomEmoji;
import org.telegram.tgnet.TLRPC$TL_messageEntityMentionName;
import org.telegram.tgnet.TLRPC$TL_messageMediaDocument;
import org.telegram.tgnet.TLRPC$TL_messageMediaGiveaway;
import org.telegram.tgnet.TLRPC$TL_messageMediaGiveawayResults;
import org.telegram.tgnet.TLRPC$TL_messageMediaPhoto;
import org.telegram.tgnet.TLRPC$TL_messageMediaPoll;
import org.telegram.tgnet.TLRPC$TL_messageMediaStory;
import org.telegram.tgnet.TLRPC$TL_messageMediaUnsupported;
import org.telegram.tgnet.TLRPC$TL_messageMediaUnsupported_old;
import org.telegram.tgnet.TLRPC$TL_messageMediaWebPage;
import org.telegram.tgnet.TLRPC$TL_messageReactions;
import org.telegram.tgnet.TLRPC$TL_messageReplies;
import org.telegram.tgnet.TLRPC$TL_messageReplyHeader;
import org.telegram.tgnet.TLRPC$TL_message_secret;
import org.telegram.tgnet.TLRPC$TL_messages_botCallbackAnswer;
import org.telegram.tgnet.TLRPC$TL_messages_botResults;
import org.telegram.tgnet.TLRPC$TL_messages_deleteMessages;
import org.telegram.tgnet.TLRPC$TL_messages_deleteScheduledMessages;
import org.telegram.tgnet.TLRPC$TL_messages_dialogs;
import org.telegram.tgnet.TLRPC$TL_messages_messages;
import org.telegram.tgnet.TLRPC$TL_peerChannel;
import org.telegram.tgnet.TLRPC$TL_peerChat;
import org.telegram.tgnet.TLRPC$TL_peerNotifySettings;
import org.telegram.tgnet.TLRPC$TL_peerUser;
import org.telegram.tgnet.TLRPC$TL_photoEmpty;
import org.telegram.tgnet.TLRPC$TL_replyInlineMarkup;
import org.telegram.tgnet.TLRPC$TL_updateDeleteScheduledMessages;
import org.telegram.tgnet.TLRPC$TL_updates;
import org.telegram.tgnet.TLRPC$TL_updates_channelDifferenceTooLong;
import org.telegram.tgnet.TLRPC$TL_userStatusLastMonth;
import org.telegram.tgnet.TLRPC$TL_userStatusLastWeek;
import org.telegram.tgnet.TLRPC$TL_userStatusRecently;
import org.telegram.tgnet.TLRPC$TL_username;
import org.telegram.tgnet.TLRPC$TL_webPageAttributeStory;
import org.telegram.tgnet.TLRPC$User;
import org.telegram.tgnet.TLRPC$UserFull;
import org.telegram.tgnet.TLRPC$UserProfilePhoto;
import org.telegram.tgnet.TLRPC$UserStatus;
import org.telegram.tgnet.TLRPC$Vector;
import org.telegram.tgnet.TLRPC$WallPaper;
import org.telegram.tgnet.TLRPC$WebPage;
import org.telegram.tgnet.TLRPC$messages_BotResults;
import org.telegram.tgnet.TLRPC$messages_Dialogs;
import org.telegram.tgnet.TLRPC$messages_Messages;
import org.telegram.tgnet.tl.TL_stories$StoryFwdHeader;
import org.telegram.tgnet.tl.TL_stories$StoryItem;
import org.telegram.tgnet.tl.TL_stories$TL_mediaAreaChannelPost;
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.Adapters.DialogsSearchAdapter;

public class MessagesStorage extends BaseController {
    public static final String[] DATABASE_TABLES;
    public static final int LAST_DB_VERSION = 139;
    private int archiveUnreadCount;
    private int[][] bots;
    private File cacheFile;
    private int[][] channels;
    private int[][] contacts;
    private SQLiteDatabase database;
    private boolean databaseCreated;
    private boolean databaseMigrationInProgress;
    private ArrayList<MessagesController.DialogFilter> dialogFilters;
    private SparseArray<MessagesController.DialogFilter> dialogFiltersMap;
    private LongSparseIntArray dialogIsForum;
    private LongSparseArray<Integer> dialogsWithMentions;
    private LongSparseArray<Integer> dialogsWithUnread;
    private int[][] groups;
    private int lastDateValue;
    private int lastPtsValue;
    private int lastQtsValue;
    private int lastSavedDate;
    private int lastSavedPts;
    private int lastSavedQts;
    private int lastSavedSeq;
    private int lastSecretVersion;
    private int lastSeqValue;
    private AtomicLong lastTaskId;
    private int mainUnreadCount;
    private int[] mentionChannels;
    private int[] mentionGroups;
    private int[][] nonContacts;
    private CountDownLatch openSync;
    private volatile int pendingArchiveUnreadCount;
    private volatile int pendingMainUnreadCount;
    private int secretG;
    private byte[] secretPBytes;
    private File shmCacheFile;
    public boolean showClearDatabaseAlert;
    private DispatchQueue storageQueue;
    private SparseArray<ArrayList<Runnable>> tasks;
    boolean tryRecover;
    private LongSparseArray<Boolean> unknownDialogsIds;
    private File walCacheFile;
    private static volatile MessagesStorage[] Instance = new MessagesStorage[4];
    private static final Object[] lockObjects = new Object[4];

    public interface BooleanCallback {
        void run(boolean z);
    }

    public interface IntCallback {
        void run(int i);
    }

    public interface LongCallback {
        void run(long j);
    }

    public interface StringCallback {
        void run(String str);
    }

    static {
        for (int i = 0; i < 4; i++) {
            lockObjects[i] = new Object();
        }
        DATABASE_TABLES = new String[]{"messages_holes", "media_holes_v2", "scheduled_messages_v2", "messages_v2", "download_queue", "user_contacts_v7", "user_phones_v7", "dialogs", "dialog_filter", "dialog_filter_ep", "dialog_filter_pin_v2", "randoms_v2", "enc_tasks_v4", "messages_seq", "params", "media_v4", "bot_keyboard", "bot_keyboard_topics", "chat_settings_v2", "user_settings", "chat_pinned_v2", "chat_pinned_count", "chat_hints", "botcache", "users_data", "users", "chats", "enc_chats", "channel_users_v2", "channel_admins_v3", "contacts", "dialog_photos", "dialog_settings", "web_recent_v3", "stickers_v2", "stickers_featured", "stickers_dice", "stickersets", "hashtag_recent_v2", "webpage_pending_v2", "sent_files_v2", "search_recent", "media_counts_v2", "keyvalue", "bot_info_v2", "pending_tasks", "requested_holes", "sharing_locations", "shortcut_widget", "emoji_keywords_v2", "emoji_keywords_info_v2", "wallpapers2", "unread_push_messages", "polls_v2", "reactions", "reaction_mentions", "downloading_documents", "animated_emoji", "attach_menu_bots", "premium_promo", "emoji_statuses", "messages_holes_topics", "messages_topics", "saved_dialogs", "media_topics", "media_holes_topics", "topics", "media_counts_topics", "reaction_mentions_topics", "emoji_groups"};
    }

    public static MessagesStorage getInstance(int i) {
        MessagesStorage messagesStorage = Instance[i];
        if (messagesStorage == null) {
            synchronized (lockObjects[i]) {
                messagesStorage = Instance[i];
                if (messagesStorage == null) {
                    MessagesStorage[] messagesStorageArr = Instance;
                    MessagesStorage messagesStorage2 = new MessagesStorage(i);
                    messagesStorageArr[i] = messagesStorage2;
                    messagesStorage = messagesStorage2;
                }
            }
        }
        return messagesStorage;
    }

    private void ensureOpened() {
        try {
            this.openSync.await();
        } catch (Throwable unused) {
        }
    }

    public int getLastDateValue() {
        ensureOpened();
        return this.lastDateValue;
    }

    public void setLastDateValue(int i) {
        ensureOpened();
        this.lastDateValue = i;
    }

    public int getLastPtsValue() {
        ensureOpened();
        return this.lastPtsValue;
    }

    public int getMainUnreadCount() {
        return this.mainUnreadCount;
    }

    public int getArchiveUnreadCount() {
        return this.archiveUnreadCount;
    }

    public void setLastPtsValue(int i) {
        ensureOpened();
        this.lastPtsValue = i;
    }

    public int getLastQtsValue() {
        ensureOpened();
        return this.lastQtsValue;
    }

    public void setLastQtsValue(int i) {
        ensureOpened();
        this.lastQtsValue = i;
    }

    public int getLastSeqValue() {
        ensureOpened();
        return this.lastSeqValue;
    }

    public void setLastSeqValue(int i) {
        ensureOpened();
        this.lastSeqValue = i;
    }

    public int getLastSecretVersion() {
        ensureOpened();
        return this.lastSecretVersion;
    }

    public void setLastSecretVersion(int i) {
        ensureOpened();
        this.lastSecretVersion = i;
    }

    public byte[] getSecretPBytes() {
        ensureOpened();
        return this.secretPBytes;
    }

    public void setSecretPBytes(byte[] bArr) {
        ensureOpened();
        this.secretPBytes = bArr;
    }

    public int getSecretG() {
        ensureOpened();
        return this.secretG;
    }

    public void setSecretG(int i) {
        ensureOpened();
        this.secretG = i;
    }

    public MessagesStorage(int i) {
        super(i);
        this.lastTaskId = new AtomicLong(System.currentTimeMillis());
        this.tasks = new SparseArray<>();
        this.lastDateValue = 0;
        this.lastPtsValue = 0;
        this.lastQtsValue = 0;
        this.lastSeqValue = 0;
        this.lastSecretVersion = 0;
        this.secretPBytes = null;
        this.secretG = 0;
        this.lastSavedSeq = 0;
        this.lastSavedPts = 0;
        this.lastSavedDate = 0;
        this.lastSavedQts = 0;
        this.dialogFilters = new ArrayList<>();
        this.dialogFiltersMap = new SparseArray<>();
        this.unknownDialogsIds = new LongSparseArray<>();
        this.openSync = new CountDownLatch(1);
        this.dialogIsForum = new LongSparseIntArray();
        this.contacts = new int[][]{new int[2], new int[2]};
        this.nonContacts = new int[][]{new int[2], new int[2]};
        this.bots = new int[][]{new int[2], new int[2]};
        this.channels = new int[][]{new int[2], new int[2]};
        this.groups = new int[][]{new int[2], new int[2]};
        this.mentionChannels = new int[2];
        this.mentionGroups = new int[2];
        this.dialogsWithMentions = new LongSparseArray<>();
        this.dialogsWithUnread = new LongSparseArray<>();
        DispatchQueue dispatchQueue = new DispatchQueue("storageQueue_" + i);
        this.storageQueue = dispatchQueue;
        dispatchQueue.setPriority(8);
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$new$0();
            }
        });
    }

    public void lambda$new$0() {
        openDatabase(1);
    }

    public SQLiteDatabase getDatabase() {
        return this.database;
    }

    public DispatchQueue getStorageQueue() {
        return this.storageQueue;
    }

    public void bindTaskToGuid(Runnable runnable, int i) {
        ArrayList<Runnable> arrayList = this.tasks.get(i);
        if (arrayList == null) {
            arrayList = new ArrayList<>();
            this.tasks.put(i, arrayList);
        }
        arrayList.add(runnable);
    }

    public void cancelTasksForGuid(int i) {
        ArrayList<Runnable> arrayList = this.tasks.get(i);
        if (arrayList == null) {
            return;
        }
        int size = arrayList.size();
        for (int i2 = 0; i2 < size; i2++) {
            this.storageQueue.cancelRunnable(arrayList.get(i2));
        }
        this.tasks.remove(i);
    }

    public void completeTaskForGuid(Runnable runnable, int i) {
        ArrayList<Runnable> arrayList = this.tasks.get(i);
        if (arrayList == null) {
            return;
        }
        arrayList.remove(runnable);
        if (arrayList.isEmpty()) {
            this.tasks.remove(i);
        }
    }

    public long getDatabaseSize() {
        File file = this.cacheFile;
        long length = file != null ? 0 + file.length() : 0L;
        File file2 = this.shmCacheFile;
        return file2 != null ? length + file2.length() : length;
    }

    public void openDatabase(int i) {
        if (!NativeLoader.loaded()) {
            int i2 = 0;
            while (!NativeLoader.loaded()) {
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i2++;
                if (i2 > 5) {
                    break;
                }
            }
        }
        File filesDirFixed = ApplicationLoader.getFilesDirFixed();
        if (this.currentAccount != 0) {
            File file = new File(filesDirFixed, "account" + this.currentAccount + "/");
            file.mkdirs();
            filesDirFixed = file;
        }
        this.cacheFile = new File(filesDirFixed, "cache4.db");
        this.walCacheFile = new File(filesDirFixed, "cache4.db-wal");
        this.shmCacheFile = new File(filesDirFixed, "cache4.db-shm");
        this.databaseCreated = false;
        boolean z = !this.cacheFile.exists();
        try {
            SQLiteDatabase sQLiteDatabase = new SQLiteDatabase(this.cacheFile.getPath());
            this.database = sQLiteDatabase;
            sQLiteDatabase.executeFast("PRAGMA secure_delete = ON").stepThis().dispose();
            this.database.executeFast("PRAGMA temp_store = MEMORY").stepThis().dispose();
            this.database.executeFast("PRAGMA journal_mode = WAL").stepThis().dispose();
            this.database.executeFast("PRAGMA journal_size_limit = 10485760").stepThis().dispose();
            if (z) {
                if (BuildVars.LOGS_ENABLED) {
                    FileLog.d("create new database");
                }
                createTables(this.database);
            } else {
                int intValue = this.database.executeInt("PRAGMA user_version", new Object[0]).intValue();
                if (BuildVars.LOGS_ENABLED) {
                    FileLog.d("current db version = " + intValue);
                }
                if (intValue == 0) {
                    throw new Exception("malformed");
                }
                try {
                    SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT seq, pts, date, qts, lsv, sg, pbytes FROM params WHERE id = 1", new Object[0]);
                    if (queryFinalized.next()) {
                        this.lastSeqValue = queryFinalized.intValue(0);
                        this.lastPtsValue = queryFinalized.intValue(1);
                        this.lastDateValue = queryFinalized.intValue(2);
                        this.lastQtsValue = queryFinalized.intValue(3);
                        this.lastSecretVersion = queryFinalized.intValue(4);
                        this.secretG = queryFinalized.intValue(5);
                        if (queryFinalized.isNull(6)) {
                            this.secretPBytes = null;
                        } else {
                            byte[] byteArrayValue = queryFinalized.byteArrayValue(6);
                            this.secretPBytes = byteArrayValue;
                            if (byteArrayValue != null && byteArrayValue.length == 1) {
                                this.secretPBytes = null;
                            }
                        }
                    }
                    queryFinalized.dispose();
                } catch (Exception e2) {
                    FileLog.e(e2);
                    if (e2.getMessage() != null && e2.getMessage().contains("malformed")) {
                        throw new RuntimeException("malformed");
                    }
                    try {
                        this.database.executeFast("CREATE TABLE IF NOT EXISTS params(id INTEGER PRIMARY KEY, seq INTEGER, pts INTEGER, date INTEGER, qts INTEGER, lsv INTEGER, sg INTEGER, pbytes BLOB)").stepThis().dispose();
                        this.database.executeFast("INSERT INTO params VALUES(1, 0, 0, 0, 0, 0, 0, NULL)").stepThis().dispose();
                    } catch (Exception e3) {
                        FileLog.e(e3);
                    }
                }
                if (intValue < 139) {
                    try {
                        updateDbToLastVersion(intValue);
                    } catch (Exception e4) {
                        if (BuildVars.DEBUG_PRIVATE_VERSION) {
                            throw e4;
                        }
                        FileLog.e(e4);
                        throw new RuntimeException("malformed");
                    }
                }
            }
            this.databaseCreated = true;
        } catch (Exception e5) {
            FileLog.e(e5);
            if (i < 3 && e5.getMessage() != null && e5.getMessage().contains("malformed")) {
                if (i == 2) {
                    cleanupInternal(true);
                    clearLoadingDialogsOffsets();
                } else {
                    cleanupInternal(false);
                }
                openDatabase(i != 1 ? 3 : 2);
                return;
            }
        }
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$openDatabase$1();
            }
        });
        loadDialogFilters();
        loadUnreadMessages();
        loadPendingTasks();
        try {
            this.openSync.countDown();
        } catch (Throwable unused) {
        }
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$openDatabase$2();
            }
        });
    }

    public void lambda$openDatabase$1() {
        if (this.databaseMigrationInProgress) {
            this.databaseMigrationInProgress = false;
            NotificationCenter.getInstance(this.currentAccount).lambda$postNotificationNameOnUIThread$1(NotificationCenter.onDatabaseMigration, Boolean.FALSE);
        }
    }

    public void lambda$openDatabase$2() {
        this.showClearDatabaseAlert = false;
        NotificationCenter.getInstance(this.currentAccount).lambda$postNotificationNameOnUIThread$1(NotificationCenter.onDatabaseOpened, new Object[0]);
    }

    private void clearLoadingDialogsOffsets() {
        for (int i = 0; i < 2; i++) {
            getUserConfig().setDialogsLoadOffset(i, 0, 0, 0L, 0L, 0L, 0L);
            getUserConfig().setTotalDialogsCount(i, 0);
        }
        getUserConfig().saveConfig(false);
    }

    private boolean recoverDatabase() {
        this.database.close();
        boolean recoverDatabase = DatabaseMigrationHelper.recoverDatabase(this.cacheFile, this.walCacheFile, this.shmCacheFile, this.currentAccount);
        FileLog.e("Database restored = " + recoverDatabase);
        if (recoverDatabase) {
            try {
                SQLiteDatabase sQLiteDatabase = new SQLiteDatabase(this.cacheFile.getPath());
                this.database = sQLiteDatabase;
                sQLiteDatabase.executeFast("PRAGMA secure_delete = ON").stepThis().dispose();
                this.database.executeFast("PRAGMA temp_store = MEMORY").stepThis().dispose();
                this.database.executeFast("PRAGMA journal_mode = WAL").stepThis().dispose();
                this.database.executeFast("PRAGMA journal_size_limit = 10485760").stepThis().dispose();
            } catch (SQLiteException e) {
                FileLog.e(new Exception(e));
                recoverDatabase = false;
            }
        }
        if (!recoverDatabase) {
            cleanupInternal(true);
            openDatabase(1);
            recoverDatabase = this.databaseCreated;
            FileLog.e("Try create new database = " + recoverDatabase);
        }
        if (recoverDatabase) {
            reset();
        }
        return recoverDatabase;
    }

    public static void createTables(SQLiteDatabase sQLiteDatabase) throws SQLiteException {
        sQLiteDatabase.executeFast("CREATE TABLE messages_holes(uid INTEGER, start INTEGER, end INTEGER, PRIMARY KEY(uid, start));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_end_messages_holes ON messages_holes(uid, end);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE media_holes_v2(uid INTEGER, type INTEGER, start INTEGER, end INTEGER, PRIMARY KEY(uid, type, start));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_end_media_holes_v2 ON media_holes_v2(uid, type, end);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE scheduled_messages_v2(mid INTEGER, uid INTEGER, send_state INTEGER, date INTEGER, data BLOB, ttl INTEGER, replydata BLOB, reply_to_message_id INTEGER, PRIMARY KEY(mid, uid))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS send_state_idx_scheduled_messages_v2 ON scheduled_messages_v2(mid, send_state, date);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_date_idx_scheduled_messages_v2 ON scheduled_messages_v2(uid, date);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS reply_to_idx_scheduled_messages_v2 ON scheduled_messages_v2(mid, reply_to_message_id);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS idx_to_reply_scheduled_messages_v2 ON scheduled_messages_v2(reply_to_message_id, mid);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE messages_v2(mid INTEGER, uid INTEGER, read_state INTEGER, send_state INTEGER, date INTEGER, data BLOB, out INTEGER, ttl INTEGER, media INTEGER, replydata BLOB, imp INTEGER, mention INTEGER, forwards INTEGER, replies_data BLOB, thread_reply_id INTEGER, is_channel INTEGER, reply_to_message_id INTEGER, custom_params BLOB, group_id INTEGER, reply_to_story_id INTEGER, PRIMARY KEY(mid, uid))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_mid_read_out_idx_messages_v2 ON messages_v2(uid, mid, read_state, out);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_date_mid_idx_messages_v2 ON messages_v2(uid, date, mid);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS mid_out_idx_messages_v2 ON messages_v2(mid, out);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS task_idx_messages_v2 ON messages_v2(uid, out, read_state, ttl, date, send_state);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS send_state_idx_messages_v2 ON messages_v2(mid, send_state, date);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_mention_idx_messages_v2 ON messages_v2(uid, mention, read_state);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS is_channel_idx_messages_v2 ON messages_v2(mid, is_channel);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS reply_to_idx_messages_v2 ON messages_v2(mid, reply_to_message_id);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS idx_to_reply_messages_v2 ON messages_v2(reply_to_message_id, mid);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_mid_groupid_messages_v2 ON messages_v2(uid, mid, group_id);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE saved_dialogs(did INTEGER PRIMARY KEY, date INTEGER, last_mid INTEGER, pinned INTEGER, flags INTEGER, folder_id INTEGER, last_mid_group INTEGER, count INTEGER)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS date_idx_dialogs ON saved_dialogs(date);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS last_mid_idx_dialogs ON saved_dialogs(last_mid);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS folder_id_idx_dialogs ON saved_dialogs(folder_id);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS flags_idx_dialogs ON saved_dialogs(flags);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE download_queue(uid INTEGER, type INTEGER, date INTEGER, data BLOB, parent TEXT, PRIMARY KEY (uid, type));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS type_date_idx_download_queue ON download_queue(type, date);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE user_contacts_v7(key TEXT PRIMARY KEY, uid INTEGER, fname TEXT, sname TEXT, imported INTEGER)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE user_phones_v7(key TEXT, phone TEXT, sphone TEXT, deleted INTEGER, PRIMARY KEY (key, phone))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS sphone_deleted_idx_user_phones ON user_phones_v7(sphone, deleted);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE dialogs(did INTEGER PRIMARY KEY, date INTEGER, unread_count INTEGER, last_mid INTEGER, inbox_max INTEGER, outbox_max INTEGER, last_mid_i INTEGER, unread_count_i INTEGER, pts INTEGER, date_i INTEGER, pinned INTEGER, flags INTEGER, folder_id INTEGER, data BLOB, unread_reactions INTEGER, last_mid_group INTEGER, ttl_period INTEGER)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS date_idx_dialogs ON dialogs(date);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS last_mid_idx_dialogs ON dialogs(last_mid);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS unread_count_idx_dialogs ON dialogs(unread_count);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS last_mid_i_idx_dialogs ON dialogs(last_mid_i);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS unread_count_i_idx_dialogs ON dialogs(unread_count_i);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS folder_id_idx_dialogs ON dialogs(folder_id);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS flags_idx_dialogs ON dialogs(flags);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE dialog_filter(id INTEGER PRIMARY KEY, ord INTEGER, unread_count INTEGER, flags INTEGER, title TEXT)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE dialog_filter_ep(id INTEGER, peer INTEGER, PRIMARY KEY (id, peer))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE dialog_filter_pin_v2(id INTEGER, peer INTEGER, pin INTEGER, PRIMARY KEY (id, peer))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE randoms_v2(random_id INTEGER, mid INTEGER, uid INTEGER, PRIMARY KEY (random_id, mid, uid))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS mid_idx_randoms_v2 ON randoms_v2(mid, uid);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE enc_tasks_v4(mid INTEGER, uid INTEGER, date INTEGER, media INTEGER, PRIMARY KEY(mid, uid, media))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS date_idx_enc_tasks_v4 ON enc_tasks_v4(date);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE messages_seq(mid INTEGER PRIMARY KEY, seq_in INTEGER, seq_out INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS seq_idx_messages_seq ON messages_seq(seq_in, seq_out);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE params(id INTEGER PRIMARY KEY, seq INTEGER, pts INTEGER, date INTEGER, qts INTEGER, lsv INTEGER, sg INTEGER, pbytes BLOB)").stepThis().dispose();
        sQLiteDatabase.executeFast("INSERT INTO params VALUES(1, 0, 0, 0, 0, 0, 0, NULL)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE media_v4(mid INTEGER, uid INTEGER, date INTEGER, type INTEGER, data BLOB, PRIMARY KEY(mid, uid, type))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_mid_type_date_idx_media_v4 ON media_v4(uid, mid, type, date);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE bot_keyboard(uid INTEGER PRIMARY KEY, mid INTEGER, info BLOB)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS bot_keyboard_idx_mid_v2 ON bot_keyboard(mid, uid);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE bot_keyboard_topics(uid INTEGER, tid INTEGER, mid INTEGER, info BLOB, PRIMARY KEY(uid, tid))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS bot_keyboard_topics_idx_mid_v2 ON bot_keyboard_topics(mid, uid, tid);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE chat_settings_v2(uid INTEGER PRIMARY KEY, info BLOB, pinned INTEGER, online INTEGER, inviter INTEGER, links INTEGER, participants_count INTEGER)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS chat_settings_pinned_idx ON chat_settings_v2(uid, pinned) WHERE pinned != 0;").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE user_settings(uid INTEGER PRIMARY KEY, info BLOB, pinned INTEGER)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS user_settings_pinned_idx ON user_settings(uid, pinned) WHERE pinned != 0;").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE chat_pinned_v2(uid INTEGER, mid INTEGER, data BLOB, PRIMARY KEY (uid, mid));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE chat_pinned_count(uid INTEGER PRIMARY KEY, count INTEGER, end INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE chat_hints(did INTEGER, type INTEGER, rating REAL, date INTEGER, PRIMARY KEY(did, type))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS chat_hints_rating_idx ON chat_hints(rating);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE botcache(id TEXT PRIMARY KEY, date INTEGER, data BLOB)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS botcache_date_idx ON botcache(date);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE users_data(uid INTEGER PRIMARY KEY, about TEXT)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE users(uid INTEGER PRIMARY KEY, name TEXT, status INTEGER, data BLOB)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE chats(uid INTEGER PRIMARY KEY, name TEXT, data BLOB)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE enc_chats(uid INTEGER PRIMARY KEY, user INTEGER, name TEXT, data BLOB, g BLOB, authkey BLOB, ttl INTEGER, layer INTEGER, seq_in INTEGER, seq_out INTEGER, use_count INTEGER, exchange_id INTEGER, key_date INTEGER, fprint INTEGER, fauthkey BLOB, khash BLOB, in_seq_no INTEGER, admin_id INTEGER, mtproto_seq INTEGER)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE channel_users_v2(did INTEGER, uid INTEGER, date INTEGER, data BLOB, PRIMARY KEY(did, uid))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE channel_admins_v3(did INTEGER, uid INTEGER, data BLOB, PRIMARY KEY(did, uid))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE contacts(uid INTEGER PRIMARY KEY, mutual INTEGER)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE dialog_photos(uid INTEGER, id INTEGER, num INTEGER, data BLOB, PRIMARY KEY (uid, id))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE dialog_photos_count(uid INTEGER PRIMARY KEY, count INTEGER)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE dialog_settings(did INTEGER PRIMARY KEY, flags INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE web_recent_v3(id TEXT, type INTEGER, image_url TEXT, thumb_url TEXT, local_url TEXT, width INTEGER, height INTEGER, size INTEGER, date INTEGER, document BLOB, PRIMARY KEY (id, type));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE stickers_v2(id INTEGER PRIMARY KEY, data BLOB, date INTEGER, hash INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE stickers_featured(id INTEGER PRIMARY KEY, data BLOB, unread BLOB, date INTEGER, hash INTEGER, premium INTEGER, emoji INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE stickers_dice(emoji TEXT PRIMARY KEY, data BLOB, date INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE hashtag_recent_v2(id TEXT PRIMARY KEY, date INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE webpage_pending_v2(id INTEGER, mid INTEGER, uid INTEGER, PRIMARY KEY (id, mid, uid));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE sent_files_v2(uid TEXT, type INTEGER, data BLOB, parent TEXT, PRIMARY KEY (uid, type))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE search_recent(did INTEGER PRIMARY KEY, date INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE media_counts_v2(uid INTEGER, type INTEGER, count INTEGER, old INTEGER, PRIMARY KEY(uid, type))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE keyvalue(id TEXT PRIMARY KEY, value TEXT)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE bot_info_v2(uid INTEGER, dialogId INTEGER, info BLOB, PRIMARY KEY(uid, dialogId))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE pending_tasks(id INTEGER PRIMARY KEY, data BLOB);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE requested_holes(uid INTEGER, seq_out_start INTEGER, seq_out_end INTEGER, PRIMARY KEY (uid, seq_out_start, seq_out_end));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE sharing_locations(uid INTEGER PRIMARY KEY, mid INTEGER, date INTEGER, period INTEGER, message BLOB, proximity INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE stickersets2(id INTEGER PRIMATE KEY, data BLOB, hash INTEGER, date INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS stickersets2_id_index ON stickersets2(id);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS stickers_featured_emoji_index ON stickers_featured(emoji);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE shortcut_widget(id INTEGER, did INTEGER, ord INTEGER, PRIMARY KEY (id, did));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS shortcut_widget_did ON shortcut_widget(did);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE emoji_keywords_v2(lang TEXT, keyword TEXT, emoji TEXT, PRIMARY KEY(lang, keyword, emoji));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS emoji_keywords_v2_keyword ON emoji_keywords_v2(keyword);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE emoji_keywords_info_v2(lang TEXT PRIMARY KEY, alias TEXT, version INTEGER, date INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE wallpapers2(uid INTEGER PRIMARY KEY, data BLOB, num INTEGER)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS wallpapers_num ON wallpapers2(num);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE unread_push_messages(uid INTEGER, mid INTEGER, random INTEGER, date INTEGER, data BLOB, fm TEXT, name TEXT, uname TEXT, flags INTEGER, topicId INTEGER, is_reaction INTEGER, PRIMARY KEY(uid, mid))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS unread_push_messages_idx_date ON unread_push_messages(date);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS unread_push_messages_idx_random ON unread_push_messages(random);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE polls_v2(mid INTEGER, uid INTEGER, id INTEGER, PRIMARY KEY (mid, uid));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS polls_id_v2 ON polls_v2(id);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE reactions(data BLOB, hash INTEGER, date INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE reaction_mentions(message_id INTEGER, state INTEGER, dialog_id INTEGER, PRIMARY KEY(message_id, dialog_id))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS reaction_mentions_did ON reaction_mentions(dialog_id);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE downloading_documents(data BLOB, hash INTEGER, id INTEGER, state INTEGER, date INTEGER, PRIMARY KEY(hash, id));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE animated_emoji(document_id INTEGER PRIMARY KEY, data BLOB);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE attach_menu_bots(data BLOB, hash INTEGER, date INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE premium_promo(data BLOB, date INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE emoji_statuses(data BLOB, type INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE messages_holes_topics(uid INTEGER, topic_id INTEGER, start INTEGER, end INTEGER, PRIMARY KEY(uid, topic_id, start));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_end_messages_holes ON messages_holes_topics(uid, topic_id, end);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE messages_topics(mid INTEGER, uid INTEGER, topic_id INTEGER, read_state INTEGER, send_state INTEGER, date INTEGER, data BLOB, out INTEGER, ttl INTEGER, media INTEGER, replydata BLOB, imp INTEGER, mention INTEGER, forwards INTEGER, replies_data BLOB, thread_reply_id INTEGER, is_channel INTEGER, reply_to_message_id INTEGER, custom_params BLOB, reply_to_story_id INTEGER, PRIMARY KEY(mid, topic_id, uid))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_date_mid_idx_messages_topics ON messages_topics(uid, date, mid);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS mid_out_idx_messages_topics ON messages_topics(mid, out);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS task_idx_messages_topics ON messages_topics(uid, out, read_state, ttl, date, send_state);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS send_state_idx_messages_topics ON messages_topics(mid, send_state, date);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS is_channel_idx_messages_topics ON messages_topics(mid, is_channel);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS reply_to_idx_messages_topics ON messages_topics(mid, reply_to_message_id);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS idx_to_reply_messages_topics ON messages_topics(reply_to_message_id, mid);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS mid_uid_messages_topics ON messages_topics(mid, uid);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_mid_read_out_idx_messages_topics ON messages_topics(uid, topic_id, mid, read_state, out);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_mention_idx_messages_topics ON messages_topics(uid, topic_id, mention, read_state);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_topic_id_messages_topics ON messages_topics(uid, topic_id);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_topic_id_date_mid_messages_topics ON messages_topics(uid, topic_id, date, mid);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_topic_id_mid_messages_topics ON messages_topics(uid, topic_id, mid);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE media_topics(mid INTEGER, uid INTEGER, topic_id INTEGER, date INTEGER, type INTEGER, data BLOB, PRIMARY KEY(mid, uid, topic_id, type))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_mid_type_date_idx_media_topics ON media_topics(uid, topic_id, mid, type, date);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE media_holes_topics(uid INTEGER, topic_id INTEGER, type INTEGER, start INTEGER, end INTEGER, PRIMARY KEY(uid, topic_id, type, start));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS uid_end_media_holes_topics ON media_holes_topics(uid, topic_id, type, end);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE topics(did INTEGER, topic_id INTEGER, data BLOB, top_message INTEGER, topic_message BLOB, unread_count INTEGER, max_read_id INTEGER, unread_mentions INTEGER, unread_reactions INTEGER, read_outbox INTEGER, pinned INTEGER, total_messages_count INTEGER, hidden INTEGER, PRIMARY KEY(did, topic_id));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS did_top_message_topics ON topics(did, top_message);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS did_topics ON topics(did);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE media_counts_topics(uid INTEGER, topic_id INTEGER, type INTEGER, count INTEGER, old INTEGER, PRIMARY KEY(uid, topic_id, type))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE reaction_mentions_topics(message_id INTEGER, state INTEGER, dialog_id INTEGER, topic_id INTEGER, PRIMARY KEY(message_id, dialog_id, topic_id))").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE INDEX IF NOT EXISTS reaction_mentions_topics_did ON reaction_mentions_topics(dialog_id, topic_id);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE emoji_groups(type INTEGER PRIMARY KEY, data BLOB)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE app_config(data BLOB)").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE stories (dialog_id INTEGER, story_id INTEGER, data BLOB, custom_params BLOB, PRIMARY KEY (dialog_id, story_id));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE stories_counter (dialog_id INTEGER PRIMARY KEY, count INTEGER, max_read INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE profile_stories (dialog_id INTEGER, story_id INTEGER, data BLOB, type INTEGER, PRIMARY KEY(dialog_id, story_id));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE story_drafts (id INTEGER PRIMARY KEY, date INTEGER, data BLOB, type INTEGER);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE story_pushes (uid INTEGER, sid INTEGER, date INTEGER, localName TEXT, flags INTEGER, expire_date INTEGER, PRIMARY KEY(uid, sid));").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE unconfirmed_auth (data BLOB);").stepThis().dispose();
        sQLiteDatabase.executeFast("CREATE TABLE saved_reaction_tags (data BLOB);").stepThis().dispose();
        sQLiteDatabase.executeFast("PRAGMA user_version = 139").stepThis().dispose();
    }

    public boolean isDatabaseMigrationInProgress() {
        return this.databaseMigrationInProgress;
    }

    private void updateDbToLastVersion(int i) throws Exception {
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateDbToLastVersion$3();
            }
        });
        FileLog.d("MessagesStorage start db migration from " + i + " to " + LAST_DB_VERSION);
        int migrate = DatabaseMigrationHelper.migrate(this, i);
        StringBuilder sb = new StringBuilder();
        sb.append("MessagesStorage db migration finished to varsion ");
        sb.append(migrate);
        FileLog.d(sb.toString());
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateDbToLastVersion$4();
            }
        });
    }

    public void lambda$updateDbToLastVersion$3() {
        this.databaseMigrationInProgress = true;
        NotificationCenter.getInstance(this.currentAccount).lambda$postNotificationNameOnUIThread$1(NotificationCenter.onDatabaseMigration, Boolean.TRUE);
    }

    public void lambda$updateDbToLastVersion$4() {
        this.databaseMigrationInProgress = false;
        NotificationCenter.getInstance(this.currentAccount).lambda$postNotificationNameOnUIThread$1(NotificationCenter.onDatabaseMigration, Boolean.FALSE);
    }

    public void executeNoException(String str) {
        try {
            this.database.executeFast(str).stepThis().dispose();
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    private void cleanupInternal(boolean z) {
        if (z) {
            reset();
        } else {
            clearDatabaseValues();
        }
        SQLiteDatabase sQLiteDatabase = this.database;
        if (sQLiteDatabase != null) {
            sQLiteDatabase.close();
            this.database = null;
        }
        if (z) {
            File file = this.cacheFile;
            if (file != null) {
                file.delete();
                this.cacheFile = null;
            }
            File file2 = this.walCacheFile;
            if (file2 != null) {
                file2.delete();
                this.walCacheFile = null;
            }
            File file3 = this.shmCacheFile;
            if (file3 != null) {
                file3.delete();
                this.shmCacheFile = null;
            }
        }
    }

    public void clearDatabaseValues() {
        this.lastDateValue = 0;
        this.lastSeqValue = 0;
        this.lastPtsValue = 0;
        this.lastQtsValue = 0;
        this.lastSecretVersion = 0;
        this.mainUnreadCount = 0;
        this.archiveUnreadCount = 0;
        this.pendingMainUnreadCount = 0;
        this.pendingArchiveUnreadCount = 0;
        this.dialogFilters.clear();
        this.dialogFiltersMap.clear();
        this.unknownDialogsIds.clear();
        this.lastSavedSeq = 0;
        this.lastSavedPts = 0;
        this.lastSavedDate = 0;
        this.lastSavedQts = 0;
        this.secretPBytes = null;
        this.secretG = 0;
    }

    public void cleanup(final boolean z) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$cleanup$6(z);
            }
        });
    }

    public void lambda$cleanup$6(boolean z) {
        cleanupInternal(true);
        openDatabase(1);
        if (z) {
            Utilities.stageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$cleanup$5();
                }
            });
        }
    }

    public void lambda$cleanup$5() {
        getMessagesController().getDifference();
    }

    public void saveSecretParams(final int i, final int i2, final byte[] bArr) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$saveSecretParams$7(i, i2, bArr);
            }
        });
    }

    public void lambda$saveSecretParams$7(int i, int i2, byte[] bArr) {
        try {
            SQLitePreparedStatement executeFast = this.database.executeFast("UPDATE params SET lsv = ?, sg = ?, pbytes = ? WHERE id = 1");
            executeFast.bindInteger(1, i);
            executeFast.bindInteger(2, i2);
            NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(bArr != null ? bArr.length : 1);
            if (bArr != null) {
                nativeByteBuffer.writeBytes(bArr);
            }
            executeFast.bindByteBuffer(3, nativeByteBuffer);
            executeFast.step();
            executeFast.dispose();
            nativeByteBuffer.reuse();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void checkSQLException(Throwable th) {
        checkSQLException(th, true);
    }

    private void checkSQLException(Throwable th, boolean z) {
        if ((th instanceof SQLiteException) && th.getMessage() != null && th.getMessage().contains("is malformed") && !this.tryRecover) {
            this.tryRecover = true;
            FileLog.e("disk image malformed detected, try recover");
            if (recoverDatabase()) {
                this.tryRecover = false;
                clearLoadingDialogsOffsets();
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$checkSQLException$8();
                    }
                });
                FileLog.e(new Exception("database restored!!"));
                return;
            }
            FileLog.e(new Exception(th), z);
            return;
        }
        FileLog.e(th, z);
    }

    public void lambda$checkSQLException$8() {
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.onDatabaseReset, new Object[0]);
    }

    public void fixNotificationSettings() {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$fixNotificationSettings$9();
            }
        });
    }

    public void lambda$fixNotificationSettings$9() {
        try {
            LongSparseArray longSparseArray = new LongSparseArray();
            Map<String, ?> all = MessagesController.getNotificationsSettings(this.currentAccount).getAll();
            for (Map.Entry<String, ?> entry : all.entrySet()) {
                String key = entry.getKey();
                if (key.startsWith(NotificationsSettingsFacade.PROPERTY_NOTIFY)) {
                    Integer num = (Integer) entry.getValue();
                    if (num.intValue() == 2 || num.intValue() == 3) {
                        String replace = key.replace(NotificationsSettingsFacade.PROPERTY_NOTIFY, "");
                        long j = 1;
                        if (num.intValue() != 2) {
                            if (((Integer) all.get(NotificationsSettingsFacade.PROPERTY_NOTIFY_UNTIL + replace)) != null) {
                                j = 1 | (r4.intValue() << 32);
                            }
                        }
                        try {
                            longSparseArray.put(Long.parseLong(replace), Long.valueOf(j));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            try {
                this.database.beginTransaction();
                SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO dialog_settings VALUES(?, ?)");
                for (int i = 0; i < longSparseArray.size(); i++) {
                    executeFast.requery();
                    executeFast.bindLong(1, longSparseArray.keyAt(i));
                    executeFast.bindLong(2, ((Long) longSparseArray.valueAt(i)).longValue());
                    executeFast.step();
                }
                executeFast.dispose();
                this.database.commitTransaction();
            } catch (Exception e2) {
                checkSQLException(e2);
            }
        } catch (Throwable th) {
            checkSQLException(th);
        }
    }

    public long createPendingTask(final NativeByteBuffer nativeByteBuffer) {
        if (nativeByteBuffer == null) {
            return 0L;
        }
        final long andAdd = this.lastTaskId.getAndAdd(1L);
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$createPendingTask$10(andAdd, nativeByteBuffer);
            }
        });
        return andAdd;
    }

    public void lambda$createPendingTask$10(long j, NativeByteBuffer nativeByteBuffer) {
        try {
            try {
                SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO pending_tasks VALUES(?, ?)");
                executeFast.bindLong(1, j);
                executeFast.bindByteBuffer(2, nativeByteBuffer);
                executeFast.step();
                executeFast.dispose();
            } catch (Exception e) {
                checkSQLException(e);
            }
        } finally {
            nativeByteBuffer.reuse();
        }
    }

    public void removePendingTask(final long j) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$removePendingTask$11(j);
            }
        });
    }

    public void lambda$removePendingTask$11(long j) {
        try {
            this.database.executeFast("DELETE FROM pending_tasks WHERE id = " + j).stepThis().dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    private void loadPendingTasks() {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$loadPendingTasks$32();
            }
        });
    }

    public void lambda$loadPendingTasks$32() {
        try {
            SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT id, data FROM pending_tasks WHERE 1", new Object[0]);
            while (queryFinalized.next()) {
                final long longValue = queryFinalized.longValue(0);
                NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(1);
                if (byteBufferValue != null) {
                    final int readInt32 = byteBufferValue.readInt32(false);
                    switch (readInt32) {
                        case 0:
                            final TLRPC$Chat TLdeserialize = TLRPC$Chat.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                            if (TLdeserialize != null) {
                                Utilities.stageQueue.postRunnable(new Runnable() {
                                    @Override
                                    public final void run() {
                                        MessagesStorage.this.lambda$loadPendingTasks$12(TLdeserialize, longValue);
                                    }
                                });
                                break;
                            }
                            break;
                        case 1:
                            final long readInt322 = byteBufferValue.readInt32(false);
                            final int readInt323 = byteBufferValue.readInt32(false);
                            Utilities.stageQueue.postRunnable(new Runnable() {
                                @Override
                                public final void run() {
                                    MessagesStorage.this.lambda$loadPendingTasks$13(readInt322, readInt323, longValue);
                                }
                            });
                            break;
                        case 2:
                        case 5:
                        case 8:
                        case 10:
                        case 14:
                            final TLRPC$TL_dialog tLRPC$TL_dialog = new TLRPC$TL_dialog();
                            tLRPC$TL_dialog.id = byteBufferValue.readInt64(false);
                            tLRPC$TL_dialog.top_message = byteBufferValue.readInt32(false);
                            tLRPC$TL_dialog.read_inbox_max_id = byteBufferValue.readInt32(false);
                            tLRPC$TL_dialog.read_outbox_max_id = byteBufferValue.readInt32(false);
                            tLRPC$TL_dialog.unread_count = byteBufferValue.readInt32(false);
                            tLRPC$TL_dialog.last_message_date = byteBufferValue.readInt32(false);
                            tLRPC$TL_dialog.pts = byteBufferValue.readInt32(false);
                            tLRPC$TL_dialog.flags = byteBufferValue.readInt32(false);
                            if (readInt32 >= 5) {
                                tLRPC$TL_dialog.pinned = byteBufferValue.readBool(false);
                                tLRPC$TL_dialog.pinnedNum = byteBufferValue.readInt32(false);
                            }
                            if (readInt32 >= 8) {
                                tLRPC$TL_dialog.unread_mentions_count = byteBufferValue.readInt32(false);
                            }
                            if (readInt32 >= 10) {
                                tLRPC$TL_dialog.unread_mark = byteBufferValue.readBool(false);
                            }
                            if (readInt32 >= 14) {
                                tLRPC$TL_dialog.folder_id = byteBufferValue.readInt32(false);
                            }
                            final TLRPC$InputPeer TLdeserialize2 = TLRPC$InputPeer.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                            AndroidUtilities.runOnUIThread(new Runnable() {
                                @Override
                                public final void run() {
                                    MessagesStorage.this.lambda$loadPendingTasks$14(tLRPC$TL_dialog, TLdeserialize2, longValue);
                                }
                            });
                            break;
                        case 3:
                            getSendMessagesHelper().sendGame(TLRPC$InputPeer.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false), (TLRPC$TL_inputMediaGame) TLRPC$InputMedia.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false), byteBufferValue.readInt64(false), longValue);
                            break;
                        case 4:
                            final long readInt64 = byteBufferValue.readInt64(false);
                            final boolean readBool = byteBufferValue.readBool(false);
                            final TLRPC$InputPeer TLdeserialize3 = TLRPC$InputPeer.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                            AndroidUtilities.runOnUIThread(new Runnable() {
                                @Override
                                public final void run() {
                                    MessagesStorage.this.lambda$loadPendingTasks$15(readInt64, readBool, TLdeserialize3, longValue);
                                }
                            });
                            break;
                        case 6:
                            final long readInt324 = byteBufferValue.readInt32(false);
                            final int readInt325 = byteBufferValue.readInt32(false);
                            final TLRPC$InputChannel TLdeserialize4 = TLRPC$InputChannel.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                            Utilities.stageQueue.postRunnable(new Runnable() {
                                @Override
                                public final void run() {
                                    MessagesStorage.this.lambda$loadPendingTasks$16(readInt324, readInt325, longValue, TLdeserialize4);
                                }
                            });
                            break;
                        case 7:
                            final long readInt326 = byteBufferValue.readInt32(false);
                            int readInt327 = byteBufferValue.readInt32(false);
                            TLObject TLdeserialize5 = TLRPC$TL_messages_deleteMessages.TLdeserialize(byteBufferValue, readInt327, false);
                            final TLObject TLdeserialize6 = TLdeserialize5 == null ? TLRPC$TL_channels_deleteMessages.TLdeserialize(byteBufferValue, readInt327, false) : TLdeserialize5;
                            if (TLdeserialize6 == null) {
                                removePendingTask(longValue);
                                break;
                            } else {
                                AndroidUtilities.runOnUIThread(new Runnable() {
                                    @Override
                                    public final void run() {
                                        MessagesStorage.this.lambda$loadPendingTasks$18(readInt326, longValue, TLdeserialize6);
                                    }
                                });
                                break;
                            }
                        case 9:
                            final long readInt642 = byteBufferValue.readInt64(false);
                            final TLRPC$InputPeer TLdeserialize7 = TLRPC$InputPeer.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                            AndroidUtilities.runOnUIThread(new Runnable() {
                                @Override
                                public final void run() {
                                    MessagesStorage.this.lambda$loadPendingTasks$20(readInt642, TLdeserialize7, longValue);
                                }
                            });
                            break;
                        case 11:
                            final int readInt328 = byteBufferValue.readInt32(false);
                            final long readInt329 = byteBufferValue.readInt32(false);
                            final int readInt3210 = byteBufferValue.readInt32(false);
                            final TLRPC$InputChannel TLdeserialize8 = readInt329 != 0 ? TLRPC$InputChannel.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false) : null;
                            AndroidUtilities.runOnUIThread(new Runnable() {
                                @Override
                                public final void run() {
                                    MessagesStorage.this.lambda$loadPendingTasks$21(readInt329, readInt328, TLdeserialize8, readInt3210, longValue);
                                }
                            });
                            break;
                        case 12:
                        case 19:
                        case 20:
                            removePendingTask(longValue);
                            break;
                        case 13:
                            final long readInt643 = byteBufferValue.readInt64(false);
                            final boolean readBool2 = byteBufferValue.readBool(false);
                            final int readInt3211 = byteBufferValue.readInt32(false);
                            final int readInt3212 = byteBufferValue.readInt32(false);
                            final boolean readBool3 = byteBufferValue.readBool(false);
                            final TLRPC$InputPeer TLdeserialize9 = TLRPC$InputPeer.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                            AndroidUtilities.runOnUIThread(new Runnable() {
                                @Override
                                public final void run() {
                                    MessagesStorage.this.lambda$loadPendingTasks$24(readInt643, readBool2, readInt3211, readInt3212, readBool3, TLdeserialize9, longValue);
                                }
                            });
                            break;
                        case 15:
                            final TLRPC$InputPeer TLdeserialize10 = TLRPC$InputPeer.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                            Utilities.stageQueue.postRunnable(new Runnable() {
                                @Override
                                public final void run() {
                                    MessagesStorage.this.lambda$loadPendingTasks$25(TLdeserialize10, longValue);
                                }
                            });
                            break;
                        case 16:
                            final int readInt3213 = byteBufferValue.readInt32(false);
                            int readInt3214 = byteBufferValue.readInt32(false);
                            final ArrayList arrayList = new ArrayList();
                            for (int i = 0; i < readInt3214; i++) {
                                arrayList.add(TLRPC$InputDialogPeer.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false));
                            }
                            AndroidUtilities.runOnUIThread(new Runnable() {
                                @Override
                                public final void run() {
                                    MessagesStorage.this.lambda$loadPendingTasks$26(readInt3213, arrayList, longValue);
                                }
                            });
                            break;
                        case 17:
                            final int readInt3215 = byteBufferValue.readInt32(false);
                            int readInt3216 = byteBufferValue.readInt32(false);
                            final ArrayList arrayList2 = new ArrayList();
                            for (int i2 = 0; i2 < readInt3216; i2++) {
                                arrayList2.add(TLRPC$TL_inputFolderPeer.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false));
                            }
                            AndroidUtilities.runOnUIThread(new Runnable() {
                                @Override
                                public final void run() {
                                    MessagesStorage.this.lambda$loadPendingTasks$27(readInt3215, arrayList2, longValue);
                                }
                            });
                            break;
                        case 18:
                            final long readInt644 = byteBufferValue.readInt64(false);
                            byteBufferValue.readInt32(false);
                            final TLRPC$TL_messages_deleteScheduledMessages TLdeserialize11 = TLRPC$TL_messages_deleteScheduledMessages.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                            if (TLdeserialize11 == null) {
                                removePendingTask(longValue);
                                break;
                            } else {
                                AndroidUtilities.runOnUIThread(new Runnable() {
                                    @Override
                                    public final void run() {
                                        MessagesStorage.this.lambda$loadPendingTasks$28(readInt644, longValue, TLdeserialize11);
                                    }
                                });
                                break;
                            }
                        case 21:
                            final Theme.OverrideWallpaperInfo overrideWallpaperInfo = new Theme.OverrideWallpaperInfo();
                            byteBufferValue.readInt64(false);
                            overrideWallpaperInfo.isBlurred = byteBufferValue.readBool(false);
                            overrideWallpaperInfo.isMotion = byteBufferValue.readBool(false);
                            overrideWallpaperInfo.color = byteBufferValue.readInt32(false);
                            overrideWallpaperInfo.gradientColor1 = byteBufferValue.readInt32(false);
                            overrideWallpaperInfo.rotation = byteBufferValue.readInt32(false);
                            overrideWallpaperInfo.intensity = (float) byteBufferValue.readDouble(false);
                            final boolean readBool4 = byteBufferValue.readBool(false);
                            overrideWallpaperInfo.slug = byteBufferValue.readString(false);
                            overrideWallpaperInfo.originalFileName = byteBufferValue.readString(false);
                            AndroidUtilities.runOnUIThread(new Runnable() {
                                @Override
                                public final void run() {
                                    MessagesStorage.this.lambda$loadPendingTasks$23(overrideWallpaperInfo, readBool4, longValue);
                                }
                            });
                            break;
                        case 22:
                            final TLRPC$InputPeer TLdeserialize12 = TLRPC$InputPeer.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                            AndroidUtilities.runOnUIThread(new Runnable() {
                                @Override
                                public final void run() {
                                    MessagesStorage.this.lambda$loadPendingTasks$29(TLdeserialize12, longValue);
                                }
                            });
                            break;
                        case 23:
                            break;
                        case 24:
                            final long readInt645 = byteBufferValue.readInt64(false);
                            int readInt3217 = byteBufferValue.readInt32(false);
                            TLObject TLdeserialize13 = TLRPC$TL_messages_deleteMessages.TLdeserialize(byteBufferValue, readInt3217, false);
                            final TLObject TLdeserialize14 = TLdeserialize13 == null ? TLRPC$TL_channels_deleteMessages.TLdeserialize(byteBufferValue, readInt3217, false) : TLdeserialize13;
                            if (TLdeserialize14 == null) {
                                removePendingTask(longValue);
                                break;
                            } else {
                                AndroidUtilities.runOnUIThread(new Runnable() {
                                    @Override
                                    public final void run() {
                                        MessagesStorage.this.lambda$loadPendingTasks$19(readInt645, longValue, TLdeserialize14);
                                    }
                                });
                                break;
                            }
                        case 25:
                            final long readInt646 = byteBufferValue.readInt64(false);
                            final int readInt3218 = byteBufferValue.readInt32(false);
                            final TLRPC$InputChannel TLdeserialize15 = TLRPC$InputChannel.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                            Utilities.stageQueue.postRunnable(new Runnable() {
                                @Override
                                public final void run() {
                                    MessagesStorage.this.lambda$loadPendingTasks$17(readInt646, readInt3218, longValue, TLdeserialize15);
                                }
                            });
                            break;
                        default:
                            switch (readInt32) {
                                case 100:
                                    final int readInt3219 = byteBufferValue.readInt32(false);
                                    final boolean readBool5 = byteBufferValue.readBool(false);
                                    AndroidUtilities.runOnUIThread(new Runnable() {
                                        @Override
                                        public final void run() {
                                            MessagesStorage.this.lambda$loadPendingTasks$30(readInt3219, readBool5, longValue);
                                        }
                                    });
                                    break;
                                case R.styleable.AppCompatTheme_textAppearanceLargePopupMenu:
                                    final long readInt647 = byteBufferValue.readInt64(false);
                                    final int readInt3220 = byteBufferValue.readInt32(false);
                                    AndroidUtilities.runOnUIThread(new Runnable() {
                                        @Override
                                        public final void run() {
                                            MessagesStorage.this.lambda$loadPendingTasks$31(longValue, readInt647, readInt3220);
                                        }
                                    });
                                    break;
                            }
                    }
                    final long readInt648 = byteBufferValue.readInt64(false);
                    final int readInt3221 = byteBufferValue.readInt32(false);
                    final int readInt3222 = byteBufferValue.readInt32(false);
                    if (!DialogObject.isEncryptedDialog(readInt648) && DialogObject.isChatDialog(readInt648) && byteBufferValue.hasRemaining()) {
                        r1 = TLRPC$InputChannel.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                    }
                    final TLRPC$InputChannel tLRPC$InputChannel = r1;
                    AndroidUtilities.runOnUIThread(new Runnable() {
                        @Override
                        public final void run() {
                            MessagesStorage.this.lambda$loadPendingTasks$22(readInt648, readInt3221, tLRPC$InputChannel, readInt3222, longValue, readInt32);
                        }
                    });
                    byteBufferValue.reuse();
                }
            }
            queryFinalized.dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void lambda$loadPendingTasks$12(TLRPC$Chat tLRPC$Chat, long j) {
        getMessagesController().loadUnknownChannel(tLRPC$Chat, j);
    }

    public void lambda$loadPendingTasks$13(long j, int i, long j2) {
        getMessagesController().getChannelDifference(j, i, j2, null);
    }

    public void lambda$loadPendingTasks$14(TLRPC$Dialog tLRPC$Dialog, TLRPC$InputPeer tLRPC$InputPeer, long j) {
        getMessagesController().checkLastDialogMessage(tLRPC$Dialog, tLRPC$InputPeer, j);
    }

    public void lambda$loadPendingTasks$15(long j, boolean z, TLRPC$InputPeer tLRPC$InputPeer, long j2) {
        getMessagesController().pinDialog(j, z, tLRPC$InputPeer, j2);
    }

    public void lambda$loadPendingTasks$16(long j, int i, long j2, TLRPC$InputChannel tLRPC$InputChannel) {
        getMessagesController().getChannelDifference(j, i, j2, tLRPC$InputChannel);
    }

    public void lambda$loadPendingTasks$17(long j, int i, long j2, TLRPC$InputChannel tLRPC$InputChannel) {
        getMessagesController().getChannelDifference(j, i, j2, tLRPC$InputChannel);
    }

    public void lambda$loadPendingTasks$18(long j, long j2, TLObject tLObject) {
        getMessagesController().deleteMessages(null, null, null, -j, true, false, false, j2, tLObject);
    }

    public void lambda$loadPendingTasks$19(long j, long j2, TLObject tLObject) {
        getMessagesController().deleteMessages(null, null, null, j, true, false, false, j2, tLObject);
    }

    public void lambda$loadPendingTasks$20(long j, TLRPC$InputPeer tLRPC$InputPeer, long j2) {
        getMessagesController().markDialogAsUnread(j, tLRPC$InputPeer, j2);
    }

    public void lambda$loadPendingTasks$21(long j, int i, TLRPC$InputChannel tLRPC$InputChannel, int i2, long j2) {
        getMessagesController().markMessageAsRead2(-j, i, tLRPC$InputChannel, i2, j2);
    }

    public void lambda$loadPendingTasks$22(long j, int i, TLRPC$InputChannel tLRPC$InputChannel, int i2, long j2, int i3) {
        getMessagesController().markMessageAsRead2(j, i, tLRPC$InputChannel, i2, j2, i3 == 23);
    }

    public void lambda$loadPendingTasks$23(Theme.OverrideWallpaperInfo overrideWallpaperInfo, boolean z, long j) {
        getMessagesController().saveWallpaperToServer(null, overrideWallpaperInfo, z, j);
    }

    public void lambda$loadPendingTasks$24(long j, boolean z, int i, int i2, boolean z2, TLRPC$InputPeer tLRPC$InputPeer, long j2) {
        getMessagesController().deleteDialog(j, z ? 1 : 0, i, i2, z2, tLRPC$InputPeer, j2);
    }

    public void lambda$loadPendingTasks$25(TLRPC$InputPeer tLRPC$InputPeer, long j) {
        getMessagesController().loadUnknownDialog(tLRPC$InputPeer, j);
    }

    public void lambda$loadPendingTasks$26(int i, ArrayList arrayList, long j) {
        getMessagesController().reorderPinnedDialogs(i, arrayList, j);
    }

    public void lambda$loadPendingTasks$27(int i, ArrayList arrayList, long j) {
        getMessagesController().addDialogToFolder(null, i, -1, arrayList, j);
    }

    public void lambda$loadPendingTasks$28(long j, long j2, TLObject tLObject) {
        getMessagesController().deleteMessages(null, null, null, j, true, true, false, j2, tLObject);
    }

    public void lambda$loadPendingTasks$29(TLRPC$InputPeer tLRPC$InputPeer, long j) {
        getMessagesController().reloadMentionsCountForChannel(tLRPC$InputPeer, j);
    }

    public void lambda$loadPendingTasks$30(int i, boolean z, long j) {
        getSecretChatHelper().declineSecretChat(i, z, j);
    }

    public void lambda$loadPendingTasks$31(long j, long j2, int i) {
        getMessagesController().lambda$checkDeletingTask$76(j, j2, i);
    }

    public void saveChannelPts(final long j, final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$saveChannelPts$33(i, j);
            }
        });
    }

    public void lambda$saveChannelPts$33(int i, long j) {
        try {
            SQLitePreparedStatement executeFast = this.database.executeFast("UPDATE dialogs SET pts = ? WHERE did = ?");
            executeFast.bindInteger(1, i);
            executeFast.bindLong(2, -j);
            executeFast.step();
            executeFast.dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void lambda$saveDiffParams$34(int i, int i2, int i3, int i4) {
        try {
            if (this.lastSavedSeq == i && this.lastSavedPts == i2 && this.lastSavedDate == i3 && this.lastQtsValue == i4) {
                return;
            }
            SQLitePreparedStatement executeFast = this.database.executeFast("UPDATE params SET seq = ?, pts = ?, date = ?, qts = ? WHERE id = 1");
            executeFast.bindInteger(1, i);
            executeFast.bindInteger(2, i2);
            executeFast.bindInteger(3, i3);
            executeFast.bindInteger(4, i4);
            executeFast.step();
            executeFast.dispose();
            this.lastSavedSeq = i;
            this.lastSavedPts = i2;
            this.lastSavedDate = i3;
            this.lastSavedQts = i4;
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void saveDiffParams(final int i, final int i2, final int i3, final int i4) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$saveDiffParams$34(i, i2, i3, i4);
            }
        });
    }

    public void lambda$updateMutedDialogsFiltersCounters$35() {
        resetAllUnreadCounters(true);
    }

    public void updateMutedDialogsFiltersCounters() {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateMutedDialogsFiltersCounters$35();
            }
        });
    }

    public void setDialogFlags(final long j, final long j2) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$setDialogFlags$36(j, j2);
            }
        });
    }

    public void lambda$setDialogFlags$36(long j, long j2) {
        try {
            SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT flags FROM dialog_settings WHERE did = " + j, new Object[0]);
            int intValue = queryFinalized.next() ? queryFinalized.intValue(0) : 0;
            queryFinalized.dispose();
            if (j2 == intValue) {
                return;
            }
            this.database.executeFast(String.format(Locale.US, "REPLACE INTO dialog_settings VALUES(%d, %d)", Long.valueOf(j), Long.valueOf(j2))).stepThis().dispose();
            resetAllUnreadCounters(true);
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void putStoryPushMessage(final NotificationsController.StoryNotification storyNotification) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$putStoryPushMessage$37(storyNotification);
            }
        });
    }

    public void lambda$putStoryPushMessage$37(NotificationsController.StoryNotification storyNotification) {
        try {
            this.database.executeFast("DELETE FROM story_pushes WHERE uid = " + storyNotification.dialogId).stepThis().dispose();
            SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO story_pushes VALUES(?, ?, ?, ?, ?, ?)");
            for (Map.Entry<Integer, Pair<Long, Long>> entry : storyNotification.dateByIds.entrySet()) {
                int intValue = entry.getKey().intValue();
                long longValue = ((Long) entry.getValue().first).longValue();
                long longValue2 = ((Long) entry.getValue().second).longValue();
                executeFast.requery();
                int i = 1;
                executeFast.bindLong(1, storyNotification.dialogId);
                executeFast.bindInteger(2, intValue);
                executeFast.bindLong(3, longValue);
                if (storyNotification.localName == null) {
                    storyNotification.localName = "";
                }
                executeFast.bindString(4, storyNotification.localName);
                if (!storyNotification.hidden) {
                    i = 0;
                }
                executeFast.bindInteger(5, i);
                executeFast.bindLong(6, longValue2);
                executeFast.step();
            }
            executeFast.dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void deleteStoryPushMessage(final long j) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$deleteStoryPushMessage$38(j);
            }
        });
    }

    public void lambda$deleteStoryPushMessage$38(long j) {
        try {
            this.database.executeFast("DELETE FROM story_pushes WHERE uid = " + j).stepThis().dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void deleteAllStoryPushMessages() {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$deleteAllStoryPushMessages$39();
            }
        });
    }

    public void lambda$deleteAllStoryPushMessages$39() {
        try {
            this.database.executeFast("DELETE FROM story_pushes").stepThis().dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void putPushMessage(final MessageObject messageObject) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$putPushMessage$40(messageObject);
            }
        });
    }

    public void lambda$putPushMessage$40(MessageObject messageObject) {
        try {
            NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(messageObject.messageOwner.getObjectSize());
            messageObject.messageOwner.serializeToStream(nativeByteBuffer);
            int i = 1;
            int i2 = messageObject.localType == 2 ? 1 : 0;
            if (messageObject.localChannel) {
                i2 |= 2;
            }
            SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO unread_push_messages VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            executeFast.requery();
            executeFast.bindLong(1, messageObject.getDialogId());
            executeFast.bindInteger(2, messageObject.getId());
            executeFast.bindLong(3, messageObject.messageOwner.random_id);
            executeFast.bindInteger(4, messageObject.messageOwner.date);
            executeFast.bindByteBuffer(5, nativeByteBuffer);
            CharSequence charSequence = messageObject.messageText;
            if (charSequence == null) {
                executeFast.bindNull(6);
            } else {
                executeFast.bindString(6, charSequence.toString());
            }
            String str = messageObject.localName;
            if (str == null) {
                executeFast.bindNull(7);
            } else {
                executeFast.bindString(7, str);
            }
            String str2 = messageObject.localUserName;
            if (str2 == null) {
                executeFast.bindNull(8);
            } else {
                executeFast.bindString(8, str2);
            }
            executeFast.bindInteger(9, i2);
            executeFast.bindLong(10, MessageObject.getTopicId(this.currentAccount, messageObject.messageOwner, false));
            if (!messageObject.isReactionPush) {
                i = 0;
            }
            executeFast.bindInteger(11, i);
            executeFast.step();
            nativeByteBuffer.reuse();
            executeFast.dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void clearLocalDatabase() {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$clearLocalDatabase$42();
            }
        });
    }

    public void lambda$clearLocalDatabase$42() {
        Throwable th;
        SQLitePreparedStatement sQLitePreparedStatement;
        Exception exc;
        SQLitePreparedStatement sQLitePreparedStatement2;
        SQLiteCursor sQLiteCursor;
        SQLiteCursor sQLiteCursor2;
        SQLitePreparedStatement sQLitePreparedStatement3;
        SQLiteDatabase sQLiteDatabase;
        SQLiteDatabase sQLiteDatabase2;
        ArrayList arrayList;
        int i;
        SQLitePreparedStatement executeFast;
        SQLiteCursor sQLiteCursor3;
        ArrayList arrayList2;
        int i2;
        SQLiteCursor sQLiteCursor4;
        int i3;
        Exception exc2;
        try {
            arrayList = new ArrayList();
            this.database.executeFast("DELETE FROM reaction_mentions").stepThis().dispose();
            this.database.executeFast("DELETE FROM reaction_mentions_topics").stepThis().dispose();
            this.database.executeFast("DELETE FROM downloading_documents").stepThis().dispose();
            this.database.executeFast("DELETE FROM attach_menu_bots").stepThis().dispose();
            this.database.executeFast("DELETE FROM animated_emoji").stepThis().dispose();
            this.database.executeFast("DELETE FROM stickers_v2").stepThis().dispose();
            this.database.executeFast("DELETE FROM stickersets2").stepThis().dispose();
            this.database.executeFast("DELETE FROM messages_holes_topics").stepThis().dispose();
            this.database.executeFast("DELETE FROM messages_topics").stepThis().dispose();
            this.database.executeFast("DELETE FROM saved_dialogs").stepThis().dispose();
            this.database.executeFast("DELETE FROM topics").stepThis().dispose();
            this.database.executeFast("DELETE FROM media_holes_topics").stepThis().dispose();
            this.database.executeFast("DELETE FROM media_topics").stepThis().dispose();
            this.database.executeFast("DELETE FROM media_counts_topics").stepThis().dispose();
            this.database.executeFast("DELETE FROM chat_pinned_v2").stepThis().dispose();
            this.database.executeFast("DELETE FROM chat_pinned_count").stepThis().dispose();
            this.database.executeFast("DELETE FROM profile_stories").stepThis().dispose();
            this.database.executeFast("DELETE FROM story_pushes").stepThis().dispose();
            this.database.executeFast("DELETE FROM dialog_photos").stepThis().dispose();
            this.database.executeFast("DELETE FROM dialog_photos_count").stepThis().dispose();
            i = 0;
            SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT did FROM dialogs WHERE 1", new Object[0]);
            while (queryFinalized.next()) {
                try {
                    long longValue = queryFinalized.longValue(0);
                    if (!DialogObject.isEncryptedDialog(longValue)) {
                        arrayList.add(Long.valueOf(longValue));
                    }
                } catch (Exception e) {
                    exc = e;
                    sQLiteCursor = queryFinalized;
                } catch (Throwable th2) {
                    th = th2;
                    sQLiteCursor2 = queryFinalized;
                }
            }
            queryFinalized.dispose();
            executeFast = this.database.executeFast("REPLACE INTO messages_holes VALUES(?, ?, ?)");
        } catch (Exception e2) {
            exc = e2;
            sQLitePreparedStatement = null;
        } catch (Throwable th3) {
            th = th3;
            sQLitePreparedStatement = null;
        }
        try {
            sQLitePreparedStatement2 = this.database.executeFast("REPLACE INTO media_holes_v2 VALUES(?, ?, ?, ?)");
            try {
                this.database.beginTransaction();
                int i4 = 0;
                sQLiteCursor = null;
                while (i4 < arrayList.size()) {
                    try {
                        Long l = (Long) arrayList.get(i4);
                        sQLiteCursor = this.database.queryFinalized("SELECT COUNT(mid) FROM messages_v2 WHERE uid = " + l, new Object[i]);
                        int intValue = sQLiteCursor.next() ? sQLiteCursor.intValue(i) : i;
                        sQLiteCursor.dispose();
                        if (intValue <= 2) {
                            arrayList2 = arrayList;
                            i2 = i4;
                        } else {
                            SQLiteCursor queryFinalized2 = this.database.queryFinalized("SELECT last_mid_i, last_mid FROM dialogs WHERE did = " + l, new Object[i]);
                            try {
                                if (queryFinalized2.next()) {
                                    long longValue2 = queryFinalized2.longValue(i);
                                    arrayList2 = arrayList;
                                    long longValue3 = queryFinalized2.longValue(1);
                                    SQLiteCursor queryFinalized3 = this.database.queryFinalized("SELECT data FROM messages_v2 WHERE uid = " + l + " AND mid IN (" + longValue2 + "," + longValue3 + ")", new Object[i]);
                                    int i5 = -1;
                                    ?? r7 = i;
                                    while (queryFinalized3.next()) {
                                        try {
                                            NativeByteBuffer byteBufferValue = queryFinalized3.byteBufferValue(r7);
                                            if (byteBufferValue != 0) {
                                                sQLiteCursor4 = queryFinalized2;
                                                try {
                                                    try {
                                                        TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(r7), r7);
                                                        if (TLdeserialize != null) {
                                                            i5 = TLdeserialize.id;
                                                            try {
                                                                i2 = i4;
                                                                try {
                                                                    TLdeserialize.readAttachPath(byteBufferValue, UserConfig.getInstance(this.currentAccount).clientUserId);
                                                                    i5 = i5;
                                                                } catch (Exception e3) {
                                                                    exc2 = e3;
                                                                    i5 = i5;
                                                                    try {
                                                                        checkSQLException(exc2);
                                                                        i3 = i5;
                                                                        queryFinalized3.dispose();
                                                                        this.database.executeFast("DELETE FROM messages_v2 WHERE uid = " + l + " AND mid != " + longValue2 + " AND mid != " + longValue3).stepThis().dispose();
                                                                        SQLiteDatabase sQLiteDatabase3 = this.database;
                                                                        StringBuilder sb = new StringBuilder();
                                                                        sb.append("DELETE FROM messages_holes WHERE uid = ");
                                                                        sb.append(l);
                                                                        sQLiteDatabase3.executeFast(sb.toString()).stepThis().dispose();
                                                                        this.database.executeFast("DELETE FROM bot_keyboard WHERE uid = " + l).stepThis().dispose();
                                                                        this.database.executeFast("DELETE FROM bot_keyboard_topics WHERE uid = " + l).stepThis().dispose();
                                                                        this.database.executeFast("DELETE FROM media_counts_v2 WHERE uid = " + l).stepThis().dispose();
                                                                        this.database.executeFast("DELETE FROM media_v4 WHERE uid = " + l).stepThis().dispose();
                                                                        this.database.executeFast("DELETE FROM media_holes_v2 WHERE uid = " + l).stepThis().dispose();
                                                                        MediaDataController.getInstance(this.currentAccount).clearBotKeyboard(l.longValue());
                                                                        if (i3 == -1) {
                                                                        }
                                                                        sQLiteCursor3.dispose();
                                                                        sQLiteCursor = null;
                                                                        i4 = i2 + 1;
                                                                        arrayList = arrayList2;
                                                                        i = 0;
                                                                    } catch (Exception e4) {
                                                                        e = e4;
                                                                        sQLiteCursor3 = sQLiteCursor4;
                                                                        exc = e;
                                                                        sQLiteCursor = sQLiteCursor3;
                                                                        sQLitePreparedStatement = executeFast;
                                                                        sQLitePreparedStatement3 = sQLitePreparedStatement2;
                                                                        try {
                                                                            checkSQLException(exc);
                                                                            sQLiteDatabase2 = this.database;
                                                                            if (sQLiteDatabase2 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement3 != null) {
                                                                            }
                                                                        } catch (Throwable th4) {
                                                                            th = th4;
                                                                            sQLitePreparedStatement2 = sQLitePreparedStatement3;
                                                                            sQLiteCursor2 = sQLiteCursor;
                                                                            sQLiteDatabase = this.database;
                                                                            if (sQLiteDatabase != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement2 != null) {
                                                                            }
                                                                            if (sQLiteCursor2 != null) {
                                                                            }
                                                                            reset();
                                                                            throw th;
                                                                        }
                                                                    } catch (Throwable th5) {
                                                                        th = th5;
                                                                        sQLiteCursor3 = sQLiteCursor4;
                                                                        th = th;
                                                                        sQLiteCursor2 = sQLiteCursor3;
                                                                        sQLitePreparedStatement = executeFast;
                                                                        sQLiteDatabase = this.database;
                                                                        if (sQLiteDatabase != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement2 != null) {
                                                                        }
                                                                        if (sQLiteCursor2 != null) {
                                                                        }
                                                                        reset();
                                                                        throw th;
                                                                    }
                                                                }
                                                            } catch (Exception e5) {
                                                                e = e5;
                                                                i2 = i4;
                                                                exc2 = e;
                                                                checkSQLException(exc2);
                                                                i3 = i5;
                                                                queryFinalized3.dispose();
                                                                this.database.executeFast("DELETE FROM messages_v2 WHERE uid = " + l + " AND mid != " + longValue2 + " AND mid != " + longValue3).stepThis().dispose();
                                                                SQLiteDatabase sQLiteDatabase32 = this.database;
                                                                StringBuilder sb2 = new StringBuilder();
                                                                sb2.append("DELETE FROM messages_holes WHERE uid = ");
                                                                sb2.append(l);
                                                                sQLiteDatabase32.executeFast(sb2.toString()).stepThis().dispose();
                                                                this.database.executeFast("DELETE FROM bot_keyboard WHERE uid = " + l).stepThis().dispose();
                                                                this.database.executeFast("DELETE FROM bot_keyboard_topics WHERE uid = " + l).stepThis().dispose();
                                                                this.database.executeFast("DELETE FROM media_counts_v2 WHERE uid = " + l).stepThis().dispose();
                                                                this.database.executeFast("DELETE FROM media_v4 WHERE uid = " + l).stepThis().dispose();
                                                                this.database.executeFast("DELETE FROM media_holes_v2 WHERE uid = " + l).stepThis().dispose();
                                                                MediaDataController.getInstance(this.currentAccount).clearBotKeyboard(l.longValue());
                                                                if (i3 == -1) {
                                                                }
                                                                sQLiteCursor3.dispose();
                                                                sQLiteCursor = null;
                                                                i4 = i2 + 1;
                                                                arrayList = arrayList2;
                                                                i = 0;
                                                            }
                                                        } else {
                                                            i2 = i4;
                                                        }
                                                    } catch (Throwable th6) {
                                                        th = th6;
                                                        th = th;
                                                        sQLitePreparedStatement = executeFast;
                                                        sQLiteCursor2 = sQLiteCursor4;
                                                        sQLiteDatabase = this.database;
                                                        if (sQLiteDatabase != null) {
                                                            sQLiteDatabase.commitTransaction();
                                                        }
                                                        if (sQLitePreparedStatement != null) {
                                                            sQLitePreparedStatement.dispose();
                                                        }
                                                        if (sQLitePreparedStatement2 != null) {
                                                            sQLitePreparedStatement2.dispose();
                                                        }
                                                        if (sQLiteCursor2 != null) {
                                                            sQLiteCursor2.dispose();
                                                        }
                                                        reset();
                                                        throw th;
                                                    }
                                                } catch (Exception e6) {
                                                    e = e6;
                                                }
                                                try {
                                                    byteBufferValue.reuse();
                                                } catch (Exception e7) {
                                                    e = e7;
                                                    exc2 = e;
                                                    checkSQLException(exc2);
                                                    i3 = i5;
                                                    queryFinalized3.dispose();
                                                    this.database.executeFast("DELETE FROM messages_v2 WHERE uid = " + l + " AND mid != " + longValue2 + " AND mid != " + longValue3).stepThis().dispose();
                                                    SQLiteDatabase sQLiteDatabase322 = this.database;
                                                    StringBuilder sb22 = new StringBuilder();
                                                    sb22.append("DELETE FROM messages_holes WHERE uid = ");
                                                    sb22.append(l);
                                                    sQLiteDatabase322.executeFast(sb22.toString()).stepThis().dispose();
                                                    this.database.executeFast("DELETE FROM bot_keyboard WHERE uid = " + l).stepThis().dispose();
                                                    this.database.executeFast("DELETE FROM bot_keyboard_topics WHERE uid = " + l).stepThis().dispose();
                                                    this.database.executeFast("DELETE FROM media_counts_v2 WHERE uid = " + l).stepThis().dispose();
                                                    this.database.executeFast("DELETE FROM media_v4 WHERE uid = " + l).stepThis().dispose();
                                                    this.database.executeFast("DELETE FROM media_holes_v2 WHERE uid = " + l).stepThis().dispose();
                                                    MediaDataController.getInstance(this.currentAccount).clearBotKeyboard(l.longValue());
                                                    if (i3 == -1) {
                                                    }
                                                    sQLiteCursor3.dispose();
                                                    sQLiteCursor = null;
                                                    i4 = i2 + 1;
                                                    arrayList = arrayList2;
                                                    i = 0;
                                                }
                                            } else {
                                                sQLiteCursor4 = queryFinalized2;
                                                i2 = i4;
                                            }
                                            queryFinalized2 = sQLiteCursor4;
                                            i4 = i2;
                                            r7 = 0;
                                        } catch (Exception e8) {
                                            e = e8;
                                            sQLiteCursor4 = queryFinalized2;
                                        } catch (Throwable th7) {
                                            th = th7;
                                            sQLiteCursor4 = queryFinalized2;
                                        }
                                    }
                                    sQLiteCursor4 = queryFinalized2;
                                    i2 = i4;
                                    i3 = i5;
                                    queryFinalized3.dispose();
                                    this.database.executeFast("DELETE FROM messages_v2 WHERE uid = " + l + " AND mid != " + longValue2 + " AND mid != " + longValue3).stepThis().dispose();
                                    SQLiteDatabase sQLiteDatabase3222 = this.database;
                                    StringBuilder sb222 = new StringBuilder();
                                    sb222.append("DELETE FROM messages_holes WHERE uid = ");
                                    sb222.append(l);
                                    sQLiteDatabase3222.executeFast(sb222.toString()).stepThis().dispose();
                                    this.database.executeFast("DELETE FROM bot_keyboard WHERE uid = " + l).stepThis().dispose();
                                    this.database.executeFast("DELETE FROM bot_keyboard_topics WHERE uid = " + l).stepThis().dispose();
                                    this.database.executeFast("DELETE FROM media_counts_v2 WHERE uid = " + l).stepThis().dispose();
                                    this.database.executeFast("DELETE FROM media_v4 WHERE uid = " + l).stepThis().dispose();
                                    this.database.executeFast("DELETE FROM media_holes_v2 WHERE uid = " + l).stepThis().dispose();
                                    MediaDataController.getInstance(this.currentAccount).clearBotKeyboard(l.longValue());
                                    if (i3 == -1) {
                                        sQLiteCursor3 = sQLiteCursor4;
                                        try {
                                            createFirstHoles(l.longValue(), executeFast, sQLitePreparedStatement2, i3, 0L);
                                        } catch (Exception e9) {
                                            e = e9;
                                            exc = e;
                                            sQLiteCursor = sQLiteCursor3;
                                            sQLitePreparedStatement = executeFast;
                                            sQLitePreparedStatement3 = sQLitePreparedStatement2;
                                            checkSQLException(exc);
                                            sQLiteDatabase2 = this.database;
                                            if (sQLiteDatabase2 != null) {
                                            }
                                            if (sQLitePreparedStatement != null) {
                                            }
                                            if (sQLitePreparedStatement3 != null) {
                                            }
                                        } catch (Throwable th8) {
                                            th = th8;
                                            th = th;
                                            sQLiteCursor2 = sQLiteCursor3;
                                            sQLitePreparedStatement = executeFast;
                                            sQLiteDatabase = this.database;
                                            if (sQLiteDatabase != null) {
                                            }
                                            if (sQLitePreparedStatement != null) {
                                            }
                                            if (sQLitePreparedStatement2 != null) {
                                            }
                                            if (sQLiteCursor2 != null) {
                                            }
                                            reset();
                                            throw th;
                                        }
                                    } else {
                                        sQLiteCursor3 = sQLiteCursor4;
                                    }
                                } else {
                                    arrayList2 = arrayList;
                                    sQLiteCursor3 = queryFinalized2;
                                    i2 = i4;
                                }
                                sQLiteCursor3.dispose();
                                sQLiteCursor = null;
                            } catch (Exception e10) {
                                e = e10;
                                sQLiteCursor3 = queryFinalized2;
                            } catch (Throwable th9) {
                                th = th9;
                                sQLiteCursor3 = queryFinalized2;
                            }
                        }
                        i4 = i2 + 1;
                        arrayList = arrayList2;
                        i = 0;
                    } catch (Exception e11) {
                        exc = e11;
                    } catch (Throwable th10) {
                        th = th10;
                        sQLitePreparedStatement = executeFast;
                        sQLiteCursor2 = sQLiteCursor;
                        sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                        }
                        if (sQLitePreparedStatement != null) {
                        }
                        if (sQLitePreparedStatement2 != null) {
                        }
                        if (sQLiteCursor2 != null) {
                        }
                        reset();
                        throw th;
                    }
                }
                executeFast.dispose();
                sQLitePreparedStatement2.dispose();
                try {
                    this.database.commitTransaction();
                    this.database.executeFast("PRAGMA journal_size_limit = 0").stepThis().dispose();
                    this.database.executeFast("VACUUM").stepThis().dispose();
                    this.database.executeFast("PRAGMA journal_size_limit = -1").stepThis().dispose();
                    getMessagesController().getTopicsController().databaseCleared();
                    AndroidUtilities.runOnUIThread(new Runnable() {
                        @Override
                        public final void run() {
                            MessagesStorage.this.lambda$clearLocalDatabase$41();
                        }
                    });
                    SQLiteDatabase sQLiteDatabase4 = this.database;
                    if (sQLiteDatabase4 != null) {
                        sQLiteDatabase4.commitTransaction();
                    }
                } catch (Exception e12) {
                    exc = e12;
                    sQLitePreparedStatement = null;
                    sQLitePreparedStatement3 = null;
                    checkSQLException(exc);
                    sQLiteDatabase2 = this.database;
                    if (sQLiteDatabase2 != null) {
                        sQLiteDatabase2.commitTransaction();
                    }
                    if (sQLitePreparedStatement != null) {
                        sQLitePreparedStatement.dispose();
                    }
                    if (sQLitePreparedStatement3 != null) {
                        sQLitePreparedStatement3.dispose();
                    }
                } catch (Throwable th11) {
                    th = th11;
                    sQLiteCursor2 = sQLiteCursor;
                    sQLitePreparedStatement = null;
                    sQLitePreparedStatement2 = null;
                    sQLiteDatabase = this.database;
                    if (sQLiteDatabase != null) {
                    }
                    if (sQLitePreparedStatement != null) {
                    }
                    if (sQLitePreparedStatement2 != null) {
                    }
                    if (sQLiteCursor2 != null) {
                    }
                    reset();
                    throw th;
                }
            } catch (Exception e13) {
                exc = e13;
                sQLitePreparedStatement = executeFast;
                sQLitePreparedStatement3 = sQLitePreparedStatement2;
                sQLiteCursor = null;
            } catch (Throwable th12) {
                th = th12;
                sQLitePreparedStatement = executeFast;
                sQLiteCursor2 = null;
                sQLiteDatabase = this.database;
                if (sQLiteDatabase != null) {
                }
                if (sQLitePreparedStatement != null) {
                }
                if (sQLitePreparedStatement2 != null) {
                }
                if (sQLiteCursor2 != null) {
                }
                reset();
                throw th;
            }
        } catch (Exception e14) {
            exc = e14;
            sQLitePreparedStatement = executeFast;
            sQLiteCursor = null;
            sQLitePreparedStatement3 = null;
            checkSQLException(exc);
            sQLiteDatabase2 = this.database;
            if (sQLiteDatabase2 != null) {
            }
            if (sQLitePreparedStatement != null) {
            }
            if (sQLitePreparedStatement3 != null) {
            }
        } catch (Throwable th13) {
            th = th13;
            sQLitePreparedStatement = executeFast;
            sQLitePreparedStatement2 = null;
            sQLiteCursor2 = null;
            sQLiteDatabase = this.database;
            if (sQLiteDatabase != null) {
            }
            if (sQLitePreparedStatement != null) {
            }
            if (sQLitePreparedStatement2 != null) {
            }
            if (sQLiteCursor2 != null) {
            }
            reset();
            throw th;
        }
    }

    public void lambda$clearLocalDatabase$41() {
        getMessagesController().getSavedMessagesController().cleanup();
    }

    public void saveTopics(final long j, final List<TLRPC$TL_forumTopic> list, final boolean z, boolean z2) {
        if (z2) {
            this.storageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$saveTopics$43(j, list, z);
                }
            });
        } else {
            saveTopicsInternal(j, list, z, false);
        }
    }

    public void lambda$saveTopics$43(long j, List list, boolean z) {
        saveTopicsInternal(j, list, z, true);
    }

    private void saveTopicsInternal(long j, List<TLRPC$TL_forumTopic> list, boolean z, boolean z2) {
        int i;
        SQLitePreparedStatement sQLitePreparedStatement;
        int i2;
        int i3;
        SQLitePreparedStatement sQLitePreparedStatement2 = null;
        try {
            try {
                HashSet hashSet = new HashSet();
                HashMap hashMap = new HashMap();
                boolean z3 = false;
                int i4 = 0;
                while (true) {
                    i = 2;
                    if (i4 >= list.size()) {
                        break;
                    }
                    TLRPC$TL_forumTopic tLRPC$TL_forumTopic = list.get(i4);
                    SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT did, pinned FROM topics WHERE did = " + j + " AND topic_id = " + tLRPC$TL_forumTopic.id, new Object[0]);
                    boolean next = queryFinalized.next();
                    if (next) {
                        hashMap.put(Integer.valueOf(i4), Integer.valueOf(queryFinalized.intValue(2)));
                    }
                    queryFinalized.dispose();
                    if (next) {
                        hashSet.add(Integer.valueOf(i4));
                    }
                    i4++;
                }
                if (z) {
                    this.database.executeFast("DELETE FROM topics WHERE did = " + j).stepThis().dispose();
                }
                SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO topics VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                if (z2) {
                    try {
                        this.database.beginTransaction();
                    } catch (Exception e) {
                        e = e;
                        sQLitePreparedStatement2 = executeFast;
                        checkSQLException(e);
                        if (sQLitePreparedStatement2 != null) {
                        }
                        this.database.commitTransaction();
                    } catch (Throwable th) {
                        th = th;
                        sQLitePreparedStatement2 = executeFast;
                        if (sQLitePreparedStatement2 != null) {
                        }
                        this.database.commitTransaction();
                        throw th;
                    }
                }
                int i5 = 0;
                while (i5 < list.size()) {
                    try {
                        TLRPC$TL_forumTopic tLRPC$TL_forumTopic2 = list.get(i5);
                        boolean contains = hashSet.contains(Integer.valueOf(i5));
                        executeFast.requery();
                        executeFast.bindLong(1, j);
                        executeFast.bindInteger(i, tLRPC$TL_forumTopic2.id);
                        NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$TL_forumTopic2.getObjectSize());
                        tLRPC$TL_forumTopic2.serializeToStream(nativeByteBuffer);
                        executeFast.bindByteBuffer(3, nativeByteBuffer);
                        executeFast.bindInteger(4, tLRPC$TL_forumTopic2.top_message);
                        NativeByteBuffer nativeByteBuffer2 = new NativeByteBuffer(tLRPC$TL_forumTopic2.topicStartMessage.getObjectSize());
                        tLRPC$TL_forumTopic2.topicStartMessage.serializeToStream(nativeByteBuffer2);
                        executeFast.bindByteBuffer(5, nativeByteBuffer2);
                        executeFast.bindInteger(6, tLRPC$TL_forumTopic2.unread_count);
                        executeFast.bindInteger(7, tLRPC$TL_forumTopic2.read_inbox_max_id);
                        executeFast.bindInteger(8, tLRPC$TL_forumTopic2.unread_mentions_count);
                        executeFast.bindInteger(9, tLRPC$TL_forumTopic2.unread_reactions_count);
                        executeFast.bindInteger(10, tLRPC$TL_forumTopic2.read_outbox_max_id);
                        if (tLRPC$TL_forumTopic2.isShort) {
                            if (hashMap.containsKey(Integer.valueOf(i5))) {
                                executeFast.bindInteger(11, ((Integer) hashMap.get(Integer.valueOf(i5))).intValue());
                                executeFast.bindInteger(12, tLRPC$TL_forumTopic2.totalMessagesCount);
                                executeFast.bindInteger(13, !tLRPC$TL_forumTopic2.hidden ? 1 : 0);
                                executeFast.step();
                                nativeByteBuffer2.reuse();
                                nativeByteBuffer.reuse();
                                if (!contains) {
                                    int i6 = tLRPC$TL_forumTopic2.top_message;
                                    sQLitePreparedStatement = executeFast;
                                    i2 = i5;
                                    try {
                                        closeHolesInTable("messages_holes_topics", j, i6, i6, tLRPC$TL_forumTopic2.id);
                                        int i7 = tLRPC$TL_forumTopic2.top_message;
                                        closeHolesInMedia(j, i7, i7, -1, 0L);
                                        i3 = 2;
                                    } catch (Exception e2) {
                                        e = e2;
                                        sQLitePreparedStatement2 = sQLitePreparedStatement;
                                        checkSQLException(e);
                                        if (sQLitePreparedStatement2 != null) {
                                            sQLitePreparedStatement2.dispose();
                                        }
                                        this.database.commitTransaction();
                                    } catch (Throwable th2) {
                                        th = th2;
                                        sQLitePreparedStatement2 = sQLitePreparedStatement;
                                        if (sQLitePreparedStatement2 != null) {
                                            sQLitePreparedStatement2.dispose();
                                        }
                                        this.database.commitTransaction();
                                        throw th;
                                    }
                                } else {
                                    sQLitePreparedStatement = executeFast;
                                    i2 = i5;
                                    SQLiteDatabase sQLiteDatabase = this.database;
                                    Locale locale = Locale.ENGLISH;
                                    sQLiteDatabase.executeFast(String.format(locale, "DELETE FROM messages_holes_topics WHERE uid = %d AND topic_id = %d", Long.valueOf(j), Integer.valueOf(tLRPC$TL_forumTopic2.id))).stepThis().dispose();
                                    this.database.executeFast(String.format(locale, "DELETE FROM media_holes_topics WHERE uid = %d AND topic_id = %d", Long.valueOf(j), Integer.valueOf(tLRPC$TL_forumTopic2.id))).stepThis().dispose();
                                    this.database.executeFast(String.format(locale, "DELETE FROM messages_topics WHERE uid = %d AND topic_id = %d", Long.valueOf(j), Integer.valueOf(tLRPC$TL_forumTopic2.id))).stepThis().dispose();
                                    i3 = 2;
                                    this.database.executeFast(String.format(locale, "DELETE FROM media_topics WHERE uid = %d AND topic_id = %d", Long.valueOf(j), Integer.valueOf(tLRPC$TL_forumTopic2.id))).stepThis().dispose();
                                    SQLitePreparedStatement executeFast2 = this.database.executeFast("REPLACE INTO messages_holes_topics VALUES(?, ?, ?, ?)");
                                    createFirstHoles(j, executeFast2, this.database.executeFast("REPLACE INTO media_holes_topics VALUES(?, ?, ?, ?, ?)"), tLRPC$TL_forumTopic2.top_message, tLRPC$TL_forumTopic2.id);
                                    executeFast2.dispose();
                                    executeFast2.dispose();
                                }
                                i = i3;
                                executeFast = sQLitePreparedStatement;
                                z3 = false;
                                i5 = i2 + 1;
                            }
                        }
                        executeFast.bindInteger(11, tLRPC$TL_forumTopic2.pinned ? tLRPC$TL_forumTopic2.pinnedOrder + 1 : 0);
                        executeFast.bindInteger(12, tLRPC$TL_forumTopic2.totalMessagesCount);
                        executeFast.bindInteger(13, !tLRPC$TL_forumTopic2.hidden ? 1 : 0);
                        executeFast.step();
                        nativeByteBuffer2.reuse();
                        nativeByteBuffer.reuse();
                        if (!contains) {
                        }
                        i = i3;
                        executeFast = sQLitePreparedStatement;
                        z3 = false;
                        i5 = i2 + 1;
                    } catch (Exception e3) {
                        e = e3;
                        sQLitePreparedStatement = executeFast;
                    } catch (Throwable th3) {
                        th = th3;
                        sQLitePreparedStatement = executeFast;
                    }
                }
                boolean z4 = z3;
                sQLitePreparedStatement = executeFast;
                resetAllUnreadCounters(z4);
                if (sQLitePreparedStatement != null) {
                    sQLitePreparedStatement.dispose();
                }
            } catch (Exception e4) {
                e = e4;
            }
            this.database.commitTransaction();
        } catch (Throwable th4) {
            th = th4;
        }
    }

    public void updateTopicData(final long j, final TLRPC$TL_forumTopic tLRPC$TL_forumTopic, final int i) {
        if (tLRPC$TL_forumTopic == null) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateTopicData$44(i, tLRPC$TL_forumTopic, j);
            }
        });
    }

    public void lambda$updateTopicData$44(int i, TLRPC$TL_forumTopic tLRPC$TL_forumTopic, long j) {
        SQLiteCursor sQLiteCursor;
        SQLitePreparedStatement executeFast;
        TLRPC$TL_forumTopic tLRPC$TL_forumTopic2;
        NativeByteBuffer byteBufferValue;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        int i2 = 1;
        if ((i & 16) != 0) {
            try {
                executeFast = this.database.executeFast("UPDATE topics SET total_messages_count = ? WHERE did = ? AND topic_id = ?");
            } catch (Exception e) {
                e = e;
                sQLiteCursor = null;
                try {
                    checkSQLException(e);
                    if (sQLitePreparedStatement != null) {
                    }
                    if (sQLiteCursor != null) {
                    }
                    this.database.commitTransaction();
                } catch (Throwable th) {
                    th = th;
                    if (sQLitePreparedStatement != null) {
                        sQLitePreparedStatement.dispose();
                    }
                    if (sQLiteCursor != null) {
                        sQLiteCursor.dispose();
                    }
                    this.database.commitTransaction();
                    throw th;
                }
            } catch (Throwable th2) {
                th = th2;
                sQLiteCursor = null;
                if (sQLitePreparedStatement != null) {
                }
                if (sQLiteCursor != null) {
                }
                this.database.commitTransaction();
                throw th;
            }
            try {
                executeFast.requery();
                executeFast.bindInteger(1, tLRPC$TL_forumTopic.totalMessagesCount);
                executeFast.bindLong(2, j);
                executeFast.bindInteger(3, tLRPC$TL_forumTopic.id);
                executeFast.step();
                executeFast.dispose();
                if (i == 16) {
                    executeFast.dispose();
                    this.database.commitTransaction();
                    return;
                }
            } catch (Exception e2) {
                e = e2;
                sQLiteCursor = null;
                sQLitePreparedStatement = executeFast;
                checkSQLException(e);
                if (sQLitePreparedStatement != null) {
                }
                if (sQLiteCursor != null) {
                }
                this.database.commitTransaction();
            } catch (Throwable th3) {
                th = th3;
                sQLiteCursor = null;
                sQLitePreparedStatement = executeFast;
                if (sQLitePreparedStatement != null) {
                }
                if (sQLiteCursor != null) {
                }
                this.database.commitTransaction();
                throw th;
            }
        } else {
            executeFast = null;
        }
        sQLiteCursor = this.database.queryFinalized(String.format(Locale.US, "SELECT data FROM topics WHERE did = %d AND topic_id = %d", Long.valueOf(j), Integer.valueOf(tLRPC$TL_forumTopic.id)), new Object[0]);
        try {
            if (!sQLiteCursor.next() || (byteBufferValue = sQLiteCursor.byteBufferValue(0)) == null) {
                tLRPC$TL_forumTopic2 = null;
            } else {
                tLRPC$TL_forumTopic2 = TLRPC$TL_forumTopic.TLdeserialize((AbstractSerializedData) byteBufferValue, byteBufferValue.readInt32(true), true);
                byteBufferValue.reuse();
            }
            sQLiteCursor.dispose();
            if (tLRPC$TL_forumTopic2 != null) {
                if ((i & 1) != 0) {
                    tLRPC$TL_forumTopic2.title = tLRPC$TL_forumTopic.title;
                }
                if ((i & 2) != 0) {
                    tLRPC$TL_forumTopic2.icon_emoji_id = tLRPC$TL_forumTopic.icon_emoji_id;
                    tLRPC$TL_forumTopic2.flags |= 1;
                }
                if ((i & 4) != 0) {
                    tLRPC$TL_forumTopic2.pinned = tLRPC$TL_forumTopic.pinned;
                    tLRPC$TL_forumTopic2.pinnedOrder = tLRPC$TL_forumTopic.pinnedOrder;
                }
                int i3 = tLRPC$TL_forumTopic2.pinned ? tLRPC$TL_forumTopic2.pinnedOrder + 1 : 0;
                if ((i & 8) != 0) {
                    tLRPC$TL_forumTopic2.closed = tLRPC$TL_forumTopic.closed;
                }
                if ((i & 32) != 0) {
                    tLRPC$TL_forumTopic2.hidden = tLRPC$TL_forumTopic.hidden;
                }
                executeFast = this.database.executeFast("UPDATE topics SET data = ?, pinned = ?, hidden = ? WHERE did = ? AND topic_id = ?");
                this.database.beginTransaction();
                NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$TL_forumTopic2.getObjectSize());
                tLRPC$TL_forumTopic2.serializeToStream(nativeByteBuffer);
                executeFast.bindByteBuffer(1, nativeByteBuffer);
                executeFast.bindInteger(2, i3);
                if (!tLRPC$TL_forumTopic2.hidden) {
                    i2 = 0;
                }
                executeFast.bindInteger(3, i2);
                executeFast.bindLong(4, j);
                executeFast.bindInteger(5, tLRPC$TL_forumTopic2.id);
                executeFast.step();
                nativeByteBuffer.reuse();
            }
            if (executeFast != null) {
                executeFast.dispose();
            }
        } catch (Exception e3) {
            e = e3;
            sQLitePreparedStatement = executeFast;
            checkSQLException(e);
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            this.database.commitTransaction();
        } catch (Throwable th4) {
            th = th4;
            sQLitePreparedStatement = executeFast;
            if (sQLitePreparedStatement != null) {
            }
            if (sQLiteCursor != null) {
            }
            this.database.commitTransaction();
            throw th;
        }
        this.database.commitTransaction();
    }

    public void loadTopics(final long j, final Consumer<ArrayList<TLRPC$TL_forumTopic>> consumer) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$loadTopics$46(j, consumer);
            }
        });
    }

    public void lambda$loadTopics$46(long j, Consumer consumer) {
        SQLiteCursor sQLiteCursor;
        ArrayList<TLRPC$TL_forumTopic> arrayList;
        SQLiteCursor sQLiteCursor2;
        long j2;
        LongSparseArray<ArrayList<Integer>> longSparseArray;
        String str;
        NativeByteBuffer byteBufferValue;
        ArrayList<TLRPC$TL_forumTopic> arrayList2;
        ArrayList arrayList3;
        String str2 = ")";
        try {
            int i = 1;
            SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT top_message, data, topic_message, unread_count, max_read_id, unread_mentions, unread_reactions, read_outbox, pinned, total_messages_count FROM topics WHERE did = %d ORDER BY pinned ASC", Long.valueOf(j)), new Object[0]);
            SparseArray sparseArray = null;
            HashSet hashSet = null;
            arrayList = null;
            while (queryFinalized.next()) {
                try {
                    if (arrayList == null) {
                        try {
                            try {
                                arrayList2 = new ArrayList<>();
                            } catch (Exception e) {
                                e = e;
                            }
                            try {
                                SparseArray sparseArray2 = new SparseArray();
                                hashSet = new HashSet();
                                arrayList = arrayList2;
                                sparseArray = sparseArray2;
                            } catch (Exception e2) {
                                e = e2;
                                arrayList = arrayList2;
                                sQLiteCursor = queryFinalized;
                                try {
                                    checkSQLException(e);
                                    if (sQLiteCursor != null) {
                                    }
                                    consumer.accept(arrayList);
                                } catch (Throwable th) {
                                    th = th;
                                    if (sQLiteCursor != null) {
                                    }
                                    throw th;
                                }
                            }
                        } catch (Throwable th2) {
                            th = th2;
                            sQLiteCursor = queryFinalized;
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                            }
                            throw th;
                        }
                    }
                    int intValue = queryFinalized.intValue(0);
                    NativeByteBuffer byteBufferValue2 = queryFinalized.byteBufferValue(i);
                    if (byteBufferValue2 != null) {
                        TLRPC$TL_forumTopic TLdeserialize = TLRPC$TL_forumTopic.TLdeserialize((AbstractSerializedData) byteBufferValue2, byteBufferValue2.readInt32(false), false);
                        if (TLdeserialize != null) {
                            TLdeserialize.top_message = intValue;
                            ArrayList arrayList4 = (ArrayList) sparseArray.get(intValue);
                            if (arrayList4 == null) {
                                arrayList3 = new ArrayList();
                                sparseArray.put(intValue, arrayList3);
                            } else {
                                arrayList3 = arrayList4;
                            }
                            arrayList3.add(TLdeserialize);
                            hashSet.add(Integer.valueOf(intValue));
                            arrayList.add(TLdeserialize);
                            NativeByteBuffer byteBufferValue3 = queryFinalized.byteBufferValue(2);
                            TLdeserialize.topicStartMessage = TLRPC$Message.TLdeserialize(byteBufferValue3, byteBufferValue3.readInt32(false), false);
                            byteBufferValue3.reuse();
                            TLdeserialize.unread_count = queryFinalized.intValue(3);
                            TLdeserialize.read_inbox_max_id = queryFinalized.intValue(4);
                            TLdeserialize.unread_mentions_count = queryFinalized.intValue(5);
                            TLdeserialize.unread_reactions_count = queryFinalized.intValue(6);
                            TLdeserialize.read_outbox_max_id = queryFinalized.intValue(7);
                            int intValue2 = queryFinalized.intValue(8) - 1;
                            TLdeserialize.pinnedOrder = intValue2;
                            TLdeserialize.pinned = intValue2 >= 0;
                            TLdeserialize.totalMessagesCount = queryFinalized.intValue(9);
                        }
                        byteBufferValue2.reuse();
                    }
                    i = 1;
                } catch (Exception e3) {
                    e = e3;
                    sQLiteCursor2 = queryFinalized;
                } catch (Throwable th3) {
                    th = th3;
                    sQLiteCursor2 = queryFinalized;
                }
            }
            ArrayList<Long> arrayList5 = new ArrayList<>();
            ArrayList<Long> arrayList6 = new ArrayList<>();
            LongSparseArray<SparseArray<ArrayList<TLRPC$Message>>> longSparseArray2 = new LongSparseArray<>();
            LongSparseArray<ArrayList<Integer>> longSparseArray3 = new LongSparseArray<>();
            if (arrayList == null || arrayList.isEmpty()) {
                sQLiteCursor2 = queryFinalized;
            } else {
                SQLiteDatabase sQLiteDatabase = this.database;
                StringBuilder sb = new StringBuilder();
                sQLiteCursor2 = queryFinalized;
                try {
                    try {
                        sb.append("SELECT mid, data, replydata FROM messages_v2 WHERE uid = ");
                        sb.append(j);
                        sb.append(" AND mid IN (");
                        sb.append(TextUtils.join(",", hashSet));
                        sb.append(")");
                        String sb2 = sb.toString();
                        ArrayList<TLRPC$TL_forumTopic> arrayList7 = arrayList;
                        int i2 = 0;
                        try {
                            SQLiteCursor queryFinalized2 = sQLiteDatabase.queryFinalized(sb2, new Object[0]);
                            while (queryFinalized2.next()) {
                                try {
                                    int intValue3 = queryFinalized2.intValue(i2);
                                    NativeByteBuffer byteBufferValue4 = queryFinalized2.byteBufferValue(1);
                                    if (byteBufferValue4 != null) {
                                        str = str2;
                                        TLRPC$Message TLdeserialize2 = TLRPC$Message.TLdeserialize(byteBufferValue4, byteBufferValue4.readInt32(false), false);
                                        if (TLdeserialize2 != null) {
                                            TLdeserialize2.readAttachPath(byteBufferValue4, UserConfig.getInstance(this.currentAccount).clientUserId);
                                        }
                                        byteBufferValue4.reuse();
                                        hashSet.remove(Integer.valueOf(intValue3));
                                        ArrayList arrayList8 = (ArrayList) sparseArray.get(intValue3);
                                        if (arrayList8 != null) {
                                            for (int i3 = 0; i3 < arrayList8.size(); i3++) {
                                                ((TLRPC$TL_forumTopic) arrayList8.get(i3)).topMessage = TLdeserialize2;
                                            }
                                        }
                                        addUsersAndChatsFromMessage(TLdeserialize2, arrayList5, arrayList6, null);
                                        if (TLdeserialize2 != null) {
                                            try {
                                                TLRPC$MessageReplyHeader tLRPC$MessageReplyHeader = TLdeserialize2.reply_to;
                                                if (tLRPC$MessageReplyHeader != null && tLRPC$MessageReplyHeader.reply_to_msg_id != 0) {
                                                    TLRPC$MessageAction tLRPC$MessageAction = TLdeserialize2.action;
                                                    if ((tLRPC$MessageAction instanceof TLRPC$TL_messageActionPinMessage) || (tLRPC$MessageAction instanceof TLRPC$TL_messageActionPaymentSent) || (tLRPC$MessageAction instanceof TLRPC$TL_messageActionGameScore)) {
                                                        if (!queryFinalized2.isNull(2) && (byteBufferValue = queryFinalized2.byteBufferValue(2)) != null) {
                                                            TLRPC$Message TLdeserialize3 = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                                                            TLdeserialize2.replyMessage = TLdeserialize3;
                                                            TLdeserialize3.readAttachPath(byteBufferValue, getUserConfig().clientUserId);
                                                            byteBufferValue.reuse();
                                                            TLRPC$Message tLRPC$Message = TLdeserialize2.replyMessage;
                                                            if (tLRPC$Message != null) {
                                                                addUsersAndChatsFromMessage(tLRPC$Message, arrayList5, arrayList6, null);
                                                            }
                                                        }
                                                        if (TLdeserialize2.replyMessage == null) {
                                                            addReplyMessages(TLdeserialize2, longSparseArray2, longSparseArray3);
                                                        }
                                                    }
                                                }
                                            } catch (Exception e4) {
                                                checkSQLException(e4);
                                            }
                                        }
                                    } else {
                                        str = str2;
                                    }
                                    str2 = str;
                                    i2 = 0;
                                } catch (Exception e5) {
                                    e = e5;
                                    sQLiteCursor = sQLiteCursor2;
                                    arrayList = arrayList7;
                                    checkSQLException(e);
                                    if (sQLiteCursor != null) {
                                    }
                                    consumer.accept(arrayList);
                                }
                            }
                            String str3 = str2;
                            queryFinalized2.dispose();
                            if (hashSet.isEmpty()) {
                                j2 = j;
                            } else {
                                SQLiteDatabase sQLiteDatabase2 = this.database;
                                StringBuilder sb3 = new StringBuilder();
                                sb3.append("SELECT mid, data FROM messages_topics WHERE uid = ");
                                j2 = j;
                                sb3.append(j2);
                                sb3.append(" AND mid IN (");
                                sb3.append(TextUtils.join(",", hashSet));
                                sb3.append(str3);
                                ?? r13 = 0;
                                SQLiteCursor queryFinalized3 = sQLiteDatabase2.queryFinalized(sb3.toString(), new Object[0]);
                                while (queryFinalized3.next()) {
                                    try {
                                        int intValue4 = queryFinalized3.intValue(r13);
                                        NativeByteBuffer byteBufferValue5 = queryFinalized3.byteBufferValue(1);
                                        if (byteBufferValue5 != 0) {
                                            TLRPC$Message TLdeserialize4 = TLRPC$Message.TLdeserialize(byteBufferValue5, byteBufferValue5.readInt32(r13), r13);
                                            if (TLdeserialize4 != null) {
                                                longSparseArray = longSparseArray3;
                                                try {
                                                    TLdeserialize4.readAttachPath(byteBufferValue5, UserConfig.getInstance(this.currentAccount).clientUserId);
                                                } catch (Exception e6) {
                                                    e = e6;
                                                    checkSQLException(e);
                                                    loadReplyMessages(longSparseArray2, longSparseArray, arrayList5, arrayList6, false);
                                                    final ArrayList<TLRPC$Chat> arrayList9 = new ArrayList<>();
                                                    final ArrayList<TLRPC$User> arrayList10 = new ArrayList<>();
                                                    if (!arrayList6.isEmpty()) {
                                                    }
                                                    if (!arrayList5.isEmpty()) {
                                                    }
                                                    AndroidUtilities.runOnUIThread(new Runnable() {
                                                        @Override
                                                        public final void run() {
                                                            MessagesStorage.this.lambda$loadTopics$45(arrayList10, arrayList9);
                                                        }
                                                    });
                                                    arrayList = arrayList7;
                                                    loadGroupedMessagesForTopics(j2, arrayList);
                                                    sQLiteCursor2.dispose();
                                                    consumer.accept(arrayList);
                                                }
                                            } else {
                                                longSparseArray = longSparseArray3;
                                            }
                                            byteBufferValue5.reuse();
                                            hashSet.remove(Integer.valueOf(intValue4));
                                            addUsersAndChatsFromMessage(TLdeserialize4, arrayList5, arrayList6, null);
                                            ArrayList arrayList11 = (ArrayList) sparseArray.get(intValue4);
                                            if (arrayList11 != null) {
                                                for (int i4 = 0; i4 < arrayList11.size(); i4++) {
                                                    ((TLRPC$TL_forumTopic) arrayList11.get(i4)).topMessage = TLdeserialize4;
                                                }
                                            }
                                        } else {
                                            longSparseArray = longSparseArray3;
                                        }
                                        longSparseArray3 = longSparseArray;
                                        r13 = 0;
                                    } catch (Exception e7) {
                                        e = e7;
                                        longSparseArray = longSparseArray3;
                                    }
                                }
                            }
                            longSparseArray = longSparseArray3;
                            loadReplyMessages(longSparseArray2, longSparseArray, arrayList5, arrayList6, false);
                            final ArrayList arrayList92 = new ArrayList<>();
                            final ArrayList arrayList102 = new ArrayList<>();
                            if (!arrayList6.isEmpty()) {
                                getChatsInternal(TextUtils.join(",", arrayList6), arrayList92);
                            }
                            if (!arrayList5.isEmpty()) {
                                getUsersInternal(TextUtils.join(",", arrayList5), arrayList102);
                            }
                            AndroidUtilities.runOnUIThread(new Runnable() {
                                @Override
                                public final void run() {
                                    MessagesStorage.this.lambda$loadTopics$45(arrayList102, arrayList92);
                                }
                            });
                            arrayList = arrayList7;
                            loadGroupedMessagesForTopics(j2, arrayList);
                        } catch (Exception e8) {
                            e = e8;
                            arrayList = arrayList7;
                            sQLiteCursor = sQLiteCursor2;
                            checkSQLException(e);
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                            }
                            consumer.accept(arrayList);
                        }
                    } catch (Exception e9) {
                        e = e9;
                    }
                } catch (Throwable th4) {
                    th = th4;
                    sQLiteCursor = sQLiteCursor2;
                    if (sQLiteCursor != null) {
                    }
                    throw th;
                }
            }
            sQLiteCursor2.dispose();
        } catch (Exception e10) {
            e = e10;
            sQLiteCursor = null;
            arrayList = null;
        } catch (Throwable th5) {
            th = th5;
            sQLiteCursor = null;
        }
        consumer.accept(arrayList);
    }

    public void lambda$loadTopics$45(ArrayList arrayList, ArrayList arrayList2) {
        if (!arrayList.isEmpty()) {
            getMessagesController().putUsers(arrayList, true);
        }
        if (arrayList2.isEmpty()) {
            return;
        }
        getMessagesController().putChats(arrayList2, true);
    }

    public void loadGroupedMessagesForTopicUpdates(ArrayList<TopicsController.TopicUpdate> arrayList) {
        if (arrayList == null) {
            return;
        }
        try {
            LongSparseArray longSparseArray = new LongSparseArray();
            for (int i = 0; i < arrayList.size(); i++) {
                if (!arrayList.get(i).reloadTopic && !arrayList.get(i).onlyCounters && arrayList.get(i).topMessage != null) {
                    long j = arrayList.get(i).topMessage.grouped_id;
                    if (j != 0) {
                        ArrayList arrayList2 = (ArrayList) longSparseArray.get(j);
                        if (arrayList2 == null) {
                            arrayList2 = new ArrayList();
                            longSparseArray.put(j, arrayList2);
                        }
                        arrayList2.add(arrayList.get(i));
                    }
                }
            }
            for (int i2 = 0; i2 < longSparseArray.size(); i2++) {
                long keyAt = longSparseArray.keyAt(i2);
                ArrayList arrayList3 = (ArrayList) longSparseArray.valueAt(i2);
                SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT data FROM messages_v2 WHERE uid = %s AND group_id = %s ORDER BY date DESC", Long.valueOf(((TopicsController.TopicUpdate) arrayList3.get(0)).dialogId), Long.valueOf(keyAt)), new Object[0]);
                ArrayList<MessageObject> arrayList4 = null;
                while (queryFinalized.next()) {
                    NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(0);
                    TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                    if (TLdeserialize != null) {
                        TLdeserialize.readAttachPath(byteBufferValue, UserConfig.getInstance(this.currentAccount).clientUserId);
                    }
                    if (arrayList4 == null) {
                        arrayList4 = new ArrayList<>();
                    }
                    arrayList4.add(new MessageObject(this.currentAccount, TLdeserialize, false, false));
                }
                queryFinalized.dispose();
                for (int i3 = 0; i3 < arrayList3.size(); i3++) {
                    ((TopicsController.TopicUpdate) arrayList3.get(i3)).groupedMessages = arrayList4;
                }
            }
        } catch (Throwable th) {
            checkSQLException(th);
        }
    }

    public void loadGroupedMessagesForTopics(long j, ArrayList<TLRPC$TL_forumTopic> arrayList) {
        if (arrayList == null) {
            return;
        }
        try {
            LongSparseArray longSparseArray = new LongSparseArray();
            for (int i = 0; i < arrayList.size(); i++) {
                if (arrayList.get(i).topMessage != null) {
                    long j2 = arrayList.get(i).topMessage.grouped_id;
                    if (j2 != 0) {
                        ArrayList arrayList2 = (ArrayList) longSparseArray.get(j2);
                        if (arrayList2 == null) {
                            arrayList2 = new ArrayList();
                            longSparseArray.put(j2, arrayList2);
                        }
                        arrayList2.add(arrayList.get(i));
                    }
                }
            }
            for (int i2 = 0; i2 < longSparseArray.size(); i2++) {
                long keyAt = longSparseArray.keyAt(i2);
                ArrayList arrayList3 = (ArrayList) longSparseArray.valueAt(i2);
                SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT data FROM messages_v2 WHERE uid = %s AND group_id = %s ORDER BY date DESC", Long.valueOf(j), Long.valueOf(keyAt)), new Object[0]);
                ArrayList<MessageObject> arrayList4 = null;
                while (queryFinalized.next()) {
                    NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(0);
                    TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                    if (TLdeserialize != null) {
                        TLdeserialize.readAttachPath(byteBufferValue, UserConfig.getInstance(this.currentAccount).clientUserId);
                    }
                    if (arrayList4 == null) {
                        arrayList4 = new ArrayList<>();
                    }
                    arrayList4.add(new MessageObject(this.currentAccount, TLdeserialize, false, false));
                }
                queryFinalized.dispose();
                for (int i3 = 0; i3 < arrayList3.size(); i3++) {
                    ((TLRPC$TL_forumTopic) arrayList3.get(i3)).groupedMessages = arrayList4;
                }
            }
        } catch (Throwable th) {
            checkSQLException(th);
        }
    }

    public void getSavedDialogMaxMessageId(final long j, final IntCallback intCallback) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getSavedDialogMaxMessageId$48(j, intCallback);
            }
        });
    }

    public void lambda$getSavedDialogMaxMessageId$48(long j, final IntCallback intCallback) {
        final int[] iArr = new int[1];
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                sQLiteCursor = this.database.queryFinalized("SELECT MAX(mid) FROM messages_topics WHERE uid = ? AND topic_id = ?", Long.valueOf(getUserConfig().getClientUserId()), Long.valueOf(j));
                if (sQLiteCursor.next()) {
                    iArr[0] = sQLiteCursor.intValue(0);
                }
            } catch (Exception e) {
                checkSQLException(e);
            }
            sQLiteCursor.dispose();
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.lambda$getSavedDialogMaxMessageId$47(MessagesStorage.IntCallback.this, iArr);
                }
            });
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public static void lambda$getSavedDialogMaxMessageId$47(IntCallback intCallback, int[] iArr) {
        intCallback.run(iArr[0]);
    }

    public void deleteSavedDialog(final long j) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$deleteSavedDialog$50(j);
            }
        });
    }

    public void lambda$deleteSavedDialog$50(long j) {
        long clientUserId;
        SQLiteCursor queryFinalized;
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                clientUserId = getUserConfig().getClientUserId();
                queryFinalized = this.database.queryFinalized("SELECT mid FROM messages_topics WHERE uid = ? AND topic_id = ?", Long.valueOf(clientUserId), Long.valueOf(j));
            } catch (Exception e) {
                e = e;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            final ArrayList arrayList = new ArrayList();
            while (queryFinalized.next()) {
                arrayList.add(Integer.valueOf(queryFinalized.intValue(0)));
            }
            queryFinalized.dispose();
            if (!arrayList.isEmpty()) {
                this.database.executeFast("DELETE FROM messages_v2 WHERE uid = " + clientUserId + " AND mid IN (" + TextUtils.join(",", arrayList) + ")").stepThis().dispose();
                this.database.executeFast("DELETE FROM messages_topics WHERE uid = " + clientUserId + " AND topic_id = " + j).stepThis().dispose();
                this.database.executeFast("DELETE FROM media_v4 WHERE uid = " + clientUserId + " AND mid IN (" + TextUtils.join(",", arrayList) + ")").stepThis().dispose();
            }
            if (arrayList.isEmpty()) {
                return;
            }
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$deleteSavedDialog$49(arrayList);
                }
            });
        } catch (Exception e2) {
            e = e2;
            sQLiteCursor = queryFinalized;
            checkSQLException(e);
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
        } catch (Throwable th2) {
            th = th2;
            sQLiteCursor = queryFinalized;
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public void lambda$deleteSavedDialog$49(ArrayList arrayList) {
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.messagesDeleted, arrayList, 0L, Boolean.FALSE);
    }

    public void removeTopic(final long j, final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$removeTopic$51(j, i);
            }
        });
    }

    public void lambda$removeTopic$51(long j, int i) {
        try {
            SQLiteDatabase sQLiteDatabase = this.database;
            Locale locale = Locale.US;
            sQLiteDatabase.executeFast(String.format(locale, "DELETE FROM topics WHERE did = %d AND topic_id = %d", Long.valueOf(j), Integer.valueOf(i))).stepThis().dispose();
            this.database.executeFast(String.format(locale, "DELETE FROM messages_topics WHERE uid = %d AND topic_id = %d", Long.valueOf(j), Integer.valueOf(i))).stepThis().dispose();
        } catch (SQLiteException e) {
            e.printStackTrace();
        }
    }

    public void removeTopics(final long j, final ArrayList<Integer> arrayList) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$removeTopics$52(arrayList, j);
            }
        });
    }

    public void lambda$removeTopics$52(ArrayList arrayList, long j) {
        try {
            String join = TextUtils.join(", ", arrayList);
            SQLiteDatabase sQLiteDatabase = this.database;
            Locale locale = Locale.US;
            sQLiteDatabase.executeFast(String.format(locale, "DELETE FROM topics WHERE did = %d AND topic_id IN (%s)", Long.valueOf(j), join)).stepThis().dispose();
            this.database.executeFast(String.format(locale, "DELETE FROM messages_topics WHERE uid = %d AND topic_id IN (%s)", Long.valueOf(j), join)).stepThis().dispose();
        } catch (SQLiteException e) {
            e.printStackTrace();
        }
    }

    public void updateTopicsWithReadMessages(final HashMap<TopicKey, Integer> hashMap) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateTopicsWithReadMessages$53(hashMap);
            }
        });
    }

    public void lambda$updateTopicsWithReadMessages$53(HashMap hashMap) {
        for (TopicKey topicKey : hashMap.keySet()) {
            try {
                this.database.executeFast(String.format(Locale.US, "UPDATE topics SET read_outbox = max((SELECT read_outbox FROM topics WHERE did = %d AND topic_id = %d), %d) WHERE did = %d AND topic_id = %d", Long.valueOf(topicKey.dialogId), Long.valueOf(topicKey.topicId), Integer.valueOf(((Integer) hashMap.get(topicKey)).intValue()), Long.valueOf(topicKey.dialogId), Long.valueOf(topicKey.topicId))).stepThis().dispose();
            } catch (SQLiteException e) {
                checkSQLException(e);
            }
        }
    }

    public void setDialogTtl(final long j, final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$setDialogTtl$54(i, j);
            }
        });
    }

    public void lambda$setDialogTtl$54(int i, long j) {
        try {
            this.database.executeFast(String.format(Locale.US, "UPDATE dialogs SET ttl_period = %d WHERE did = %d", Integer.valueOf(i), Long.valueOf(j))).stepThis().dispose();
        } catch (SQLiteException e) {
            checkSQLException(e);
        }
    }

    public ArrayList<File> getDatabaseFiles() {
        ArrayList<File> arrayList = new ArrayList<>();
        arrayList.add(this.cacheFile);
        arrayList.add(this.walCacheFile);
        arrayList.add(this.shmCacheFile);
        return arrayList;
    }

    public void reset() {
        clearDatabaseValues();
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$reset$55();
            }
        });
    }

    public void lambda$reset$55() {
        for (int i = 0; i < 2; i++) {
            getUserConfig().setDialogsLoadOffset(i, 0, 0, 0L, 0L, 0L, 0L);
            getUserConfig().setTotalDialogsCount(i, 0);
        }
        getUserConfig().clearFilters();
        getUserConfig().clearPinnedDialogsLoaded();
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.didClearDatabase, new Object[0]);
        getMediaDataController().loadAttachMenuBots(false, true);
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.onDatabaseReset, new Object[0]);
        getMessagesController().getStoriesController().cleanup();
    }

    public void fullReset() {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$fullReset$57();
            }
        });
    }

    public void lambda$fullReset$57() {
        cleanupInternal(true);
        clearLoadingDialogsOffsets();
        openDatabase(1);
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$fullReset$56();
            }
        });
    }

    public void lambda$fullReset$56() {
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.onDatabaseReset, new Object[0]);
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.didClearDatabase, new Object[0]);
        getMessagesController().getSavedMessagesController().cleanup();
    }

    public static class ReadDialog {
        public int date;
        public int lastMid;
        public int unreadCount;

        private ReadDialog() {
        }
    }

    public void readAllDialogs(final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$readAllDialogs$59(i);
            }
        });
    }

    public void lambda$readAllDialogs$59(int i) {
        SQLiteCursor queryFinalized;
        SQLiteCursor sQLiteCursor = 0;
        SQLiteCursor sQLiteCursor2 = null;
        try {
            try {
                ArrayList<Long> arrayList = new ArrayList<>();
                ArrayList arrayList2 = new ArrayList();
                ArrayList arrayList3 = new ArrayList();
                final LongSparseArray longSparseArray = new LongSparseArray();
                if (i >= 0) {
                    queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT did, last_mid, unread_count, date FROM dialogs WHERE unread_count > 0 AND folder_id = %1$d", Integer.valueOf(i)), new Object[0]);
                } else {
                    queryFinalized = this.database.queryFinalized("SELECT did, last_mid, unread_count, date FROM dialogs WHERE unread_count > 0", new Object[0]);
                }
                while (queryFinalized.next()) {
                    try {
                        long longValue = queryFinalized.longValue(0);
                        if (!DialogObject.isFolderDialogId(longValue)) {
                            ReadDialog readDialog = new ReadDialog();
                            readDialog.lastMid = queryFinalized.intValue(1);
                            readDialog.unreadCount = queryFinalized.intValue(2);
                            readDialog.date = queryFinalized.intValue(3);
                            longSparseArray.put(longValue, readDialog);
                            if (!DialogObject.isEncryptedDialog(longValue)) {
                                if (DialogObject.isChatDialog(longValue)) {
                                    long j = -longValue;
                                    if (!arrayList2.contains(Long.valueOf(j))) {
                                        arrayList2.add(Long.valueOf(j));
                                    }
                                } else if (!arrayList.contains(Long.valueOf(longValue))) {
                                    arrayList.add(Long.valueOf(longValue));
                                }
                            } else {
                                int encryptedChatId = DialogObject.getEncryptedChatId(longValue);
                                if (!arrayList3.contains(Integer.valueOf(encryptedChatId))) {
                                    arrayList3.add(Integer.valueOf(encryptedChatId));
                                }
                            }
                        }
                    } catch (Exception e) {
                        sQLiteCursor = queryFinalized;
                        e = e;
                        checkSQLException(e);
                        if (sQLiteCursor != 0) {
                            sQLiteCursor.dispose();
                            return;
                        }
                        return;
                    } catch (Throwable th) {
                        sQLiteCursor2 = queryFinalized;
                        th = th;
                        if (sQLiteCursor2 != null) {
                            sQLiteCursor2.dispose();
                        }
                        throw th;
                    }
                }
                queryFinalized.dispose();
                final ArrayList<TLRPC$User> arrayList4 = new ArrayList<>();
                final ArrayList<TLRPC$Chat> arrayList5 = new ArrayList<>();
                final ArrayList<TLRPC$EncryptedChat> arrayList6 = new ArrayList<>();
                if (!arrayList3.isEmpty()) {
                    getEncryptedChatsInternal(TextUtils.join(",", arrayList3), arrayList6, arrayList);
                }
                if (!arrayList.isEmpty()) {
                    getUsersInternal(TextUtils.join(",", arrayList), arrayList4);
                }
                if (!arrayList2.isEmpty()) {
                    getChatsInternal(TextUtils.join(",", arrayList2), arrayList5);
                }
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$readAllDialogs$58(arrayList4, arrayList5, arrayList6, longSparseArray);
                    }
                });
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Exception e2) {
            e = e2;
        }
    }

    public void lambda$readAllDialogs$58(ArrayList arrayList, ArrayList arrayList2, ArrayList arrayList3, LongSparseArray longSparseArray) {
        getMessagesController().putUsers(arrayList, true);
        getMessagesController().putChats(arrayList2, true);
        getMessagesController().putEncryptedChats(arrayList3, true);
        for (int i = 0; i < longSparseArray.size(); i++) {
            long keyAt = longSparseArray.keyAt(i);
            ReadDialog readDialog = (ReadDialog) longSparseArray.valueAt(i);
            MessagesController messagesController = getMessagesController();
            int i2 = readDialog.lastMid;
            messagesController.markDialogAsRead(keyAt, i2, i2, readDialog.date, false, 0L, readDialog.unreadCount, true, 0);
        }
    }

    private TLRPC$messages_Dialogs loadDialogsByIds(String str, ArrayList<Long> arrayList, ArrayList<Long> arrayList2, ArrayList<Integer> arrayList3) throws Exception {
        SQLiteCursor sQLiteCursor;
        int i;
        SQLiteCursor queryFinalized;
        TLRPC$TL_messages_dialogs tLRPC$TL_messages_dialogs;
        TLRPC$Dialog tLRPC$Dialog;
        NativeByteBuffer byteBufferValue;
        NativeByteBuffer byteBufferValue2;
        long j;
        int i2;
        long longValue;
        long longValue2;
        long j2;
        NativeByteBuffer byteBufferValue3;
        TLRPC$TL_messages_dialogs tLRPC$TL_messages_dialogs2;
        TLRPC$TL_dialog tLRPC$TL_dialog;
        ArrayList<Integer> arrayList4;
        NativeByteBuffer byteBufferValue4;
        MessagesStorage messagesStorage = this;
        ArrayList<Long> arrayList5 = arrayList;
        TLRPC$TL_messages_dialogs tLRPC$TL_messages_dialogs3 = new TLRPC$TL_messages_dialogs();
        LongSparseArray longSparseArray = new LongSparseArray();
        LongSparseArray longSparseArray2 = new LongSparseArray();
        try {
            int i3 = 1;
            int i4 = 0;
            SQLiteCursor queryFinalized2 = messagesStorage.database.queryFinalized(String.format(Locale.US, "SELECT d.did, d.last_mid, d.unread_count, d.date, m.data, m.read_state, m.mid, m.send_state, s.flags, m.date, d.pts, d.inbox_max, d.outbox_max, m.replydata, d.pinned, d.unread_count_i, d.flags, d.folder_id, d.data, d.unread_reactions, d.last_mid_group, d.ttl_period FROM dialogs as d LEFT JOIN messages_v2 as m ON d.last_mid = m.mid AND d.did = m.uid LEFT JOIN dialog_settings as s ON d.did = s.did WHERE d.did IN (%s) ORDER BY d.pinned DESC, d.date DESC", str), new Object[0]);
            while (true) {
                try {
                    try {
                        i = 2;
                        if (!queryFinalized2.next()) {
                            break;
                        }
                        long longValue3 = queryFinalized2.longValue(i4);
                        TLRPC$TL_dialog tLRPC$TL_dialog2 = new TLRPC$TL_dialog();
                        tLRPC$TL_dialog2.id = longValue3;
                        tLRPC$TL_dialog2.top_message = queryFinalized2.intValue(i3);
                        tLRPC$TL_dialog2.unread_count = queryFinalized2.intValue(2);
                        tLRPC$TL_dialog2.last_message_date = queryFinalized2.intValue(3);
                        int intValue = queryFinalized2.intValue(10);
                        tLRPC$TL_dialog2.pts = intValue;
                        if (intValue != 0) {
                            j = longValue3;
                            if (!DialogObject.isUserDialog(tLRPC$TL_dialog2.id)) {
                                i2 = i3;
                                tLRPC$TL_dialog2.flags = i2;
                                tLRPC$TL_dialog2.read_inbox_max_id = queryFinalized2.intValue(11);
                                tLRPC$TL_dialog2.read_outbox_max_id = queryFinalized2.intValue(12);
                                int intValue2 = queryFinalized2.intValue(14);
                                tLRPC$TL_dialog2.pinnedNum = intValue2;
                                tLRPC$TL_dialog2.pinned = intValue2 == 0 ? i3 : 0;
                                tLRPC$TL_dialog2.unread_mentions_count = queryFinalized2.intValue(15);
                                int intValue3 = queryFinalized2.intValue(16);
                                tLRPC$TL_dialog2.unread_mark = (intValue3 & 1) == 0 ? i3 : 0;
                                tLRPC$TL_dialog2.view_forum_as_messages = (intValue3 & 64) == 0 ? i3 : 0;
                                longValue = queryFinalized2.longValue(8);
                                TLRPC$TL_peerNotifySettings tLRPC$TL_peerNotifySettings = new TLRPC$TL_peerNotifySettings();
                                tLRPC$TL_dialog2.notify_settings = tLRPC$TL_peerNotifySettings;
                                if ((((int) longValue) & i3) != 0) {
                                    int i5 = (int) (longValue >> 32);
                                    tLRPC$TL_peerNotifySettings.mute_until = i5;
                                    if (i5 == 0) {
                                        tLRPC$TL_peerNotifySettings.mute_until = ConnectionsManager.DEFAULT_DATACENTER_ID;
                                    }
                                }
                                tLRPC$TL_dialog2.folder_id = queryFinalized2.intValue(17);
                                tLRPC$TL_dialog2.unread_reactions_count = queryFinalized2.intValue(19);
                                longValue2 = queryFinalized2.longValue(20);
                                if (longValue2 == 0) {
                                    Long valueOf = Long.valueOf(longValue2);
                                    j2 = j;
                                    longSparseArray2.append(j2, valueOf);
                                } else {
                                    j2 = j;
                                }
                                tLRPC$TL_dialog2.ttl_period = queryFinalized2.intValue(21);
                                tLRPC$TL_messages_dialogs3.dialogs.add(tLRPC$TL_dialog2);
                                byteBufferValue3 = queryFinalized2.byteBufferValue(4);
                                if (byteBufferValue3 == null) {
                                    TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue3, byteBufferValue3.readInt32(false), false);
                                    if (TLdeserialize != null) {
                                        TLdeserialize.readAttachPath(byteBufferValue3, getUserConfig().clientUserId);
                                        byteBufferValue3.reuse();
                                        MessageObject.setUnreadFlags(TLdeserialize, queryFinalized2.intValue(5));
                                        TLdeserialize.id = queryFinalized2.intValue(6);
                                        int intValue4 = queryFinalized2.intValue(9);
                                        if (intValue4 != 0) {
                                            tLRPC$TL_dialog = tLRPC$TL_dialog2;
                                            tLRPC$TL_dialog.last_message_date = intValue4;
                                        } else {
                                            tLRPC$TL_dialog = tLRPC$TL_dialog2;
                                        }
                                        TLdeserialize.send_state = queryFinalized2.intValue(7);
                                        TLdeserialize.dialog_id = tLRPC$TL_dialog.id;
                                        tLRPC$TL_messages_dialogs3.messages.add(TLdeserialize);
                                        addUsersAndChatsFromMessage(TLdeserialize, arrayList5, arrayList2, null);
                                        try {
                                            TLRPC$MessageReplyHeader tLRPC$MessageReplyHeader = TLdeserialize.reply_to;
                                            if (tLRPC$MessageReplyHeader != null && tLRPC$MessageReplyHeader.reply_to_msg_id != 0) {
                                                TLRPC$MessageAction tLRPC$MessageAction = TLdeserialize.action;
                                                if ((tLRPC$MessageAction instanceof TLRPC$TL_messageActionPinMessage) || (tLRPC$MessageAction instanceof TLRPC$TL_messageActionPaymentSent) || (tLRPC$MessageAction instanceof TLRPC$TL_messageActionGameScore)) {
                                                    if (queryFinalized2.isNull(13) || (byteBufferValue4 = queryFinalized2.byteBufferValue(13)) == null) {
                                                        tLRPC$TL_messages_dialogs2 = tLRPC$TL_messages_dialogs3;
                                                    } else {
                                                        TLRPC$Message TLdeserialize2 = TLRPC$Message.TLdeserialize(byteBufferValue4, byteBufferValue4.readInt32(false), false);
                                                        TLdeserialize.replyMessage = TLdeserialize2;
                                                        tLRPC$TL_messages_dialogs2 = tLRPC$TL_messages_dialogs3;
                                                        try {
                                                            TLdeserialize2.readAttachPath(byteBufferValue4, getUserConfig().clientUserId);
                                                            byteBufferValue4.reuse();
                                                            TLRPC$Message tLRPC$Message = TLdeserialize.replyMessage;
                                                            if (tLRPC$Message != null) {
                                                                addUsersAndChatsFromMessage(tLRPC$Message, arrayList5, arrayList2, null);
                                                            }
                                                        } catch (Exception e) {
                                                            e = e;
                                                            messagesStorage.checkSQLException(e);
                                                            if (!DialogObject.isEncryptedDialog(j2)) {
                                                            }
                                                            if (DialogObject.isEncryptedDialog(j2)) {
                                                            }
                                                            tLRPC$TL_messages_dialogs3 = tLRPC$TL_messages_dialogs2;
                                                            i3 = 1;
                                                            i4 = 0;
                                                        }
                                                    }
                                                    if (TLdeserialize.replyMessage == null) {
                                                        longSparseArray.put(tLRPC$TL_dialog.id, TLdeserialize);
                                                    }
                                                }
                                            }
                                            tLRPC$TL_messages_dialogs2 = tLRPC$TL_messages_dialogs3;
                                        } catch (Exception e2) {
                                            e = e2;
                                            tLRPC$TL_messages_dialogs2 = tLRPC$TL_messages_dialogs3;
                                        }
                                    } else {
                                        tLRPC$TL_messages_dialogs2 = tLRPC$TL_messages_dialogs3;
                                        tLRPC$TL_dialog = tLRPC$TL_dialog2;
                                        byteBufferValue3.reuse();
                                    }
                                } else {
                                    tLRPC$TL_messages_dialogs2 = tLRPC$TL_messages_dialogs3;
                                    tLRPC$TL_dialog = tLRPC$TL_dialog2;
                                }
                                if (!DialogObject.isEncryptedDialog(j2) && tLRPC$TL_dialog.read_inbox_max_id > tLRPC$TL_dialog.top_message) {
                                    tLRPC$TL_dialog.read_inbox_max_id = 0;
                                }
                                if (DialogObject.isEncryptedDialog(j2)) {
                                    int encryptedChatId = DialogObject.getEncryptedChatId(j2);
                                    arrayList4 = arrayList3;
                                    if (!arrayList4.contains(Integer.valueOf(encryptedChatId))) {
                                        arrayList4.add(Integer.valueOf(encryptedChatId));
                                    }
                                } else {
                                    arrayList4 = arrayList3;
                                    if (DialogObject.isUserDialog(j2)) {
                                        if (!arrayList5.contains(Long.valueOf(j2))) {
                                            arrayList5.add(Long.valueOf(j2));
                                        }
                                    } else {
                                        long j3 = -j2;
                                        if (!arrayList2.contains(Long.valueOf(j3))) {
                                            arrayList2.add(Long.valueOf(j3));
                                        }
                                    }
                                }
                                tLRPC$TL_messages_dialogs3 = tLRPC$TL_messages_dialogs2;
                                i3 = 1;
                                i4 = 0;
                            }
                        } else {
                            j = longValue3;
                        }
                        i2 = 0;
                        tLRPC$TL_dialog2.flags = i2;
                        tLRPC$TL_dialog2.read_inbox_max_id = queryFinalized2.intValue(11);
                        tLRPC$TL_dialog2.read_outbox_max_id = queryFinalized2.intValue(12);
                        int intValue22 = queryFinalized2.intValue(14);
                        tLRPC$TL_dialog2.pinnedNum = intValue22;
                        tLRPC$TL_dialog2.pinned = intValue22 == 0 ? i3 : 0;
                        tLRPC$TL_dialog2.unread_mentions_count = queryFinalized2.intValue(15);
                        int intValue32 = queryFinalized2.intValue(16);
                        tLRPC$TL_dialog2.unread_mark = (intValue32 & 1) == 0 ? i3 : 0;
                        tLRPC$TL_dialog2.view_forum_as_messages = (intValue32 & 64) == 0 ? i3 : 0;
                        longValue = queryFinalized2.longValue(8);
                        TLRPC$TL_peerNotifySettings tLRPC$TL_peerNotifySettings2 = new TLRPC$TL_peerNotifySettings();
                        tLRPC$TL_dialog2.notify_settings = tLRPC$TL_peerNotifySettings2;
                        if ((((int) longValue) & i3) != 0) {
                        }
                        tLRPC$TL_dialog2.folder_id = queryFinalized2.intValue(17);
                        tLRPC$TL_dialog2.unread_reactions_count = queryFinalized2.intValue(19);
                        longValue2 = queryFinalized2.longValue(20);
                        if (longValue2 == 0) {
                        }
                        tLRPC$TL_dialog2.ttl_period = queryFinalized2.intValue(21);
                        tLRPC$TL_messages_dialogs3.dialogs.add(tLRPC$TL_dialog2);
                        byteBufferValue3 = queryFinalized2.byteBufferValue(4);
                        if (byteBufferValue3 == null) {
                        }
                        if (!DialogObject.isEncryptedDialog(j2)) {
                            tLRPC$TL_dialog.read_inbox_max_id = 0;
                        }
                        if (DialogObject.isEncryptedDialog(j2)) {
                        }
                        tLRPC$TL_messages_dialogs3 = tLRPC$TL_messages_dialogs2;
                        i3 = 1;
                        i4 = 0;
                    } catch (Exception e3) {
                        e = e3;
                        sQLiteCursor = queryFinalized2;
                    }
                } catch (Throwable th) {
                    th = th;
                    sQLiteCursor = queryFinalized2;
                }
            }
            TLRPC$TL_messages_dialogs tLRPC$TL_messages_dialogs4 = tLRPC$TL_messages_dialogs3;
            queryFinalized2.dispose();
            if (longSparseArray2.isEmpty()) {
                tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs4;
                queryFinalized = null;
            } else {
                try {
                    StringBuilder sb = new StringBuilder();
                    int i6 = 0;
                    while (i6 < longSparseArray2.size()) {
                        sb.append("uid = ");
                        sb.append(longSparseArray2.keyAt(i6));
                        sb.append(" AND group_id = ");
                        sb.append(longSparseArray2.valueAt(i6));
                        i6++;
                        if (i6 < longSparseArray2.size()) {
                            sb.append(" OR ");
                        }
                    }
                    int i7 = 0;
                    queryFinalized = messagesStorage.database.queryFinalized(String.format(Locale.US, "SELECT uid, data, read_state, mid, send_state, date, replydata FROM messages_v2 WHERE %s ORDER BY date DESC", sb), new Object[0]);
                    while (queryFinalized.next()) {
                        try {
                            try {
                                long longValue4 = queryFinalized.longValue(i7);
                                TLRPC$TL_messages_dialogs tLRPC$TL_messages_dialogs5 = tLRPC$TL_messages_dialogs4;
                                int i8 = 0;
                                while (true) {
                                    if (i8 >= tLRPC$TL_messages_dialogs5.dialogs.size()) {
                                        tLRPC$Dialog = null;
                                        break;
                                    }
                                    tLRPC$Dialog = tLRPC$TL_messages_dialogs5.dialogs.get(i8);
                                    if (tLRPC$Dialog != null && tLRPC$Dialog.id == longValue4) {
                                        break;
                                    }
                                    i8++;
                                }
                                if (tLRPC$Dialog != null && (byteBufferValue = queryFinalized.byteBufferValue(1)) != null) {
                                    TLRPC$Message TLdeserialize3 = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                                    if (TLdeserialize3 != null) {
                                        TLdeserialize3.readAttachPath(byteBufferValue, getUserConfig().clientUserId);
                                        byteBufferValue.reuse();
                                        MessageObject.setUnreadFlags(TLdeserialize3, queryFinalized.intValue(2));
                                        TLdeserialize3.id = queryFinalized.intValue(3);
                                        int intValue5 = queryFinalized.intValue(5);
                                        if (intValue5 != 0) {
                                            tLRPC$Dialog.last_message_date = intValue5;
                                        }
                                        TLdeserialize3.send_state = queryFinalized.intValue(4);
                                        TLdeserialize3.dialog_id = tLRPC$Dialog.id;
                                        tLRPC$TL_messages_dialogs5.messages.add(TLdeserialize3);
                                        addUsersAndChatsFromMessage(TLdeserialize3, arrayList5, arrayList2, null);
                                        try {
                                            TLRPC$MessageReplyHeader tLRPC$MessageReplyHeader2 = TLdeserialize3.reply_to;
                                            if (tLRPC$MessageReplyHeader2 != null && tLRPC$MessageReplyHeader2.reply_to_msg_id != 0) {
                                                TLRPC$MessageAction tLRPC$MessageAction2 = TLdeserialize3.action;
                                                if ((tLRPC$MessageAction2 instanceof TLRPC$TL_messageActionPinMessage) || (tLRPC$MessageAction2 instanceof TLRPC$TL_messageActionPaymentSent) || (tLRPC$MessageAction2 instanceof TLRPC$TL_messageActionGameScore)) {
                                                    if (!queryFinalized.isNull(6) && (byteBufferValue2 = queryFinalized.byteBufferValue(6)) != null) {
                                                        TLRPC$Message TLdeserialize4 = TLRPC$Message.TLdeserialize(byteBufferValue2, byteBufferValue2.readInt32(false), false);
                                                        TLdeserialize3.replyMessage = TLdeserialize4;
                                                        TLdeserialize4.readAttachPath(byteBufferValue2, getUserConfig().clientUserId);
                                                        byteBufferValue2.reuse();
                                                        TLRPC$Message tLRPC$Message2 = TLdeserialize3.replyMessage;
                                                        if (tLRPC$Message2 != null) {
                                                            addUsersAndChatsFromMessage(tLRPC$Message2, arrayList5, arrayList2, null);
                                                        }
                                                    }
                                                    if (TLdeserialize3.replyMessage == null) {
                                                        longSparseArray.put(tLRPC$Dialog.id, TLdeserialize3);
                                                    }
                                                }
                                            }
                                        } catch (Exception e4) {
                                            messagesStorage.checkSQLException(e4);
                                        }
                                    } else {
                                        byteBufferValue.reuse();
                                    }
                                }
                                tLRPC$TL_messages_dialogs4 = tLRPC$TL_messages_dialogs5;
                                i7 = 0;
                            } catch (Exception e5) {
                                e = e5;
                                sQLiteCursor = queryFinalized;
                                try {
                                    throw e;
                                } catch (Throwable th2) {
                                    th = th2;
                                    if (sQLiteCursor != null) {
                                    }
                                    throw th;
                                }
                            }
                        } catch (Throwable th3) {
                            th = th3;
                            sQLiteCursor = queryFinalized;
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                            }
                            throw th;
                        }
                    }
                    tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs4;
                    queryFinalized.dispose();
                } catch (Exception e6) {
                    e = e6;
                    sQLiteCursor = null;
                    throw e;
                } catch (Throwable th4) {
                    th = th4;
                    sQLiteCursor = null;
                    if (sQLiteCursor != null) {
                    }
                    throw th;
                }
            }
            if (!longSparseArray.isEmpty()) {
                int size = longSparseArray.size();
                int i9 = 0;
                while (i9 < size) {
                    long keyAt = longSparseArray.keyAt(i9);
                    TLRPC$Message tLRPC$Message3 = (TLRPC$Message) longSparseArray.valueAt(i9);
                    SQLiteDatabase sQLiteDatabase = messagesStorage.database;
                    Locale locale = Locale.US;
                    Object[] objArr = new Object[i];
                    ?? r1 = 0;
                    objArr[0] = Integer.valueOf(tLRPC$Message3.id);
                    objArr[1] = Long.valueOf(keyAt);
                    SQLiteCursor queryFinalized3 = sQLiteDatabase.queryFinalized(String.format(locale, "SELECT data, mid, date, uid FROM messages_v2 WHERE mid = %d and uid = %d", objArr), new Object[0]);
                    while (queryFinalized3.next()) {
                        NativeByteBuffer byteBufferValue5 = queryFinalized3.byteBufferValue(r1);
                        if (byteBufferValue5 != 0) {
                            TLRPC$Message TLdeserialize5 = TLRPC$Message.TLdeserialize(byteBufferValue5, byteBufferValue5.readInt32(r1), r1);
                            TLdeserialize5.readAttachPath(byteBufferValue5, getUserConfig().clientUserId);
                            byteBufferValue5.reuse();
                            TLdeserialize5.id = queryFinalized3.intValue(1);
                            TLdeserialize5.date = queryFinalized3.intValue(2);
                            TLdeserialize5.dialog_id = queryFinalized3.longValue(3);
                            addUsersAndChatsFromMessage(TLdeserialize5, arrayList5, arrayList2, null);
                            tLRPC$Message3.replyMessage = TLdeserialize5;
                            TLdeserialize5.dialog_id = tLRPC$Message3.dialog_id;
                        }
                        arrayList5 = arrayList;
                        r1 = 0;
                    }
                    queryFinalized3.dispose();
                    i9++;
                    messagesStorage = this;
                    arrayList5 = arrayList;
                    i = 2;
                }
            }
            if (queryFinalized != null) {
                queryFinalized.dispose();
            }
            return tLRPC$TL_messages_dialogs;
        } catch (Exception e7) {
            e = e7;
            sQLiteCursor = null;
        } catch (Throwable th5) {
            th = th5;
            sQLiteCursor = null;
        }
    }

    private void loadDialogFilters() {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$loadDialogFilters$61();
            }
        });
    }

    public void lambda$loadDialogFilters$61() {
        SQLiteCursor sQLiteCursor;
        SQLitePreparedStatement sQLitePreparedStatement;
        TLRPC$messages_Dialogs tLRPC$TL_messages_dialogs;
        SQLiteCursor queryFinalized;
        int i;
        try {
            ArrayList<Long> arrayList = new ArrayList<>();
            ArrayList<Long> arrayList2 = new ArrayList<>();
            ArrayList<Integer> arrayList3 = new ArrayList<>();
            ArrayList arrayList4 = new ArrayList();
            SparseArray sparseArray = new SparseArray();
            arrayList.add(Long.valueOf(getUserConfig().getClientUserId()));
            int i2 = 0;
            SQLiteCursor queryFinalized2 = this.database.queryFinalized("SELECT id, ord, unread_count, flags, title FROM dialog_filter WHERE 1", new Object[0]);
            boolean z = false;
            boolean z2 = false;
            while (true) {
                try {
                    int i3 = 2;
                    int i4 = 1;
                    if (!queryFinalized2.next()) {
                        break;
                    }
                    MessagesController.DialogFilter dialogFilter = new MessagesController.DialogFilter();
                    dialogFilter.id = queryFinalized2.intValue(i2);
                    dialogFilter.order = queryFinalized2.intValue(1);
                    dialogFilter.unreadCount = -1;
                    dialogFilter.pendingUnreadCount = -1;
                    dialogFilter.flags = queryFinalized2.intValue(3);
                    dialogFilter.name = queryFinalized2.stringValue(4);
                    this.dialogFilters.add(dialogFilter);
                    this.dialogFiltersMap.put(dialogFilter.id, dialogFilter);
                    sparseArray.put(dialogFilter.id, dialogFilter);
                    if (dialogFilter.pendingUnreadCount < 0) {
                        z2 = true;
                    }
                    int i5 = i2;
                    while (i5 < i3) {
                        if (i5 == 0) {
                            queryFinalized = this.database.queryFinalized("SELECT peer, pin FROM dialog_filter_pin_v2 WHERE id = " + dialogFilter.id, new Object[i2]);
                        } else {
                            queryFinalized = this.database.queryFinalized("SELECT peer FROM dialog_filter_ep WHERE id = " + dialogFilter.id, new Object[i2]);
                        }
                        while (queryFinalized.next()) {
                            long longValue = queryFinalized.longValue(i2);
                            if (i5 == 0) {
                                if (DialogObject.isEncryptedDialog(longValue)) {
                                    i = i4;
                                } else {
                                    dialogFilter.alwaysShow.add(Long.valueOf(longValue));
                                    i = 1;
                                }
                                int intValue = queryFinalized.intValue(i);
                                if (intValue != Integer.MIN_VALUE) {
                                    dialogFilter.pinnedDialogs.put(longValue, intValue);
                                    if (!arrayList4.contains(Long.valueOf(longValue))) {
                                        arrayList4.add(Long.valueOf(longValue));
                                    }
                                }
                            } else if (!DialogObject.isEncryptedDialog(longValue)) {
                                dialogFilter.neverShow.add(Long.valueOf(longValue));
                            }
                            if (DialogObject.isChatDialog(longValue)) {
                                long j = -longValue;
                                if (!arrayList2.contains(Long.valueOf(j))) {
                                    arrayList2.add(Long.valueOf(j));
                                }
                            } else if (DialogObject.isUserDialog(longValue)) {
                                if (!arrayList.contains(Long.valueOf(longValue))) {
                                    arrayList.add(Long.valueOf(longValue));
                                }
                            } else {
                                int encryptedChatId = DialogObject.getEncryptedChatId(longValue);
                                if (!arrayList3.contains(Integer.valueOf(encryptedChatId))) {
                                    arrayList3.add(Integer.valueOf(encryptedChatId));
                                }
                            }
                            i2 = 0;
                            i4 = 1;
                        }
                        queryFinalized.dispose();
                        i5++;
                        i2 = 0;
                        i3 = 2;
                        i4 = 1;
                    }
                    if (dialogFilter.id == 0) {
                        z = true;
                    }
                    i2 = 0;
                } catch (Exception e) {
                    e = e;
                    sQLiteCursor = queryFinalized2;
                    sQLitePreparedStatement = null;
                    try {
                        checkSQLException(e);
                        if (sQLiteCursor != null) {
                        }
                        if (sQLitePreparedStatement == null) {
                        }
                    } catch (Throwable th) {
                        th = th;
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                        throw th;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    sQLiteCursor = queryFinalized2;
                    sQLitePreparedStatement = null;
                    if (sQLiteCursor != null) {
                    }
                    if (sQLitePreparedStatement != null) {
                    }
                    throw th;
                }
            }
            queryFinalized2.dispose();
            if (!z) {
                MessagesController.DialogFilter dialogFilter2 = new MessagesController.DialogFilter();
                dialogFilter2.id = 0;
                dialogFilter2.order = 0;
                dialogFilter2.name = "ALL_CHATS";
                for (int i6 = 0; i6 < this.dialogFilters.size(); i6++) {
                    this.dialogFilters.get(i6).order++;
                }
                this.dialogFilters.add(dialogFilter2);
                this.dialogFiltersMap.put(dialogFilter2.id, dialogFilter2);
                sparseArray.put(dialogFilter2.id, dialogFilter2);
                SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO dialog_filter VALUES(?, ?, ?, ?, ?)");
                try {
                    executeFast.bindInteger(1, dialogFilter2.id);
                    executeFast.bindInteger(2, dialogFilter2.order);
                    executeFast.bindInteger(3, dialogFilter2.unreadCount);
                    executeFast.bindInteger(4, dialogFilter2.flags);
                    executeFast.bindString(5, dialogFilter2.name);
                    executeFast.stepThis().dispose();
                } catch (Exception e2) {
                    e = e2;
                    sQLitePreparedStatement = executeFast;
                    sQLiteCursor = null;
                    checkSQLException(e);
                    if (sQLiteCursor != null) {
                        sQLiteCursor.dispose();
                    }
                    if (sQLitePreparedStatement == null) {
                        sQLitePreparedStatement.dispose();
                        return;
                    }
                    return;
                } catch (Throwable th3) {
                    th = th3;
                    sQLitePreparedStatement = executeFast;
                    sQLiteCursor = null;
                    if (sQLiteCursor != null) {
                    }
                    if (sQLitePreparedStatement != null) {
                    }
                    throw th;
                }
            }
            Collections.sort(this.dialogFilters, new Comparator() {
                @Override
                public final int compare(Object obj, Object obj2) {
                    int lambda$loadDialogFilters$60;
                    lambda$loadDialogFilters$60 = MessagesStorage.lambda$loadDialogFilters$60((MessagesController.DialogFilter) obj, (MessagesController.DialogFilter) obj2);
                    return lambda$loadDialogFilters$60;
                }
            });
            if (z2) {
                calcUnreadCounters(true);
            }
            if (!arrayList4.isEmpty()) {
                tLRPC$TL_messages_dialogs = loadDialogsByIds(TextUtils.join(",", arrayList4), arrayList, arrayList2, arrayList3);
            } else {
                tLRPC$TL_messages_dialogs = new TLRPC$TL_messages_dialogs();
            }
            TLRPC$messages_Dialogs tLRPC$messages_Dialogs = tLRPC$TL_messages_dialogs;
            ArrayList<TLRPC$User> arrayList5 = new ArrayList<>();
            ArrayList<TLRPC$Chat> arrayList6 = new ArrayList<>();
            ArrayList<TLRPC$EncryptedChat> arrayList7 = new ArrayList<>();
            if (!arrayList3.isEmpty()) {
                getEncryptedChatsInternal(TextUtils.join(",", arrayList3), arrayList7, arrayList);
            }
            if (!arrayList.isEmpty()) {
                getUsersInternal(TextUtils.join(",", arrayList), arrayList5);
            }
            if (!arrayList2.isEmpty()) {
                getChatsInternal(TextUtils.join(",", arrayList2), arrayList6);
            }
            getMessagesController().processLoadedDialogFilters(new ArrayList<>(this.dialogFilters), tLRPC$messages_Dialogs, null, arrayList5, arrayList6, arrayList7, 0, null);
        } catch (Exception e3) {
            e = e3;
            sQLiteCursor = null;
        } catch (Throwable th4) {
            th = th4;
            sQLiteCursor = null;
        }
    }

    public static int lambda$loadDialogFilters$60(MessagesController.DialogFilter dialogFilter, MessagesController.DialogFilter dialogFilter2) {
        int i = dialogFilter.order;
        int i2 = dialogFilter2.order;
        if (i > i2) {
            return 1;
        }
        return i < i2 ? -1 : 0;
    }

    private void calcUnreadCounters(boolean z) {
        Throwable th;
        SQLiteCursor sQLiteCursor;
        Exception exc;
        int i;
        LongSparseArray longSparseArray;
        int size;
        int i2;
        int i3;
        int i4;
        MessagesController.DialogFilter dialogFilter;
        int i5;
        int i6;
        LongSparseArray longSparseArray2;
        int i7;
        int i8;
        int i9;
        int i10;
        int i11;
        int i12;
        int i13;
        int i14;
        int i15;
        MessagesController.DialogFilter dialogFilter2;
        int i16;
        LongSparseArray longSparseArray3;
        int i17;
        int i18;
        MessagesController.DialogFilter dialogFilter3;
        LongSparseArray longSparseArray4;
        int i19;
        int i20;
        int i21;
        LongSparseArray longSparseArray5;
        ArrayList<TLRPC$EncryptedChat> arrayList;
        LongSparseArray longSparseArray6;
        int i22;
        int i23;
        int intValue;
        LongSparseIntArray longSparseIntArray;
        int i24 = 0;
        for (int i25 = 0; i25 < 2; i25++) {
            for (int i26 = 0; i26 < 2; i26++) {
                try {
                    int[] iArr = this.contacts[i25];
                    int[] iArr2 = this.nonContacts[i25];
                    int[] iArr3 = this.bots[i25];
                    int[] iArr4 = this.channels[i25];
                    this.groups[i25][i26] = 0;
                    iArr4[i26] = 0;
                    iArr3[i26] = 0;
                    iArr2[i26] = 0;
                    iArr[i26] = 0;
                } catch (Exception e) {
                    exc = e;
                    sQLiteCursor = null;
                    checkSQLException(exc);
                    if (sQLiteCursor == null) {
                    }
                } catch (Throwable th2) {
                    th = th2;
                    sQLiteCursor = null;
                    if (sQLiteCursor != null) {
                    }
                }
            }
        }
        this.dialogsWithMentions.clear();
        this.dialogsWithUnread.clear();
        ArrayList<TLRPC$User> arrayList2 = new ArrayList<>();
        ArrayList<TLRPC$User> arrayList3 = new ArrayList<>();
        ArrayList<TLRPC$Chat> arrayList4 = new ArrayList<>();
        ArrayList arrayList5 = new ArrayList();
        ArrayList arrayList6 = new ArrayList();
        ArrayList arrayList7 = new ArrayList();
        LongSparseIntArray longSparseIntArray2 = new LongSparseIntArray();
        LongSparseIntArray longSparseIntArray3 = new LongSparseIntArray();
        SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT DISTINCT did FROM topics WHERE unread_count > 0 OR unread_mentions > 0", new Object[0]);
        while (true) {
            try {
                i = 1;
                if (!queryFinalized.next()) {
                    break;
                }
                long longValue = queryFinalized.longValue(0);
                if (isForum(longValue)) {
                    longSparseIntArray3.put(longValue, 1);
                }
            } catch (Exception e2) {
                exc = e2;
                sQLiteCursor = queryFinalized;
            } catch (Throwable th3) {
                th = th3;
                sQLiteCursor = queryFinalized;
                if (sQLiteCursor != null) {
                }
            }
        }
        sQLiteCursor = this.database.queryFinalized("SELECT did, folder_id, unread_count, unread_count_i FROM dialogs WHERE unread_count > 0 OR flags > 0 UNION ALL SELECT did, folder_id, unread_count, unread_count_i FROM dialogs WHERE unread_count_i > 0", new Object[0]);
        while (sQLiteCursor.next()) {
            try {
                try {
                    int intValue2 = sQLiteCursor.intValue(i);
                    long longValue2 = sQLiteCursor.longValue(i24);
                    if (isForum(longValue2)) {
                        int i27 = longSparseIntArray3.get(longValue2, i24);
                        if (i27 != 0) {
                            intValue = i27;
                        }
                    } else {
                        intValue = sQLiteCursor.intValue(2);
                        i24 = sQLiteCursor.intValue(3);
                    }
                    if (intValue > 0) {
                        longSparseIntArray = longSparseIntArray3;
                        this.dialogsWithUnread.put(longValue2, Integer.valueOf(intValue));
                    } else {
                        longSparseIntArray = longSparseIntArray3;
                    }
                    if (i24 > 0) {
                        this.dialogsWithMentions.put(longValue2, Integer.valueOf(i24));
                    }
                    longSparseIntArray2.put(longValue2, intValue2);
                    if (DialogObject.isEncryptedDialog(longValue2)) {
                        int encryptedChatId = DialogObject.getEncryptedChatId(longValue2);
                        if (!arrayList7.contains(Integer.valueOf(encryptedChatId))) {
                            arrayList7.add(Integer.valueOf(encryptedChatId));
                        }
                    } else if (DialogObject.isUserDialog(longValue2)) {
                        if (!arrayList5.contains(Long.valueOf(longValue2))) {
                            arrayList5.add(Long.valueOf(longValue2));
                        }
                    } else {
                        long j = -longValue2;
                        if (!arrayList6.contains(Long.valueOf(j))) {
                            arrayList6.add(Long.valueOf(j));
                        }
                    }
                    longSparseIntArray3 = longSparseIntArray;
                    i24 = 0;
                    i = 1;
                } catch (Exception e3) {
                    exc = e3;
                    checkSQLException(exc);
                    if (sQLiteCursor == null) {
                        sQLiteCursor.dispose();
                        return;
                    }
                    return;
                }
            } catch (Throwable th4) {
                th = th4;
                if (sQLiteCursor != null) {
                    sQLiteCursor.dispose();
                    throw th;
                }
                throw th;
            }
        }
        sQLiteCursor.dispose();
        LongSparseArray longSparseArray7 = new LongSparseArray();
        LongSparseArray longSparseArray8 = new LongSparseArray();
        LongSparseArray longSparseArray9 = new LongSparseArray();
        LongSparseIntArray longSparseIntArray4 = new LongSparseIntArray();
        LongSparseArray longSparseArray10 = new LongSparseArray();
        LongSparseArray longSparseArray11 = new LongSparseArray();
        LongSparseArray longSparseArray12 = longSparseArray8;
        LongSparseArray longSparseArray13 = longSparseArray9;
        if (!arrayList5.isEmpty()) {
            getUsersInternal(TextUtils.join(",", arrayList5), arrayList2);
            int size2 = arrayList2.size();
            int i28 = 0;
            while (i28 < size2) {
                TLRPC$User tLRPC$User = arrayList2.get(i28);
                ArrayList<TLRPC$User> arrayList8 = arrayList2;
                int i29 = size2;
                ArrayList<TLRPC$User> arrayList9 = arrayList3;
                ArrayList arrayList10 = arrayList6;
                ArrayList arrayList11 = arrayList7;
                boolean isDialogMuted = getMessagesController().isDialogMuted(tLRPC$User.id, 0L);
                int i30 = longSparseIntArray2.get(tLRPC$User.id);
                if (isDialogMuted) {
                    longSparseArray10.put(tLRPC$User.id, Boolean.TRUE);
                }
                if (i30 == 1) {
                    longSparseArray11.put(tLRPC$User.id, Boolean.TRUE);
                }
                if (tLRPC$User.bot) {
                    int[] iArr5 = this.bots[i30];
                    iArr5[isDialogMuted ? 1 : 0] = iArr5[isDialogMuted ? 1 : 0] + 1;
                } else {
                    if (!tLRPC$User.self && !tLRPC$User.contact) {
                        int[] iArr6 = this.nonContacts[i30];
                        iArr6[isDialogMuted ? 1 : 0] = iArr6[isDialogMuted ? 1 : 0] + 1;
                    }
                    int[] iArr7 = this.contacts[i30];
                    iArr7[isDialogMuted ? 1 : 0] = iArr7[isDialogMuted ? 1 : 0] + 1;
                }
                longSparseArray7.put(tLRPC$User.id, tLRPC$User);
                i28++;
                arrayList2 = arrayList8;
                arrayList3 = arrayList9;
                size2 = i29;
                arrayList6 = arrayList10;
                arrayList7 = arrayList11;
            }
        }
        ArrayList<TLRPC$User> arrayList12 = arrayList3;
        ArrayList arrayList13 = arrayList6;
        ArrayList arrayList14 = arrayList7;
        if (!arrayList14.isEmpty()) {
            ArrayList<Long> arrayList15 = new ArrayList<>();
            ArrayList<TLRPC$EncryptedChat> arrayList16 = new ArrayList<>();
            getEncryptedChatsInternal(TextUtils.join(",", arrayList14), arrayList16, arrayList15);
            if (!arrayList15.isEmpty()) {
                ArrayList<TLRPC$User> arrayList17 = arrayList12;
                getUsersInternal(TextUtils.join(",", arrayList15), arrayList17);
                int size3 = arrayList17.size();
                int i31 = 0;
                while (i31 < size3) {
                    TLRPC$User tLRPC$User2 = arrayList17.get(i31);
                    ArrayList<TLRPC$User> arrayList18 = arrayList17;
                    LongSparseArray longSparseArray14 = longSparseArray13;
                    longSparseArray14.put(tLRPC$User2.id, tLRPC$User2);
                    i31++;
                    longSparseArray13 = longSparseArray14;
                    longSparseArray7 = longSparseArray7;
                    size3 = size3;
                    arrayList17 = arrayList18;
                }
                LongSparseArray longSparseArray15 = longSparseArray7;
                LongSparseArray longSparseArray16 = longSparseArray13;
                int size4 = arrayList16.size();
                int i32 = 0;
                while (i32 < size4) {
                    LongSparseArray longSparseArray17 = longSparseArray15;
                    TLRPC$User tLRPC$User3 = (TLRPC$User) longSparseArray16.get(arrayList16.get(i32).user_id);
                    if (tLRPC$User3 == null) {
                        i22 = size4;
                        i23 = i32;
                        arrayList = arrayList16;
                        longSparseArray6 = longSparseArray16;
                    } else {
                        arrayList = arrayList16;
                        longSparseArray6 = longSparseArray16;
                        long makeEncryptedDialogId = DialogObject.makeEncryptedDialogId(r8.id);
                        i22 = size4;
                        i23 = i32;
                        boolean isDialogMuted2 = getMessagesController().isDialogMuted(makeEncryptedDialogId, 0L);
                        int i33 = longSparseIntArray2.get(makeEncryptedDialogId);
                        if (isDialogMuted2) {
                            longSparseArray10.put(tLRPC$User3.id, Boolean.TRUE);
                        }
                        if (i33 == 1) {
                            longSparseArray11.put(tLRPC$User3.id, Boolean.TRUE);
                        }
                        if (!tLRPC$User3.self && !tLRPC$User3.contact) {
                            int[] iArr8 = this.nonContacts[i33];
                            iArr8[isDialogMuted2 ? 1 : 0] = iArr8[isDialogMuted2 ? 1 : 0] + 1;
                            longSparseIntArray4.put(tLRPC$User3.id, longSparseIntArray4.get(tLRPC$User3.id, 0) + 1);
                        }
                        int[] iArr9 = this.contacts[i33];
                        iArr9[isDialogMuted2 ? 1 : 0] = iArr9[isDialogMuted2 ? 1 : 0] + 1;
                        longSparseIntArray4.put(tLRPC$User3.id, longSparseIntArray4.get(tLRPC$User3.id, 0) + 1);
                    }
                    i32 = i23 + 1;
                    arrayList16 = arrayList;
                    longSparseArray15 = longSparseArray17;
                    longSparseArray16 = longSparseArray6;
                    size4 = i22;
                }
                longSparseArray13 = longSparseArray16;
                longSparseArray = longSparseArray15;
                if (!arrayList13.isEmpty()) {
                    getChatsInternal(TextUtils.join(",", arrayList13), arrayList4);
                    int size5 = arrayList4.size();
                    int i34 = 0;
                    while (i34 < size5) {
                        TLRPC$Chat tLRPC$Chat = arrayList4.get(i34);
                        if (!(tLRPC$Chat.migrated_to instanceof TLRPC$TL_inputChannel) && !ChatObject.isNotInChat(tLRPC$Chat)) {
                            boolean isDialogMuted3 = getMessagesController().isDialogMuted(-tLRPC$Chat.id, 0L, tLRPC$Chat);
                            int i35 = longSparseIntArray2.get(-tLRPC$Chat.id);
                            char c = (!isDialogMuted3 || this.dialogsWithMentions.indexOfKey(-tLRPC$Chat.id) >= 0) ? (char) 0 : (char) 1;
                            if (isDialogMuted3) {
                                longSparseArray10.put(-tLRPC$Chat.id, Boolean.TRUE);
                            }
                            if (i35 == 1) {
                                longSparseArray11.put(-tLRPC$Chat.id, Boolean.TRUE);
                            }
                            if (ChatObject.isChannel(tLRPC$Chat) && !tLRPC$Chat.megagroup) {
                                int[] iArr10 = this.channels[i35];
                                iArr10[c] = iArr10[c] + 1;
                            } else {
                                int[] iArr11 = this.groups[i35];
                                iArr11[c] = iArr11[c] + 1;
                            }
                            longSparseArray5 = longSparseArray12;
                            longSparseArray5.put(tLRPC$Chat.id, tLRPC$Chat);
                            i34++;
                            longSparseArray12 = longSparseArray5;
                        }
                        longSparseArray5 = longSparseArray12;
                        this.dialogsWithUnread.remove(-tLRPC$Chat.id);
                        this.dialogsWithMentions.remove(-tLRPC$Chat.id);
                        i34++;
                        longSparseArray12 = longSparseArray5;
                    }
                }
                LongSparseArray longSparseArray18 = longSparseArray12;
                size = this.dialogFilters.size();
                i2 = 0;
                while (i2 < size + 2) {
                    if (i2 < size) {
                        dialogFilter = this.dialogFilters.get(i2);
                        if (dialogFilter.pendingUnreadCount >= 0) {
                            i6 = i2;
                            longSparseArray2 = longSparseArray;
                            i2 = i6 + 1;
                            longSparseArray = longSparseArray2;
                        } else {
                            i4 = dialogFilter.flags;
                        }
                    } else {
                        int i36 = MessagesController.DIALOG_FILTER_FLAG_ALL_CHATS;
                        if (i2 == size) {
                            if (!getNotificationsController().showBadgeMuted) {
                                i36 |= MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED;
                            }
                            i3 = MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED;
                        } else {
                            i3 = MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED;
                        }
                        i4 = i3 | i36;
                        dialogFilter = null;
                    }
                    if ((MessagesController.DIALOG_FILTER_FLAG_CONTACTS & i4) != 0) {
                        if ((MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED & i4) == 0) {
                            int[][] iArr12 = this.contacts;
                            int i37 = iArr12[0][0] + 0;
                            i5 = (MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i4) == 0 ? iArr12[0][1] + i37 : i37;
                        } else {
                            i5 = 0;
                        }
                        if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i4) == 0) {
                            int[][] iArr13 = this.contacts;
                            i5 += iArr13[1][0];
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i4) == 0) {
                                i5 += iArr13[1][1];
                            }
                        }
                    } else {
                        i5 = 0;
                    }
                    if ((MessagesController.DIALOG_FILTER_FLAG_NON_CONTACTS & i4) != 0) {
                        if ((MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED & i4) == 0) {
                            int[][] iArr14 = this.nonContacts;
                            i5 += iArr14[0][0];
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i4) == 0) {
                                i5 += iArr14[0][1];
                            }
                        }
                        if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i4) == 0) {
                            int[][] iArr15 = this.nonContacts;
                            i5 += iArr15[1][0];
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i4) == 0) {
                                i5 += iArr15[1][1];
                            }
                        }
                    }
                    if ((MessagesController.DIALOG_FILTER_FLAG_GROUPS & i4) != 0) {
                        if ((MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED & i4) == 0) {
                            int[][] iArr16 = this.groups;
                            i5 += iArr16[0][0];
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i4) == 0) {
                                i5 += iArr16[0][1];
                            }
                        }
                        if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i4) == 0) {
                            int[][] iArr17 = this.groups;
                            i5 += iArr17[1][0];
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i4) == 0) {
                                i5 += iArr17[1][1];
                            }
                        }
                    }
                    if ((MessagesController.DIALOG_FILTER_FLAG_CHANNELS & i4) != 0) {
                        if ((MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED & i4) == 0) {
                            int[][] iArr18 = this.channels;
                            i5 += iArr18[0][0];
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i4) == 0) {
                                i5 += iArr18[0][1];
                            }
                        }
                        if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i4) == 0) {
                            int[][] iArr19 = this.channels;
                            i5 += iArr19[1][0];
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i4) == 0) {
                                i5 += iArr19[1][1];
                            }
                        }
                    }
                    if ((MessagesController.DIALOG_FILTER_FLAG_BOTS & i4) != 0) {
                        if ((MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED & i4) == 0) {
                            int[][] iArr20 = this.bots;
                            i5 += iArr20[0][0];
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i4) == 0) {
                                i5 += iArr20[0][1];
                            }
                        }
                        if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i4) == 0) {
                            int[][] iArr21 = this.bots;
                            i5 += iArr21[1][0];
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i4) == 0) {
                                i5 += iArr21[1][1];
                            }
                            if (dialogFilter == null) {
                                int size6 = dialogFilter.alwaysShow.size();
                                int i38 = i5;
                                int i39 = 0;
                                while (i39 < size6) {
                                    int i40 = i38;
                                    long longValue3 = dialogFilter.alwaysShow.get(i39).longValue();
                                    if (DialogObject.isUserDialog(longValue3)) {
                                        i16 = size6;
                                        int i41 = 0;
                                        for (int i42 = 2; i41 < i42; i42 = 2) {
                                            TLRPC$User tLRPC$User4 = (TLRPC$User) (i41 == 0 ? longSparseArray : longSparseArray13).get(longValue3);
                                            if (tLRPC$User4 != null) {
                                                if (i41 == 0) {
                                                    longSparseArray4 = longSparseArray;
                                                    i20 = 1;
                                                } else {
                                                    longSparseArray4 = longSparseArray;
                                                    i20 = longSparseIntArray4.get(longValue3, 0);
                                                    if (i20 == 0) {
                                                        i19 = size;
                                                        dialogFilter3 = dialogFilter;
                                                    }
                                                }
                                                if (tLRPC$User4.bot) {
                                                    i21 = MessagesController.DIALOG_FILTER_FLAG_BOTS;
                                                } else {
                                                    if (!tLRPC$User4.self && !tLRPC$User4.contact) {
                                                        i21 = MessagesController.DIALOG_FILTER_FLAG_NON_CONTACTS;
                                                    }
                                                    i21 = MessagesController.DIALOG_FILTER_FLAG_CONTACTS;
                                                }
                                                if ((i21 & i4) == 0) {
                                                    i40 += i20;
                                                    i19 = size;
                                                    dialogFilter3 = dialogFilter;
                                                } else {
                                                    if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i4) != 0) {
                                                        i19 = size;
                                                        dialogFilter3 = dialogFilter;
                                                    } else {
                                                        i19 = size;
                                                        dialogFilter3 = dialogFilter;
                                                    }
                                                    if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i4) != 0 && longSparseArray11.indexOfKey(tLRPC$User4.id) >= 0) {
                                                        i40 += i20;
                                                    }
                                                }
                                            } else {
                                                dialogFilter3 = dialogFilter;
                                                longSparseArray4 = longSparseArray;
                                                i19 = size;
                                            }
                                            i41++;
                                            size = i19;
                                            longSparseArray = longSparseArray4;
                                            dialogFilter = dialogFilter3;
                                        }
                                        dialogFilter2 = dialogFilter;
                                        longSparseArray3 = longSparseArray;
                                        i17 = size;
                                    } else {
                                        dialogFilter2 = dialogFilter;
                                        i16 = size6;
                                        longSparseArray3 = longSparseArray;
                                        i17 = size;
                                        TLRPC$Chat tLRPC$Chat2 = (TLRPC$Chat) longSparseArray18.get(-longValue3);
                                        if (tLRPC$Chat2 != null) {
                                            if (ChatObject.isChannel(tLRPC$Chat2) && !tLRPC$Chat2.megagroup) {
                                                i18 = MessagesController.DIALOG_FILTER_FLAG_CHANNELS;
                                            } else {
                                                i18 = MessagesController.DIALOG_FILTER_FLAG_GROUPS;
                                            }
                                            if ((i18 & i4) != 0) {
                                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i4) != 0) {
                                                    if (longSparseArray10.indexOfKey(-tLRPC$Chat2.id) >= 0 && this.dialogsWithMentions.indexOfKey(-tLRPC$Chat2.id) < 0) {
                                                    }
                                                }
                                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i4) != 0 && longSparseArray11.indexOfKey(-tLRPC$Chat2.id) >= 0) {
                                                }
                                            }
                                            i38 = i40 + 1;
                                            i39++;
                                            size = i17;
                                            size6 = i16;
                                            longSparseArray = longSparseArray3;
                                            dialogFilter = dialogFilter2;
                                        }
                                    }
                                    i38 = i40;
                                    i39++;
                                    size = i17;
                                    size6 = i16;
                                    longSparseArray = longSparseArray3;
                                    dialogFilter = dialogFilter2;
                                }
                                longSparseArray2 = longSparseArray;
                                int i43 = size;
                                int size7 = dialogFilter.neverShow.size();
                                int i44 = i38;
                                int i45 = 0;
                                while (i45 < size7) {
                                    int i46 = size7;
                                    MessagesController.DialogFilter dialogFilter4 = dialogFilter;
                                    long longValue4 = dialogFilter.neverShow.get(i45).longValue();
                                    if (DialogObject.isUserDialog(longValue4)) {
                                        int i47 = 0;
                                        while (i47 < 2) {
                                            TLRPC$User tLRPC$User5 = (TLRPC$User) (i47 == 0 ? longSparseArray2 : longSparseArray13).get(longValue4);
                                            if (tLRPC$User5 != null) {
                                                if (i47 == 0) {
                                                    i12 = i46;
                                                    i14 = 1;
                                                } else {
                                                    i12 = i46;
                                                    i14 = longSparseIntArray4.get(longValue4, 0);
                                                }
                                                if (tLRPC$User5.bot) {
                                                    i15 = MessagesController.DIALOG_FILTER_FLAG_BOTS;
                                                } else {
                                                    if (!tLRPC$User5.self && !tLRPC$User5.contact) {
                                                        i15 = MessagesController.DIALOG_FILTER_FLAG_NON_CONTACTS;
                                                    }
                                                    i15 = MessagesController.DIALOG_FILTER_FLAG_CONTACTS;
                                                }
                                                if ((i15 & i4) != 0) {
                                                    if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i4) != 0) {
                                                        i13 = i2;
                                                        i11 = i45;
                                                        if (longSparseArray11.indexOfKey(tLRPC$User5.id) < 0) {
                                                        }
                                                    } else {
                                                        i13 = i2;
                                                        i11 = i45;
                                                    }
                                                    if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i4) == 0 || longSparseArray10.indexOfKey(tLRPC$User5.id) < 0) {
                                                        i44 -= i14;
                                                    }
                                                }
                                                i13 = i2;
                                                i11 = i45;
                                            } else {
                                                i11 = i45;
                                                i12 = i46;
                                                i13 = i2;
                                            }
                                            i47++;
                                            i2 = i13;
                                            i46 = i12;
                                            i45 = i11;
                                        }
                                        i7 = i45;
                                        i8 = i46;
                                        i9 = i2;
                                    } else {
                                        i7 = i45;
                                        i8 = i46;
                                        i9 = i2;
                                        TLRPC$Chat tLRPC$Chat3 = (TLRPC$Chat) longSparseArray18.get(-longValue4);
                                        if (tLRPC$Chat3 != null) {
                                            if (ChatObject.isChannel(tLRPC$Chat3) && !tLRPC$Chat3.megagroup) {
                                                i10 = MessagesController.DIALOG_FILTER_FLAG_CHANNELS;
                                            } else {
                                                i10 = MessagesController.DIALOG_FILTER_FLAG_GROUPS;
                                            }
                                            if ((i10 & i4) != 0 && (((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i4) == 0 || longSparseArray11.indexOfKey(-tLRPC$Chat3.id) < 0) && ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i4) == 0 || longSparseArray10.indexOfKey(-tLRPC$Chat3.id) < 0 || this.dialogsWithMentions.indexOfKey(-tLRPC$Chat3.id) >= 0))) {
                                                i44--;
                                            }
                                        }
                                    }
                                    i45 = i7 + 1;
                                    i2 = i9;
                                    size7 = i8;
                                    dialogFilter = dialogFilter4;
                                }
                                i6 = i2;
                                dialogFilter.pendingUnreadCount = i44;
                                if (z) {
                                    dialogFilter.unreadCount = i44;
                                }
                                size = i43;
                            } else {
                                i6 = i2;
                                longSparseArray2 = longSparseArray;
                                if (i6 == size) {
                                    this.pendingMainUnreadCount = i5;
                                    if (z) {
                                        this.mainUnreadCount = i5;
                                    }
                                } else if (i6 == size + 1) {
                                    this.pendingArchiveUnreadCount = i5;
                                    if (z) {
                                        this.archiveUnreadCount = i5;
                                    }
                                }
                            }
                            i2 = i6 + 1;
                            longSparseArray = longSparseArray2;
                        }
                    }
                    if (dialogFilter == null) {
                    }
                    i2 = i6 + 1;
                    longSparseArray = longSparseArray2;
                }
            }
        }
        longSparseArray = longSparseArray7;
        if (!arrayList13.isEmpty()) {
        }
        LongSparseArray longSparseArray182 = longSparseArray12;
        size = this.dialogFilters.size();
        i2 = 0;
        while (i2 < size + 2) {
        }
    }

    private void saveDialogFilterInternal(MessagesController.DialogFilter dialogFilter, boolean z, boolean z2) {
        int i;
        SQLitePreparedStatement executeFast;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                if (!this.dialogFilters.contains(dialogFilter)) {
                    if (z) {
                        if (this.dialogFilters.get(0).isDefault()) {
                            this.dialogFilters.add(1, dialogFilter);
                        } else {
                            this.dialogFilters.add(0, dialogFilter);
                        }
                    } else {
                        this.dialogFilters.add(dialogFilter);
                    }
                    this.dialogFiltersMap.put(dialogFilter.id, dialogFilter);
                }
                executeFast = this.database.executeFast("REPLACE INTO dialog_filter VALUES(?, ?, ?, ?, ?)");
            } catch (Exception e) {
                e = e;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            executeFast.bindInteger(1, dialogFilter.id);
            executeFast.bindInteger(2, dialogFilter.order);
            executeFast.bindInteger(3, dialogFilter.unreadCount);
            executeFast.bindInteger(4, dialogFilter.flags);
            executeFast.bindString(5, dialogFilter.id == 0 ? "ALL_CHATS" : dialogFilter.name);
            executeFast.step();
            executeFast.dispose();
            if (z2) {
                this.database.executeFast("DELETE FROM dialog_filter_ep WHERE id = " + dialogFilter.id).stepThis().dispose();
                this.database.executeFast("DELETE FROM dialog_filter_pin_v2 WHERE id = " + dialogFilter.id).stepThis().dispose();
                this.database.beginTransaction();
                SQLitePreparedStatement executeFast2 = this.database.executeFast("REPLACE INTO dialog_filter_pin_v2 VALUES(?, ?, ?)");
                int size = dialogFilter.alwaysShow.size();
                for (int i2 = 0; i2 < size; i2++) {
                    long longValue = dialogFilter.alwaysShow.get(i2).longValue();
                    executeFast2.requery();
                    executeFast2.bindInteger(1, dialogFilter.id);
                    executeFast2.bindLong(2, longValue);
                    executeFast2.bindInteger(3, dialogFilter.pinnedDialogs.get(longValue, Integer.MIN_VALUE));
                    executeFast2.step();
                }
                int size2 = dialogFilter.pinnedDialogs.size();
                for (int i3 = 0; i3 < size2; i3++) {
                    long keyAt = dialogFilter.pinnedDialogs.keyAt(i3);
                    if (DialogObject.isEncryptedDialog(keyAt)) {
                        executeFast2.requery();
                        executeFast2.bindInteger(1, dialogFilter.id);
                        executeFast2.bindLong(2, keyAt);
                        executeFast2.bindInteger(3, dialogFilter.pinnedDialogs.valueAt(i3));
                        executeFast2.step();
                    }
                }
                executeFast2.dispose();
                executeFast = this.database.executeFast("REPLACE INTO dialog_filter_ep VALUES(?, ?)");
                int size3 = dialogFilter.neverShow.size();
                for (i = 0; i < size3; i++) {
                    executeFast.requery();
                    executeFast.bindInteger(1, dialogFilter.id);
                    executeFast.bindLong(2, dialogFilter.neverShow.get(i).longValue());
                    executeFast.step();
                }
                executeFast.dispose();
                this.database.commitTransaction();
            }
            SQLiteDatabase sQLiteDatabase = this.database;
            if (sQLiteDatabase != null) {
                sQLiteDatabase.commitTransaction();
            }
        } catch (Exception e2) {
            e = e2;
            sQLitePreparedStatement = executeFast;
            checkSQLException(e);
            SQLiteDatabase sQLiteDatabase2 = this.database;
            if (sQLiteDatabase2 != null) {
                sQLiteDatabase2.commitTransaction();
            }
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
        } catch (Throwable th2) {
            th = th2;
            sQLitePreparedStatement = executeFast;
            SQLiteDatabase sQLiteDatabase3 = this.database;
            if (sQLiteDatabase3 != null) {
                sQLiteDatabase3.commitTransaction();
            }
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    private ArrayList<Long> toPeerIds(ArrayList<TLRPC$InputPeer> arrayList) {
        ArrayList<Long> arrayList2 = new ArrayList<>();
        if (arrayList == null) {
            return arrayList2;
        }
        int size = arrayList.size();
        for (int i = 0; i < size; i++) {
            TLRPC$InputPeer tLRPC$InputPeer = arrayList.get(i);
            if (tLRPC$InputPeer != null) {
                long j = tLRPC$InputPeer.user_id;
                if (j == 0) {
                    long j2 = tLRPC$InputPeer.chat_id;
                    if (j2 == 0) {
                        j2 = tLRPC$InputPeer.channel_id;
                    }
                    j = -j2;
                }
                arrayList2.add(Long.valueOf(j));
            }
        }
        return arrayList2;
    }

    public void checkLoadedRemoteFilters(final TLRPC$Vector tLRPC$Vector, final Runnable runnable) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$checkLoadedRemoteFilters$63(tLRPC$Vector, runnable);
            }
        });
    }

    public void lambda$checkLoadedRemoteFilters$63(TLRPC$Vector tLRPC$Vector, Runnable runnable) {
        TLRPC$messages_Dialogs tLRPC$TL_messages_dialogs;
        int i;
        int i2;
        SparseArray<MessagesController.DialogFilter> sparseArray;
        int i3;
        HashMap<Long, TLRPC$InputPeer> hashMap;
        HashMap<Integer, HashSet<Long>> hashMap2;
        HashSet<Integer> hashSet;
        HashMap<Long, TLRPC$InputPeer> hashMap3;
        ArrayList<Long> arrayList;
        HashSet<Integer> hashSet2;
        ArrayList<Long> arrayList2;
        TLRPC$DialogFilter tLRPC$DialogFilter;
        HashMap<Long, TLRPC$InputPeer> hashMap4;
        ArrayList<Long> arrayList3;
        Long valueOf;
        boolean z;
        boolean z2;
        ArrayList<Long> arrayList4;
        HashMap<Long, TLRPC$InputPeer> hashMap5;
        LinkedHashMap linkedHashMap;
        int size;
        int i4;
        int size2;
        int i5;
        int i6;
        HashMap<Integer, HashSet<Long>> hashMap6;
        HashSet<Integer> hashSet3;
        int i7;
        Long valueOf2;
        int i8;
        ArrayList arrayList5;
        TLRPC$Vector tLRPC$Vector2 = tLRPC$Vector;
        try {
            SparseArray<MessagesController.DialogFilter> sparseArray2 = new SparseArray<>();
            int size3 = this.dialogFilters.size();
            for (int i9 = 0; i9 < size3; i9++) {
                MessagesController.DialogFilter dialogFilter = this.dialogFilters.get(i9);
                sparseArray2.put(dialogFilter.id, dialogFilter);
            }
            ArrayList<Integer> arrayList6 = new ArrayList<>();
            ArrayList<Long> arrayList7 = new ArrayList<>();
            HashMap<Long, TLRPC$InputPeer> hashMap7 = new HashMap<>();
            ArrayList<Long> arrayList8 = new ArrayList<>();
            HashMap<Long, TLRPC$InputPeer> hashMap8 = new HashMap<>();
            ArrayList arrayList9 = new ArrayList();
            HashMap<Long, TLRPC$InputPeer> hashMap9 = new HashMap<>();
            ArrayList<MessagesController.DialogFilter> arrayList10 = new ArrayList<>();
            HashMap<Integer, HashSet<Long>> hashMap10 = new HashMap<>();
            HashSet<Integer> hashSet4 = new HashSet<>();
            int size4 = tLRPC$Vector2.objects.size();
            int i10 = 0;
            while (i10 < size4) {
                TLRPC$DialogFilter tLRPC$DialogFilter2 = (TLRPC$DialogFilter) tLRPC$Vector2.objects.get(i10);
                arrayList6.add(Integer.valueOf(tLRPC$DialogFilter2.id));
                if (tLRPC$DialogFilter2.contacts) {
                    i2 = MessagesController.DIALOG_FILTER_FLAG_CONTACTS | 0;
                    i = size4;
                } else {
                    i = size4;
                    i2 = 0;
                }
                if (tLRPC$DialogFilter2.non_contacts) {
                    i2 |= MessagesController.DIALOG_FILTER_FLAG_NON_CONTACTS;
                }
                if (tLRPC$DialogFilter2.groups) {
                    i2 |= MessagesController.DIALOG_FILTER_FLAG_GROUPS;
                }
                if (tLRPC$DialogFilter2.broadcasts) {
                    i2 |= MessagesController.DIALOG_FILTER_FLAG_CHANNELS;
                }
                if (tLRPC$DialogFilter2.bots) {
                    i2 |= MessagesController.DIALOG_FILTER_FLAG_BOTS;
                }
                if (tLRPC$DialogFilter2.exclude_muted) {
                    i2 |= MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED;
                }
                if (tLRPC$DialogFilter2.exclude_read) {
                    i2 |= MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_READ;
                }
                if (tLRPC$DialogFilter2.exclude_archived) {
                    i2 |= MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED;
                }
                if (tLRPC$DialogFilter2 instanceof TLRPC$TL_dialogFilterChatlist) {
                    i2 |= MessagesController.DIALOG_FILTER_FLAG_CHATLIST;
                    if (tLRPC$DialogFilter2.has_my_invites) {
                        i2 |= MessagesController.DIALOG_FILTER_FLAG_CHATLIST_ADMIN;
                    }
                }
                ArrayList<Integer> arrayList11 = arrayList6;
                MessagesController.DialogFilter dialogFilter2 = this.dialogFiltersMap.get(tLRPC$DialogFilter2.id);
                boolean z3 = true;
                if (dialogFilter2 != null) {
                    sparseArray2.remove(tLRPC$DialogFilter2.id);
                    sparseArray = sparseArray2;
                    if (TextUtils.equals(dialogFilter2.name, tLRPC$DialogFilter2.title)) {
                        z = false;
                    } else {
                        dialogFilter2.name = tLRPC$DialogFilter2.title;
                        z = true;
                    }
                    if (dialogFilter2.flags != i2) {
                        dialogFilter2.flags = i2;
                        z2 = true;
                        z = true;
                    } else {
                        z2 = false;
                    }
                    boolean z4 = z2;
                    HashSet hashSet5 = new HashSet(dialogFilter2.alwaysShow);
                    hashSet5.addAll(dialogFilter2.neverShow);
                    HashSet<Long> hashSet6 = new HashSet<>();
                    boolean z5 = z;
                    if (dialogFilter2.pinnedDialogs.size() != 0) {
                        ArrayList arrayList12 = new ArrayList();
                        i3 = i10;
                        int size5 = dialogFilter2.pinnedDialogs.size();
                        arrayList4 = arrayList8;
                        int i11 = 0;
                        boolean z6 = false;
                        while (i11 < size5) {
                            int i12 = size5;
                            long keyAt = dialogFilter2.pinnedDialogs.keyAt(i11);
                            if (DialogObject.isEncryptedDialog(keyAt)) {
                                z6 = true;
                            }
                            arrayList12.add(Long.valueOf(keyAt));
                            i11++;
                            size5 = i12;
                        }
                        if (z6) {
                            linkedHashMap = new LinkedHashMap();
                            final LongSparseIntArray longSparseIntArray = dialogFilter2.pinnedDialogs;
                            hashMap5 = hashMap8;
                            Collections.sort(arrayList12, new Comparator() {
                                @Override
                                public final int compare(Object obj, Object obj2) {
                                    int lambda$checkLoadedRemoteFilters$62;
                                    lambda$checkLoadedRemoteFilters$62 = MessagesStorage.lambda$checkLoadedRemoteFilters$62(LongSparseIntArray.this, (Long) obj, (Long) obj2);
                                    return lambda$checkLoadedRemoteFilters$62;
                                }
                            });
                            int size6 = arrayList12.size();
                            int i13 = 0;
                            while (i13 < size6) {
                                long longValue = ((Long) arrayList12.get(i13)).longValue();
                                if (DialogObject.isEncryptedDialog(longValue)) {
                                    i8 = size6;
                                    arrayList5 = arrayList12;
                                    linkedHashMap.put(Integer.valueOf(i13), Long.valueOf(longValue));
                                } else {
                                    i8 = size6;
                                    arrayList5 = arrayList12;
                                }
                                i13++;
                                size6 = i8;
                                arrayList12 = arrayList5;
                            }
                            size = dialogFilter2.pinnedDialogs.size();
                            for (i4 = 0; i4 < size; i4++) {
                                long keyAt2 = dialogFilter2.pinnedDialogs.keyAt(i4);
                                if (!DialogObject.isEncryptedDialog(keyAt2)) {
                                    hashSet6.add(Long.valueOf(keyAt2));
                                    hashSet5.remove(Long.valueOf(keyAt2));
                                }
                            }
                            dialogFilter2.pinnedDialogs.clear();
                            size2 = tLRPC$DialogFilter2.pinned_peers.size();
                            boolean z7 = z5;
                            i5 = 0;
                            while (i5 < size2) {
                                int i14 = size2;
                                TLRPC$InputPeer tLRPC$InputPeer = tLRPC$DialogFilter2.pinned_peers.get(i5);
                                ArrayList<MessagesController.DialogFilter> arrayList13 = arrayList10;
                                boolean z8 = z7;
                                long j = tLRPC$InputPeer.user_id;
                                if (j != 0) {
                                    valueOf2 = Long.valueOf(j);
                                } else {
                                    long j2 = tLRPC$InputPeer.chat_id;
                                    if (j2 == 0) {
                                        j2 = tLRPC$InputPeer.channel_id;
                                    }
                                    valueOf2 = Long.valueOf(-j2);
                                }
                                int size7 = dialogFilter2.pinnedDialogs.size();
                                ArrayList<Long> arrayList14 = arrayList7;
                                if (linkedHashMap != null) {
                                    while (true) {
                                        Long l = (Long) linkedHashMap.remove(Integer.valueOf(size7));
                                        if (l == null) {
                                            break;
                                        }
                                        dialogFilter2.pinnedDialogs.put(l.longValue(), size7);
                                        size7++;
                                        hashMap7 = hashMap7;
                                        hashMap10 = hashMap10;
                                        hashSet4 = hashSet4;
                                    }
                                }
                                HashMap<Integer, HashSet<Long>> hashMap11 = hashMap10;
                                HashSet<Integer> hashSet7 = hashSet4;
                                HashMap<Long, TLRPC$InputPeer> hashMap12 = hashMap7;
                                dialogFilter2.pinnedDialogs.put(valueOf2.longValue(), size7);
                                hashSet5.remove(valueOf2);
                                if (hashSet6.remove(valueOf2)) {
                                    z7 = z8;
                                } else {
                                    if (!hashMap9.containsKey(valueOf2)) {
                                        arrayList9.add(valueOf2);
                                        hashMap9.put(valueOf2, tLRPC$InputPeer);
                                    }
                                    z7 = true;
                                }
                                i5++;
                                size2 = i14;
                                arrayList10 = arrayList13;
                                arrayList7 = arrayList14;
                                hashMap7 = hashMap12;
                                hashMap10 = hashMap11;
                                hashSet4 = hashSet7;
                            }
                            ArrayList<Long> arrayList15 = arrayList7;
                            ArrayList<MessagesController.DialogFilter> arrayList16 = arrayList10;
                            boolean z9 = z7;
                            HashMap<Integer, HashSet<Long>> hashMap13 = hashMap10;
                            HashSet<Integer> hashSet8 = hashSet4;
                            hashMap = hashMap7;
                            if (linkedHashMap != null) {
                                Iterator it = linkedHashMap.entrySet().iterator();
                                while (it.hasNext()) {
                                    dialogFilter2.pinnedDialogs.put(((Long) ((Map.Entry) it.next()).getValue()).longValue(), dialogFilter2.pinnedDialogs.size());
                                }
                            }
                            boolean z10 = z9;
                            i6 = 0;
                            while (i6 < 2) {
                                ArrayList<Long> peerIds = toPeerIds(i6 == 0 ? tLRPC$DialogFilter2.include_peers : tLRPC$DialogFilter2.exclude_peers);
                                ArrayList<Long> arrayList17 = i6 == 0 ? dialogFilter2.alwaysShow : dialogFilter2.neverShow;
                                if (i6 == 0) {
                                    ArrayList<Long> peerIds2 = toPeerIds(tLRPC$DialogFilter2.pinned_peers);
                                    for (int i15 = 0; i15 < peerIds2.size(); i15++) {
                                        peerIds.remove(peerIds2.get(i15));
                                    }
                                    i7 = 0;
                                    peerIds.addAll(0, peerIds2);
                                } else {
                                    i7 = 0;
                                }
                                int size8 = peerIds.size();
                                int i16 = peerIds.size() != arrayList17.size() ? 1 : i7;
                                if (i16 == 0) {
                                    int i17 = i7;
                                    while (true) {
                                        if (i17 >= size8) {
                                            break;
                                        }
                                        if (!arrayList17.contains(peerIds.get(i17))) {
                                            i16 = 1;
                                            break;
                                        }
                                        i17++;
                                    }
                                }
                                if (i16 != 0) {
                                    if (i6 == 0) {
                                        dialogFilter2.alwaysShow = peerIds;
                                    } else {
                                        dialogFilter2.neverShow = peerIds;
                                    }
                                    z10 = true;
                                    z4 = true;
                                }
                                i6++;
                            }
                            if (hashSet6.isEmpty()) {
                                hashMap6 = hashMap13;
                                hashMap6.put(Integer.valueOf(dialogFilter2.id), hashSet6);
                            } else {
                                hashMap6 = hashMap13;
                                z3 = z10;
                            }
                            if (z3) {
                                arrayList10 = arrayList16;
                            } else {
                                arrayList10 = arrayList16;
                                arrayList10.add(dialogFilter2);
                            }
                            if (z4) {
                                hashSet3 = hashSet8;
                            } else {
                                hashSet3 = hashSet8;
                                hashSet3.add(Integer.valueOf(dialogFilter2.id));
                            }
                            hashMap2 = hashMap6;
                            hashSet = hashSet3;
                            arrayList8 = arrayList4;
                            hashMap3 = hashMap5;
                            arrayList = arrayList15;
                        } else {
                            hashMap5 = hashMap8;
                        }
                    } else {
                        arrayList4 = arrayList8;
                        hashMap5 = hashMap8;
                        i3 = i10;
                    }
                    linkedHashMap = null;
                    size = dialogFilter2.pinnedDialogs.size();
                    while (i4 < size) {
                    }
                    dialogFilter2.pinnedDialogs.clear();
                    size2 = tLRPC$DialogFilter2.pinned_peers.size();
                    boolean z72 = z5;
                    i5 = 0;
                    while (i5 < size2) {
                    }
                    ArrayList<Long> arrayList152 = arrayList7;
                    ArrayList<MessagesController.DialogFilter> arrayList162 = arrayList10;
                    boolean z92 = z72;
                    HashMap<Integer, HashSet<Long>> hashMap132 = hashMap10;
                    HashSet<Integer> hashSet82 = hashSet4;
                    hashMap = hashMap7;
                    if (linkedHashMap != null) {
                    }
                    boolean z102 = z92;
                    i6 = 0;
                    while (i6 < 2) {
                    }
                    if (hashSet6.isEmpty()) {
                    }
                    if (z3) {
                    }
                    if (z4) {
                    }
                    hashMap2 = hashMap6;
                    hashSet = hashSet3;
                    arrayList8 = arrayList4;
                    hashMap3 = hashMap5;
                    arrayList = arrayList152;
                } else {
                    ArrayList<Long> arrayList18 = arrayList7;
                    ArrayList<Long> arrayList19 = arrayList8;
                    HashMap<Long, TLRPC$InputPeer> hashMap14 = hashMap8;
                    sparseArray = sparseArray2;
                    HashSet<Integer> hashSet9 = hashSet4;
                    i3 = i10;
                    hashMap = hashMap7;
                    HashMap<Integer, HashSet<Long>> hashMap15 = hashMap10;
                    int i18 = 0;
                    MessagesController.DialogFilter dialogFilter3 = new MessagesController.DialogFilter();
                    dialogFilter3.id = tLRPC$DialogFilter2.id;
                    dialogFilter3.flags = i2;
                    dialogFilter3.name = tLRPC$DialogFilter2.title;
                    dialogFilter3.pendingUnreadCount = -1;
                    int i19 = 0;
                    int i20 = 2;
                    while (i19 < i20) {
                        if (i19 == 0) {
                            int size9 = tLRPC$DialogFilter2.pinned_peers.size();
                            int i21 = i18;
                            while (i21 < size9) {
                                TLRPC$InputPeer tLRPC$InputPeer2 = tLRPC$DialogFilter2.pinned_peers.get(i21);
                                int i22 = size9;
                                long j3 = tLRPC$InputPeer2.user_id;
                                if (j3 != 0) {
                                    valueOf = Long.valueOf(j3);
                                } else {
                                    long j4 = tLRPC$InputPeer2.chat_id;
                                    if (j4 == 0) {
                                        j4 = tLRPC$InputPeer2.channel_id;
                                    }
                                    valueOf = Long.valueOf(-j4);
                                }
                                if (!dialogFilter3.alwaysShow.contains(valueOf)) {
                                    dialogFilter3.alwaysShow.add(valueOf);
                                }
                                HashMap<Integer, HashSet<Long>> hashMap16 = hashMap15;
                                dialogFilter3.pinnedDialogs.put(valueOf.longValue(), dialogFilter3.pinnedDialogs.size() + 1);
                                if (!hashMap9.containsKey(valueOf)) {
                                    arrayList9.add(valueOf);
                                    hashMap9.put(valueOf, tLRPC$InputPeer2);
                                }
                                i21++;
                                size9 = i22;
                                hashMap15 = hashMap16;
                            }
                        }
                        HashMap<Integer, HashSet<Long>> hashMap17 = hashMap15;
                        ArrayList<TLRPC$InputPeer> arrayList20 = i19 == 0 ? tLRPC$DialogFilter2.include_peers : tLRPC$DialogFilter2.exclude_peers;
                        ArrayList<Long> arrayList21 = i19 == 0 ? dialogFilter3.alwaysShow : dialogFilter3.neverShow;
                        int size10 = arrayList20.size();
                        int i23 = 0;
                        while (i23 < size10) {
                            TLRPC$InputPeer tLRPC$InputPeer3 = arrayList20.get(i23);
                            TLRPC$DialogFilter tLRPC$DialogFilter3 = tLRPC$DialogFilter2;
                            ArrayList<TLRPC$InputPeer> arrayList22 = arrayList20;
                            long j5 = tLRPC$InputPeer3.user_id;
                            if (j5 != 0) {
                                Long valueOf3 = Long.valueOf(j5);
                                if (!arrayList21.contains(valueOf3)) {
                                    arrayList21.add(valueOf3);
                                }
                                HashMap<Long, TLRPC$InputPeer> hashMap18 = hashMap;
                                if (hashMap18.containsKey(valueOf3)) {
                                    arrayList2 = arrayList18;
                                } else {
                                    arrayList2 = arrayList18;
                                    arrayList2.add(valueOf3);
                                    hashMap18.put(valueOf3, tLRPC$InputPeer3);
                                }
                                hashMap = hashMap18;
                                hashSet2 = hashSet9;
                                arrayList3 = arrayList19;
                                hashMap4 = hashMap14;
                                tLRPC$DialogFilter = tLRPC$DialogFilter3;
                            } else {
                                hashSet2 = hashSet9;
                                arrayList2 = arrayList18;
                                HashMap<Long, TLRPC$InputPeer> hashMap19 = hashMap;
                                long j6 = tLRPC$InputPeer3.chat_id;
                                if (j6 == 0) {
                                    j6 = tLRPC$InputPeer3.channel_id;
                                }
                                Long valueOf4 = Long.valueOf(j6);
                                tLRPC$DialogFilter = tLRPC$DialogFilter3;
                                hashMap = hashMap19;
                                Long valueOf5 = Long.valueOf(-valueOf4.longValue());
                                if (!arrayList21.contains(valueOf5)) {
                                    arrayList21.add(valueOf5);
                                }
                                hashMap4 = hashMap14;
                                if (hashMap4.containsKey(valueOf4)) {
                                    arrayList3 = arrayList19;
                                } else {
                                    arrayList3 = arrayList19;
                                    arrayList3.add(valueOf4);
                                    hashMap4.put(valueOf4, tLRPC$InputPeer3);
                                }
                            }
                            i23++;
                            hashMap14 = hashMap4;
                            arrayList19 = arrayList3;
                            tLRPC$DialogFilter2 = tLRPC$DialogFilter;
                            arrayList20 = arrayList22;
                            arrayList18 = arrayList2;
                            hashSet9 = hashSet2;
                        }
                        i19++;
                        tLRPC$DialogFilter2 = tLRPC$DialogFilter2;
                        hashMap15 = hashMap17;
                        hashSet9 = hashSet9;
                        i20 = 2;
                        i18 = 0;
                    }
                    hashMap2 = hashMap15;
                    hashSet = hashSet9;
                    arrayList8 = arrayList19;
                    hashMap3 = hashMap14;
                    arrayList = arrayList18;
                    arrayList10.add(dialogFilter3);
                }
                i10 = i3 + 1;
                tLRPC$Vector2 = tLRPC$Vector;
                hashMap8 = hashMap3;
                arrayList7 = arrayList;
                size4 = i;
                sparseArray2 = sparseArray;
                arrayList6 = arrayList11;
                hashMap7 = hashMap;
                hashMap10 = hashMap2;
                hashSet4 = hashSet;
            }
            HashMap<Long, TLRPC$InputPeer> hashMap20 = hashMap8;
            SparseArray<MessagesController.DialogFilter> sparseArray3 = sparseArray2;
            ArrayList<Integer> arrayList23 = arrayList6;
            HashMap<Integer, HashSet<Long>> hashMap21 = hashMap10;
            HashSet<Integer> hashSet10 = hashSet4;
            HashMap<Long, TLRPC$InputPeer> hashMap22 = hashMap7;
            ArrayList<Long> arrayList24 = arrayList7;
            if (!arrayList9.isEmpty()) {
                tLRPC$TL_messages_dialogs = loadDialogsByIds(TextUtils.join(",", arrayList9), arrayList24, arrayList8, new ArrayList<>());
                int size11 = tLRPC$TL_messages_dialogs.dialogs.size();
                for (int i24 = 0; i24 < size11; i24++) {
                    hashMap9.remove(Long.valueOf(tLRPC$TL_messages_dialogs.dialogs.get(i24).id));
                }
            } else {
                tLRPC$TL_messages_dialogs = new TLRPC$TL_messages_dialogs();
            }
            TLRPC$messages_Dialogs tLRPC$messages_Dialogs = tLRPC$TL_messages_dialogs;
            ArrayList<TLRPC$User> arrayList25 = new ArrayList<>();
            if (!arrayList24.isEmpty()) {
                getUsersInternal(TextUtils.join(",", arrayList24), arrayList25);
                int size12 = arrayList25.size();
                int i25 = 0;
                while (i25 < size12) {
                    HashMap<Long, TLRPC$InputPeer> hashMap23 = hashMap22;
                    hashMap23.remove(Long.valueOf(arrayList25.get(i25).id));
                    i25++;
                    hashMap22 = hashMap23;
                }
            }
            HashMap<Long, TLRPC$InputPeer> hashMap24 = hashMap22;
            ArrayList<TLRPC$Chat> arrayList26 = new ArrayList<>();
            if (!arrayList8.isEmpty()) {
                getChatsInternal(TextUtils.join(",", arrayList8), arrayList26);
                int size13 = arrayList26.size();
                for (int i26 = 0; i26 < size13; i26++) {
                    hashMap20.remove(Long.valueOf(arrayList26.get(i26).id));
                }
            }
            if (hashMap24.isEmpty() && hashMap20.isEmpty() && hashMap9.isEmpty()) {
                lambda$processLoadedFilterPeers$65(tLRPC$messages_Dialogs, null, arrayList25, arrayList26, arrayList10, sparseArray3, arrayList23, hashMap21, hashSet10, runnable);
            } else {
                getMessagesController().loadFilterPeers(hashMap9, hashMap24, hashMap20, tLRPC$messages_Dialogs, new TLRPC$TL_messages_dialogs(), arrayList25, arrayList26, arrayList10, sparseArray3, arrayList23, hashMap21, hashSet10, runnable);
            }
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public static int lambda$checkLoadedRemoteFilters$62(LongSparseIntArray longSparseIntArray, Long l, Long l2) {
        int i = longSparseIntArray.get(l.longValue());
        int i2 = longSparseIntArray.get(l2.longValue());
        if (i > i2) {
            return 1;
        }
        return i < i2 ? -1 : 0;
    }

    public void lambda$processLoadedFilterPeers$65(TLRPC$messages_Dialogs tLRPC$messages_Dialogs, TLRPC$messages_Dialogs tLRPC$messages_Dialogs2, ArrayList<TLRPC$User> arrayList, ArrayList<TLRPC$Chat> arrayList2, ArrayList<MessagesController.DialogFilter> arrayList3, SparseArray<MessagesController.DialogFilter> sparseArray, ArrayList<Integer> arrayList4, HashMap<Integer, HashSet<Long>> hashMap, HashSet<Integer> hashSet, Runnable runnable) {
        putUsersAndChats(arrayList, arrayList2, true, false);
        int size = sparseArray.size();
        int i = 0;
        boolean z = false;
        while (i < size) {
            lambda$deleteDialogFilter$66(sparseArray.valueAt(i));
            i++;
            z = true;
        }
        Iterator<Integer> it = hashSet.iterator();
        while (it.hasNext()) {
            MessagesController.DialogFilter dialogFilter = this.dialogFiltersMap.get(it.next().intValue());
            if (dialogFilter != null) {
                dialogFilter.pendingUnreadCount = -1;
            }
        }
        for (Map.Entry<Integer, HashSet<Long>> entry : hashMap.entrySet()) {
            MessagesController.DialogFilter dialogFilter2 = this.dialogFiltersMap.get(entry.getKey().intValue());
            if (dialogFilter2 != null) {
                Iterator<Long> it2 = entry.getValue().iterator();
                while (it2.hasNext()) {
                    dialogFilter2.pinnedDialogs.delete(it2.next().longValue());
                }
                z = true;
            }
        }
        int size2 = arrayList3.size();
        int i2 = 0;
        while (i2 < size2) {
            saveDialogFilterInternal(arrayList3.get(i2), false, true);
            i2++;
            z = true;
        }
        int size3 = this.dialogFilters.size();
        boolean z2 = false;
        for (int i3 = 0; i3 < size3; i3++) {
            MessagesController.DialogFilter dialogFilter3 = this.dialogFilters.get(i3);
            int indexOf = arrayList4.indexOf(Integer.valueOf(dialogFilter3.id));
            if (dialogFilter3.order != indexOf) {
                dialogFilter3.order = indexOf;
                z2 = true;
                z = true;
            }
        }
        if (z2) {
            Collections.sort(this.dialogFilters, new Comparator() {
                @Override
                public final int compare(Object obj, Object obj2) {
                    int lambda$processLoadedFilterPeersInternal$64;
                    lambda$processLoadedFilterPeersInternal$64 = MessagesStorage.lambda$processLoadedFilterPeersInternal$64((MessagesController.DialogFilter) obj, (MessagesController.DialogFilter) obj2);
                    return lambda$processLoadedFilterPeersInternal$64;
                }
            });
            saveDialogFiltersOrderInternal();
        }
        int i4 = z ? 1 : 2;
        calcUnreadCounters(true);
        getMessagesController().processLoadedDialogFilters(new ArrayList<>(this.dialogFilters), tLRPC$messages_Dialogs, tLRPC$messages_Dialogs2, arrayList, arrayList2, null, i4, runnable);
    }

    public static int lambda$processLoadedFilterPeersInternal$64(MessagesController.DialogFilter dialogFilter, MessagesController.DialogFilter dialogFilter2) {
        int i = dialogFilter.order;
        int i2 = dialogFilter2.order;
        if (i > i2) {
            return 1;
        }
        return i < i2 ? -1 : 0;
    }

    public void processLoadedFilterPeers(final TLRPC$messages_Dialogs tLRPC$messages_Dialogs, final TLRPC$messages_Dialogs tLRPC$messages_Dialogs2, final ArrayList<TLRPC$User> arrayList, final ArrayList<TLRPC$Chat> arrayList2, final ArrayList<MessagesController.DialogFilter> arrayList3, final SparseArray<MessagesController.DialogFilter> sparseArray, final ArrayList<Integer> arrayList4, final HashMap<Integer, HashSet<Long>> hashMap, final HashSet<Integer> hashSet, final Runnable runnable) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$processLoadedFilterPeers$65(tLRPC$messages_Dialogs, tLRPC$messages_Dialogs2, arrayList, arrayList2, arrayList3, sparseArray, arrayList4, hashMap, hashSet, runnable);
            }
        });
    }

    public void lambda$deleteDialogFilter$66(MessagesController.DialogFilter dialogFilter) {
        try {
            this.dialogFilters.remove(dialogFilter);
            this.dialogFiltersMap.remove(dialogFilter.id);
            this.database.executeFast("DELETE FROM dialog_filter WHERE id = " + dialogFilter.id).stepThis().dispose();
            this.database.executeFast("DELETE FROM dialog_filter_ep WHERE id = " + dialogFilter.id).stepThis().dispose();
            this.database.executeFast("DELETE FROM dialog_filter_pin_v2 WHERE id = " + dialogFilter.id).stepThis().dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void deleteDialogFilter(final MessagesController.DialogFilter dialogFilter) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$deleteDialogFilter$66(dialogFilter);
            }
        });
    }

    public void saveDialogFilter(final MessagesController.DialogFilter dialogFilter, final boolean z, final boolean z2) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$saveDialogFilter$68(dialogFilter, z, z2);
            }
        });
    }

    public void lambda$saveDialogFilter$68(MessagesController.DialogFilter dialogFilter, boolean z, boolean z2) {
        saveDialogFilterInternal(dialogFilter, z, z2);
        calcUnreadCounters(false);
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$saveDialogFilter$67();
            }
        });
    }

    public void lambda$saveDialogFilter$67() {
        ArrayList<MessagesController.DialogFilter> arrayList = getMessagesController().dialogFilters;
        int size = arrayList.size();
        for (int i = 0; i < size; i++) {
            arrayList.get(i).unreadCount = arrayList.get(i).pendingUnreadCount;
        }
        this.mainUnreadCount = this.pendingMainUnreadCount;
        this.archiveUnreadCount = this.pendingArchiveUnreadCount;
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.updateInterfaces, Integer.valueOf(MessagesController.UPDATE_MASK_READ_DIALOG_MESSAGE));
    }

    public void saveDialogFiltersOrderInternal() {
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                sQLitePreparedStatement = this.database.executeFast("UPDATE dialog_filter SET ord = ?, flags = ? WHERE id = ?");
                int size = this.dialogFilters.size();
                for (int i = 0; i < size; i++) {
                    MessagesController.DialogFilter dialogFilter = this.dialogFilters.get(i);
                    sQLitePreparedStatement.requery();
                    sQLitePreparedStatement.bindInteger(1, dialogFilter.order);
                    sQLitePreparedStatement.bindInteger(2, dialogFilter.flags);
                    sQLitePreparedStatement.bindInteger(3, dialogFilter.id);
                    sQLitePreparedStatement.step();
                }
                sQLitePreparedStatement.dispose();
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLitePreparedStatement != null) {
                    sQLitePreparedStatement.dispose();
                }
            }
        } catch (Throwable th) {
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void saveDialogFiltersOrder() {
        final ArrayList arrayList = new ArrayList(getMessagesController().dialogFilters);
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$saveDialogFiltersOrder$69(arrayList);
            }
        });
    }

    public void lambda$saveDialogFiltersOrder$69(ArrayList arrayList) {
        this.dialogFilters.clear();
        this.dialogFiltersMap.clear();
        this.dialogFilters.addAll(arrayList);
        for (int i = 0; i < arrayList.size(); i++) {
            ((MessagesController.DialogFilter) arrayList.get(i)).order = i;
            this.dialogFiltersMap.put(((MessagesController.DialogFilter) arrayList.get(i)).id, (MessagesController.DialogFilter) arrayList.get(i));
        }
        saveDialogFiltersOrderInternal();
    }

    public static void addReplyMessages(TLRPC$Message tLRPC$Message, LongSparseArray<SparseArray<ArrayList<TLRPC$Message>>> longSparseArray, LongSparseArray<ArrayList<Integer>> longSparseArray2) {
        int i = tLRPC$Message.reply_to.reply_to_msg_id;
        long replyToDialogId = MessageObject.getReplyToDialogId(tLRPC$Message);
        SparseArray<ArrayList<TLRPC$Message>> sparseArray = longSparseArray.get(replyToDialogId);
        ArrayList<Integer> arrayList = longSparseArray2.get(replyToDialogId);
        if (sparseArray == null) {
            sparseArray = new SparseArray<>();
            longSparseArray.put(replyToDialogId, sparseArray);
        }
        if (arrayList == null) {
            arrayList = new ArrayList<>();
            longSparseArray2.put(replyToDialogId, arrayList);
        }
        ArrayList<TLRPC$Message> arrayList2 = sparseArray.get(tLRPC$Message.reply_to.reply_to_msg_id);
        if (arrayList2 == null) {
            arrayList2 = new ArrayList<>();
            sparseArray.put(tLRPC$Message.reply_to.reply_to_msg_id, arrayList2);
            if (!arrayList.contains(Integer.valueOf(tLRPC$Message.reply_to.reply_to_msg_id))) {
                arrayList.add(Integer.valueOf(tLRPC$Message.reply_to.reply_to_msg_id));
            }
        }
        arrayList2.add(tLRPC$Message);
    }

    public void loadReplyMessages(LongSparseArray<SparseArray<ArrayList<TLRPC$Message>>> longSparseArray, LongSparseArray<ArrayList<Integer>> longSparseArray2, ArrayList<Long> arrayList, ArrayList<Long> arrayList2, boolean z) throws SQLiteException {
        ?? r14;
        LongSparseArray<SparseArray<ArrayList<TLRPC$Message>>> longSparseArray3 = longSparseArray;
        if (longSparseArray.isEmpty()) {
            return;
        }
        int size = longSparseArray.size();
        int i = 0;
        while (i < size) {
            long keyAt = longSparseArray3.keyAt(i);
            SparseArray<ArrayList<TLRPC$Message>> valueAt = longSparseArray3.valueAt(i);
            ArrayList<Integer> arrayList3 = longSparseArray2.get(keyAt);
            if (arrayList3 != null) {
                int i2 = 0;
                SQLiteCursor sQLiteCursor = 0;
                while (i2 < 2) {
                    if (i2 != 1 || z) {
                        if (i2 == 1) {
                            try {
                                try {
                                    sQLiteCursor = this.database.queryFinalized(String.format(Locale.US, "SELECT data, mid, date, uid FROM scheduled_messages_v2 WHERE mid IN(%s) AND uid = %d", TextUtils.join(",", arrayList3), Long.valueOf(keyAt)), new Object[0]);
                                    r14 = 0;
                                    sQLiteCursor = sQLiteCursor;
                                    if (sQLiteCursor.next()) {
                                        NativeByteBuffer byteBufferValue = sQLiteCursor.byteBufferValue(r14);
                                        if (byteBufferValue != 0) {
                                            TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(r14), r14);
                                            TLdeserialize.readAttachPath(byteBufferValue, getUserConfig().clientUserId);
                                            byteBufferValue.reuse();
                                            TLdeserialize.id = sQLiteCursor.intValue(1);
                                            TLdeserialize.date = sQLiteCursor.intValue(2);
                                            TLdeserialize.dialog_id = sQLiteCursor.longValue(3);
                                            addUsersAndChatsFromMessage(TLdeserialize, arrayList, arrayList2, null);
                                            ArrayList<TLRPC$Message> arrayList4 = valueAt.get(TLdeserialize.id);
                                            if (arrayList4 != null) {
                                                int size2 = arrayList4.size();
                                                int i3 = 0;
                                                while (i3 < size2) {
                                                    arrayList4.get(i3).replyMessage = TLdeserialize;
                                                    MessageObject.getDialogId(TLdeserialize);
                                                    i3++;
                                                    arrayList4 = arrayList4;
                                                }
                                            }
                                        }
                                        r14 = 0;
                                        sQLiteCursor = sQLiteCursor;
                                        if (sQLiteCursor.next()) {
                                        }
                                    } else {
                                        sQLiteCursor.dispose();
                                    }
                                } catch (Exception e) {
                                    throw e;
                                }
                            } finally {
                                if (sQLiteCursor != 0) {
                                    sQLiteCursor.dispose();
                                }
                            }
                        } else {
                            r14 = 0;
                            sQLiteCursor = this.database.queryFinalized(String.format(Locale.US, "SELECT data, mid, date, uid FROM messages_v2 WHERE mid IN(%s) AND uid = %d", TextUtils.join(",", arrayList3), Long.valueOf(keyAt)), new Object[0]);
                            if (sQLiteCursor.next()) {
                            }
                        }
                    }
                    i2++;
                    sQLiteCursor = sQLiteCursor;
                }
            }
            i++;
            longSparseArray3 = longSparseArray;
        }
    }

    public void loadUnreadMessages() {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$loadUnreadMessages$71();
            }
        });
    }

    public void lambda$loadUnreadMessages$71() {
        Throwable th;
        SQLiteCursor sQLiteCursor;
        Exception exc;
        Throwable th2;
        ArrayList<Long> arrayList;
        ArrayList<Long> arrayList2;
        ArrayList arrayList3;
        LongSparseArray longSparseArray;
        SQLiteCursor queryFinalized;
        ArrayList arrayList4;
        ArrayList<TLRPC$EncryptedChat> arrayList5;
        ArrayList arrayList6;
        LongSparseArray longSparseArray2;
        final ArrayList<TLRPC$User> arrayList7;
        ArrayList<TLRPC$Chat> arrayList8;
        CharSequence charSequence;
        LongSparseArray longSparseArray3;
        ArrayList arrayList9;
        ArrayList<TLRPC$EncryptedChat> arrayList10;
        LongSparseArray longSparseArray4;
        ArrayList arrayList11;
        String stringValue;
        int i;
        LongSparseArray longSparseArray5;
        ArrayList<TLRPC$User> arrayList12;
        ArrayList<TLRPC$Chat> arrayList13;
        ArrayList arrayList14;
        int i2;
        TLRPC$MessageReplyHeader tLRPC$MessageReplyHeader;
        NativeByteBuffer byteBufferValue;
        try {
            arrayList = new ArrayList<>();
            arrayList2 = new ArrayList<>();
            arrayList3 = new ArrayList();
            longSparseArray = new LongSparseArray();
            queryFinalized = this.database.queryFinalized("SELECT d.did, d.unread_count, s.flags FROM dialogs as d LEFT JOIN dialog_settings as s ON d.did = s.did WHERE d.unread_count > 0", new Object[0]);
        } catch (Exception e) {
            exc = e;
            sQLiteCursor = null;
        } catch (Throwable th3) {
            th = th3;
            sQLiteCursor = null;
        }
        try {
            StringBuilder sb = new StringBuilder();
            int currentTime = getConnectionsManager().getCurrentTime();
            while (queryFinalized.next()) {
                long longValue = queryFinalized.longValue(2);
                boolean z = (longValue & 1) != 0;
                int i3 = (int) (longValue >> 32);
                if (queryFinalized.isNull(2) || !z || (i3 != 0 && i3 < currentTime)) {
                    long longValue2 = queryFinalized.longValue(0);
                    if (!DialogObject.isFolderDialogId(longValue2)) {
                        longSparseArray.put(longValue2, Integer.valueOf(queryFinalized.intValue(1)));
                        if (sb.length() != 0) {
                            sb.append(",");
                        }
                        sb.append(longValue2);
                        if (DialogObject.isEncryptedDialog(longValue2)) {
                            int encryptedChatId = DialogObject.getEncryptedChatId(longValue2);
                            if (!arrayList3.contains(Integer.valueOf(encryptedChatId))) {
                                arrayList3.add(Integer.valueOf(encryptedChatId));
                            }
                        } else if (DialogObject.isUserDialog(longValue2)) {
                            if (!arrayList.contains(Long.valueOf(longValue2))) {
                                arrayList.add(Long.valueOf(longValue2));
                            }
                        } else {
                            long j = -longValue2;
                            if (!arrayList2.contains(Long.valueOf(j))) {
                                arrayList2.add(Long.valueOf(j));
                            }
                        }
                    }
                }
            }
            queryFinalized.dispose();
            LongSparseArray<SparseArray<ArrayList<TLRPC$Message>>> longSparseArray6 = new LongSparseArray<>();
            LongSparseArray<ArrayList<Integer>> longSparseArray7 = new LongSparseArray<>();
            ArrayList arrayList15 = new ArrayList();
            ArrayList arrayList16 = new ArrayList();
            ArrayList<TLRPC$User> arrayList17 = new ArrayList<>();
            ArrayList<TLRPC$Chat> arrayList18 = new ArrayList<>();
            ArrayList<TLRPC$EncryptedChat> arrayList19 = new ArrayList<>();
            if (sb.length() > 0) {
                ?? queryFinalized2 = this.database.queryFinalized("SELECT read_state, data, send_state, mid, date, uid, replydata FROM messages_v2 WHERE uid IN (" + sb.toString() + ") AND out = 0 AND read_state IN(0,2) ORDER BY date DESC LIMIT 50", new Object[0]);
                int i4 = 0;
                while (queryFinalized2.next()) {
                    try {
                        try {
                            NativeByteBuffer byteBufferValue2 = queryFinalized2.byteBufferValue(1);
                            if (byteBufferValue2 != null) {
                                longSparseArray5 = longSparseArray;
                                TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue2, byteBufferValue2.readInt32(false), false);
                                arrayList12 = arrayList17;
                                arrayList13 = arrayList18;
                                TLdeserialize.readAttachPath(byteBufferValue2, getUserConfig().clientUserId);
                                byteBufferValue2.reuse();
                                MessageObject.setUnreadFlags(TLdeserialize, queryFinalized2.intValue(0));
                                TLdeserialize.id = queryFinalized2.intValue(3);
                                TLdeserialize.date = queryFinalized2.intValue(4);
                                TLdeserialize.dialog_id = queryFinalized2.longValue(5);
                                arrayList15.add(TLdeserialize);
                                int max = Math.max(i4, TLdeserialize.date);
                                addUsersAndChatsFromMessage(TLdeserialize, arrayList, arrayList2, null);
                                TLdeserialize.send_state = queryFinalized2.intValue(2);
                                if ((TLdeserialize.peer_id.channel_id == 0 && !MessageObject.isUnread(TLdeserialize) && !DialogObject.isEncryptedDialog(TLdeserialize.dialog_id)) || TLdeserialize.id > 0) {
                                    TLdeserialize.send_state = 0;
                                }
                                if (DialogObject.isEncryptedDialog(TLdeserialize.dialog_id) && !queryFinalized2.isNull(5)) {
                                    TLdeserialize.random_id = queryFinalized2.longValue(5);
                                }
                                try {
                                    tLRPC$MessageReplyHeader = TLdeserialize.reply_to;
                                } catch (Exception e2) {
                                    e = e2;
                                    i2 = max;
                                    arrayList14 = arrayList16;
                                }
                                if (tLRPC$MessageReplyHeader != null && tLRPC$MessageReplyHeader.reply_to_msg_id != 0) {
                                    TLRPC$MessageAction tLRPC$MessageAction = TLdeserialize.action;
                                    if ((tLRPC$MessageAction instanceof TLRPC$TL_messageActionPinMessage) || (tLRPC$MessageAction instanceof TLRPC$TL_messageActionPaymentSent) || (tLRPC$MessageAction instanceof TLRPC$TL_messageActionGameScore)) {
                                        if (queryFinalized2.isNull(6) || (byteBufferValue = queryFinalized2.byteBufferValue(6)) == null) {
                                            i2 = max;
                                            arrayList14 = arrayList16;
                                        } else {
                                            TLRPC$Message TLdeserialize2 = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                                            TLdeserialize.replyMessage = TLdeserialize2;
                                            i2 = max;
                                            arrayList14 = arrayList16;
                                            try {
                                                TLdeserialize2.readAttachPath(byteBufferValue, getUserConfig().clientUserId);
                                                byteBufferValue.reuse();
                                                TLRPC$Message tLRPC$Message = TLdeserialize.replyMessage;
                                                if (tLRPC$Message != null) {
                                                    addUsersAndChatsFromMessage(tLRPC$Message, arrayList, arrayList2, null);
                                                }
                                            } catch (Exception e3) {
                                                e = e3;
                                                checkSQLException(e);
                                                i4 = i2;
                                                arrayList16 = arrayList14;
                                                longSparseArray = longSparseArray5;
                                                arrayList17 = arrayList12;
                                                arrayList18 = arrayList13;
                                            }
                                        }
                                        if (TLdeserialize.replyMessage == null) {
                                            addReplyMessages(TLdeserialize, longSparseArray6, longSparseArray7);
                                        }
                                        i4 = i2;
                                    }
                                }
                                i2 = max;
                                arrayList14 = arrayList16;
                                i4 = i2;
                            } else {
                                longSparseArray5 = longSparseArray;
                                arrayList12 = arrayList17;
                                arrayList13 = arrayList18;
                                arrayList14 = arrayList16;
                            }
                            arrayList16 = arrayList14;
                            longSparseArray = longSparseArray5;
                            arrayList17 = arrayList12;
                            arrayList18 = arrayList13;
                        } catch (Throwable th4) {
                            th = th4;
                            sQLiteCursor = queryFinalized2;
                            if (sQLiteCursor != null) {
                            }
                        }
                    } catch (Exception e4) {
                        exc = e4;
                        sQLiteCursor = queryFinalized2;
                        try {
                            checkSQLException(exc);
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                                return;
                            }
                            return;
                        } catch (Throwable th5) {
                            th2 = th5;
                            th = th2;
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                                throw th;
                            }
                            throw th;
                        }
                    }
                }
                LongSparseArray longSparseArray8 = longSparseArray;
                ArrayList<TLRPC$User> arrayList20 = arrayList17;
                ArrayList<TLRPC$Chat> arrayList21 = arrayList18;
                ArrayList arrayList22 = arrayList16;
                queryFinalized2.dispose();
                this.database.executeFast("DELETE FROM unread_push_messages WHERE date <= " + i4).stepThis().dispose();
                ?? r6 = 0;
                queryFinalized2 = this.database.queryFinalized("SELECT data, mid, date, uid, random, fm, name, uname, flags, topicId, is_reaction FROM unread_push_messages WHERE 1 ORDER BY date DESC LIMIT 50", new Object[0]);
                while (queryFinalized2.next()) {
                    ?? byteBufferValue3 = queryFinalized2.byteBufferValue(r6);
                    if (byteBufferValue3 != 0) {
                        TLRPC$Message TLdeserialize3 = TLRPC$Message.TLdeserialize(byteBufferValue3, byteBufferValue3.readInt32(r6), r6);
                        byteBufferValue3.reuse();
                        TLdeserialize3.id = queryFinalized2.intValue(1);
                        TLdeserialize3.date = queryFinalized2.intValue(2);
                        TLdeserialize3.dialog_id = queryFinalized2.longValue(3);
                        TLdeserialize3.random_id = queryFinalized2.longValue(4);
                        if (queryFinalized2.isNull(5)) {
                            i = 6;
                            stringValue = null;
                        } else {
                            stringValue = queryFinalized2.stringValue(5);
                            i = 6;
                        }
                        String stringValue2 = queryFinalized2.isNull(i) ? null : queryFinalized2.stringValue(i);
                        String stringValue3 = queryFinalized2.isNull(7) ? null : queryFinalized2.stringValue(7);
                        int intValue = queryFinalized2.intValue(8);
                        int intValue2 = queryFinalized2.intValue(9);
                        if (MessageObject.getFromChatId(TLdeserialize3) == 0 && DialogObject.isUserDialog(TLdeserialize3.dialog_id)) {
                            TLRPC$TL_peerUser tLRPC$TL_peerUser = new TLRPC$TL_peerUser();
                            TLdeserialize3.from_id = tLRPC$TL_peerUser;
                            arrayList11 = arrayList22;
                            tLRPC$TL_peerUser.user_id = TLdeserialize3.dialog_id;
                        } else {
                            arrayList11 = arrayList22;
                        }
                        if (DialogObject.isUserDialog(TLdeserialize3.dialog_id)) {
                            if (!arrayList.contains(Long.valueOf(TLdeserialize3.dialog_id))) {
                                arrayList.add(Long.valueOf(TLdeserialize3.dialog_id));
                            }
                        } else if (DialogObject.isChatDialog(TLdeserialize3.dialog_id) && !arrayList2.contains(Long.valueOf(-TLdeserialize3.dialog_id))) {
                            arrayList2.add(Long.valueOf(-TLdeserialize3.dialog_id));
                        }
                        if (intValue2 != 0) {
                            TLRPC$TL_messageReplyHeader tLRPC$TL_messageReplyHeader = new TLRPC$TL_messageReplyHeader();
                            TLdeserialize3.reply_to = tLRPC$TL_messageReplyHeader;
                            tLRPC$TL_messageReplyHeader.forum_topic = true;
                            tLRPC$TL_messageReplyHeader.reply_to_top_id = intValue2;
                        }
                        MessageObject messageObject = new MessageObject(this.currentAccount, TLdeserialize3, stringValue, stringValue2, stringValue3, (intValue & 1) != 0, (intValue & 2) != 0, (TLdeserialize3.flags & Integer.MIN_VALUE) != 0, false);
                        messageObject.isReactionPush = queryFinalized2.intValue(10) != 0;
                        arrayList11.add(messageObject);
                        addUsersAndChatsFromMessage(TLdeserialize3, arrayList, arrayList2, null);
                    } else {
                        arrayList11 = arrayList22;
                    }
                    arrayList22 = arrayList11;
                    r6 = 0;
                }
                ArrayList arrayList23 = arrayList22;
                queryFinalized2.dispose();
                arrayList4 = arrayList15;
                ArrayList<TLRPC$EncryptedChat> arrayList24 = arrayList19;
                arrayList6 = arrayList23;
                charSequence = ",";
                loadReplyMessages(longSparseArray6, longSparseArray7, arrayList, arrayList2, false);
                if (!arrayList3.isEmpty()) {
                    getEncryptedChatsInternal(TextUtils.join(charSequence, arrayList3), arrayList24, arrayList);
                }
                if (arrayList.isEmpty()) {
                    arrayList7 = arrayList20;
                } else {
                    arrayList7 = arrayList20;
                    getUsersInternal(TextUtils.join(charSequence, arrayList), arrayList7);
                }
                if (arrayList2.isEmpty()) {
                    arrayList5 = arrayList24;
                    longSparseArray2 = longSparseArray8;
                    arrayList8 = arrayList21;
                } else {
                    arrayList8 = arrayList21;
                    getChatsInternal(TextUtils.join(charSequence, arrayList2), arrayList8);
                    int i5 = 0;
                    while (i5 < arrayList8.size()) {
                        TLRPC$Chat tLRPC$Chat = arrayList8.get(i5);
                        if (tLRPC$Chat == null || !(ChatObject.isNotInChat(tLRPC$Chat) || tLRPC$Chat.min || tLRPC$Chat.migrated_to != null)) {
                            arrayList10 = arrayList24;
                            longSparseArray4 = longSparseArray8;
                        } else {
                            long j2 = -tLRPC$Chat.id;
                            this.database.executeFast("UPDATE dialogs SET unread_count = 0 WHERE did = " + j2).stepThis().dispose();
                            arrayList10 = arrayList24;
                            this.database.executeFast(String.format(Locale.US, "UPDATE messages_v2 SET read_state = 3 WHERE uid = %d AND mid > 0 AND read_state IN(0,2) AND out = 0", Long.valueOf(j2))).stepThis().dispose();
                            arrayList8.remove(i5);
                            i5--;
                            longSparseArray4 = longSparseArray8;
                            longSparseArray4.remove(j2);
                            int i6 = 0;
                            while (i6 < arrayList4.size()) {
                                if (((TLRPC$Message) arrayList4.get(i6)).dialog_id == j2) {
                                    arrayList4.remove(i6);
                                    i6--;
                                }
                                i6++;
                            }
                        }
                        i5++;
                        longSparseArray8 = longSparseArray4;
                        arrayList24 = arrayList10;
                    }
                    arrayList5 = arrayList24;
                    longSparseArray2 = longSparseArray8;
                }
            } else {
                arrayList4 = arrayList15;
                arrayList5 = arrayList19;
                arrayList6 = arrayList16;
                longSparseArray2 = longSparseArray;
                arrayList7 = arrayList17;
                arrayList8 = arrayList18;
                charSequence = ",";
            }
            Collections.reverse(arrayList4);
            arrayList.clear();
            arrayList2.clear();
            queryFinalized = this.database.queryFinalized("SELECT uid, sid, date, expire_date, localName, flags FROM story_pushes", new Object[0]);
            final HashMap hashMap = new HashMap();
            while (queryFinalized.next()) {
                long longValue3 = queryFinalized.longValue(0);
                if (longValue3 >= 0) {
                    if (!arrayList.contains(Long.valueOf(longValue3))) {
                        arrayList.add(Long.valueOf(longValue3));
                    }
                } else if (!arrayList2.contains(Long.valueOf(longValue3))) {
                    arrayList2.add(Long.valueOf(longValue3));
                }
                int intValue3 = queryFinalized.intValue(1);
                long longValue4 = queryFinalized.longValue(2);
                long longValue5 = queryFinalized.longValue(3);
                String stringValue4 = queryFinalized.stringValue(4);
                int intValue4 = queryFinalized.intValue(5);
                NotificationsController.StoryNotification storyNotification = (NotificationsController.StoryNotification) hashMap.get(Long.valueOf(longValue3));
                if (storyNotification != null) {
                    arrayList9 = arrayList4;
                    longSparseArray3 = longSparseArray2;
                    storyNotification.dateByIds.put(Integer.valueOf(intValue3), new Pair<>(Long.valueOf(longValue4), Long.valueOf(longValue5)));
                    storyNotification.date = storyNotification.getLeastDate();
                    storyNotification.hidden |= (intValue4 & 1) != 0;
                    if (!TextUtils.isEmpty(stringValue4)) {
                        storyNotification.localName = stringValue4;
                    }
                } else {
                    longSparseArray3 = longSparseArray2;
                    arrayList9 = arrayList4;
                    NotificationsController.StoryNotification storyNotification2 = new NotificationsController.StoryNotification(longValue3, stringValue4, intValue3, longValue4, longValue5);
                    storyNotification2.hidden = (intValue4 & 1) != 0;
                    hashMap.put(Long.valueOf(longValue3), storyNotification2);
                }
                arrayList4 = arrayList9;
                longSparseArray2 = longSparseArray3;
            }
            final LongSparseArray longSparseArray9 = longSparseArray2;
            final ArrayList arrayList25 = arrayList4;
            queryFinalized.dispose();
            if (!arrayList.isEmpty()) {
                getUsersInternal(TextUtils.join(charSequence, arrayList), arrayList7);
            }
            if (!arrayList2.isEmpty()) {
                getChatsInternal(TextUtils.join(charSequence, arrayList2), arrayList8);
            }
            final ArrayList arrayList26 = arrayList6;
            final ArrayList<TLRPC$Chat> arrayList27 = arrayList8;
            final ArrayList<TLRPC$EncryptedChat> arrayList28 = arrayList5;
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$loadUnreadMessages$70(longSparseArray9, arrayList25, arrayList26, arrayList7, arrayList27, arrayList28, hashMap);
                }
            });
        } catch (Exception e5) {
            sQLiteCursor = queryFinalized;
            exc = e5;
        } catch (Throwable th6) {
            th2 = th6;
            sQLiteCursor = queryFinalized;
            th = th2;
            if (sQLiteCursor != null) {
            }
        }
    }

    public void lambda$loadUnreadMessages$70(LongSparseArray longSparseArray, ArrayList arrayList, ArrayList arrayList2, ArrayList arrayList3, ArrayList arrayList4, ArrayList arrayList5, HashMap hashMap) {
        getNotificationsController().processLoadedUnreadMessages(longSparseArray, arrayList, arrayList2, arrayList3, arrayList4, arrayList5, hashMap.values());
    }

    public void putWallpapers(final ArrayList<TLRPC$WallPaper> arrayList, final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$putWallpapers$72(i, arrayList);
            }
        });
    }

    public void lambda$putWallpapers$72(int i, ArrayList arrayList) {
        Throwable th;
        SQLiteDatabase sQLiteDatabase;
        Exception e;
        SQLiteDatabase sQLiteDatabase2;
        SQLitePreparedStatement executeFast;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        if (i == 1) {
            try {
                try {
                    this.database.executeFast("DELETE FROM wallpapers2 WHERE num >= -1").stepThis().dispose();
                } catch (Exception e2) {
                    e = e2;
                    checkSQLException(e);
                    sQLiteDatabase2 = this.database;
                    if (sQLiteDatabase2 != null) {
                        sQLiteDatabase2.commitTransaction();
                    }
                    if (sQLitePreparedStatement == null) {
                        sQLitePreparedStatement.dispose();
                        return;
                    }
                    return;
                }
            } catch (Throwable th2) {
                th = th2;
                sQLiteDatabase = this.database;
                if (sQLiteDatabase != null) {
                    sQLiteDatabase.commitTransaction();
                }
                if (sQLitePreparedStatement != null) {
                    sQLitePreparedStatement.dispose();
                }
                throw th;
            }
        }
        this.database.beginTransaction();
        if (i != 0) {
            executeFast = this.database.executeFast("REPLACE INTO wallpapers2 VALUES(?, ?, ?)");
        } else {
            executeFast = this.database.executeFast("UPDATE wallpapers2 SET data = ? WHERE uid = ?");
        }
        try {
            int size = arrayList.size();
            for (int i2 = 0; i2 < size; i2++) {
                TLRPC$WallPaper tLRPC$WallPaper = (TLRPC$WallPaper) arrayList.get(i2);
                executeFast.requery();
                NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$WallPaper.getObjectSize());
                tLRPC$WallPaper.serializeToStream(nativeByteBuffer);
                if (i != 0) {
                    executeFast.bindLong(1, tLRPC$WallPaper.id);
                    executeFast.bindByteBuffer(2, nativeByteBuffer);
                    if (i < 0) {
                        executeFast.bindInteger(3, i);
                    } else {
                        executeFast.bindInteger(3, i == 2 ? -1 : i2);
                    }
                } else {
                    executeFast.bindByteBuffer(1, nativeByteBuffer);
                    executeFast.bindLong(2, tLRPC$WallPaper.id);
                }
                executeFast.step();
                nativeByteBuffer.reuse();
            }
            executeFast.dispose();
            this.database.commitTransaction();
            SQLiteDatabase sQLiteDatabase3 = this.database;
            if (sQLiteDatabase3 != null) {
                sQLiteDatabase3.commitTransaction();
            }
        } catch (Exception e3) {
            e = e3;
            sQLitePreparedStatement = executeFast;
            checkSQLException(e);
            sQLiteDatabase2 = this.database;
            if (sQLiteDatabase2 != null) {
            }
            if (sQLitePreparedStatement == null) {
            }
        } catch (Throwable th3) {
            th = th3;
            sQLitePreparedStatement = executeFast;
            sQLiteDatabase = this.database;
            if (sQLiteDatabase != null) {
            }
            if (sQLitePreparedStatement != null) {
            }
            throw th;
        }
    }

    public void deleteWallpaper(final long j) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$deleteWallpaper$73(j);
            }
        });
    }

    public void lambda$deleteWallpaper$73(long j) {
        try {
            this.database.executeFast("DELETE FROM wallpapers2 WHERE uid = " + j).stepThis().dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void getWallpapers() {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getWallpapers$75();
            }
        });
    }

    public void lambda$getWallpapers$75() {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                sQLiteCursor = this.database.queryFinalized("SELECT data FROM wallpapers2 WHERE 1 ORDER BY num ASC", new Object[0]);
                final ArrayList arrayList = new ArrayList();
                while (sQLiteCursor.next()) {
                    NativeByteBuffer byteBufferValue = sQLiteCursor.byteBufferValue(0);
                    if (byteBufferValue != null) {
                        TLRPC$WallPaper TLdeserialize = TLRPC$WallPaper.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                        byteBufferValue.reuse();
                        if (TLdeserialize != null) {
                            arrayList.add(TLdeserialize);
                        }
                    }
                }
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.lambda$getWallpapers$74(arrayList);
                    }
                });
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLiteCursor == null) {
                    return;
                }
            }
            sQLiteCursor.dispose();
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public static void lambda$getWallpapers$74(ArrayList arrayList) {
        NotificationCenter.getGlobalInstance().lambda$postNotificationNameOnUIThread$1(NotificationCenter.wallpapersDidLoad, arrayList);
    }

    public void addRecentLocalFile(final String str, final String str2, final TLRPC$Document tLRPC$Document) {
        if (str == null || str.length() == 0) {
            return;
        }
        if ((str2 == null || str2.length() == 0) && tLRPC$Document == null) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$addRecentLocalFile$76(tLRPC$Document, str, str2);
            }
        });
    }

    public void lambda$addRecentLocalFile$76(TLRPC$Document tLRPC$Document, String str, String str2) {
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                if (tLRPC$Document != null) {
                    sQLitePreparedStatement = this.database.executeFast("UPDATE web_recent_v3 SET document = ? WHERE image_url = ?");
                    sQLitePreparedStatement.requery();
                    NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$Document.getObjectSize());
                    tLRPC$Document.serializeToStream(nativeByteBuffer);
                    sQLitePreparedStatement.bindByteBuffer(1, nativeByteBuffer);
                    sQLitePreparedStatement.bindString(2, str);
                    sQLitePreparedStatement.step();
                    sQLitePreparedStatement.dispose();
                    nativeByteBuffer.reuse();
                } else {
                    sQLitePreparedStatement = this.database.executeFast("UPDATE web_recent_v3 SET local_url = ? WHERE image_url = ?");
                    sQLitePreparedStatement.requery();
                    sQLitePreparedStatement.bindString(1, str2);
                    sQLitePreparedStatement.bindString(2, str);
                    sQLitePreparedStatement.step();
                    sQLitePreparedStatement.dispose();
                }
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLitePreparedStatement == null) {
                    return;
                }
            }
            sQLitePreparedStatement.dispose();
        } catch (Throwable th) {
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void deleteUserChatHistory(final long j, final long j2) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$deleteUserChatHistory$79(j, j2);
            }
        });
    }

    public void lambda$deleteUserChatHistory$79(final long j, long j2) {
        SQLiteCursor sQLiteCursor;
        boolean z;
        ArrayList<Pair<Long, Integer>> arrayList;
        try {
            final ArrayList<Integer> arrayList2 = new ArrayList<>();
            ?? r13 = 0;
            SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT data FROM messages_v2 WHERE uid = " + j, new Object[0]);
            try {
                try {
                    ArrayList<File> arrayList3 = new ArrayList<>();
                    final ArrayList<String> arrayList4 = new ArrayList<>();
                    ArrayList<Pair<Long, Integer>> arrayList5 = new ArrayList<>();
                    while (queryFinalized.next()) {
                        try {
                            NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(r13);
                            if (byteBufferValue != 0) {
                                TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(r13), r13);
                                if (TLdeserialize != null) {
                                    TLdeserialize.readAttachPath(byteBufferValue, getUserConfig().clientUserId);
                                    if ((UserObject.isReplyUser(j) && MessageObject.getPeerId(TLdeserialize.fwd_from.from_id) == j2) || (MessageObject.getFromChatId(TLdeserialize) == j2 && TLdeserialize.id != 1)) {
                                        arrayList2.add(Integer.valueOf(TLdeserialize.id));
                                        z = true;
                                        arrayList = arrayList5;
                                        try {
                                            addFilesToDelete(TLdeserialize, arrayList3, arrayList5, arrayList4, false);
                                            byteBufferValue.reuse();
                                        } catch (Exception e) {
                                            e = e;
                                            checkSQLException(e);
                                            queryFinalized.dispose();
                                            deleteFromDownloadQueue(arrayList, z);
                                            AndroidUtilities.runOnUIThread(new Runnable() {
                                                @Override
                                                public final void run() {
                                                    MessagesStorage.this.lambda$deleteUserChatHistory$77(arrayList4, j, arrayList2);
                                                }
                                            });
                                            lambda$markMessagesAsDeleted$199(j, arrayList2, false, false);
                                            lambda$updateDialogsWithDeletedMessages$198(j, !DialogObject.isChatDialog(j) ? -j : 0L, arrayList2, null);
                                            getFileLoader().deleteFiles(arrayList3, 0);
                                            if (arrayList2.isEmpty()) {
                                            }
                                        }
                                    }
                                }
                                z = true;
                                arrayList = arrayList5;
                                byteBufferValue.reuse();
                            } else {
                                arrayList = arrayList5;
                            }
                            arrayList5 = arrayList;
                            r13 = 0;
                        } catch (Exception e2) {
                            e = e2;
                            z = true;
                            arrayList = arrayList5;
                        }
                    }
                    z = true;
                    arrayList = arrayList5;
                    queryFinalized.dispose();
                    deleteFromDownloadQueue(arrayList, z);
                    AndroidUtilities.runOnUIThread(new Runnable() {
                        @Override
                        public final void run() {
                            MessagesStorage.this.lambda$deleteUserChatHistory$77(arrayList4, j, arrayList2);
                        }
                    });
                    lambda$markMessagesAsDeleted$199(j, arrayList2, false, false);
                    lambda$updateDialogsWithDeletedMessages$198(j, !DialogObject.isChatDialog(j) ? -j : 0L, arrayList2, null);
                    getFileLoader().deleteFiles(arrayList3, 0);
                    if (arrayList2.isEmpty()) {
                        AndroidUtilities.runOnUIThread(new Runnable() {
                            @Override
                            public final void run() {
                                MessagesStorage.this.lambda$deleteUserChatHistory$78(arrayList2, j);
                            }
                        });
                    }
                } catch (Exception e3) {
                    e = e3;
                    sQLiteCursor = queryFinalized;
                    try {
                        checkSQLException(e);
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                    } catch (Throwable th) {
                        th = th;
                        if (sQLiteCursor != null) {
                        }
                        throw th;
                    }
                }
            } catch (Throwable th2) {
                th = th2;
                sQLiteCursor = queryFinalized;
                if (sQLiteCursor != null) {
                    sQLiteCursor.dispose();
                }
                throw th;
            }
        } catch (Exception e4) {
            e = e4;
            sQLiteCursor = null;
        } catch (Throwable th3) {
            th = th3;
            sQLiteCursor = null;
        }
    }

    public void lambda$deleteUserChatHistory$77(ArrayList arrayList, long j, ArrayList arrayList2) {
        getFileLoader().cancelLoadFiles(arrayList);
        getMessagesController().markDialogMessageAsDeleted(j, arrayList2);
    }

    public void lambda$deleteUserChatHistory$78(ArrayList arrayList, long j) {
        NotificationCenter notificationCenter = getNotificationCenter();
        int i = NotificationCenter.messagesDeleted;
        Object[] objArr = new Object[3];
        objArr[0] = arrayList;
        objArr[1] = Long.valueOf(DialogObject.isChatDialog(j) ? -j : 0L);
        objArr[2] = Boolean.FALSE;
        notificationCenter.lambda$postNotificationNameOnUIThread$1(i, objArr);
    }

    private boolean addFilesToDelete(TLRPC$Message tLRPC$Message, ArrayList<File> arrayList, ArrayList<Pair<Long, Integer>> arrayList2, ArrayList<String> arrayList3, boolean z) {
        long j;
        int i;
        int i2 = 0;
        if (tLRPC$Message == null) {
            return false;
        }
        TLRPC$Document document = MessageObject.getDocument(tLRPC$Message);
        TLRPC$Photo photo = MessageObject.getPhoto(tLRPC$Message);
        if (MessageObject.isVoiceMessage(tLRPC$Message)) {
            if (document == null || getMediaDataController().ringtoneDataStore.contains(document.id)) {
                return false;
            }
            j = document.id;
            i = 2;
        } else {
            if (MessageObject.isStickerMessage(tLRPC$Message) || MessageObject.isAnimatedStickerMessage(tLRPC$Message)) {
                if (document == null) {
                    return false;
                }
                j = document.id;
            } else if (MessageObject.isVideoMessage(tLRPC$Message) || MessageObject.isRoundVideoMessage(tLRPC$Message) || MessageObject.isGifMessage(tLRPC$Message)) {
                if (document == null) {
                    return false;
                }
                j = document.id;
                i = 4;
            } else if (document != null) {
                if (getMediaDataController().ringtoneDataStore.contains(document.id)) {
                    return false;
                }
                j = document.id;
                i = 8;
            } else if (photo == null || FileLoader.getClosestPhotoSizeWithSize(photo.sizes, AndroidUtilities.getPhotoSize()) == null) {
                i = 0;
                j = 0;
            } else {
                j = photo.id;
            }
            i = 1;
        }
        if (j != 0) {
            arrayList2.add(new Pair<>(Long.valueOf(j), Integer.valueOf(i)));
        }
        if (photo != null) {
            int size = photo.sizes.size();
            while (i2 < size) {
                TLRPC$PhotoSize tLRPC$PhotoSize = photo.sizes.get(i2);
                String attachFileName = FileLoader.getAttachFileName(tLRPC$PhotoSize);
                if (!TextUtils.isEmpty(attachFileName)) {
                    arrayList3.add(attachFileName);
                }
                File pathToAttach = getFileLoader().getPathToAttach(tLRPC$PhotoSize, z);
                if (pathToAttach.toString().length() > 0) {
                    arrayList.add(pathToAttach);
                }
                i2++;
            }
            return true;
        }
        if (document == null) {
            return false;
        }
        String attachFileName2 = FileLoader.getAttachFileName(document);
        if (!TextUtils.isEmpty(attachFileName2)) {
            arrayList3.add(attachFileName2);
        }
        File pathToAttach2 = getFileLoader().getPathToAttach(document, z);
        if (pathToAttach2.toString().length() > 0) {
            arrayList.add(pathToAttach2);
        }
        int size2 = document.thumbs.size();
        while (i2 < size2) {
            File pathToAttach3 = getFileLoader().getPathToAttach(document.thumbs.get(i2));
            if (pathToAttach3.toString().length() > 0) {
                arrayList.add(pathToAttach3);
            }
            i2++;
        }
        return true;
    }

    public void deleteDialog(final long j, final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$deleteDialog$82(i, j);
            }
        });
    }

    public void lambda$deleteDialog$82(int i, long j) {
        Throwable th;
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLitePreparedStatement sQLitePreparedStatement2;
        SQLiteCursor sQLiteCursor;
        Exception exc;
        SQLiteCursor queryFinalized;
        SQLiteCursor sQLiteCursor2;
        SQLiteCursor sQLiteCursor3;
        final ArrayList arrayList;
        ArrayList<File> arrayList2;
        String str;
        boolean z;
        String str2;
        String str3;
        String str4;
        String str5;
        long j2;
        String str6;
        String str7;
        String str8;
        String str9;
        int i2;
        int i3;
        long j3;
        ?? r12 = 0;
        if (i == 3) {
            try {
                queryFinalized = this.database.queryFinalized("SELECT last_mid FROM dialogs WHERE did = " + j, new Object[0]);
            } catch (Exception e) {
                exc = e;
                sQLitePreparedStatement = null;
                sQLitePreparedStatement2 = null;
                sQLiteCursor = null;
                sQLiteCursor2 = null;
                try {
                    checkSQLException(exc);
                    if (sQLiteCursor != null) {
                    }
                    if (sQLiteCursor2 != null) {
                    }
                    if (sQLitePreparedStatement != null) {
                    }
                    if (sQLitePreparedStatement2 != null) {
                    }
                } catch (Throwable th2) {
                    th = th2;
                    if (sQLiteCursor != null) {
                    }
                    if (sQLiteCursor2 != null) {
                    }
                    if (sQLitePreparedStatement != null) {
                    }
                    if (sQLitePreparedStatement2 != null) {
                    }
                }
            } catch (Throwable th3) {
                th = th3;
                sQLitePreparedStatement = null;
                sQLitePreparedStatement2 = null;
                sQLiteCursor = null;
                sQLiteCursor2 = null;
                if (sQLiteCursor != null) {
                }
                if (sQLiteCursor2 != null) {
                }
                if (sQLitePreparedStatement != null) {
                }
                if (sQLitePreparedStatement2 != null) {
                }
            }
            try {
                int intValue = queryFinalized.next() ? queryFinalized.intValue(0) : -1;
                queryFinalized.dispose();
                if (intValue != 0) {
                    return;
                }
            } catch (Exception e2) {
                exc = e2;
                sQLiteCursor = queryFinalized;
                sQLitePreparedStatement = null;
                sQLitePreparedStatement2 = null;
                sQLiteCursor2 = null;
                checkSQLException(exc);
                if (sQLiteCursor != null) {
                }
                if (sQLiteCursor2 != null) {
                }
                if (sQLitePreparedStatement != null) {
                }
                if (sQLitePreparedStatement2 != null) {
                }
            } catch (Throwable th4) {
                th = th4;
                sQLiteCursor = queryFinalized;
                sQLitePreparedStatement = null;
                sQLitePreparedStatement2 = null;
                sQLiteCursor2 = null;
                if (sQLiteCursor != null) {
                }
                if (sQLiteCursor2 != null) {
                }
                if (sQLitePreparedStatement != null) {
                }
                if (sQLitePreparedStatement2 != null) {
                }
            }
        }
        String str10 = "SELECT data FROM messages_v2 WHERE uid = ";
        if (DialogObject.isEncryptedDialog(j) || i == 2) {
            ?? queryFinalized2 = this.database.queryFinalized("SELECT data FROM messages_v2 WHERE uid = " + j, new Object[0]);
            try {
                try {
                    ArrayList<File> arrayList3 = new ArrayList<>();
                    ArrayList arrayList4 = new ArrayList();
                    ArrayList arrayList5 = new ArrayList();
                    queryFinalized2 = queryFinalized2;
                    while (queryFinalized2.next()) {
                        try {
                            ?? byteBufferValue = queryFinalized2.byteBufferValue(r12);
                            if (byteBufferValue != 0) {
                                ?? TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(r12), r12);
                                TLdeserialize.readAttachPath(byteBufferValue, getUserConfig().clientUserId);
                                byteBufferValue.reuse();
                                arrayList = arrayList4;
                                arrayList2 = arrayList3;
                                sQLiteCursor3 = queryFinalized2;
                                str = str10;
                                z = true;
                                try {
                                    try {
                                        addFilesToDelete(TLdeserialize, arrayList2, arrayList5, arrayList, false);
                                    } catch (Throwable th5) {
                                        th = th5;
                                        th = th;
                                        sQLiteCursor = sQLiteCursor3;
                                        sQLitePreparedStatement = null;
                                        sQLitePreparedStatement2 = null;
                                        sQLiteCursor2 = null;
                                        if (sQLiteCursor != null) {
                                            sQLiteCursor.dispose();
                                        }
                                        if (sQLiteCursor2 != null) {
                                            sQLiteCursor2.dispose();
                                        }
                                        if (sQLitePreparedStatement != null) {
                                            sQLitePreparedStatement.dispose();
                                        }
                                        if (sQLitePreparedStatement2 != null) {
                                            sQLitePreparedStatement2.dispose();
                                            throw th;
                                        }
                                        throw th;
                                    }
                                } catch (Exception e3) {
                                    e = e3;
                                    try {
                                        checkSQLException(e);
                                        sQLiteCursor3.dispose();
                                        deleteFromDownloadQueue(arrayList5, z);
                                        AndroidUtilities.runOnUIThread(new Runnable() {
                                            @Override
                                            public final void run() {
                                                MessagesStorage.this.lambda$deleteDialog$80(arrayList);
                                            }
                                        });
                                        getFileLoader().deleteFiles(arrayList2, i);
                                        String str11 = "DELETE FROM media_holes_v2 WHERE uid = ";
                                        String str12 = "DELETE FROM messages_holes WHERE uid = ";
                                        if (i != 0) {
                                        }
                                        str2 = "DELETE FROM media_holes_v2 WHERE uid = ";
                                        str3 = "DELETE FROM media_v4 WHERE uid = ";
                                        str4 = "DELETE FROM messages_v2 WHERE uid = ";
                                        str5 = "DELETE FROM messages_holes WHERE uid = ";
                                        j2 = j;
                                        str6 = "DELETE FROM media_counts_v2 WHERE uid = ";
                                        str7 = "DELETE FROM bot_keyboard WHERE uid = ";
                                        this.database.executeFast("DELETE FROM dialogs WHERE did = " + j2).stepThis().dispose();
                                        this.database.executeFast("DELETE FROM chat_pinned_v2 WHERE uid = " + j2).stepThis().dispose();
                                        this.database.executeFast("DELETE FROM chat_pinned_count WHERE uid = " + j2).stepThis().dispose();
                                        this.database.executeFast("DELETE FROM channel_users_v2 WHERE did = " + j2).stepThis().dispose();
                                        this.database.executeFast("DELETE FROM search_recent WHERE did = " + j2).stepThis().dispose();
                                        if (DialogObject.isEncryptedDialog(j)) {
                                        }
                                        this.database.executeFast("UPDATE dialogs SET unread_count = 0, unread_count_i = 0 WHERE did = " + j2).stepThis().dispose();
                                        this.database.executeFast(str4 + j2).stepThis().dispose();
                                        this.database.executeFast(str7 + j2).stepThis().dispose();
                                        this.database.executeFast("DELETE FROM bot_keyboard_topics WHERE uid = " + j2).stepThis().dispose();
                                        this.database.executeFast(str6 + j2).stepThis().dispose();
                                        this.database.executeFast(str3 + j2).stepThis().dispose();
                                        this.database.executeFast(str5 + j2).stepThis().dispose();
                                        this.database.executeFast(str2 + j2).stepThis().dispose();
                                        getMediaDataController().clearBotKeyboard(j2);
                                        AndroidUtilities.runOnUIThread(new Runnable() {
                                            @Override
                                            public final void run() {
                                                MessagesStorage.this.lambda$deleteDialog$81();
                                            }
                                        });
                                        resetAllUnreadCounters(false);
                                        updateWidgets(j2);
                                    } catch (Exception e4) {
                                        e = e4;
                                        exc = e;
                                        sQLiteCursor = sQLiteCursor3;
                                        sQLitePreparedStatement = null;
                                        sQLitePreparedStatement2 = null;
                                        sQLiteCursor2 = null;
                                        checkSQLException(exc);
                                        if (sQLiteCursor != null) {
                                            sQLiteCursor.dispose();
                                        }
                                        if (sQLiteCursor2 != null) {
                                            sQLiteCursor2.dispose();
                                        }
                                        if (sQLitePreparedStatement != null) {
                                            sQLitePreparedStatement.dispose();
                                        }
                                        if (sQLitePreparedStatement2 != null) {
                                            sQLitePreparedStatement2.dispose();
                                            return;
                                        }
                                        return;
                                    }
                                }
                            } else {
                                arrayList = arrayList4;
                                arrayList2 = arrayList3;
                                sQLiteCursor3 = queryFinalized2;
                                str = str10;
                            }
                            arrayList4 = arrayList;
                            arrayList3 = arrayList2;
                            str10 = str;
                            queryFinalized2 = sQLiteCursor3;
                            r12 = 0;
                        } catch (Exception e5) {
                            e = e5;
                            arrayList = arrayList4;
                            arrayList2 = arrayList3;
                            sQLiteCursor3 = queryFinalized2;
                            str = str10;
                            z = true;
                        }
                    }
                    arrayList = arrayList4;
                    arrayList2 = arrayList3;
                    sQLiteCursor3 = queryFinalized2;
                    str = str10;
                    z = true;
                    sQLiteCursor3.dispose();
                    deleteFromDownloadQueue(arrayList5, z);
                    AndroidUtilities.runOnUIThread(new Runnable() {
                        @Override
                        public final void run() {
                            MessagesStorage.this.lambda$deleteDialog$80(arrayList);
                        }
                    });
                    getFileLoader().deleteFiles(arrayList2, i);
                } catch (Exception e6) {
                    e = e6;
                    sQLiteCursor3 = queryFinalized2;
                }
            } catch (Throwable th6) {
                th = th6;
                sQLiteCursor3 = queryFinalized2;
            }
        } else {
            str = "SELECT data FROM messages_v2 WHERE uid = ";
        }
        String str112 = "DELETE FROM media_holes_v2 WHERE uid = ";
        String str122 = "DELETE FROM messages_holes WHERE uid = ";
        if (i != 0 || i == 3) {
            str2 = "DELETE FROM media_holes_v2 WHERE uid = ";
            str3 = "DELETE FROM media_v4 WHERE uid = ";
            str4 = "DELETE FROM messages_v2 WHERE uid = ";
            str5 = "DELETE FROM messages_holes WHERE uid = ";
            j2 = j;
            str6 = "DELETE FROM media_counts_v2 WHERE uid = ";
            str7 = "DELETE FROM bot_keyboard WHERE uid = ";
            this.database.executeFast("DELETE FROM dialogs WHERE did = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM chat_pinned_v2 WHERE uid = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM chat_pinned_count WHERE uid = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM channel_users_v2 WHERE did = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM search_recent WHERE did = " + j2).stepThis().dispose();
            if (DialogObject.isEncryptedDialog(j)) {
                if (DialogObject.isChatDialog(j)) {
                    this.database.executeFast("DELETE FROM chat_settings_v2 WHERE uid = " + (-j2)).stepThis().dispose();
                }
            } else {
                this.database.executeFast("DELETE FROM enc_chats WHERE uid = " + DialogObject.getEncryptedChatId(j)).stepThis().dispose();
            }
        } else if (i == 2) {
            SQLiteCursor queryFinalized3 = this.database.queryFinalized("SELECT last_mid_i, last_mid FROM dialogs WHERE did = " + j, new Object[0]);
            try {
                if (queryFinalized3.next()) {
                    long longValue = queryFinalized3.longValue(0);
                    long longValue2 = queryFinalized3.longValue(1);
                    ?? r14 = 0;
                    try {
                        ?? queryFinalized4 = this.database.queryFinalized(str + j + " AND mid IN (" + longValue + "," + longValue2 + ")", new Object[0]);
                        int i4 = -1;
                        while (queryFinalized4.next()) {
                            try {
                                try {
                                    ?? byteBufferValue2 = queryFinalized4.byteBufferValue(r14);
                                    if (byteBufferValue2 != 0) {
                                        i2 = i4;
                                        try {
                                            ?? TLdeserialize2 = TLRPC$Message.TLdeserialize(byteBufferValue2, byteBufferValue2.readInt32(r14), r14);
                                            if (TLdeserialize2 != 0) {
                                                str8 = str112;
                                                str9 = str122;
                                                try {
                                                    TLdeserialize2.readAttachPath(byteBufferValue2, getUserConfig().clientUserId);
                                                } catch (Exception e7) {
                                                    e = e7;
                                                    try {
                                                        checkSQLException(e);
                                                        queryFinalized4.dispose();
                                                        this.database.executeFast("DELETE FROM messages_v2 WHERE uid = " + j + " AND mid != " + longValue + " AND mid != " + longValue2).stepThis().dispose();
                                                        SQLiteDatabase sQLiteDatabase = this.database;
                                                        StringBuilder sb = new StringBuilder();
                                                        sb.append(str9);
                                                        sb.append(j);
                                                        sQLiteDatabase.executeFast(sb.toString()).stepThis().dispose();
                                                        this.database.executeFast("DELETE FROM bot_keyboard WHERE uid = " + j).stepThis().dispose();
                                                        this.database.executeFast("DELETE FROM bot_keyboard_topics WHERE uid = " + j).stepThis().dispose();
                                                        this.database.executeFast("DELETE FROM media_counts_v2 WHERE uid = " + j).stepThis().dispose();
                                                        this.database.executeFast("DELETE FROM media_v4 WHERE uid = " + j).stepThis().dispose();
                                                        this.database.executeFast(str8 + j).stepThis().dispose();
                                                        getMediaDataController().clearBotKeyboard(j);
                                                        sQLitePreparedStatement = this.database.executeFast("REPLACE INTO messages_holes VALUES(?, ?, ?)");
                                                        sQLitePreparedStatement2 = this.database.executeFast("REPLACE INTO media_holes_v2 VALUES(?, ?, ?, ?)");
                                                        i3 = i2;
                                                        if (i3 == -1) {
                                                        }
                                                        sQLitePreparedStatement.dispose();
                                                        try {
                                                            sQLitePreparedStatement2.dispose();
                                                            updateWidgets(j3);
                                                            sQLiteCursor.dispose();
                                                            return;
                                                        } catch (Exception e8) {
                                                            exc = e8;
                                                            sQLitePreparedStatement = null;
                                                            sQLiteCursor2 = null;
                                                            checkSQLException(exc);
                                                            if (sQLiteCursor != null) {
                                                            }
                                                            if (sQLiteCursor2 != null) {
                                                            }
                                                            if (sQLitePreparedStatement != null) {
                                                            }
                                                            if (sQLitePreparedStatement2 != null) {
                                                            }
                                                        } catch (Throwable th7) {
                                                            th = th7;
                                                            sQLitePreparedStatement = null;
                                                            sQLiteCursor2 = null;
                                                            if (sQLiteCursor != null) {
                                                            }
                                                            if (sQLiteCursor2 != null) {
                                                            }
                                                            if (sQLitePreparedStatement != null) {
                                                            }
                                                            if (sQLitePreparedStatement2 != null) {
                                                            }
                                                        }
                                                    } catch (Exception e9) {
                                                        sQLiteCursor = queryFinalized3;
                                                        exc = e9;
                                                        sQLiteCursor2 = queryFinalized4;
                                                        sQLitePreparedStatement = null;
                                                        sQLitePreparedStatement2 = null;
                                                        checkSQLException(exc);
                                                        if (sQLiteCursor != null) {
                                                        }
                                                        if (sQLiteCursor2 != null) {
                                                        }
                                                        if (sQLitePreparedStatement != null) {
                                                        }
                                                        if (sQLitePreparedStatement2 != null) {
                                                        }
                                                    } catch (Throwable th8) {
                                                        sQLiteCursor = queryFinalized3;
                                                        th = th8;
                                                        sQLiteCursor2 = queryFinalized4;
                                                        sQLitePreparedStatement = null;
                                                        sQLitePreparedStatement2 = null;
                                                        if (sQLiteCursor != null) {
                                                        }
                                                        if (sQLiteCursor2 != null) {
                                                        }
                                                        if (sQLitePreparedStatement != null) {
                                                        }
                                                        if (sQLitePreparedStatement2 != null) {
                                                        }
                                                    }
                                                }
                                            } else {
                                                str8 = str112;
                                                str9 = str122;
                                            }
                                            byteBufferValue2.reuse();
                                            if (TLdeserialize2 != 0) {
                                                i4 = TLdeserialize2.id;
                                                str112 = str8;
                                                str122 = str9;
                                                r14 = 0;
                                            }
                                        } catch (Exception e10) {
                                            e = e10;
                                            str8 = str112;
                                            str9 = str122;
                                        }
                                    } else {
                                        str8 = str112;
                                        str9 = str122;
                                        i2 = i4;
                                    }
                                    i4 = i2;
                                    str112 = str8;
                                    str122 = str9;
                                    r14 = 0;
                                } catch (Throwable th9) {
                                    th = th9;
                                    sQLiteCursor2 = queryFinalized4;
                                    sQLiteCursor = queryFinalized3;
                                    sQLitePreparedStatement = null;
                                    sQLitePreparedStatement2 = null;
                                    if (sQLiteCursor != null) {
                                    }
                                    if (sQLiteCursor2 != null) {
                                    }
                                    if (sQLitePreparedStatement != null) {
                                    }
                                    if (sQLitePreparedStatement2 != null) {
                                    }
                                }
                            } catch (Exception e11) {
                                e = e11;
                                str8 = str112;
                                str9 = str122;
                                i2 = i4;
                            }
                        }
                        str8 = str112;
                        str9 = str122;
                        i2 = i4;
                        queryFinalized4.dispose();
                        this.database.executeFast("DELETE FROM messages_v2 WHERE uid = " + j + " AND mid != " + longValue + " AND mid != " + longValue2).stepThis().dispose();
                        SQLiteDatabase sQLiteDatabase2 = this.database;
                        StringBuilder sb2 = new StringBuilder();
                        sb2.append(str9);
                        sb2.append(j);
                        sQLiteDatabase2.executeFast(sb2.toString()).stepThis().dispose();
                        this.database.executeFast("DELETE FROM bot_keyboard WHERE uid = " + j).stepThis().dispose();
                        this.database.executeFast("DELETE FROM bot_keyboard_topics WHERE uid = " + j).stepThis().dispose();
                        this.database.executeFast("DELETE FROM media_counts_v2 WHERE uid = " + j).stepThis().dispose();
                        this.database.executeFast("DELETE FROM media_v4 WHERE uid = " + j).stepThis().dispose();
                        this.database.executeFast(str8 + j).stepThis().dispose();
                        getMediaDataController().clearBotKeyboard(j);
                        sQLitePreparedStatement = this.database.executeFast("REPLACE INTO messages_holes VALUES(?, ?, ?)");
                        try {
                            sQLitePreparedStatement2 = this.database.executeFast("REPLACE INTO media_holes_v2 VALUES(?, ?, ?, ?)");
                            i3 = i2;
                            if (i3 == -1) {
                                j3 = j;
                                sQLiteCursor = queryFinalized3;
                                try {
                                    createFirstHoles(j, sQLitePreparedStatement, sQLitePreparedStatement2, i3, 0L);
                                } catch (Exception e12) {
                                    exc = e12;
                                    sQLiteCursor2 = null;
                                    checkSQLException(exc);
                                    if (sQLiteCursor != null) {
                                    }
                                    if (sQLiteCursor2 != null) {
                                    }
                                    if (sQLitePreparedStatement != null) {
                                    }
                                    if (sQLitePreparedStatement2 != null) {
                                    }
                                } catch (Throwable th10) {
                                    th = th10;
                                    sQLiteCursor2 = null;
                                    if (sQLiteCursor != null) {
                                    }
                                    if (sQLiteCursor2 != null) {
                                    }
                                    if (sQLitePreparedStatement != null) {
                                    }
                                    if (sQLitePreparedStatement2 != null) {
                                    }
                                }
                            } else {
                                j3 = j;
                                sQLiteCursor = queryFinalized3;
                            }
                            sQLitePreparedStatement.dispose();
                            sQLitePreparedStatement2.dispose();
                        } catch (Exception e13) {
                            sQLiteCursor = queryFinalized3;
                            exc = e13;
                            sQLitePreparedStatement2 = null;
                            sQLiteCursor2 = null;
                            checkSQLException(exc);
                            if (sQLiteCursor != null) {
                            }
                            if (sQLiteCursor2 != null) {
                            }
                            if (sQLitePreparedStatement != null) {
                            }
                            if (sQLitePreparedStatement2 != null) {
                            }
                        } catch (Throwable th11) {
                            sQLiteCursor = queryFinalized3;
                            th = th11;
                            sQLitePreparedStatement2 = null;
                            sQLiteCursor2 = null;
                            if (sQLiteCursor != null) {
                            }
                            if (sQLiteCursor2 != null) {
                            }
                            if (sQLitePreparedStatement != null) {
                            }
                            if (sQLitePreparedStatement2 != null) {
                            }
                        }
                        try {
                            updateWidgets(j3);
                        } catch (Exception e14) {
                            e = e14;
                            exc = e;
                            sQLitePreparedStatement = null;
                            sQLitePreparedStatement2 = null;
                            sQLiteCursor2 = null;
                            checkSQLException(exc);
                            if (sQLiteCursor != null) {
                            }
                            if (sQLiteCursor2 != null) {
                            }
                            if (sQLitePreparedStatement != null) {
                            }
                            if (sQLitePreparedStatement2 != null) {
                            }
                        } catch (Throwable th12) {
                            th = th12;
                            th = th;
                            sQLitePreparedStatement = null;
                            sQLitePreparedStatement2 = null;
                            sQLiteCursor2 = null;
                            if (sQLiteCursor != null) {
                            }
                            if (sQLiteCursor2 != null) {
                            }
                            if (sQLitePreparedStatement != null) {
                            }
                            if (sQLitePreparedStatement2 != null) {
                            }
                        }
                    } catch (Exception e15) {
                        e = e15;
                        sQLiteCursor = queryFinalized3;
                        exc = e;
                        sQLitePreparedStatement = null;
                        sQLitePreparedStatement2 = null;
                        sQLiteCursor2 = null;
                        checkSQLException(exc);
                        if (sQLiteCursor != null) {
                        }
                        if (sQLiteCursor2 != null) {
                        }
                        if (sQLitePreparedStatement != null) {
                        }
                        if (sQLitePreparedStatement2 != null) {
                        }
                    } catch (Throwable th13) {
                        th = th13;
                        sQLiteCursor = queryFinalized3;
                        th = th;
                        sQLitePreparedStatement = null;
                        sQLitePreparedStatement2 = null;
                        sQLiteCursor2 = null;
                        if (sQLiteCursor != null) {
                        }
                        if (sQLiteCursor2 != null) {
                        }
                        if (sQLitePreparedStatement != null) {
                        }
                        if (sQLitePreparedStatement2 != null) {
                        }
                    }
                } else {
                    sQLiteCursor = queryFinalized3;
                }
                sQLiteCursor.dispose();
                return;
            } catch (Exception e16) {
                e = e16;
                sQLiteCursor = queryFinalized3;
            } catch (Throwable th14) {
                th = th14;
                sQLiteCursor = queryFinalized3;
            }
        } else {
            str2 = "DELETE FROM media_holes_v2 WHERE uid = ";
            str3 = "DELETE FROM media_v4 WHERE uid = ";
            str4 = "DELETE FROM messages_v2 WHERE uid = ";
            str5 = "DELETE FROM messages_holes WHERE uid = ";
            j2 = j;
            str6 = "DELETE FROM media_counts_v2 WHERE uid = ";
            str7 = "DELETE FROM bot_keyboard WHERE uid = ";
        }
        this.database.executeFast("UPDATE dialogs SET unread_count = 0, unread_count_i = 0 WHERE did = " + j2).stepThis().dispose();
        this.database.executeFast(str4 + j2).stepThis().dispose();
        this.database.executeFast(str7 + j2).stepThis().dispose();
        this.database.executeFast("DELETE FROM bot_keyboard_topics WHERE uid = " + j2).stepThis().dispose();
        this.database.executeFast(str6 + j2).stepThis().dispose();
        this.database.executeFast(str3 + j2).stepThis().dispose();
        this.database.executeFast(str5 + j2).stepThis().dispose();
        this.database.executeFast(str2 + j2).stepThis().dispose();
        getMediaDataController().clearBotKeyboard(j2);
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$deleteDialog$81();
            }
        });
        resetAllUnreadCounters(false);
        updateWidgets(j2);
    }

    public void lambda$deleteDialog$80(ArrayList arrayList) {
        getFileLoader().cancelLoadFiles(arrayList);
    }

    public void lambda$deleteDialog$81() {
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.needReloadRecentDialogsSearch, new Object[0]);
    }

    public void onDeleteQueryComplete(final long j) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$onDeleteQueryComplete$83(j);
            }
        });
    }

    public void lambda$onDeleteQueryComplete$83(long j) {
        try {
            this.database.executeFast("DELETE FROM media_counts_v2 WHERE uid = " + j).stepThis().dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void clearUserPhotos(final long j) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$clearUserPhotos$84(j);
            }
        });
    }

    public void lambda$clearUserPhotos$84(long j) {
        try {
            this.database.executeFast("DELETE FROM dialog_photos WHERE uid = " + j).stepThis().dispose();
            this.database.executeFast("DELETE FROM dialog_photos_count WHERE uid = " + j).stepThis().dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void clearUserPhoto(final long j, final long j2) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$clearUserPhoto$85(j, j2);
            }
        });
    }

    public void lambda$clearUserPhoto$85(long j, long j2) {
        try {
            this.database.executeFast("DELETE FROM dialog_photos WHERE uid = " + j + " AND id = " + j2).stepThis().dispose();
            this.database.executeFast("UPDATE dialog_photos_count SET count = count - 1 WHERE uid = " + j + " AND count > 0").stepThis().dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void resetDialogs(final TLRPC$messages_Dialogs tLRPC$messages_Dialogs, final int i, final int i2, final int i3, final int i4, final int i5, final LongSparseArray<TLRPC$Dialog> longSparseArray, final LongSparseArray<ArrayList<MessageObject>> longSparseArray2, final TLRPC$Message tLRPC$Message, final int i6) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$resetDialogs$87(tLRPC$messages_Dialogs, i6, i2, i3, i4, i5, tLRPC$Message, i, longSparseArray, longSparseArray2);
            }
        });
    }

    public void lambda$resetDialogs$87(TLRPC$messages_Dialogs tLRPC$messages_Dialogs, int i, int i2, int i3, int i4, int i5, TLRPC$Message tLRPC$Message, int i6, LongSparseArray longSparseArray, LongSparseArray longSparseArray2) {
        SQLiteCursor sQLiteCursor;
        SQLiteDatabase sQLiteDatabase;
        long j;
        long j2;
        long j3;
        long j4;
        long j5;
        int i7;
        SQLiteDatabase sQLiteDatabase2;
        long j6;
        try {
            ArrayList arrayList = new ArrayList();
            int size = tLRPC$messages_Dialogs.dialogs.size() - i;
            final LongSparseIntArray longSparseIntArray = new LongSparseIntArray();
            ArrayList arrayList2 = new ArrayList();
            ArrayList arrayList3 = new ArrayList();
            for (int i8 = i; i8 < tLRPC$messages_Dialogs.dialogs.size(); i8++) {
                arrayList3.add(Long.valueOf(tLRPC$messages_Dialogs.dialogs.get(i8).id));
            }
            int i9 = 0;
            SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT did, pinned FROM dialogs WHERE 1", new Object[0]);
            int i10 = 0;
            while (queryFinalized.next()) {
                try {
                    long longValue = queryFinalized.longValue(0);
                    int intValue = queryFinalized.intValue(1);
                    if (!DialogObject.isEncryptedDialog(longValue)) {
                        arrayList.add(Long.valueOf(longValue));
                        if (intValue > 0) {
                            i10 = Math.max(intValue, i10);
                            longSparseIntArray.put(longValue, intValue);
                            arrayList2.add(Long.valueOf(longValue));
                        }
                    }
                } catch (Exception e) {
                    e = e;
                    sQLiteCursor = queryFinalized;
                    try {
                        checkSQLException(e);
                        SQLiteDatabase sQLiteDatabase3 = this.database;
                        if (sQLiteDatabase3 != null) {
                            sQLiteDatabase3.commitTransaction();
                        }
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                            return;
                        }
                        return;
                    } catch (Throwable th) {
                        th = th;
                        sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                            sQLiteDatabase.commitTransaction();
                        }
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        throw th;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    sQLiteCursor = queryFinalized;
                    sQLiteDatabase = this.database;
                    if (sQLiteDatabase != null) {
                    }
                    if (sQLiteCursor != null) {
                    }
                    throw th;
                }
            }
            Collections.sort(arrayList2, new Comparator() {
                @Override
                public final int compare(Object obj, Object obj2) {
                    int lambda$resetDialogs$86;
                    lambda$resetDialogs$86 = MessagesStorage.lambda$resetDialogs$86(LongSparseIntArray.this, (Long) obj, (Long) obj2);
                    return lambda$resetDialogs$86;
                }
            });
            while (arrayList2.size() < size) {
                arrayList2.add(0, 0L);
            }
            queryFinalized.dispose();
            String str = "(" + TextUtils.join(",", arrayList) + ")";
            this.database.beginTransaction();
            this.database.executeFast("DELETE FROM chat_pinned_count WHERE uid IN " + str).stepThis().dispose();
            this.database.executeFast("DELETE FROM chat_pinned_v2 WHERE uid IN " + str).stepThis().dispose();
            this.database.executeFast("DELETE FROM dialogs WHERE did IN " + str).stepThis().dispose();
            this.database.executeFast("DELETE FROM messages_v2 WHERE uid IN " + str).stepThis().dispose();
            this.database.executeFast("DELETE FROM polls_v2 WHERE 1").stepThis().dispose();
            this.database.executeFast("DELETE FROM bot_keyboard WHERE uid IN " + str).stepThis().dispose();
            this.database.executeFast("DELETE FROM bot_keyboard_topics WHERE uid IN " + str).stepThis().dispose();
            this.database.executeFast("DELETE FROM media_v4 WHERE uid IN " + str).stepThis().dispose();
            this.database.executeFast("DELETE FROM messages_holes WHERE uid IN " + str).stepThis().dispose();
            this.database.executeFast("DELETE FROM media_holes_v2 WHERE uid IN " + str).stepThis().dispose();
            this.database.commitTransaction();
            int i11 = 0;
            while (i11 < size) {
                TLRPC$Dialog tLRPC$Dialog = tLRPC$messages_Dialogs.dialogs.get(i + i11);
                if (!(tLRPC$Dialog instanceof TLRPC$TL_dialog) || tLRPC$Dialog.pinned) {
                    int indexOf = arrayList2.indexOf(Long.valueOf(tLRPC$Dialog.id));
                    int indexOf2 = arrayList3.indexOf(Long.valueOf(tLRPC$Dialog.id));
                    if (indexOf != -1 && indexOf2 != -1) {
                        if (indexOf == indexOf2) {
                            int i12 = longSparseIntArray.get(tLRPC$Dialog.id, -1);
                            if (i12 != -1) {
                                tLRPC$Dialog.pinnedNum = i12;
                            }
                        } else {
                            int i13 = longSparseIntArray.get(((Long) arrayList2.get(indexOf2)).longValue(), -1);
                            if (i13 != -1) {
                                tLRPC$Dialog.pinnedNum = i13;
                            }
                        }
                    }
                    if (tLRPC$Dialog.pinnedNum == 0) {
                        tLRPC$Dialog.pinnedNum = (size - i11) + i10;
                    }
                }
                i11++;
                i9 = 0;
            }
            int i14 = i9;
            putDialogsInternal(tLRPC$messages_Dialogs, i14);
            lambda$saveDiffParams$34(i2, i3, i4, i5);
            int totalDialogsCount = getUserConfig().getTotalDialogsCount(i14) + tLRPC$messages_Dialogs.dialogs.size();
            int i15 = tLRPC$Message.id;
            int i16 = tLRPC$Message.date;
            TLRPC$Peer tLRPC$Peer = tLRPC$Message.peer_id;
            long j7 = tLRPC$Peer.channel_id;
            if (j7 == 0) {
                long j8 = tLRPC$Peer.chat_id;
                if (j8 != 0) {
                    int i17 = 0;
                    while (true) {
                        if (i17 >= tLRPC$messages_Dialogs.chats.size()) {
                            j5 = 0;
                            break;
                        }
                        TLRPC$Chat tLRPC$Chat = tLRPC$messages_Dialogs.chats.get(i17);
                        if (tLRPC$Chat.id == j8) {
                            j5 = tLRPC$Chat.access_hash;
                            break;
                        }
                        i17++;
                    }
                    j4 = j5;
                    j3 = j8;
                    j = 0;
                    j2 = 0;
                } else {
                    j = tLRPC$Peer.user_id;
                    if (j != 0) {
                        for (int i18 = 0; i18 < tLRPC$messages_Dialogs.users.size(); i18++) {
                            TLRPC$User tLRPC$User = tLRPC$messages_Dialogs.users.get(i18);
                            if (tLRPC$User.id == j) {
                                j4 = tLRPC$User.access_hash;
                                j2 = 0;
                            }
                        }
                    } else {
                        j = 0;
                    }
                    j2 = 0;
                    j3 = 0;
                    j4 = 0;
                }
                for (i7 = 0; i7 < 2; i7++) {
                    getUserConfig().setDialogsLoadOffset(i7, i15, i16, j, j3, j2, j4);
                    getUserConfig().setTotalDialogsCount(i7, totalDialogsCount);
                }
                getUserConfig().draftsLoaded = false;
                getUserConfig().saveConfig(false);
                getMessagesController().completeDialogsReset(tLRPC$messages_Dialogs, i6, i2, i3, i4, i5, longSparseArray, longSparseArray2, tLRPC$Message);
                sQLiteDatabase2 = this.database;
                if (sQLiteDatabase2 == null) {
                    sQLiteDatabase2.commitTransaction();
                    return;
                }
                return;
            }
            int i19 = 0;
            while (true) {
                if (i19 >= tLRPC$messages_Dialogs.chats.size()) {
                    j6 = 0;
                    break;
                }
                TLRPC$Chat tLRPC$Chat2 = tLRPC$messages_Dialogs.chats.get(i19);
                if (tLRPC$Chat2.id == j7) {
                    j6 = tLRPC$Chat2.access_hash;
                    break;
                }
                i19++;
            }
            j4 = j6;
            j2 = j7;
            j = 0;
            j3 = 0;
            while (i7 < 2) {
            }
            getUserConfig().draftsLoaded = false;
            getUserConfig().saveConfig(false);
            getMessagesController().completeDialogsReset(tLRPC$messages_Dialogs, i6, i2, i3, i4, i5, longSparseArray, longSparseArray2, tLRPC$Message);
            sQLiteDatabase2 = this.database;
            if (sQLiteDatabase2 == null) {
            }
        } catch (Exception e2) {
            e = e2;
            sQLiteCursor = null;
        } catch (Throwable th3) {
            th = th3;
            sQLiteCursor = null;
        }
    }

    public static int lambda$resetDialogs$86(LongSparseIntArray longSparseIntArray, Long l, Long l2) {
        int i = longSparseIntArray.get(l.longValue());
        int i2 = longSparseIntArray.get(l2.longValue());
        if (i < i2) {
            return 1;
        }
        return i > i2 ? -1 : 0;
    }

    public void emptyMessagesMedia(final long j, final ArrayList<Integer> arrayList) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$emptyMessagesMedia$91(arrayList, j);
            }
        });
    }

    public void lambda$emptyMessagesMedia$91(ArrayList arrayList, long j) {
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLiteCursor sQLiteCursor;
        int i;
        NativeByteBuffer nativeByteBuffer;
        NativeByteBuffer nativeByteBuffer2;
        TLRPC$MessageReplyHeader tLRPC$MessageReplyHeader;
        NativeByteBuffer writeLocalParams;
        TLRPC$MessageReplyHeader tLRPC$MessageReplyHeader2;
        try {
            ArrayList<File> arrayList2 = new ArrayList<>();
            final ArrayList<String> arrayList3 = new ArrayList<>();
            ArrayList<Pair<Long, Integer>> arrayList4 = new ArrayList<>();
            final ArrayList arrayList5 = new ArrayList();
            int i2 = 2;
            ?? r13 = 0;
            int i3 = 1;
            SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT data, mid, date, uid, custom_params FROM messages_v2 WHERE mid IN (%s) AND uid = %d", TextUtils.join(",", arrayList), Long.valueOf(j)), new Object[0]);
            while (queryFinalized.next()) {
                try {
                    NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(r13);
                    if (byteBufferValue != 0) {
                        TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(r13), r13);
                        TLdeserialize.readAttachPath(byteBufferValue, getUserConfig().clientUserId);
                        byteBufferValue.reuse();
                        if (TLdeserialize.media != null && addFilesToDelete(TLdeserialize, arrayList2, arrayList4, arrayList3, true)) {
                            TLRPC$MessageMedia tLRPC$MessageMedia = TLdeserialize.media;
                            if (tLRPC$MessageMedia.document != null) {
                                tLRPC$MessageMedia.document = new TLRPC$TL_documentEmpty();
                            } else if (tLRPC$MessageMedia.photo != null) {
                                tLRPC$MessageMedia.photo = new TLRPC$TL_photoEmpty();
                            }
                            TLdeserialize.media.flags &= -2;
                            TLdeserialize.id = queryFinalized.intValue(1);
                            TLdeserialize.date = queryFinalized.intValue(2);
                            TLdeserialize.dialog_id = queryFinalized.longValue(3);
                            NativeByteBuffer byteBufferValue2 = queryFinalized.byteBufferValue(4);
                            if (byteBufferValue2 != null) {
                                MessageCustomParamsHelper.readLocalParams(TLdeserialize, byteBufferValue2);
                                byteBufferValue2.reuse();
                            }
                            arrayList5.add(TLdeserialize);
                        }
                    }
                    r13 = 0;
                } catch (Exception e) {
                    e = e;
                    sQLiteCursor = queryFinalized;
                    sQLitePreparedStatement = null;
                    try {
                        checkSQLException(e);
                        if (sQLiteCursor != null) {
                        }
                        if (sQLitePreparedStatement == null) {
                        }
                        sQLitePreparedStatement.dispose();
                    } catch (Throwable th) {
                        th = th;
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                        throw th;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    sQLiteCursor = queryFinalized;
                    sQLitePreparedStatement = null;
                    if (sQLiteCursor != null) {
                    }
                    if (sQLitePreparedStatement != null) {
                    }
                    throw th;
                }
            }
            int i4 = 3;
            queryFinalized.dispose();
            deleteFromDownloadQueue(arrayList4, true);
            if (arrayList5.isEmpty()) {
                sQLitePreparedStatement = null;
            } else {
                sQLitePreparedStatement = this.database.executeFast("REPLACE INTO messages_v2 VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?)");
                int i5 = 0;
                while (i5 < arrayList5.size()) {
                    try {
                        TLRPC$Message tLRPC$Message = (TLRPC$Message) arrayList5.get(i5);
                        MessageObject.normalizeFlags(tLRPC$Message);
                        NativeByteBuffer nativeByteBuffer3 = new NativeByteBuffer(tLRPC$Message.getObjectSize());
                        tLRPC$Message.serializeToStream(nativeByteBuffer3);
                        sQLitePreparedStatement.requery();
                        sQLitePreparedStatement.bindInteger(i3, tLRPC$Message.id);
                        sQLitePreparedStatement.bindLong(i2, tLRPC$Message.dialog_id);
                        sQLitePreparedStatement.bindInteger(i4, MessageObject.getUnreadFlags(tLRPC$Message));
                        sQLitePreparedStatement.bindInteger(4, tLRPC$Message.send_state);
                        sQLitePreparedStatement.bindInteger(5, tLRPC$Message.date);
                        sQLitePreparedStatement.bindByteBuffer(6, nativeByteBuffer3);
                        if (!MessageObject.isOut(tLRPC$Message) && !tLRPC$Message.from_scheduled) {
                            i = 0;
                            sQLitePreparedStatement.bindInteger(7, i);
                            sQLitePreparedStatement.bindInteger(8, tLRPC$Message.ttl);
                            if ((tLRPC$Message.flags & 1024) == 0) {
                                sQLitePreparedStatement.bindInteger(9, tLRPC$Message.views);
                            } else {
                                sQLitePreparedStatement.bindInteger(9, getMessageMediaType(tLRPC$Message));
                            }
                            if (tLRPC$Message.replyStory == null) {
                                nativeByteBuffer = new NativeByteBuffer(tLRPC$Message.replyStory.getObjectSize());
                                tLRPC$Message.replyStory.serializeToStream(nativeByteBuffer);
                                sQLitePreparedStatement.bindByteBuffer(10, nativeByteBuffer);
                            } else {
                                sQLitePreparedStatement.bindNull(10);
                                nativeByteBuffer = null;
                            }
                            int i6 = tLRPC$Message.stickerVerified;
                            sQLitePreparedStatement.bindInteger(11, i6 != 0 ? 1 : i6 == i2 ? i2 : 0);
                            sQLitePreparedStatement.bindInteger(12, !tLRPC$Message.mentioned ? 1 : 0);
                            sQLitePreparedStatement.bindInteger(13, tLRPC$Message.forwards);
                            if (tLRPC$Message.replies == null) {
                                nativeByteBuffer2 = new NativeByteBuffer(tLRPC$Message.replies.getObjectSize());
                                tLRPC$Message.replies.serializeToStream(nativeByteBuffer2);
                                sQLitePreparedStatement.bindByteBuffer(14, nativeByteBuffer2);
                            } else {
                                sQLitePreparedStatement.bindNull(14);
                                nativeByteBuffer2 = null;
                            }
                            tLRPC$MessageReplyHeader = tLRPC$Message.reply_to;
                            if (tLRPC$MessageReplyHeader == null) {
                                int i7 = tLRPC$MessageReplyHeader.reply_to_top_id;
                                if (i7 == 0) {
                                    i7 = tLRPC$MessageReplyHeader.reply_to_msg_id;
                                }
                                sQLitePreparedStatement.bindInteger(15, i7);
                            } else {
                                sQLitePreparedStatement.bindInteger(15, 0);
                            }
                            sQLitePreparedStatement.bindLong(16, MessageObject.getChannelId(tLRPC$Message));
                            writeLocalParams = MessageCustomParamsHelper.writeLocalParams(tLRPC$Message);
                            if (writeLocalParams == null) {
                                sQLitePreparedStatement.bindByteBuffer(16, writeLocalParams);
                            } else {
                                sQLitePreparedStatement.bindNull(17);
                            }
                            if ((tLRPC$Message.flags & 131072) == 0) {
                                sQLitePreparedStatement.bindLong(18, tLRPC$Message.grouped_id);
                            } else {
                                sQLitePreparedStatement.bindNull(18);
                            }
                            tLRPC$MessageReplyHeader2 = tLRPC$Message.reply_to;
                            if (tLRPC$MessageReplyHeader2 == null) {
                                sQLitePreparedStatement.bindInteger(19, tLRPC$MessageReplyHeader2.story_id);
                            } else {
                                sQLitePreparedStatement.bindInteger(19, 0);
                            }
                            sQLitePreparedStatement.step();
                            nativeByteBuffer3.reuse();
                            if (nativeByteBuffer2 != null) {
                                nativeByteBuffer2.reuse();
                            }
                            if (writeLocalParams != null) {
                                writeLocalParams.reuse();
                            }
                            if (nativeByteBuffer == null) {
                                nativeByteBuffer.reuse();
                            }
                            i5++;
                            i2 = 2;
                            i4 = 3;
                            i3 = 1;
                        }
                        i = 1;
                        sQLitePreparedStatement.bindInteger(7, i);
                        sQLitePreparedStatement.bindInteger(8, tLRPC$Message.ttl);
                        if ((tLRPC$Message.flags & 1024) == 0) {
                        }
                        if (tLRPC$Message.replyStory == null) {
                        }
                        int i62 = tLRPC$Message.stickerVerified;
                        sQLitePreparedStatement.bindInteger(11, i62 != 0 ? 1 : i62 == i2 ? i2 : 0);
                        sQLitePreparedStatement.bindInteger(12, !tLRPC$Message.mentioned ? 1 : 0);
                        sQLitePreparedStatement.bindInteger(13, tLRPC$Message.forwards);
                        if (tLRPC$Message.replies == null) {
                        }
                        tLRPC$MessageReplyHeader = tLRPC$Message.reply_to;
                        if (tLRPC$MessageReplyHeader == null) {
                        }
                        sQLitePreparedStatement.bindLong(16, MessageObject.getChannelId(tLRPC$Message));
                        writeLocalParams = MessageCustomParamsHelper.writeLocalParams(tLRPC$Message);
                        if (writeLocalParams == null) {
                        }
                        if ((tLRPC$Message.flags & 131072) == 0) {
                        }
                        tLRPC$MessageReplyHeader2 = tLRPC$Message.reply_to;
                        if (tLRPC$MessageReplyHeader2 == null) {
                        }
                        sQLitePreparedStatement.step();
                        nativeByteBuffer3.reuse();
                        if (nativeByteBuffer2 != null) {
                        }
                        if (writeLocalParams != null) {
                        }
                        if (nativeByteBuffer == null) {
                        }
                        i5++;
                        i2 = 2;
                        i4 = 3;
                        i3 = 1;
                    } catch (Exception e2) {
                        e = e2;
                        sQLiteCursor = null;
                        checkSQLException(e);
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        if (sQLitePreparedStatement == null) {
                            return;
                        }
                        sQLitePreparedStatement.dispose();
                    } catch (Throwable th3) {
                        th = th3;
                        sQLiteCursor = null;
                        if (sQLiteCursor != null) {
                        }
                        if (sQLitePreparedStatement != null) {
                        }
                        throw th;
                    }
                }
                if (sQLitePreparedStatement != null) {
                    sQLitePreparedStatement.dispose();
                    sQLitePreparedStatement = null;
                }
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$emptyMessagesMedia$88(arrayList5);
                    }
                });
            }
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$emptyMessagesMedia$89(arrayList3);
                }
            });
            getFileLoader().deleteFiles(arrayList2, 0);
            if (sQLitePreparedStatement == null) {
                return;
            }
        } catch (Exception e3) {
            e = e3;
            sQLitePreparedStatement = null;
        } catch (Throwable th4) {
            th = th4;
            sQLitePreparedStatement = null;
        }
        sQLitePreparedStatement.dispose();
    }

    public void lambda$emptyMessagesMedia$88(ArrayList arrayList) {
        for (int i = 0; i < arrayList.size(); i++) {
            getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.updateMessageMedia, arrayList.get(i));
        }
    }

    public void lambda$emptyMessagesMedia$89(ArrayList arrayList) {
        getFileLoader().cancelLoadFiles(arrayList);
    }

    private void lambda$emptyMessagesMedia$90(ArrayList arrayList) {
        if (getMessagesController().getSavedMessagesController().updateSavedDialogs(arrayList)) {
            getMessagesController().getSavedMessagesController().update();
        }
    }

    public void updateMessagePollResults(final long j, final TLRPC$Poll tLRPC$Poll, final TLRPC$PollResults tLRPC$PollResults) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateMessagePollResults$92(j, tLRPC$Poll, tLRPC$PollResults);
            }
        });
    }

    public void lambda$updateMessagePollResults$92(long j, TLRPC$Poll tLRPC$Poll, TLRPC$PollResults tLRPC$PollResults) {
        int i;
        int i2;
        LongSparseArray longSparseArray;
        int i3;
        ArrayList arrayList;
        SQLitePreparedStatement sQLitePreparedStatement;
        ?? r8;
        SQLiteCursor sQLiteCursor;
        int i4;
        SQLiteCursor sQLiteCursor2 = null;
        try {
            try {
                int i5 = 1;
                int i6 = 0;
                SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT uid, mid FROM polls_v2 WHERE id = %d", Long.valueOf(j)), new Object[0]);
                LongSparseArray longSparseArray2 = null;
                while (queryFinalized.next()) {
                    try {
                        long longValue = queryFinalized.longValue(0);
                        if (longSparseArray2 == null) {
                            longSparseArray2 = new LongSparseArray();
                        }
                        ArrayList arrayList2 = (ArrayList) longSparseArray2.get(longValue);
                        if (arrayList2 == null) {
                            arrayList2 = new ArrayList();
                            longSparseArray2.put(longValue, arrayList2);
                        }
                        arrayList2.add(Integer.valueOf(queryFinalized.intValue(1)));
                    } catch (Exception e) {
                        e = e;
                        sQLiteCursor2 = queryFinalized;
                    } catch (Throwable th) {
                        th = th;
                        sQLiteCursor2 = queryFinalized;
                    }
                }
                queryFinalized.dispose();
                if (longSparseArray2 != null) {
                    this.database.beginTransaction();
                    SQLitePreparedStatement executeFast = this.database.executeFast("UPDATE messages_v2 SET data = ? WHERE mid = ? AND uid = ?");
                    SQLitePreparedStatement executeFast2 = this.database.executeFast("UPDATE messages_topics SET data = ? WHERE mid = ? AND uid = ?");
                    int size = longSparseArray2.size();
                    int i7 = 0;
                    while (i7 < size) {
                        long keyAt = longSparseArray2.keyAt(i7);
                        ArrayList arrayList3 = (ArrayList) longSparseArray2.valueAt(i7);
                        int size2 = arrayList3.size();
                        int i8 = i6;
                        while (i8 < size2) {
                            Integer num = (Integer) arrayList3.get(i8);
                            SQLiteCursor sQLiteCursor3 = sQLiteCursor2;
                            int i9 = i6;
                            while (i6 < 2) {
                                if ((i6 == i5 ? i5 : 0) != 0) {
                                    try {
                                        longSparseArray = longSparseArray2;
                                        i3 = size;
                                        arrayList = arrayList3;
                                        sQLitePreparedStatement = executeFast2;
                                        r8 = 0;
                                        sQLiteCursor = this.database.queryFinalized(String.format(Locale.US, "SELECT data FROM messages_topics WHERE mid = %d AND uid = %d", num, Long.valueOf(keyAt)), new Object[0]);
                                    } catch (Exception e2) {
                                        e = e2;
                                        sQLiteCursor2 = sQLiteCursor3;
                                        checkSQLException(e);
                                        if (sQLiteCursor2 == null) {
                                            return;
                                        }
                                        sQLiteCursor2.dispose();
                                    } catch (Throwable th2) {
                                        th = th2;
                                        sQLiteCursor2 = sQLiteCursor3;
                                        if (sQLiteCursor2 != null) {
                                            sQLiteCursor2.dispose();
                                        }
                                        throw th;
                                    }
                                } else {
                                    longSparseArray = longSparseArray2;
                                    i3 = size;
                                    arrayList = arrayList3;
                                    r8 = 0;
                                    sQLitePreparedStatement = executeFast;
                                    sQLiteCursor = this.database.queryFinalized(String.format(Locale.US, "SELECT data FROM messages_v2 WHERE mid = %d AND uid = %d", num, Long.valueOf(keyAt)), new Object[0]);
                                }
                                if (sQLiteCursor.next()) {
                                    NativeByteBuffer byteBufferValue = sQLiteCursor.byteBufferValue(r8);
                                    if (byteBufferValue != 0) {
                                        TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(r8), r8);
                                        i4 = i7;
                                        TLdeserialize.readAttachPath(byteBufferValue, getUserConfig().clientUserId);
                                        byteBufferValue.reuse();
                                        TLRPC$MessageMedia tLRPC$MessageMedia = TLdeserialize.media;
                                        if (tLRPC$MessageMedia instanceof TLRPC$TL_messageMediaPoll) {
                                            TLRPC$TL_messageMediaPoll tLRPC$TL_messageMediaPoll = (TLRPC$TL_messageMediaPoll) tLRPC$MessageMedia;
                                            if (tLRPC$Poll != null) {
                                                tLRPC$TL_messageMediaPoll.poll = tLRPC$Poll;
                                            }
                                            if (tLRPC$PollResults != null) {
                                                MessageObject.updatePollResults(tLRPC$TL_messageMediaPoll, tLRPC$PollResults);
                                            }
                                            MessageObject.normalizeFlags(TLdeserialize);
                                            NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(TLdeserialize.getObjectSize());
                                            TLdeserialize.serializeToStream(nativeByteBuffer);
                                            sQLitePreparedStatement.requery();
                                            sQLitePreparedStatement.bindByteBuffer(1, nativeByteBuffer);
                                            sQLitePreparedStatement.bindInteger(2, num.intValue());
                                            sQLitePreparedStatement.bindLong(3, keyAt);
                                            sQLitePreparedStatement.step();
                                            nativeByteBuffer.reuse();
                                        }
                                    } else {
                                        i4 = i7;
                                    }
                                    i9 = 1;
                                } else {
                                    i4 = i7;
                                }
                                sQLiteCursor.dispose();
                                i6++;
                                sQLiteCursor3 = sQLiteCursor;
                                longSparseArray2 = longSparseArray;
                                size = i3;
                                arrayList3 = arrayList;
                                i7 = i4;
                                i5 = 1;
                            }
                            LongSparseArray longSparseArray3 = longSparseArray2;
                            int i10 = size;
                            int i11 = i7;
                            ArrayList arrayList4 = arrayList3;
                            if (i9 == 0) {
                                i = 0;
                                i2 = 1;
                                this.database.executeFast(String.format(Locale.US, "DELETE FROM polls_v2 WHERE mid = %d AND uid = %d", num, Long.valueOf(keyAt))).stepThis().dispose();
                            } else {
                                i = 0;
                                i2 = 1;
                            }
                            i8++;
                            sQLiteCursor2 = sQLiteCursor3;
                            i6 = i;
                            i5 = i2;
                            longSparseArray2 = longSparseArray3;
                            size = i10;
                            arrayList3 = arrayList4;
                            i7 = i11;
                        }
                        i7++;
                        longSparseArray2 = longSparseArray2;
                    }
                    executeFast.dispose();
                    executeFast2.dispose();
                    this.database.commitTransaction();
                }
                if (sQLiteCursor2 == null) {
                    return;
                }
            } catch (Throwable th3) {
                th = th3;
            }
        } catch (Exception e3) {
            e = e3;
        }
        sQLiteCursor2.dispose();
    }

    public void updateMessageReactions(final long j, final int i, final TLRPC$TL_messageReactions tLRPC$TL_messageReactions) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateMessageReactions$93(i, j, tLRPC$TL_messageReactions);
            }
        });
    }

    public void lambda$updateMessageReactions$93(int i, long j, TLRPC$TL_messageReactions tLRPC$TL_messageReactions) {
        SQLiteCursor queryFinalized;
        NativeByteBuffer byteBufferValue;
        SQLitePreparedStatement executeFast;
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                this.database.beginTransaction();
                for (int i2 = 0; i2 < 2; i2++) {
                    if (i2 == 0) {
                        queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT data FROM messages_v2 WHERE mid = %d AND uid = %d", Integer.valueOf(i), Long.valueOf(j)), new Object[0]);
                    } else {
                        queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT data FROM messages_topics WHERE mid = %d AND uid = %d", Integer.valueOf(i), Long.valueOf(j)), new Object[0]);
                    }
                    try {
                        if (queryFinalized.next() && (byteBufferValue = queryFinalized.byteBufferValue(0)) != null) {
                            TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                            if (TLdeserialize != null) {
                                TLdeserialize.readAttachPath(byteBufferValue, getUserConfig().clientUserId);
                                byteBufferValue.reuse();
                                MessageObject.updateReactions(TLdeserialize, tLRPC$TL_messageReactions);
                                if (i2 == 0) {
                                    executeFast = this.database.executeFast("UPDATE messages_v2 SET data = ? WHERE mid = ? AND uid = ?");
                                } else {
                                    executeFast = this.database.executeFast("UPDATE messages_topics SET data = ? WHERE mid = ? AND uid = ?");
                                }
                                MessageObject.normalizeFlags(TLdeserialize);
                                NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(TLdeserialize.getObjectSize());
                                TLdeserialize.serializeToStream(nativeByteBuffer);
                                executeFast.requery();
                                executeFast.bindByteBuffer(1, nativeByteBuffer);
                                executeFast.bindInteger(2, i);
                                executeFast.bindLong(3, j);
                                executeFast.step();
                                nativeByteBuffer.reuse();
                                executeFast.dispose();
                            } else {
                                byteBufferValue.reuse();
                            }
                        }
                        queryFinalized.dispose();
                    } catch (Exception e) {
                        e = e;
                        sQLiteCursor = queryFinalized;
                        checkSQLException(e);
                        SQLiteDatabase sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                            sQLiteDatabase.commitTransaction();
                        }
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                            return;
                        }
                        return;
                    } catch (Throwable th) {
                        th = th;
                        sQLiteCursor = queryFinalized;
                        SQLiteDatabase sQLiteDatabase2 = this.database;
                        if (sQLiteDatabase2 != null) {
                            sQLiteDatabase2.commitTransaction();
                        }
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        throw th;
                    }
                }
                this.database.commitTransaction();
                SQLiteDatabase sQLiteDatabase3 = this.database;
                if (sQLiteDatabase3 != null) {
                    sQLiteDatabase3.commitTransaction();
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Exception e2) {
            e = e2;
        }
    }

    public void updateMessageVoiceTranscriptionOpen(final long j, final int i, final TLRPC$Message tLRPC$Message) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateMessageVoiceTranscriptionOpen$94(i, j, tLRPC$Message);
            }
        });
    }

    public void lambda$updateMessageVoiceTranscriptionOpen$94(int i, long j, TLRPC$Message tLRPC$Message) {
        SQLitePreparedStatement executeFast;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                this.database.beginTransaction();
                TLRPC$Message messageWithCustomParamsOnlyInternal = getMessageWithCustomParamsOnlyInternal(i, j);
                messageWithCustomParamsOnlyInternal.voiceTranscriptionOpen = tLRPC$Message.voiceTranscriptionOpen;
                messageWithCustomParamsOnlyInternal.voiceTranscriptionRated = tLRPC$Message.voiceTranscriptionRated;
                messageWithCustomParamsOnlyInternal.voiceTranscriptionFinal = tLRPC$Message.voiceTranscriptionFinal;
                messageWithCustomParamsOnlyInternal.voiceTranscriptionForce = tLRPC$Message.voiceTranscriptionForce;
                messageWithCustomParamsOnlyInternal.voiceTranscriptionId = tLRPC$Message.voiceTranscriptionId;
                for (int i2 = 0; i2 < 2; i2++) {
                    if (i2 == 0) {
                        executeFast = this.database.executeFast("UPDATE messages_v2 SET custom_params = ? WHERE mid = ? AND uid = ?");
                    } else {
                        executeFast = this.database.executeFast("UPDATE messages_topics SET custom_params = ? WHERE mid = ? AND uid = ?");
                    }
                    try {
                        executeFast.requery();
                        NativeByteBuffer writeLocalParams = MessageCustomParamsHelper.writeLocalParams(messageWithCustomParamsOnlyInternal);
                        if (writeLocalParams != null) {
                            executeFast.bindByteBuffer(1, writeLocalParams);
                        } else {
                            executeFast.bindNull(1);
                        }
                        executeFast.bindInteger(2, i);
                        executeFast.bindLong(3, j);
                        executeFast.step();
                        executeFast.dispose();
                        if (writeLocalParams != null) {
                            writeLocalParams.reuse();
                        }
                    } catch (Exception e) {
                        e = e;
                        sQLitePreparedStatement = executeFast;
                        checkSQLException(e);
                        SQLiteDatabase sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                            sQLiteDatabase.commitTransaction();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                            return;
                        }
                        return;
                    } catch (Throwable th) {
                        th = th;
                        sQLitePreparedStatement = executeFast;
                        SQLiteDatabase sQLiteDatabase2 = this.database;
                        if (sQLiteDatabase2 != null) {
                            sQLiteDatabase2.commitTransaction();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                        throw th;
                    }
                }
                this.database.commitTransaction();
                SQLiteDatabase sQLiteDatabase3 = this.database;
                if (sQLiteDatabase3 != null) {
                    sQLiteDatabase3.commitTransaction();
                }
            } catch (Exception e2) {
                e = e2;
            }
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public void updateMessageVoiceTranscription(final long j, final int i, final String str, final long j2, final boolean z) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateMessageVoiceTranscription$95(i, j, z, j2, str);
            }
        });
    }

    public void lambda$updateMessageVoiceTranscription$95(int i, long j, boolean z, long j2, String str) {
        TLRPC$Message messageWithCustomParamsOnlyInternal;
        SQLitePreparedStatement executeFast;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                this.database.beginTransaction();
                messageWithCustomParamsOnlyInternal = getMessageWithCustomParamsOnlyInternal(i, j);
                messageWithCustomParamsOnlyInternal.voiceTranscriptionFinal = z;
                messageWithCustomParamsOnlyInternal.voiceTranscriptionId = j2;
                messageWithCustomParamsOnlyInternal.voiceTranscription = str;
                executeFast = this.database.executeFast("UPDATE messages_v2 SET custom_params = ? WHERE mid = ? AND uid = ?");
            } catch (Exception e) {
                e = e;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            executeFast.requery();
            NativeByteBuffer writeLocalParams = MessageCustomParamsHelper.writeLocalParams(messageWithCustomParamsOnlyInternal);
            if (writeLocalParams != null) {
                executeFast.bindByteBuffer(1, writeLocalParams);
            } else {
                executeFast.bindNull(1);
            }
            executeFast.bindInteger(2, i);
            executeFast.bindLong(3, j);
            executeFast.step();
            executeFast.dispose();
            this.database.commitTransaction();
            if (writeLocalParams != null) {
                writeLocalParams.reuse();
            }
            SQLiteDatabase sQLiteDatabase = this.database;
            if (sQLiteDatabase != null) {
                sQLiteDatabase.commitTransaction();
            }
        } catch (Exception e2) {
            e = e2;
            sQLitePreparedStatement = executeFast;
            checkSQLException(e);
            SQLiteDatabase sQLiteDatabase2 = this.database;
            if (sQLiteDatabase2 != null) {
                sQLiteDatabase2.commitTransaction();
            }
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
        } catch (Throwable th2) {
            th = th2;
            sQLitePreparedStatement = executeFast;
            SQLiteDatabase sQLiteDatabase3 = this.database;
            if (sQLiteDatabase3 != null) {
                sQLiteDatabase3.commitTransaction();
            }
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void updateMessageVoiceTranscription(final long j, final int i, final String str, final TLRPC$Message tLRPC$Message) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateMessageVoiceTranscription$96(i, j, tLRPC$Message, str);
            }
        });
    }

    public void lambda$updateMessageVoiceTranscription$96(int i, long j, TLRPC$Message tLRPC$Message, String str) {
        SQLitePreparedStatement executeFast;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                this.database.beginTransaction();
                TLRPC$Message messageWithCustomParamsOnlyInternal = getMessageWithCustomParamsOnlyInternal(i, j);
                messageWithCustomParamsOnlyInternal.voiceTranscriptionOpen = tLRPC$Message.voiceTranscriptionOpen;
                messageWithCustomParamsOnlyInternal.voiceTranscriptionRated = tLRPC$Message.voiceTranscriptionRated;
                messageWithCustomParamsOnlyInternal.voiceTranscriptionFinal = tLRPC$Message.voiceTranscriptionFinal;
                messageWithCustomParamsOnlyInternal.voiceTranscriptionForce = tLRPC$Message.voiceTranscriptionForce;
                messageWithCustomParamsOnlyInternal.voiceTranscriptionId = tLRPC$Message.voiceTranscriptionId;
                messageWithCustomParamsOnlyInternal.voiceTranscription = str;
                for (int i2 = 0; i2 < 2; i2++) {
                    if (i2 == 0) {
                        executeFast = this.database.executeFast("UPDATE messages_v2 SET custom_params = ? WHERE mid = ? AND uid = ?");
                    } else {
                        executeFast = this.database.executeFast("UPDATE messages_topics SET custom_params = ? WHERE mid = ? AND uid = ?");
                    }
                    try {
                        executeFast.requery();
                        NativeByteBuffer writeLocalParams = MessageCustomParamsHelper.writeLocalParams(messageWithCustomParamsOnlyInternal);
                        if (writeLocalParams != null) {
                            executeFast.bindByteBuffer(1, writeLocalParams);
                        } else {
                            executeFast.bindNull(1);
                        }
                        executeFast.bindInteger(2, i);
                        executeFast.bindLong(3, j);
                        executeFast.step();
                        executeFast.dispose();
                        this.database.commitTransaction();
                        if (writeLocalParams != null) {
                            writeLocalParams.reuse();
                        }
                    } catch (Exception e) {
                        e = e;
                        sQLitePreparedStatement = executeFast;
                        checkSQLException(e);
                        SQLiteDatabase sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                            sQLiteDatabase.commitTransaction();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                            return;
                        }
                        return;
                    } catch (Throwable th) {
                        th = th;
                        sQLitePreparedStatement = executeFast;
                        SQLiteDatabase sQLiteDatabase2 = this.database;
                        if (sQLiteDatabase2 != null) {
                            sQLiteDatabase2.commitTransaction();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                        throw th;
                    }
                }
                SQLiteDatabase sQLiteDatabase3 = this.database;
                if (sQLiteDatabase3 != null) {
                    sQLiteDatabase3.commitTransaction();
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Exception e2) {
            e = e2;
        }
    }

    public void updateMessageCustomParams(final long j, final TLRPC$Message tLRPC$Message) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateMessageCustomParams$97(tLRPC$Message, j);
            }
        });
    }

    public void lambda$updateMessageCustomParams$97(TLRPC$Message tLRPC$Message, long j) {
        SQLitePreparedStatement executeFast;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                this.database.beginTransaction();
                TLRPC$Message messageWithCustomParamsOnlyInternal = getMessageWithCustomParamsOnlyInternal(tLRPC$Message.id, j);
                MessageCustomParamsHelper.copyParams(tLRPC$Message, messageWithCustomParamsOnlyInternal);
                for (int i = 0; i < 2; i++) {
                    if (i == 0) {
                        executeFast = this.database.executeFast("UPDATE messages_v2 SET custom_params = ? WHERE mid = ? AND uid = ?");
                    } else {
                        executeFast = this.database.executeFast("UPDATE messages_topics SET custom_params = ? WHERE mid = ? AND uid = ?");
                    }
                    try {
                        executeFast.requery();
                        NativeByteBuffer writeLocalParams = MessageCustomParamsHelper.writeLocalParams(messageWithCustomParamsOnlyInternal);
                        if (writeLocalParams != null) {
                            executeFast.bindByteBuffer(1, writeLocalParams);
                        } else {
                            executeFast.bindNull(1);
                        }
                        executeFast.bindInteger(2, tLRPC$Message.id);
                        executeFast.bindLong(3, j);
                        executeFast.step();
                        executeFast.dispose();
                        if (writeLocalParams != null) {
                            writeLocalParams.reuse();
                        }
                    } catch (Exception e) {
                        e = e;
                        sQLitePreparedStatement = executeFast;
                        checkSQLException(e);
                        SQLiteDatabase sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                            sQLiteDatabase.commitTransaction();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                            return;
                        }
                        return;
                    } catch (Throwable th) {
                        th = th;
                        sQLitePreparedStatement = executeFast;
                        SQLiteDatabase sQLiteDatabase2 = this.database;
                        if (sQLiteDatabase2 != null) {
                            sQLiteDatabase2.commitTransaction();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                        throw th;
                    }
                }
                this.database.commitTransaction();
                SQLiteDatabase sQLiteDatabase3 = this.database;
                if (sQLiteDatabase3 != null) {
                    sQLiteDatabase3.commitTransaction();
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Exception e2) {
            e = e2;
        }
    }

    public TLRPC$Message getMessageWithCustomParamsOnlyInternal(int i, long j) {
        SQLiteCursor queryFinalized;
        boolean z;
        TLRPC$TL_message tLRPC$TL_message = new TLRPC$TL_message();
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                queryFinalized = this.database.queryFinalized("SELECT custom_params FROM messages_v2 WHERE mid = " + i + " AND uid = " + j, new Object[0]);
            } catch (Throwable th) {
                th = th;
            }
        } catch (SQLiteException e) {
            e = e;
        }
        try {
            if (queryFinalized.next()) {
                MessageCustomParamsHelper.readLocalParams(tLRPC$TL_message, queryFinalized.byteBufferValue(0));
                z = true;
            } else {
                z = false;
            }
            queryFinalized.dispose();
            if (!z) {
                sQLiteCursor = this.database.queryFinalized("SELECT custom_params FROM messages_topics WHERE mid = " + i + " AND uid = " + j, new Object[0]);
                if (sQLiteCursor.next()) {
                    MessageCustomParamsHelper.readLocalParams(tLRPC$TL_message, sQLiteCursor.byteBufferValue(0));
                }
                sQLiteCursor.dispose();
            }
        } catch (SQLiteException e2) {
            e = e2;
            sQLiteCursor = queryFinalized;
            checkSQLException(e);
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            return tLRPC$TL_message;
        } catch (Throwable th2) {
            th = th2;
            sQLiteCursor = queryFinalized;
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
        return tLRPC$TL_message;
    }

    public void getNewTask(final LongSparseArray<ArrayList<Integer>> longSparseArray, final LongSparseArray<ArrayList<Integer>> longSparseArray2) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getNewTask$98(longSparseArray, longSparseArray2);
            }
        });
    }

    public void lambda$getNewTask$98(LongSparseArray longSparseArray, LongSparseArray longSparseArray2) {
        Throwable th;
        Exception e;
        boolean z;
        LongSparseArray<ArrayList<Integer>> longSparseArray3;
        SQLiteCursor sQLiteCursor = null;
        if (longSparseArray != null) {
            try {
                try {
                    int size = longSparseArray.size();
                    for (int i = 0; i < size; i++) {
                        this.database.executeFast(String.format(Locale.US, "DELETE FROM enc_tasks_v4 WHERE mid IN(%s) AND uid = %d AND media = 0", TextUtils.join(",", (Iterable) longSparseArray.valueAt(i)), Long.valueOf(longSparseArray.keyAt(i)))).stepThis().dispose();
                    }
                } catch (Exception e2) {
                    e = e2;
                    checkSQLException(e);
                    if (sQLiteCursor == null) {
                        sQLiteCursor.dispose();
                        return;
                    }
                    return;
                }
            } catch (Throwable th2) {
                th = th2;
                if (sQLiteCursor != null) {
                    sQLiteCursor.dispose();
                }
                throw th;
            }
        }
        if (longSparseArray2 != null) {
            int size2 = longSparseArray2.size();
            for (int i2 = 0; i2 < size2; i2++) {
                this.database.executeFast(String.format(Locale.US, "DELETE FROM enc_tasks_v4 WHERE mid IN(%s) AND uid = %d AND media = 1", TextUtils.join(",", (Iterable) longSparseArray2.valueAt(i2)), Long.valueOf(longSparseArray2.keyAt(i2)))).stepThis().dispose();
            }
        }
        SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT mid, date, media, uid FROM enc_tasks_v4 WHERE date = (SELECT min(date) FROM enc_tasks_v4)", new Object[0]);
        LongSparseArray<ArrayList<Integer>> longSparseArray4 = null;
        LongSparseArray<ArrayList<Integer>> longSparseArray5 = null;
        int i3 = 0;
        while (queryFinalized.next()) {
            try {
                int intValue = queryFinalized.intValue(0);
                int intValue2 = queryFinalized.intValue(1);
                int intValue3 = queryFinalized.intValue(2);
                long longValue = queryFinalized.longValue(3);
                if (intValue3 != -1) {
                    z = intValue3 != 0;
                }
                if (z) {
                    if (longSparseArray5 == null) {
                        longSparseArray5 = new LongSparseArray<>();
                    }
                    longSparseArray3 = longSparseArray5;
                } else {
                    if (longSparseArray4 == null) {
                        longSparseArray4 = new LongSparseArray<>();
                    }
                    longSparseArray3 = longSparseArray5;
                    longSparseArray5 = longSparseArray4;
                }
                ArrayList<Integer> arrayList = longSparseArray5.get(longValue);
                if (arrayList == null) {
                    arrayList = new ArrayList<>();
                    longSparseArray5.put(longValue, arrayList);
                }
                arrayList.add(Integer.valueOf(intValue));
                i3 = intValue2;
                longSparseArray5 = longSparseArray3;
            } catch (Exception e3) {
                sQLiteCursor = queryFinalized;
                e = e3;
                checkSQLException(e);
                if (sQLiteCursor == null) {
                }
            } catch (Throwable th3) {
                sQLiteCursor = queryFinalized;
                th = th3;
                if (sQLiteCursor != null) {
                }
                throw th;
            }
        }
        queryFinalized.dispose();
        getMessagesController().processLoadedDeleteTask(i3, longSparseArray4, longSparseArray5);
    }

    public void markMentionMessageAsRead(final long j, final int i, final long j2) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$markMentionMessageAsRead$99(i, j, j2);
            }
        });
    }

    public void lambda$markMentionMessageAsRead$99(int i, long j, long j2) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                SQLiteDatabase sQLiteDatabase = this.database;
                Locale locale = Locale.US;
                sQLiteDatabase.executeFast(String.format(locale, "UPDATE messages_v2 SET read_state = read_state | 2 WHERE mid = %d AND uid = %d", Integer.valueOf(i), Long.valueOf(j))).stepThis().dispose();
                SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT unread_count_i FROM dialogs WHERE did = " + j2, new Object[0]);
                try {
                    int max = queryFinalized.next() ? Math.max(0, queryFinalized.intValue(0) - 1) : 0;
                    queryFinalized.dispose();
                    this.database.executeFast(String.format(locale, "UPDATE dialogs SET unread_count_i = %d WHERE did = %d", Integer.valueOf(max), Long.valueOf(j2))).stepThis().dispose();
                    LongSparseIntArray longSparseIntArray = new LongSparseIntArray(1);
                    longSparseIntArray.put(j2, max);
                    if (max == 0) {
                        updateFiltersReadCounter(null, longSparseIntArray, true);
                    }
                    getMessagesController().processDialogsUpdateRead(null, longSparseIntArray);
                    this.database.executeFast(String.format(locale, "UPDATE messages_topics SET read_state = read_state | 2 WHERE mid = %d AND uid = %d", Integer.valueOf(i), Long.valueOf(j))).stepThis().dispose();
                    SQLiteCursor queryFinalized2 = this.database.queryFinalized(String.format(locale, "SELECT data FROM messages_topics WHERE mid = %d AND uid = %d", Integer.valueOf(i), Long.valueOf(j)), new Object[0]);
                    long j3 = 0;
                    while (queryFinalized2.next()) {
                        try {
                            NativeByteBuffer byteBufferValue = queryFinalized2.byteBufferValue(0);
                            if (byteBufferValue != null) {
                                TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                                byteBufferValue.reuse();
                                j3 = MessageObject.getTopicId(this.currentAccount, TLdeserialize, isForum(j));
                            }
                        } catch (Exception e) {
                            e = e;
                            sQLiteCursor = queryFinalized2;
                        } catch (Throwable th) {
                            th = th;
                            sQLiteCursor = queryFinalized2;
                        }
                    }
                    queryFinalized2.dispose();
                    if (j3 != 0) {
                        SQLiteDatabase sQLiteDatabase2 = this.database;
                        Locale locale2 = Locale.US;
                        SQLiteCursor queryFinalized3 = sQLiteDatabase2.queryFinalized(String.format(locale2, "SELECT unread_mentions FROM topics WHERE did = %d AND topic_id = %d", Long.valueOf(j2), Long.valueOf(j3)), new Object[0]);
                        try {
                            int max2 = queryFinalized3.next() ? Math.max(0, queryFinalized3.intValue(0) - 1) : 0;
                            queryFinalized3.dispose();
                            this.database.executeFast(String.format(locale2, "UPDATE topics SET unread_mentions = %d WHERE did = %d AND topic_id = %d", Integer.valueOf(max2), Long.valueOf(j), Long.valueOf(j3))).stepThis().dispose();
                            getMessagesController().getTopicsController().updateMentionsUnread(j, j3, max2);
                        } catch (Exception e2) {
                            e = e2;
                            sQLiteCursor = queryFinalized3;
                            checkSQLException(e);
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                            }
                        } catch (Throwable th2) {
                            th = th2;
                            sQLiteCursor = queryFinalized3;
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                            }
                            throw th;
                        }
                    }
                } catch (Exception e3) {
                    e = e3;
                    sQLiteCursor = queryFinalized;
                } catch (Throwable th3) {
                    th = th3;
                    sQLiteCursor = queryFinalized;
                }
            } catch (Exception e4) {
                e = e4;
            }
        } catch (Throwable th4) {
            th = th4;
        }
    }

    public void markMessageAsMention(final long j, final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$markMessageAsMention$100(i, j);
            }
        });
    }

    public void lambda$markMessageAsMention$100(int i, long j) {
        try {
            this.database.executeFast(String.format(Locale.US, "UPDATE messages_v2 SET mention = 1, read_state = read_state & ~2 WHERE mid = %d AND uid = %d", Integer.valueOf(i), Long.valueOf(j))).stepThis().dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void resetMentionsCount(final long j, final long j2, final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$resetMentionsCount$101(j2, j, i);
            }
        });
    }

    public void lambda$resetMentionsCount$101(long j, long j2, int i) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                if (j == 0) {
                    SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT unread_count_i FROM dialogs WHERE did = " + j2, new Object[0]);
                    try {
                        int intValue = queryFinalized.next() ? queryFinalized.intValue(0) : 0;
                        queryFinalized.dispose();
                        if (intValue == 0 && i == 0) {
                            return;
                        }
                        if (i == 0) {
                            this.database.executeFast(String.format(Locale.US, "UPDATE messages_v2 SET read_state = read_state | 2 WHERE uid = %d AND mention = 1 AND read_state IN(0, 1)", Long.valueOf(j2))).stepThis().dispose();
                        }
                        this.database.executeFast(String.format(Locale.US, "UPDATE dialogs SET unread_count_i = %d WHERE did = %d", Integer.valueOf(i), Long.valueOf(j2))).stepThis().dispose();
                        LongSparseIntArray longSparseIntArray = new LongSparseIntArray(1);
                        longSparseIntArray.put(j2, i);
                        getMessagesController().processDialogsUpdateRead(null, longSparseIntArray);
                        if (i == 0) {
                            updateFiltersReadCounter(null, longSparseIntArray, true);
                            return;
                        }
                        return;
                    } catch (Exception e) {
                        e = e;
                        sQLiteCursor = queryFinalized;
                        checkSQLException(e);
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                            return;
                        }
                        return;
                    } catch (Throwable th) {
                        th = th;
                        sQLiteCursor = queryFinalized;
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        throw th;
                    }
                }
                this.database.executeFast(String.format(Locale.US, "UPDATE topics SET unread_mentions = %d WHERE did = %d AND topic_id = %d", Integer.valueOf(i), Long.valueOf(j2), Long.valueOf(j))).stepThis().dispose();
                TopicsController.TopicUpdate topicUpdate = new TopicsController.TopicUpdate();
                topicUpdate.dialogId = j2;
                topicUpdate.topicId = j;
                topicUpdate.onlyCounters = true;
                topicUpdate.unreadMentions = i;
                topicUpdate.unreadCount = -1;
                getMessagesController().getTopicsController().processUpdate(Collections.singletonList(topicUpdate));
            } catch (Exception e2) {
                e = e2;
            }
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public void createTaskForMid(final long j, final int i, final int i2, final int i3, final int i4, final boolean z) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$createTaskForMid$103(i2, i3, i4, i, z, j);
            }
        });
    }

    public void lambda$createTaskForMid$103(int i, int i2, int i3, int i4, final boolean z, final long j) {
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                int max = Math.max(i, i2) + i3;
                SparseArray<ArrayList<Integer>> sparseArray = new SparseArray<>();
                final ArrayList<Integer> arrayList = new ArrayList<>();
                arrayList.add(Integer.valueOf(i4));
                sparseArray.put(max, arrayList);
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$createTaskForMid$102(z, j, arrayList);
                    }
                });
                SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO enc_tasks_v4 VALUES(?, ?, ?, ?)");
                for (int i5 = 0; i5 < sparseArray.size(); i5++) {
                    try {
                        int keyAt = sparseArray.keyAt(i5);
                        ArrayList<Integer> arrayList2 = sparseArray.get(keyAt);
                        for (int i6 = 0; i6 < arrayList2.size(); i6++) {
                            executeFast.requery();
                            executeFast.bindInteger(1, arrayList2.get(i6).intValue());
                            executeFast.bindLong(2, j);
                            executeFast.bindInteger(3, keyAt);
                            executeFast.bindInteger(4, 1);
                            executeFast.step();
                        }
                    } catch (Exception e) {
                        e = e;
                        sQLitePreparedStatement = executeFast;
                        checkSQLException(e);
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                    } catch (Throwable th) {
                        th = th;
                        sQLitePreparedStatement = executeFast;
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                        throw th;
                    }
                }
                executeFast.dispose();
                this.database.executeFast(String.format(Locale.US, "UPDATE messages_v2 SET ttl = 0 WHERE mid = %d AND uid = %d", Integer.valueOf(i4), Long.valueOf(j))).stepThis().dispose();
                getMessagesController().didAddedNewTask(max, j, sparseArray);
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Exception e2) {
            e = e2;
        }
    }

    public void lambda$createTaskForMid$102(boolean z, long j, ArrayList arrayList) {
        if (!z) {
            markMessagesContentAsRead(j, arrayList, 0, 0);
        }
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.messagesReadContent, Long.valueOf(j), arrayList);
    }

    private void createTaskForSecretMedia(long j, SparseArray<ArrayList<Integer>> sparseArray) {
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLiteDatabase sQLiteDatabase;
        int i = ConnectionsManager.DEFAULT_DATACENTER_ID;
        try {
            ArrayList arrayList = new ArrayList();
            if (sparseArray.size() != 0) {
                this.database.beginTransaction();
                SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO enc_tasks_v4 VALUES(?, ?, ?, ?)");
                for (int i2 = 0; i2 < sparseArray.size(); i2++) {
                    try {
                        int keyAt = sparseArray.keyAt(i2);
                        ArrayList<Integer> arrayList2 = sparseArray.get(keyAt);
                        for (int i3 = 0; i3 < arrayList2.size(); i3++) {
                            int intValue = arrayList2.get(i3).intValue();
                            executeFast.requery();
                            executeFast.bindInteger(1, intValue);
                            executeFast.bindLong(2, j);
                            executeFast.bindInteger(3, keyAt);
                            executeFast.bindInteger(4, 1);
                            i = Math.min(i, intValue);
                            executeFast.step();
                            arrayList.add(arrayList2.get(i3));
                        }
                    } catch (Exception e) {
                        e = e;
                        sQLitePreparedStatement = executeFast;
                        try {
                            checkSQLException(e);
                            SQLiteDatabase sQLiteDatabase2 = this.database;
                            if (sQLiteDatabase2 != null) {
                                sQLiteDatabase2.commitTransaction();
                            }
                            if (sQLitePreparedStatement != null) {
                                sQLitePreparedStatement.dispose();
                                return;
                            }
                            return;
                        } catch (Throwable th) {
                            th = th;
                            sQLiteDatabase = this.database;
                            if (sQLiteDatabase != null) {
                                sQLiteDatabase.commitTransaction();
                            }
                            if (sQLitePreparedStatement != null) {
                                sQLitePreparedStatement.dispose();
                            }
                            throw th;
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        sQLitePreparedStatement = executeFast;
                        sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                        }
                        if (sQLitePreparedStatement != null) {
                        }
                        throw th;
                    }
                }
                executeFast.dispose();
                this.database.commitTransaction();
                this.database.executeFast(String.format(Locale.US, "UPDATE messages_v2 SET ttl = 0 WHERE uid = %d AND mid IN(%s)", Long.valueOf(j), TextUtils.join(", ", arrayList))).stepThis().dispose();
                getMessagesController().didAddedNewTask(i, j, sparseArray);
            }
            SQLiteDatabase sQLiteDatabase3 = this.database;
            if (sQLiteDatabase3 != null) {
                sQLiteDatabase3.commitTransaction();
            }
        } catch (Exception e2) {
            e = e2;
            sQLitePreparedStatement = null;
        } catch (Throwable th3) {
            th = th3;
            sQLitePreparedStatement = null;
        }
    }

    public void createTaskForSecretChat(final int i, final int i2, final int i3, final int i4, final ArrayList<Long> arrayList) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$createTaskForSecretChat$105(i, arrayList, i4, i2, i3);
            }
        });
    }

    public void lambda$createTaskForSecretChat$105(int i, ArrayList arrayList, int i2, int i3, int i4) {
        SQLiteCursor sQLiteCursor;
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLiteDatabase sQLiteDatabase;
        SQLiteDatabase sQLiteDatabase2;
        SQLiteCursor queryFinalized;
        try {
            final long makeEncryptedDialogId = DialogObject.makeEncryptedDialogId(i);
            SparseArray<ArrayList<Integer>> sparseArray = new SparseArray<>();
            final ArrayList arrayList2 = new ArrayList();
            StringBuilder sb = new StringBuilder();
            if (arrayList == null) {
                queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT mid, ttl FROM messages_v2 WHERE uid = %d AND out = %d AND read_state > 0 AND ttl > 0 AND date <= %d AND send_state = 0 AND media != 1", Long.valueOf(makeEncryptedDialogId), Integer.valueOf(i2), Integer.valueOf(i3)), new Object[0]);
            } else {
                queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT m.mid, m.ttl FROM messages_v2 as m INNER JOIN randoms_v2 as r ON m.mid = r.mid AND m.uid = r.uid WHERE r.random_id IN (%s)", TextUtils.join(",", arrayList)), new Object[0]);
            }
            int i5 = ConnectionsManager.DEFAULT_DATACENTER_ID;
            while (queryFinalized.next()) {
                try {
                    int intValue = queryFinalized.intValue(1);
                    int intValue2 = queryFinalized.intValue(0);
                    if (arrayList != null) {
                        arrayList2.add(Integer.valueOf(intValue2));
                    }
                    if (intValue > 0) {
                        int max = Math.max(i3, i4) + intValue;
                        i5 = Math.min(i5, max);
                        ArrayList<Integer> arrayList3 = sparseArray.get(max);
                        if (arrayList3 == null) {
                            arrayList3 = new ArrayList<>();
                            sparseArray.put(max, arrayList3);
                        }
                        if (sb.length() != 0) {
                            sb.append(",");
                        }
                        sb.append(intValue2);
                        arrayList3.add(Integer.valueOf(intValue2));
                    }
                } catch (Exception e) {
                    e = e;
                    sQLiteCursor = queryFinalized;
                    sQLitePreparedStatement = null;
                    try {
                        checkSQLException(e);
                        sQLiteDatabase2 = this.database;
                        if (sQLiteDatabase2 != null) {
                        }
                        if (sQLitePreparedStatement != null) {
                        }
                        if (sQLiteCursor == null) {
                        }
                    } catch (Throwable th) {
                        th = th;
                        sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                            sQLiteDatabase.commitTransaction();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        throw th;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    sQLiteCursor = queryFinalized;
                    sQLitePreparedStatement = null;
                    sQLiteDatabase = this.database;
                    if (sQLiteDatabase != null) {
                    }
                    if (sQLitePreparedStatement != null) {
                    }
                    if (sQLiteCursor != null) {
                    }
                    throw th;
                }
            }
            queryFinalized.dispose();
            if (arrayList != null) {
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$createTaskForSecretChat$104(makeEncryptedDialogId, arrayList2);
                    }
                });
            }
            if (sparseArray.size() != 0) {
                this.database.beginTransaction();
                sQLitePreparedStatement = this.database.executeFast("REPLACE INTO enc_tasks_v4 VALUES(?, ?, ?, ?)");
                for (int i6 = 0; i6 < sparseArray.size(); i6++) {
                    try {
                        int keyAt = sparseArray.keyAt(i6);
                        ArrayList<Integer> arrayList4 = sparseArray.get(keyAt);
                        for (int i7 = 0; i7 < arrayList4.size(); i7++) {
                            sQLitePreparedStatement.requery();
                            sQLitePreparedStatement.bindInteger(1, arrayList4.get(i7).intValue());
                            sQLitePreparedStatement.bindLong(2, makeEncryptedDialogId);
                            sQLitePreparedStatement.bindInteger(3, keyAt);
                            sQLitePreparedStatement.bindInteger(4, 0);
                            sQLitePreparedStatement.step();
                        }
                    } catch (Exception e2) {
                        e = e2;
                        sQLiteCursor = null;
                        checkSQLException(e);
                        sQLiteDatabase2 = this.database;
                        if (sQLiteDatabase2 != null) {
                            sQLiteDatabase2.commitTransaction();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                        if (sQLiteCursor == null) {
                            sQLiteCursor.dispose();
                            return;
                        }
                        return;
                    } catch (Throwable th3) {
                        th = th3;
                        sQLiteCursor = null;
                        sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                        }
                        if (sQLitePreparedStatement != null) {
                        }
                        if (sQLiteCursor != null) {
                        }
                        throw th;
                    }
                }
                sQLitePreparedStatement.dispose();
                this.database.commitTransaction();
                this.database.executeFast(String.format(Locale.US, "UPDATE messages_v2 SET ttl = 0 WHERE mid IN(%s) AND uid = %d", sb.toString(), Long.valueOf(makeEncryptedDialogId))).stepThis().dispose();
                getMessagesController().didAddedNewTask(i5, makeEncryptedDialogId, sparseArray);
            }
            SQLiteDatabase sQLiteDatabase3 = this.database;
            if (sQLiteDatabase3 != null) {
                sQLiteDatabase3.commitTransaction();
            }
        } catch (Exception e3) {
            e = e3;
            sQLiteCursor = null;
        } catch (Throwable th4) {
            th = th4;
            sQLiteCursor = null;
        }
    }

    public void lambda$createTaskForSecretChat$104(long j, ArrayList arrayList) {
        markMessagesContentAsRead(j, arrayList, 0, 0);
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.messagesReadContent, Long.valueOf(j), arrayList);
    }

    private void updateFiltersReadCounter(LongSparseIntArray longSparseIntArray, LongSparseIntArray longSparseIntArray2, boolean z) throws Exception {
        int i;
        LongSparseArray longSparseArray;
        LongSparseArray longSparseArray2;
        int size;
        int i2;
        MessagesController.DialogFilter dialogFilter;
        int i3;
        int i4;
        int i5;
        int i6;
        LongSparseArray longSparseArray3;
        int i7;
        LongSparseArray longSparseArray4;
        LongSparseArray longSparseArray5;
        int i8;
        int i9;
        LongSparseArray longSparseArray6;
        LongSparseArray longSparseArray7;
        LongSparseArray longSparseArray8;
        LongSparseArray longSparseArray9;
        LongSparseArray longSparseArray10;
        LongSparseArray longSparseArray11;
        LongSparseArray longSparseArray12;
        LongSparseArray longSparseArray13;
        long j;
        char c;
        char c2;
        char c3;
        int i10;
        int i11;
        char c4;
        int i12;
        int i13;
        char c5;
        char c6;
        char c7;
        char c8;
        int i14;
        int i15;
        LongSparseArray longSparseArray14;
        int i16;
        int i17;
        LongSparseArray longSparseArray15;
        int i18;
        int intValue;
        int i19;
        LongSparseArray longSparseArray16;
        int i20;
        int i21;
        LongSparseArray longSparseArray17;
        int i22;
        LongSparseArray longSparseArray18;
        int i23;
        LongSparseArray longSparseArray19;
        LongSparseArray longSparseArray20;
        int i24;
        LongSparseArray longSparseArray21;
        int intValue2;
        int i25;
        char c9;
        char c10;
        char c11;
        int i26;
        int i27;
        char c12;
        int i28;
        int i29;
        char c13;
        char c14;
        char c15;
        char c16;
        int i30;
        ArrayList<TLRPC$Chat> arrayList;
        LongSparseArray longSparseArray22;
        LongSparseArray longSparseArray23;
        int i31;
        LongSparseArray longSparseArray24;
        int i32;
        int i33;
        ArrayList<TLRPC$User> arrayList2;
        LongSparseArray longSparseArray25;
        LongSparseIntArray longSparseIntArray3;
        LongSparseIntArray longSparseIntArray4 = longSparseIntArray2;
        if ((longSparseIntArray == null || longSparseIntArray.size() == 0) && (longSparseIntArray4 == null || longSparseIntArray2.size() == 0)) {
            return;
        }
        int i34 = 0;
        int i35 = 0;
        while (true) {
            if (i35 >= 2) {
                break;
            }
            for (int i36 = 0; i36 < 2; i36++) {
                int[] iArr = this.contacts[i35];
                int[] iArr2 = this.nonContacts[i35];
                int[] iArr3 = this.bots[i35];
                int[] iArr4 = this.channels[i35];
                this.groups[i35][i36] = 0;
                iArr4[i36] = 0;
                iArr3[i36] = 0;
                iArr2[i36] = 0;
                iArr[i36] = 0;
            }
            int[] iArr5 = this.mentionChannels;
            this.mentionGroups[i35] = 0;
            iArr5[i35] = 0;
            i35++;
        }
        ArrayList<TLRPC$User> arrayList3 = new ArrayList<>();
        ArrayList<TLRPC$User> arrayList4 = new ArrayList<>();
        ArrayList<TLRPC$Chat> arrayList5 = new ArrayList<>();
        ArrayList arrayList6 = new ArrayList();
        ArrayList arrayList7 = new ArrayList();
        ArrayList arrayList8 = new ArrayList();
        LongSparseArray longSparseArray26 = new LongSparseArray();
        LongSparseArray longSparseArray27 = new LongSparseArray();
        int i37 = 0;
        for (i = 2; i37 < i; i = 2) {
            LongSparseIntArray longSparseIntArray5 = i37 == 0 ? longSparseIntArray : longSparseIntArray4;
            if (longSparseIntArray5 != null) {
                int i38 = i34;
                while (i38 < longSparseIntArray5.size()) {
                    Integer valueOf = Integer.valueOf(longSparseIntArray5.valueAt(i38));
                    if ((!z || valueOf.intValue() == 0) && (z || valueOf.intValue() != 0)) {
                        arrayList2 = arrayList4;
                        long keyAt = longSparseIntArray5.keyAt(i38);
                        if (!z) {
                            if (this.dialogsWithMentions.indexOfKey(keyAt) < 0 && this.dialogsWithUnread.indexOfKey(keyAt) < 0) {
                                longSparseArray27.put(keyAt, valueOf);
                            }
                            if (i37 == 0) {
                                this.dialogsWithUnread.put(keyAt, valueOf);
                            } else {
                                this.dialogsWithMentions.put(keyAt, valueOf);
                            }
                        } else if (i37 == 0) {
                            this.dialogsWithUnread.remove(keyAt);
                        } else {
                            this.dialogsWithMentions.remove(keyAt);
                        }
                        if (longSparseArray26.indexOfKey(keyAt) < 0) {
                            SQLiteDatabase sQLiteDatabase = this.database;
                            StringBuilder sb = new StringBuilder();
                            longSparseIntArray3 = longSparseIntArray5;
                            sb.append("SELECT folder_id FROM dialogs WHERE did = ");
                            sb.append(keyAt);
                            longSparseArray25 = longSparseArray27;
                            SQLiteCursor queryFinalized = sQLiteDatabase.queryFinalized(sb.toString(), new Object[0]);
                            int intValue3 = queryFinalized.next() ? queryFinalized.intValue(0) : 0;
                            queryFinalized.dispose();
                            longSparseArray26.put(keyAt, Integer.valueOf(intValue3));
                        } else {
                            longSparseArray25 = longSparseArray27;
                            longSparseIntArray3 = longSparseIntArray5;
                        }
                        if (DialogObject.isEncryptedDialog(keyAt)) {
                            int encryptedChatId = DialogObject.getEncryptedChatId(keyAt);
                            if (!arrayList8.contains(Integer.valueOf(encryptedChatId))) {
                                arrayList8.add(Integer.valueOf(encryptedChatId));
                            }
                        } else if (DialogObject.isUserDialog(keyAt)) {
                            if (!arrayList6.contains(Long.valueOf(keyAt))) {
                                arrayList6.add(Long.valueOf(keyAt));
                            }
                        } else {
                            long j2 = -keyAt;
                            if (!arrayList7.contains(Long.valueOf(j2))) {
                                arrayList7.add(Long.valueOf(j2));
                            }
                        }
                    } else {
                        arrayList2 = arrayList4;
                        longSparseArray25 = longSparseArray27;
                        longSparseIntArray3 = longSparseIntArray5;
                    }
                    i38++;
                    arrayList4 = arrayList2;
                    longSparseIntArray5 = longSparseIntArray3;
                    longSparseArray27 = longSparseArray25;
                }
            }
            i37++;
            arrayList4 = arrayList4;
            longSparseArray27 = longSparseArray27;
            i34 = 0;
        }
        ArrayList<TLRPC$User> arrayList9 = arrayList4;
        LongSparseArray longSparseArray28 = longSparseArray27;
        LongSparseArray longSparseArray29 = new LongSparseArray();
        LongSparseArray longSparseArray30 = new LongSparseArray();
        LongSparseArray longSparseArray31 = new LongSparseArray();
        LongSparseArray longSparseArray32 = new LongSparseArray();
        LongSparseArray longSparseArray33 = new LongSparseArray();
        LongSparseArray longSparseArray34 = new LongSparseArray();
        LongSparseArray longSparseArray35 = longSparseArray30;
        LongSparseArray longSparseArray36 = longSparseArray31;
        if (!arrayList6.isEmpty()) {
            getUsersInternal(TextUtils.join(",", arrayList6), arrayList3);
            int size2 = arrayList3.size();
            int i39 = 0;
            while (i39 < size2) {
                TLRPC$User tLRPC$User = arrayList3.get(i39);
                ArrayList<TLRPC$User> arrayList10 = arrayList3;
                ArrayList<TLRPC$Chat> arrayList11 = arrayList5;
                int i40 = size2;
                ArrayList arrayList12 = arrayList7;
                ArrayList arrayList13 = arrayList8;
                boolean isDialogMuted = getMessagesController().isDialogMuted(tLRPC$User.id, 0L);
                Integer num = (Integer) longSparseArray26.get(tLRPC$User.id);
                int intValue4 = (num == null || num.intValue() < 0 || num.intValue() > 1) ? 0 : num.intValue();
                if (isDialogMuted) {
                    longSparseArray33.put(tLRPC$User.id, Boolean.TRUE);
                }
                if (intValue4 == 1) {
                    longSparseArray34.put(tLRPC$User.id, Boolean.TRUE);
                }
                if (tLRPC$User.bot) {
                    int[] iArr6 = this.bots[intValue4];
                    iArr6[isDialogMuted ? 1 : 0] = iArr6[isDialogMuted ? 1 : 0] + 1;
                } else if (tLRPC$User.self || tLRPC$User.contact) {
                    int[] iArr7 = this.contacts[intValue4];
                    iArr7[isDialogMuted ? 1 : 0] = iArr7[isDialogMuted ? 1 : 0] + 1;
                } else {
                    int[] iArr8 = this.nonContacts[intValue4];
                    iArr8[isDialogMuted ? 1 : 0] = iArr8[isDialogMuted ? 1 : 0] + 1;
                }
                longSparseArray29.put(tLRPC$User.id, tLRPC$User);
                i39++;
                arrayList3 = arrayList10;
                arrayList5 = arrayList11;
                size2 = i40;
                arrayList7 = arrayList12;
                arrayList8 = arrayList13;
            }
        }
        ArrayList<TLRPC$Chat> arrayList14 = arrayList5;
        ArrayList arrayList15 = arrayList7;
        ArrayList arrayList16 = arrayList8;
        if (!arrayList16.isEmpty()) {
            ArrayList<Long> arrayList17 = new ArrayList<>();
            ArrayList<TLRPC$EncryptedChat> arrayList18 = new ArrayList<>();
            getEncryptedChatsInternal(TextUtils.join(",", arrayList16), arrayList18, arrayList17);
            if (!arrayList17.isEmpty()) {
                getUsersInternal(TextUtils.join(",", arrayList17), arrayList9);
                int size3 = arrayList9.size();
                int i41 = 0;
                while (i41 < size3) {
                    TLRPC$User tLRPC$User2 = arrayList9.get(i41);
                    LongSparseArray longSparseArray37 = longSparseArray36;
                    longSparseArray37.put(tLRPC$User2.id, tLRPC$User2);
                    i41++;
                    longSparseArray36 = longSparseArray37;
                }
                longSparseArray2 = longSparseArray36;
                int size4 = arrayList18.size();
                int i42 = 0;
                while (i42 < size4) {
                    TLRPC$User tLRPC$User3 = (TLRPC$User) longSparseArray2.get(arrayList18.get(i42).user_id);
                    if (tLRPC$User3 == null) {
                        longSparseArray24 = longSparseArray29;
                        i32 = size4;
                    } else {
                        long makeEncryptedDialogId = DialogObject.makeEncryptedDialogId(r7.id);
                        longSparseArray24 = longSparseArray29;
                        i32 = size4;
                        boolean isDialogMuted2 = getMessagesController().isDialogMuted(makeEncryptedDialogId, 0L);
                        Integer num2 = (Integer) longSparseArray26.get(makeEncryptedDialogId);
                        int intValue5 = (num2 == null || num2.intValue() < 0 || num2.intValue() > 1) ? 0 : num2.intValue();
                        if (isDialogMuted2) {
                            longSparseArray33.put(tLRPC$User3.id, Boolean.TRUE);
                        }
                        if (intValue5 == 1) {
                            longSparseArray34.put(tLRPC$User3.id, Boolean.TRUE);
                        }
                        if (tLRPC$User3.self || tLRPC$User3.contact) {
                            i33 = 1;
                            int[] iArr9 = this.contacts[intValue5];
                            iArr9[isDialogMuted2 ? 1 : 0] = iArr9[isDialogMuted2 ? 1 : 0] + 1;
                        } else {
                            int[] iArr10 = this.nonContacts[intValue5];
                            i33 = 1;
                            iArr10[isDialogMuted2 ? 1 : 0] = iArr10[isDialogMuted2 ? 1 : 0] + 1;
                        }
                        longSparseArray32.put(tLRPC$User3.id, Integer.valueOf(((Integer) longSparseArray32.get(tLRPC$User3.id, 0)).intValue() + i33));
                    }
                    i42++;
                    longSparseArray29 = longSparseArray24;
                    size4 = i32;
                }
                longSparseArray = longSparseArray29;
                if (!arrayList15.isEmpty()) {
                    ArrayList<TLRPC$Chat> arrayList19 = arrayList14;
                    getChatsInternal(TextUtils.join(",", arrayList15), arrayList19);
                    int size5 = arrayList19.size();
                    int i43 = 0;
                    while (i43 < size5) {
                        TLRPC$Chat tLRPC$Chat = arrayList19.get(i43);
                        if ((tLRPC$Chat.migrated_to instanceof TLRPC$TL_inputChannel) || ChatObject.isNotInChat(tLRPC$Chat)) {
                            i30 = size5;
                            arrayList = arrayList19;
                            longSparseArray22 = longSparseArray28;
                            longSparseArray23 = longSparseArray35;
                        } else {
                            boolean isDialogMuted3 = getMessagesController().isDialogMuted(-tLRPC$Chat.id, 0L, tLRPC$Chat);
                            boolean z2 = this.dialogsWithUnread.indexOfKey(-tLRPC$Chat.id) >= 0;
                            boolean z3 = this.dialogsWithMentions.indexOfKey(-tLRPC$Chat.id) >= 0;
                            Integer num3 = (Integer) longSparseArray26.get(-tLRPC$Chat.id);
                            if (num3 == null || num3.intValue() < 0) {
                                i30 = size5;
                            } else {
                                i30 = size5;
                                if (num3.intValue() <= 1) {
                                    i31 = num3.intValue();
                                    if (isDialogMuted3) {
                                        arrayList = arrayList19;
                                    } else {
                                        arrayList = arrayList19;
                                        longSparseArray33.put(-tLRPC$Chat.id, Boolean.TRUE);
                                    }
                                    if (i31 == 1) {
                                        longSparseArray34.put(-tLRPC$Chat.id, Boolean.TRUE);
                                    }
                                    if (isDialogMuted3 && longSparseIntArray4 != null && longSparseIntArray4.indexOfKey(-tLRPC$Chat.id) >= 0) {
                                        if (!ChatObject.isChannel(tLRPC$Chat) && !tLRPC$Chat.megagroup) {
                                            int[] iArr11 = this.mentionChannels;
                                            iArr11[i31] = iArr11[i31] + 1;
                                        } else {
                                            int[] iArr12 = this.mentionGroups;
                                            iArr12[i31] = iArr12[i31] + 1;
                                        }
                                    }
                                    if (z || z2 || z3) {
                                        if (z) {
                                            longSparseArray22 = longSparseArray28;
                                        } else {
                                            longSparseArray22 = longSparseArray28;
                                        }
                                        longSparseArray23 = longSparseArray35;
                                        longSparseArray23.put(tLRPC$Chat.id, tLRPC$Chat);
                                    } else {
                                        longSparseArray22 = longSparseArray28;
                                    }
                                    if (ChatObject.isChannel(tLRPC$Chat) && !tLRPC$Chat.megagroup) {
                                        int[] iArr13 = this.channels[i31];
                                        iArr13[isDialogMuted3 ? 1 : 0] = iArr13[isDialogMuted3 ? 1 : 0] + 1;
                                    } else {
                                        int[] iArr14 = this.groups[i31];
                                        iArr14[isDialogMuted3 ? 1 : 0] = iArr14[isDialogMuted3 ? 1 : 0] + 1;
                                    }
                                    longSparseArray23 = longSparseArray35;
                                    longSparseArray23.put(tLRPC$Chat.id, tLRPC$Chat);
                                }
                            }
                            i31 = 0;
                            if (isDialogMuted3) {
                            }
                            if (i31 == 1) {
                            }
                            if (isDialogMuted3) {
                                if (!ChatObject.isChannel(tLRPC$Chat)) {
                                }
                                int[] iArr122 = this.mentionGroups;
                                iArr122[i31] = iArr122[i31] + 1;
                            }
                            if (z) {
                            }
                            if (z) {
                            }
                            longSparseArray23 = longSparseArray35;
                            longSparseArray23.put(tLRPC$Chat.id, tLRPC$Chat);
                        }
                        i43++;
                        longSparseArray35 = longSparseArray23;
                        longSparseArray28 = longSparseArray22;
                        size5 = i30;
                        arrayList19 = arrayList;
                    }
                }
                LongSparseArray longSparseArray38 = longSparseArray35;
                LongSparseArray longSparseArray39 = longSparseArray28;
                size = this.dialogFilters.size();
                i2 = 0;
                while (i2 < size + 2) {
                    if (i2 < size) {
                        dialogFilter = this.dialogFilters.get(i2);
                        if (dialogFilter.pendingUnreadCount < 0) {
                            longSparseArray10 = longSparseArray39;
                            i15 = size;
                            longSparseArray9 = longSparseArray2;
                            longSparseArray7 = longSparseArray32;
                            longSparseArray6 = longSparseArray34;
                            i14 = i2;
                            longSparseArray8 = longSparseArray;
                            i2 = i14 + 1;
                            longSparseArray34 = longSparseArray6;
                            size = i15;
                            longSparseArray39 = longSparseArray10;
                            longSparseArray2 = longSparseArray9;
                            longSparseArray = longSparseArray8;
                            longSparseArray32 = longSparseArray7;
                            longSparseIntArray4 = longSparseIntArray2;
                        } else {
                            i6 = dialogFilter.pendingUnreadCount;
                            i5 = dialogFilter.flags;
                        }
                    } else {
                        dialogFilter = null;
                        int i44 = MessagesController.DIALOG_FILTER_FLAG_ALL_CHATS;
                        if (i2 == size) {
                            i3 = this.pendingMainUnreadCount;
                            i44 |= MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED;
                            if (!getNotificationsController().showBadgeMuted) {
                                i4 = MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED;
                            }
                            int i45 = i3;
                            i5 = i44;
                            i6 = i45;
                        } else {
                            i3 = this.pendingArchiveUnreadCount;
                            i4 = MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED;
                        }
                        i44 |= i4;
                        int i452 = i3;
                        i5 = i44;
                        i6 = i452;
                    }
                    if (z) {
                        if ((MessagesController.DIALOG_FILTER_FLAG_CONTACTS & i5) != 0) {
                            if ((MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED & i5) == 0) {
                                int[][] iArr15 = this.contacts;
                                c15 = 0;
                                i6 -= iArr15[0][0];
                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                    c16 = 1;
                                    i6 -= iArr15[0][1];
                                    if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                                        int[][] iArr16 = this.contacts;
                                        i6 -= iArr16[c16][c15];
                                        if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                            i6 -= iArr16[c16][c16];
                                        }
                                    }
                                }
                            } else {
                                c15 = 0;
                            }
                            c16 = 1;
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                            }
                        }
                        if ((MessagesController.DIALOG_FILTER_FLAG_NON_CONTACTS & i5) != 0) {
                            if ((MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED & i5) == 0) {
                                int[][] iArr17 = this.nonContacts;
                                c13 = 0;
                                i6 -= iArr17[0][0];
                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                    c14 = 1;
                                    i6 -= iArr17[0][1];
                                    if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                                        int[][] iArr18 = this.nonContacts;
                                        i6 -= iArr18[c14][c13];
                                        if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                            i6 -= iArr18[c14][c14];
                                        }
                                    }
                                }
                            } else {
                                c13 = 0;
                            }
                            c14 = 1;
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                            }
                        }
                        if ((MessagesController.DIALOG_FILTER_FLAG_GROUPS & i5) != 0) {
                            if ((MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED & i5) == 0) {
                                int[][] iArr19 = this.groups;
                                c12 = 0;
                                int i46 = i6 - iArr19[0][0];
                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                    i29 = iArr19[0][1];
                                } else {
                                    i29 = this.mentionGroups[0];
                                }
                                i6 = i46 - i29;
                            } else {
                                c12 = 0;
                            }
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                                int[][] iArr20 = this.groups;
                                int i47 = i6 - iArr20[1][c12];
                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                    i28 = iArr20[1][1];
                                } else {
                                    i28 = this.mentionGroups[1];
                                }
                                i6 = i47 - i28;
                            }
                        }
                        if ((MessagesController.DIALOG_FILTER_FLAG_CHANNELS & i5) != 0) {
                            if ((MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED & i5) == 0) {
                                int[][] iArr21 = this.channels;
                                c11 = 0;
                                int i48 = i6 - iArr21[0][0];
                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                    i27 = iArr21[0][1];
                                } else {
                                    i27 = this.mentionChannels[0];
                                }
                                i6 = i48 - i27;
                            } else {
                                c11 = 0;
                            }
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                                int[][] iArr22 = this.channels;
                                int i49 = i6 - iArr22[1][c11];
                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                    i26 = iArr22[1][1];
                                } else {
                                    i26 = this.mentionChannels[1];
                                }
                                i6 = i49 - i26;
                            }
                        }
                        if ((MessagesController.DIALOG_FILTER_FLAG_BOTS & i5) != 0) {
                            if ((MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED & i5) == 0) {
                                int[][] iArr23 = this.bots;
                                c9 = 0;
                                i6 -= iArr23[0][0];
                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                    c10 = 1;
                                    i6 -= iArr23[0][1];
                                    if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                                        int[][] iArr24 = this.bots;
                                        i6 -= iArr24[c10][c9];
                                        if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                            i6 -= iArr24[c10][c10];
                                        }
                                    }
                                }
                            } else {
                                c9 = 0;
                            }
                            c10 = 1;
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                            }
                        }
                        if (dialogFilter != null) {
                            int size6 = dialogFilter.alwaysShow.size();
                            int i50 = 0;
                            while (i50 < size6) {
                                int i51 = i2;
                                long longValue = dialogFilter.alwaysShow.get(i50).longValue();
                                if (DialogObject.isUserDialog(longValue)) {
                                    i20 = size;
                                    i21 = size6;
                                    int i52 = 0;
                                    for (int i53 = 2; i52 < i53; i53 = 2) {
                                        TLRPC$User tLRPC$User4 = (TLRPC$User) (i52 == 0 ? longSparseArray : longSparseArray2).get(longValue);
                                        if (tLRPC$User4 != null) {
                                            if (i52 == 0) {
                                                longSparseArray19 = longSparseArray39;
                                                longSparseArray20 = longSparseArray2;
                                                intValue2 = 1;
                                            } else {
                                                longSparseArray20 = longSparseArray2;
                                                intValue2 = ((Integer) longSparseArray32.get(longValue, 0)).intValue();
                                                longSparseArray19 = longSparseArray39;
                                            }
                                            if (tLRPC$User4.bot) {
                                                i25 = MessagesController.DIALOG_FILTER_FLAG_BOTS;
                                            } else if (tLRPC$User4.self || tLRPC$User4.contact) {
                                                i25 = MessagesController.DIALOG_FILTER_FLAG_CONTACTS;
                                            } else {
                                                i25 = MessagesController.DIALOG_FILTER_FLAG_NON_CONTACTS;
                                            }
                                            if ((i25 & i5) == 0) {
                                                i6 -= intValue2;
                                            } else {
                                                i24 = i50;
                                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) != 0) {
                                                    longSparseArray21 = longSparseArray32;
                                                } else {
                                                    longSparseArray21 = longSparseArray32;
                                                }
                                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) != 0) {
                                                    if (longSparseArray34.indexOfKey(tLRPC$User4.id) < 0) {
                                                    }
                                                    i6 -= intValue2;
                                                }
                                                i52++;
                                                longSparseArray32 = longSparseArray21;
                                                longSparseArray2 = longSparseArray20;
                                                longSparseArray39 = longSparseArray19;
                                                i50 = i24;
                                            }
                                        } else {
                                            longSparseArray19 = longSparseArray39;
                                            longSparseArray20 = longSparseArray2;
                                        }
                                        i24 = i50;
                                        longSparseArray21 = longSparseArray32;
                                        i52++;
                                        longSparseArray32 = longSparseArray21;
                                        longSparseArray2 = longSparseArray20;
                                        longSparseArray39 = longSparseArray19;
                                        i50 = i24;
                                    }
                                    longSparseArray16 = longSparseArray39;
                                    longSparseArray17 = longSparseArray2;
                                    i22 = i50;
                                    longSparseArray18 = longSparseArray32;
                                } else {
                                    longSparseArray16 = longSparseArray39;
                                    i20 = size;
                                    i21 = size6;
                                    longSparseArray17 = longSparseArray2;
                                    i22 = i50;
                                    longSparseArray18 = longSparseArray32;
                                    TLRPC$Chat tLRPC$Chat2 = (TLRPC$Chat) longSparseArray38.get(-longValue);
                                    if (tLRPC$Chat2 != null) {
                                        if (ChatObject.isChannel(tLRPC$Chat2) && !tLRPC$Chat2.megagroup) {
                                            i23 = MessagesController.DIALOG_FILTER_FLAG_CHANNELS;
                                        } else {
                                            i23 = MessagesController.DIALOG_FILTER_FLAG_GROUPS;
                                        }
                                        if ((i23 & i5) == 0 || (((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) != 0 && longSparseArray33.indexOfKey(-tLRPC$Chat2.id) >= 0 && this.dialogsWithMentions.indexOfKey(-tLRPC$Chat2.id) < 0) || ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) != 0 && longSparseArray34.indexOfKey(-tLRPC$Chat2.id) >= 0))) {
                                            i6--;
                                        }
                                    }
                                }
                                i50 = i22 + 1;
                                size6 = i21;
                                longSparseArray32 = longSparseArray18;
                                i2 = i51;
                                size = i20;
                                longSparseArray2 = longSparseArray17;
                                longSparseArray39 = longSparseArray16;
                            }
                            longSparseArray3 = longSparseArray39;
                            i7 = size;
                            longSparseArray4 = longSparseArray2;
                            LongSparseArray longSparseArray40 = longSparseArray32;
                            i8 = i2;
                            int size7 = dialogFilter.neverShow.size();
                            int i54 = 0;
                            while (i54 < size7) {
                                long longValue2 = dialogFilter.neverShow.get(i54).longValue();
                                if (longSparseIntArray4 == null || longSparseIntArray4.indexOfKey(longValue2) < 0 || longSparseArray33.indexOfKey(longValue2) >= 0) {
                                    if (DialogObject.isUserDialog(longValue2)) {
                                        int i55 = 0;
                                        while (i55 < 2) {
                                            TLRPC$User tLRPC$User5 = (TLRPC$User) (i55 == 0 ? longSparseArray : longSparseArray4).get(longValue2);
                                            if (tLRPC$User5 != null) {
                                                if (i55 == 0) {
                                                    i18 = size7;
                                                    intValue = 1;
                                                } else {
                                                    intValue = ((Integer) longSparseArray40.get(longValue2, 0)).intValue();
                                                    if (intValue != 0) {
                                                        i18 = size7;
                                                    }
                                                }
                                                if (tLRPC$User5.bot) {
                                                    i19 = MessagesController.DIALOG_FILTER_FLAG_BOTS;
                                                } else if (tLRPC$User5.self || tLRPC$User5.contact) {
                                                    i19 = MessagesController.DIALOG_FILTER_FLAG_CONTACTS;
                                                } else {
                                                    i19 = MessagesController.DIALOG_FILTER_FLAG_NON_CONTACTS;
                                                }
                                                if ((i19 & i5) != 0) {
                                                    longSparseArray15 = longSparseArray40;
                                                    if (((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0 || longSparseArray34.indexOfKey(tLRPC$User5.id) < 0) && ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0 || longSparseArray33.indexOfKey(tLRPC$User5.id) < 0)) {
                                                        i6 += intValue;
                                                    }
                                                } else {
                                                    longSparseArray15 = longSparseArray40;
                                                }
                                                i55++;
                                                size7 = i18;
                                                longSparseArray40 = longSparseArray15;
                                            }
                                            longSparseArray15 = longSparseArray40;
                                            i18 = size7;
                                            i55++;
                                            size7 = i18;
                                            longSparseArray40 = longSparseArray15;
                                        }
                                    } else {
                                        longSparseArray14 = longSparseArray40;
                                        i16 = size7;
                                        TLRPC$Chat tLRPC$Chat3 = (TLRPC$Chat) longSparseArray38.get(-longValue2);
                                        if (tLRPC$Chat3 != null) {
                                            if (ChatObject.isChannel(tLRPC$Chat3) && !tLRPC$Chat3.megagroup) {
                                                i17 = MessagesController.DIALOG_FILTER_FLAG_CHANNELS;
                                            } else {
                                                i17 = MessagesController.DIALOG_FILTER_FLAG_GROUPS;
                                            }
                                            if ((i17 & i5) != 0 && (((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0 || longSparseArray34.indexOfKey(-tLRPC$Chat3.id) < 0) && ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0 || longSparseArray33.indexOfKey(-tLRPC$Chat3.id) < 0 || this.dialogsWithMentions.indexOfKey(-tLRPC$Chat3.id) >= 0))) {
                                                i6++;
                                            }
                                        }
                                        i54++;
                                        size7 = i16;
                                        longSparseArray40 = longSparseArray14;
                                    }
                                }
                                longSparseArray14 = longSparseArray40;
                                i16 = size7;
                                i54++;
                                size7 = i16;
                                longSparseArray40 = longSparseArray14;
                            }
                            longSparseArray5 = longSparseArray40;
                        } else {
                            longSparseArray3 = longSparseArray39;
                            i7 = size;
                            longSparseArray4 = longSparseArray2;
                            longSparseArray5 = longSparseArray32;
                            i8 = i2;
                        }
                        if (i6 < 0) {
                            longSparseArray6 = longSparseArray34;
                            longSparseArray7 = longSparseArray5;
                            longSparseArray8 = longSparseArray;
                            longSparseArray9 = longSparseArray4;
                            longSparseArray10 = longSparseArray3;
                            i9 = 0;
                            if (dialogFilter == null) {
                                dialogFilter.pendingUnreadCount = i9;
                                i14 = i8;
                                i15 = i7;
                            } else {
                                i14 = i8;
                                i15 = i7;
                                if (i14 == i15) {
                                    this.pendingMainUnreadCount = i9;
                                } else if (i14 == i15 + 1) {
                                    this.pendingArchiveUnreadCount = i9;
                                }
                            }
                        } else {
                            i9 = i6;
                            longSparseArray6 = longSparseArray34;
                            longSparseArray7 = longSparseArray5;
                            longSparseArray8 = longSparseArray;
                            longSparseArray9 = longSparseArray4;
                            longSparseArray10 = longSparseArray3;
                            if (dialogFilter == null) {
                            }
                        }
                    } else {
                        longSparseArray3 = longSparseArray39;
                        i7 = size;
                        longSparseArray4 = longSparseArray2;
                        longSparseArray5 = longSparseArray32;
                        i8 = i2;
                        if ((MessagesController.DIALOG_FILTER_FLAG_CONTACTS & i5) != 0) {
                            if ((MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED & i5) == 0) {
                                int[][] iArr25 = this.contacts;
                                c7 = 0;
                                i6 += iArr25[0][0];
                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                    c8 = 1;
                                    i6 += iArr25[0][1];
                                    if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                                        int[][] iArr26 = this.contacts;
                                        i6 += iArr26[c8][c7];
                                        if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                            i6 += iArr26[c8][c8];
                                        }
                                    }
                                }
                            } else {
                                c7 = 0;
                            }
                            c8 = 1;
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                            }
                        }
                        if ((MessagesController.DIALOG_FILTER_FLAG_NON_CONTACTS & i5) != 0) {
                            if ((MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED & i5) == 0) {
                                int[][] iArr27 = this.nonContacts;
                                c5 = 0;
                                i6 += iArr27[0][0];
                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                    c6 = 1;
                                    i6 += iArr27[0][1];
                                    if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                                        int[][] iArr28 = this.nonContacts;
                                        i6 += iArr28[c6][c5];
                                        if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                            i6 += iArr28[c6][c6];
                                        }
                                    }
                                }
                            } else {
                                c5 = 0;
                            }
                            c6 = 1;
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                            }
                        }
                        if ((MessagesController.DIALOG_FILTER_FLAG_GROUPS & i5) != 0) {
                            if ((MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED & i5) == 0) {
                                int[][] iArr29 = this.groups;
                                c4 = 0;
                                int i56 = i6 + iArr29[0][0];
                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                    i13 = iArr29[0][1];
                                } else {
                                    i13 = this.mentionGroups[0];
                                }
                                i6 = i56 + i13;
                            } else {
                                c4 = 0;
                            }
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                                int[][] iArr30 = this.groups;
                                int i57 = i6 + iArr30[1][c4];
                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                    i12 = iArr30[1][1];
                                } else {
                                    i12 = this.mentionGroups[1];
                                }
                                i6 = i57 + i12;
                            }
                        }
                        if ((MessagesController.DIALOG_FILTER_FLAG_CHANNELS & i5) != 0) {
                            if ((MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED & i5) == 0) {
                                int[][] iArr31 = this.channels;
                                c3 = 0;
                                int i58 = i6 + iArr31[0][0];
                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                    i11 = iArr31[0][1];
                                } else {
                                    i11 = this.mentionChannels[0];
                                }
                                i6 = i58 + i11;
                            } else {
                                c3 = 0;
                            }
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                                int[][] iArr32 = this.channels;
                                int i59 = i6 + iArr32[1][c3];
                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                    i10 = iArr32[1][1];
                                } else {
                                    i10 = this.mentionChannels[1];
                                }
                                i6 = i59 + i10;
                            }
                        }
                        if ((MessagesController.DIALOG_FILTER_FLAG_BOTS & i5) != 0) {
                            if ((MessagesController.DIALOG_FILTER_FLAG_ONLY_ARCHIVED & i5) == 0) {
                                int[][] iArr33 = this.bots;
                                c = 0;
                                i6 += iArr33[0][0];
                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                    c2 = 1;
                                    i6 += iArr33[0][1];
                                    if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                                        int[][] iArr34 = this.bots;
                                        i6 += iArr34[c2][c];
                                        if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0) {
                                            i6 += iArr34[c2][c2];
                                        }
                                    }
                                }
                            } else {
                                c = 0;
                            }
                            c2 = 1;
                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_ARCHIVED & i5) == 0) {
                            }
                        }
                        i9 = i6;
                        if (dialogFilter != null) {
                            if (dialogFilter.alwaysShow.isEmpty()) {
                                longSparseArray6 = longSparseArray34;
                                longSparseArray7 = longSparseArray5;
                                longSparseArray8 = longSparseArray;
                                longSparseArray9 = longSparseArray4;
                                longSparseArray10 = longSparseArray3;
                            } else {
                                if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) != 0 && longSparseIntArray4 != null) {
                                    int size8 = longSparseIntArray2.size();
                                    int i60 = 0;
                                    while (i60 < size8) {
                                        long keyAt2 = longSparseIntArray4.keyAt(i60);
                                        LongSparseArray longSparseArray41 = longSparseArray34;
                                        TLRPC$Chat tLRPC$Chat4 = (TLRPC$Chat) longSparseArray38.get(-keyAt2);
                                        if (ChatObject.isChannel(tLRPC$Chat4) && !tLRPC$Chat4.megagroup) {
                                            if ((MessagesController.DIALOG_FILTER_FLAG_CHANNELS & i5) == 0) {
                                                i60++;
                                                longSparseArray34 = longSparseArray41;
                                            }
                                            if (longSparseArray33.indexOfKey(keyAt2) >= 0) {
                                                i9--;
                                            }
                                            i60++;
                                            longSparseArray34 = longSparseArray41;
                                        } else {
                                            if ((MessagesController.DIALOG_FILTER_FLAG_GROUPS & i5) == 0) {
                                                i60++;
                                                longSparseArray34 = longSparseArray41;
                                            }
                                            if (longSparseArray33.indexOfKey(keyAt2) >= 0 && dialogFilter.alwaysShow.contains(Long.valueOf(keyAt2))) {
                                                i9--;
                                            }
                                            i60++;
                                            longSparseArray34 = longSparseArray41;
                                        }
                                    }
                                }
                                longSparseArray6 = longSparseArray34;
                                int size9 = dialogFilter.alwaysShow.size();
                                int i61 = i9;
                                int i62 = 0;
                                while (i62 < size9) {
                                    long longValue3 = dialogFilter.alwaysShow.get(i62).longValue();
                                    LongSparseArray longSparseArray42 = longSparseArray3;
                                    if (longSparseArray42.indexOfKey(longValue3) < 0) {
                                        longSparseArray11 = longSparseArray5;
                                        longSparseArray12 = longSparseArray;
                                        longSparseArray13 = longSparseArray4;
                                    } else if (DialogObject.isUserDialog(longValue3)) {
                                        longSparseArray12 = longSparseArray;
                                        TLRPC$User tLRPC$User6 = (TLRPC$User) longSparseArray12.get(longValue3);
                                        if (tLRPC$User6 != null) {
                                            j = longValue3;
                                            if (((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) != 0 && longSparseArray33.indexOfKey(tLRPC$User6.id) >= 0) || (!tLRPC$User6.bot ? !(tLRPC$User6.self || tLRPC$User6.contact ? (MessagesController.DIALOG_FILTER_FLAG_CONTACTS & i5) != 0 : (MessagesController.DIALOG_FILTER_FLAG_NON_CONTACTS & i5) != 0) : (MessagesController.DIALOG_FILTER_FLAG_BOTS & i5) == 0)) {
                                                i61++;
                                            }
                                        } else {
                                            j = longValue3;
                                        }
                                        longSparseArray13 = longSparseArray4;
                                        long j3 = j;
                                        TLRPC$User tLRPC$User7 = (TLRPC$User) longSparseArray13.get(j3);
                                        if (tLRPC$User7 != null) {
                                            longSparseArray11 = longSparseArray5;
                                            int intValue6 = ((Integer) longSparseArray11.get(j3, 0)).intValue();
                                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0 || longSparseArray33.indexOfKey(tLRPC$User7.id) < 0) {
                                                if (tLRPC$User7.bot) {
                                                    if ((MessagesController.DIALOG_FILTER_FLAG_BOTS & i5) != 0) {
                                                    }
                                                } else if (tLRPC$User7.self || tLRPC$User7.contact) {
                                                    if ((MessagesController.DIALOG_FILTER_FLAG_CONTACTS & i5) != 0) {
                                                    }
                                                } else if ((MessagesController.DIALOG_FILTER_FLAG_NON_CONTACTS & i5) != 0) {
                                                }
                                            }
                                            i61 += intValue6;
                                        } else {
                                            longSparseArray11 = longSparseArray5;
                                        }
                                    } else {
                                        longSparseArray11 = longSparseArray5;
                                        longSparseArray12 = longSparseArray;
                                        longSparseArray13 = longSparseArray4;
                                        TLRPC$Chat tLRPC$Chat5 = (TLRPC$Chat) longSparseArray38.get(-longValue3);
                                        if (tLRPC$Chat5 != null) {
                                            if ((MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED & i5) == 0 || longSparseArray33.indexOfKey(-tLRPC$Chat5.id) < 0) {
                                                if (ChatObject.isChannel(tLRPC$Chat5) && !tLRPC$Chat5.megagroup) {
                                                    if ((MessagesController.DIALOG_FILTER_FLAG_CHANNELS & i5) != 0) {
                                                    }
                                                } else if ((MessagesController.DIALOG_FILTER_FLAG_GROUPS & i5) != 0) {
                                                }
                                            }
                                            i61++;
                                        }
                                    }
                                    i62++;
                                    longSparseArray3 = longSparseArray42;
                                    longSparseArray4 = longSparseArray13;
                                    longSparseArray = longSparseArray12;
                                    longSparseArray5 = longSparseArray11;
                                }
                                longSparseArray7 = longSparseArray5;
                                longSparseArray8 = longSparseArray;
                                longSparseArray9 = longSparseArray4;
                                longSparseArray10 = longSparseArray3;
                                i9 = i61;
                            }
                            int size10 = dialogFilter.neverShow.size();
                            int i63 = i9;
                            for (int i64 = 0; i64 < size10; i64++) {
                                long longValue4 = dialogFilter.neverShow.get(i64).longValue();
                                if (DialogObject.isUserDialog(longValue4)) {
                                    if (((TLRPC$User) longSparseArray8.get(longValue4)) != null) {
                                        i63--;
                                    }
                                    if (((TLRPC$User) longSparseArray9.get(longValue4)) != null) {
                                        i63 -= ((Integer) longSparseArray7.get(longValue4, 0)).intValue();
                                    }
                                } else if (((TLRPC$Chat) longSparseArray38.get(-longValue4)) != null) {
                                    i63--;
                                }
                            }
                            i9 = i63;
                            if (dialogFilter == null) {
                            }
                        }
                        longSparseArray6 = longSparseArray34;
                        longSparseArray7 = longSparseArray5;
                        longSparseArray8 = longSparseArray;
                        longSparseArray9 = longSparseArray4;
                        longSparseArray10 = longSparseArray3;
                        if (dialogFilter == null) {
                        }
                    }
                    i2 = i14 + 1;
                    longSparseArray34 = longSparseArray6;
                    size = i15;
                    longSparseArray39 = longSparseArray10;
                    longSparseArray2 = longSparseArray9;
                    longSparseArray = longSparseArray8;
                    longSparseArray32 = longSparseArray7;
                    longSparseIntArray4 = longSparseIntArray2;
                }
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$updateFiltersReadCounter$106();
                    }
                });
            }
        }
        longSparseArray = longSparseArray29;
        longSparseArray2 = longSparseArray36;
        if (!arrayList15.isEmpty()) {
        }
        LongSparseArray longSparseArray382 = longSparseArray35;
        LongSparseArray longSparseArray392 = longSparseArray28;
        size = this.dialogFilters.size();
        i2 = 0;
        while (i2 < size + 2) {
        }
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateFiltersReadCounter$106();
            }
        });
    }

    public void lambda$updateFiltersReadCounter$106() {
        ArrayList<MessagesController.DialogFilter> arrayList = getMessagesController().dialogFilters;
        int size = arrayList.size();
        for (int i = 0; i < size; i++) {
            arrayList.get(i).unreadCount = arrayList.get(i).pendingUnreadCount;
        }
        this.mainUnreadCount = this.pendingMainUnreadCount;
        this.archiveUnreadCount = this.pendingArchiveUnreadCount;
    }

    private void updateDialogsWithReadMessagesInternal(ArrayList<Integer> arrayList, LongSparseIntArray longSparseIntArray, LongSparseIntArray longSparseIntArray2, LongSparseArray<ArrayList<Integer>> longSparseArray, LongSparseIntArray longSparseIntArray3) {
        int i;
        boolean z;
        int i2;
        int i3;
        int i4;
        LongSparseIntArray longSparseIntArray4 = longSparseIntArray;
        LongSparseIntArray longSparseIntArray5 = longSparseIntArray3;
        try {
            LongSparseIntArray longSparseIntArray6 = new LongSparseIntArray();
            LongSparseIntArray longSparseIntArray7 = new LongSparseIntArray();
            ArrayList<Long> arrayList2 = new ArrayList<>();
            int i5 = 2;
            if (!isEmpty(arrayList)) {
                SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT uid, read_state, out FROM messages_v2 WHERE mid IN(%s) AND is_channel = 0", TextUtils.join(",", arrayList)), new Object[0]);
                while (queryFinalized.next()) {
                    if (queryFinalized.intValue(2) == 0 && queryFinalized.intValue(1) == 0) {
                        long longValue = queryFinalized.longValue(0);
                        int i6 = longSparseIntArray6.get(longValue);
                        if (i6 == 0) {
                            longSparseIntArray6.put(longValue, 1);
                        } else {
                            longSparseIntArray6.put(longValue, i6 + 1);
                        }
                    }
                }
                queryFinalized.dispose();
            } else {
                if (!isEmpty(longSparseIntArray)) {
                    int i7 = 0;
                    while (i7 < longSparseIntArray.size()) {
                        long keyAt = longSparseIntArray4.keyAt(i7);
                        int i8 = longSparseIntArray4.get(keyAt);
                        int i9 = longSparseIntArray5 == null ? -2 : longSparseIntArray5.get(keyAt, -2);
                        if (i9 >= 0) {
                            longSparseIntArray6.put(keyAt, i9);
                            if (BuildVars.DEBUG_VERSION) {
                                FileLog.d(keyAt + " update unread messages count by still unread " + i9);
                            }
                        } else {
                            if (longSparseIntArray5 == null || i9 == -2) {
                                z = true;
                            } else {
                                SQLiteDatabase sQLiteDatabase = this.database;
                                Locale locale = Locale.US;
                                Object[] objArr = new Object[i5];
                                objArr[0] = Long.valueOf(keyAt);
                                objArr[1] = Integer.valueOf(i8);
                                SQLiteCursor queryFinalized2 = sQLiteDatabase.queryFinalized(String.format(locale, "SELECT start, end FROM messages_holes WHERE uid = %d AND end > %d", objArr), new Object[0]);
                                z = true;
                                while (queryFinalized2.next()) {
                                    z = false;
                                }
                                queryFinalized2.dispose();
                            }
                            if (z) {
                                SQLiteCursor queryFinalized3 = this.database.queryFinalized(String.format(Locale.US, "SELECT COUNT(mid) FROM messages_v2 WHERE uid = %d AND mid > %d AND read_state IN(0,2) AND out = 0", Long.valueOf(keyAt), Integer.valueOf(i8)), new Object[0]);
                                if (queryFinalized3.next()) {
                                    int intValue = queryFinalized3.intValue(0);
                                    longSparseIntArray6.put(keyAt, intValue);
                                    if (BuildVars.DEBUG_VERSION) {
                                        FileLog.d(keyAt + " update unread messages count " + intValue);
                                    }
                                } else if (BuildVars.DEBUG_VERSION) {
                                    FileLog.d(keyAt + " can't update unread messages count cursor trouble");
                                }
                                queryFinalized3.dispose();
                            } else if (BuildVars.DEBUG_VERSION) {
                                FileLog.d(keyAt + " can't update unread messages count");
                            }
                        }
                        FileLog.d(keyAt + " set inbox max " + i8);
                        SQLitePreparedStatement executeFast = this.database.executeFast("UPDATE dialogs SET inbox_max = max((SELECT inbox_max FROM dialogs WHERE did = ?), ?) WHERE did = ?");
                        executeFast.requery();
                        executeFast.bindLong(1, keyAt);
                        executeFast.bindInteger(2, i8);
                        executeFast.bindLong(3, keyAt);
                        executeFast.step();
                        executeFast.dispose();
                        i7++;
                        longSparseIntArray4 = longSparseIntArray;
                        longSparseIntArray5 = longSparseIntArray3;
                        i5 = 2;
                    }
                }
                if (!isEmpty(longSparseArray)) {
                    int size = longSparseArray.size();
                    int i10 = 0;
                    while (i10 < size) {
                        ArrayList<Integer> valueAt = longSparseArray.valueAt(i10);
                        ArrayList arrayList3 = new ArrayList(valueAt);
                        int i11 = 0;
                        SQLiteCursor queryFinalized4 = this.database.queryFinalized(String.format(Locale.US, "SELECT uid, read_state, out, mention, mid, is_channel FROM messages_v2 WHERE mid IN(%s)", TextUtils.join(",", valueAt)), new Object[0]);
                        long j = 0;
                        while (queryFinalized4.next()) {
                            long longValue2 = queryFinalized4.longValue(i11);
                            arrayList3.remove(Integer.valueOf(queryFinalized4.intValue(4)));
                            if (queryFinalized4.intValue(1) < 2 && queryFinalized4.intValue(2) == 0 && queryFinalized4.intValue(3) == 1) {
                                int i12 = longSparseIntArray7.get(longValue2, -1);
                                if (i12 < 0) {
                                    i = size;
                                    SQLiteCursor queryFinalized5 = this.database.queryFinalized("SELECT unread_count_i FROM dialogs WHERE did = " + longValue2, new Object[0]);
                                    int intValue2 = queryFinalized5.next() ? queryFinalized5.intValue(0) : 0;
                                    queryFinalized5.dispose();
                                    longSparseIntArray7.put(longValue2, Math.max(0, intValue2 - 1));
                                } else {
                                    i = size;
                                    longSparseIntArray7.put(longValue2, Math.max(0, i12 - 1));
                                }
                            } else {
                                i = size;
                            }
                            j = queryFinalized4.longValue(5);
                            size = i;
                            i11 = 0;
                        }
                        int i13 = size;
                        queryFinalized4.dispose();
                        if (!arrayList3.isEmpty() && j != 0 && !arrayList2.contains(Long.valueOf(j))) {
                            arrayList2.add(Long.valueOf(j));
                        }
                        i10++;
                        size = i13;
                    }
                }
                if (!isEmpty(longSparseIntArray2)) {
                    for (int i14 = 0; i14 < longSparseIntArray2.size(); i14++) {
                        long keyAt2 = longSparseIntArray2.keyAt(i14);
                        int i15 = longSparseIntArray2.get(keyAt2);
                        SQLitePreparedStatement executeFast2 = this.database.executeFast("UPDATE dialogs SET outbox_max = max((SELECT outbox_max FROM dialogs WHERE did = ?), ?) WHERE did = ?");
                        executeFast2.requery();
                        executeFast2.bindLong(1, keyAt2);
                        executeFast2.bindInteger(2, i15);
                        executeFast2.bindLong(3, keyAt2);
                        executeFast2.step();
                        executeFast2.dispose();
                    }
                }
            }
            if (longSparseIntArray6.size() <= 0) {
                if (longSparseIntArray7.size() > 0) {
                }
                updateFiltersReadCounter(longSparseIntArray6, longSparseIntArray7, true);
                getMessagesController().processDialogsUpdateRead(longSparseIntArray6, longSparseIntArray7);
                if (arrayList2.isEmpty()) {
                    getMessagesController().reloadMentionsCountForChannels(arrayList2);
                    return;
                }
                return;
            }
            this.database.beginTransaction();
            if (longSparseIntArray6.size() > 0) {
                ArrayList<Long> arrayList4 = new ArrayList<>();
                SQLitePreparedStatement executeFast3 = this.database.executeFast("UPDATE dialogs SET unread_count = ? WHERE did = ?");
                int i16 = 0;
                while (i16 < longSparseIntArray6.size()) {
                    long keyAt3 = longSparseIntArray6.keyAt(i16);
                    if (isForum(keyAt3)) {
                        longSparseIntArray6.removeAt(i16);
                        i16--;
                        i4 = 1;
                    } else {
                        int valueAt2 = longSparseIntArray6.valueAt(i16);
                        SQLiteCursor queryFinalized6 = this.database.queryFinalized("SELECT unread_count FROM dialogs WHERE did = " + keyAt3, new Object[0]);
                        int intValue3 = queryFinalized6.next() ? queryFinalized6.intValue(0) : 0;
                        queryFinalized6.dispose();
                        if (intValue3 == valueAt2) {
                            longSparseIntArray6.removeAt(i16);
                            i16--;
                        } else {
                            executeFast3.requery();
                            executeFast3.bindInteger(1, valueAt2);
                            executeFast3.bindLong(2, keyAt3);
                            executeFast3.step();
                            arrayList4.add(Long.valueOf(keyAt3));
                        }
                        i4 = 1;
                    }
                    i16 += i4;
                }
                i2 = 0;
                executeFast3.dispose();
                updateWidgets(arrayList4);
            } else {
                i2 = 0;
            }
            if (longSparseIntArray7.size() > 0) {
                SQLitePreparedStatement executeFast4 = this.database.executeFast("UPDATE dialogs SET unread_count_i = ? WHERE did = ?");
                int i17 = i2;
                while (i17 < longSparseIntArray7.size()) {
                    long keyAt4 = longSparseIntArray7.keyAt(i17);
                    if (isForum(keyAt4)) {
                        longSparseIntArray7.removeAt(i17);
                        i17--;
                        i3 = 1;
                    } else {
                        executeFast4.requery();
                        i3 = 1;
                        executeFast4.bindInteger(1, longSparseIntArray7.valueAt(i17));
                        executeFast4.bindLong(2, keyAt4);
                        executeFast4.step();
                    }
                    i17 += i3;
                }
                executeFast4.dispose();
            }
            this.database.commitTransaction();
            updateFiltersReadCounter(longSparseIntArray6, longSparseIntArray7, true);
            getMessagesController().processDialogsUpdateRead(longSparseIntArray6, longSparseIntArray7);
            if (arrayList2.isEmpty()) {
            }
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    private static boolean isEmpty(SparseArray<?> sparseArray) {
        return sparseArray == null || sparseArray.size() == 0;
    }

    private static boolean isEmpty(LongSparseIntArray longSparseIntArray) {
        return longSparseIntArray == null || longSparseIntArray.size() == 0;
    }

    private static boolean isEmpty(List<?> list) {
        return list == null || list.isEmpty();
    }

    private static boolean isEmpty(SparseIntArray sparseIntArray) {
        return sparseIntArray == null || sparseIntArray.size() == 0;
    }

    private static boolean isEmpty(LongSparseArray<?> longSparseArray) {
        return longSparseArray == null || longSparseArray.size() == 0;
    }

    public void updateDialogsWithReadMessages(final LongSparseIntArray longSparseIntArray, final LongSparseIntArray longSparseIntArray2, final LongSparseArray<ArrayList<Integer>> longSparseArray, final LongSparseIntArray longSparseIntArray3, boolean z) {
        if (isEmpty(longSparseIntArray) && isEmpty(longSparseIntArray2) && isEmpty(longSparseArray) && isEmpty(longSparseIntArray3)) {
            return;
        }
        if (z) {
            this.storageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$updateDialogsWithReadMessages$107(longSparseIntArray, longSparseIntArray2, longSparseArray, longSparseIntArray3);
                }
            });
        } else {
            updateDialogsWithReadMessagesInternal(null, longSparseIntArray, longSparseIntArray2, longSparseArray, longSparseIntArray3);
        }
    }

    public void lambda$updateDialogsWithReadMessages$107(LongSparseIntArray longSparseIntArray, LongSparseIntArray longSparseIntArray2, LongSparseArray longSparseArray, LongSparseIntArray longSparseIntArray3) {
        updateDialogsWithReadMessagesInternal(null, longSparseIntArray, longSparseIntArray2, longSparseArray, longSparseIntArray3);
    }

    public void updateChatParticipants(final TLRPC$ChatParticipants tLRPC$ChatParticipants) {
        if (tLRPC$ChatParticipants == null) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateChatParticipants$109(tLRPC$ChatParticipants);
            }
        });
    }

    public void lambda$updateChatParticipants$109(TLRPC$ChatParticipants tLRPC$ChatParticipants) {
        SQLiteCursor queryFinalized;
        final TLRPC$ChatFull tLRPC$ChatFull;
        NativeByteBuffer byteBufferValue;
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                queryFinalized = this.database.queryFinalized("SELECT info, pinned, online, inviter FROM chat_settings_v2 WHERE uid = " + tLRPC$ChatParticipants.chat_id, new Object[0]);
            } catch (Exception e) {
                e = e;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            new ArrayList();
            if (!queryFinalized.next() || (byteBufferValue = queryFinalized.byteBufferValue(0)) == null) {
                tLRPC$ChatFull = null;
            } else {
                tLRPC$ChatFull = TLRPC$ChatFull.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                byteBufferValue.reuse();
                tLRPC$ChatFull.pinned_msg_id = queryFinalized.intValue(1);
                tLRPC$ChatFull.online_count = queryFinalized.intValue(2);
                tLRPC$ChatFull.inviterId = queryFinalized.longValue(3);
            }
            queryFinalized.dispose();
            if (tLRPC$ChatFull instanceof TLRPC$TL_chatFull) {
                tLRPC$ChatFull.participants = tLRPC$ChatParticipants;
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$updateChatParticipants$108(tLRPC$ChatFull);
                    }
                });
                SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO chat_settings_v2 VALUES(?, ?, ?, ?, ?, ?, ?)");
                NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$ChatFull.getObjectSize());
                tLRPC$ChatFull.serializeToStream(nativeByteBuffer);
                executeFast.bindLong(1, tLRPC$ChatFull.id);
                executeFast.bindByteBuffer(2, nativeByteBuffer);
                executeFast.bindInteger(3, tLRPC$ChatFull.pinned_msg_id);
                executeFast.bindInteger(4, tLRPC$ChatFull.online_count);
                executeFast.bindLong(5, tLRPC$ChatFull.inviterId);
                executeFast.bindInteger(6, tLRPC$ChatFull.invitesCount);
                executeFast.bindInteger(7, tLRPC$ChatFull.participants_count);
                executeFast.step();
                executeFast.dispose();
                nativeByteBuffer.reuse();
            }
        } catch (Exception e2) {
            e = e2;
            sQLiteCursor = queryFinalized;
            checkSQLException(e);
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
        } catch (Throwable th2) {
            th = th2;
            sQLiteCursor = queryFinalized;
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public void lambda$updateChatParticipants$108(TLRPC$ChatFull tLRPC$ChatFull) {
        NotificationCenter notificationCenter = getNotificationCenter();
        int i = NotificationCenter.chatInfoDidLoad;
        Boolean bool = Boolean.FALSE;
        notificationCenter.lambda$postNotificationNameOnUIThread$1(i, tLRPC$ChatFull, 0, bool, bool);
    }

    public void loadChannelAdmins(final long j) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$loadChannelAdmins$110(j);
            }
        });
    }

    public void lambda$loadChannelAdmins$110(long j) {
        SQLiteCursor queryFinalized;
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                queryFinalized = this.database.queryFinalized("SELECT uid, data FROM channel_admins_v3 WHERE did = " + j, new Object[0]);
            } catch (Throwable th) {
                th = th;
            }
        } catch (Exception e) {
            e = e;
        }
        try {
            LongSparseArray<TLRPC$ChannelParticipant> longSparseArray = new LongSparseArray<>();
            while (queryFinalized.next()) {
                NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(1);
                if (byteBufferValue != null) {
                    TLRPC$ChannelParticipant TLdeserialize = TLRPC$ChannelParticipant.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                    byteBufferValue.reuse();
                    if (TLdeserialize != null) {
                        longSparseArray.put(queryFinalized.longValue(0), TLdeserialize);
                    }
                }
            }
            queryFinalized.dispose();
            getMessagesController().processLoadedChannelAdmins(longSparseArray, j, true);
        } catch (Exception e2) {
            e = e2;
            sQLiteCursor = queryFinalized;
            checkSQLException(e);
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
        } catch (Throwable th2) {
            th = th2;
            sQLiteCursor = queryFinalized;
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public void putChannelAdmins(final long j, final LongSparseArray<TLRPC$ChannelParticipant> longSparseArray) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$putChannelAdmins$111(j, longSparseArray);
            }
        });
    }

    public void lambda$putChannelAdmins$111(long j, LongSparseArray longSparseArray) {
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                this.database.executeFast("DELETE FROM channel_admins_v3 WHERE did = " + j).stepThis().dispose();
                this.database.beginTransaction();
                SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO channel_admins_v3 VALUES(?, ?, ?)");
                for (int i = 0; i < longSparseArray.size(); i++) {
                    try {
                        executeFast.requery();
                        executeFast.bindLong(1, j);
                        executeFast.bindLong(2, longSparseArray.keyAt(i));
                        TLRPC$ChannelParticipant tLRPC$ChannelParticipant = (TLRPC$ChannelParticipant) longSparseArray.valueAt(i);
                        NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$ChannelParticipant.getObjectSize());
                        tLRPC$ChannelParticipant.serializeToStream(nativeByteBuffer);
                        executeFast.bindByteBuffer(3, nativeByteBuffer);
                        executeFast.step();
                        nativeByteBuffer.reuse();
                    } catch (Exception e) {
                        e = e;
                        sQLitePreparedStatement = executeFast;
                        checkSQLException(e);
                        SQLiteDatabase sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                            sQLiteDatabase.commitTransaction();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                            return;
                        }
                        return;
                    } catch (Throwable th) {
                        th = th;
                        sQLitePreparedStatement = executeFast;
                        SQLiteDatabase sQLiteDatabase2 = this.database;
                        if (sQLiteDatabase2 != null) {
                            sQLiteDatabase2.commitTransaction();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                        throw th;
                    }
                }
                executeFast.dispose();
                this.database.commitTransaction();
                SQLiteDatabase sQLiteDatabase3 = this.database;
                if (sQLiteDatabase3 != null) {
                    sQLiteDatabase3.commitTransaction();
                }
            } catch (Exception e2) {
                e = e2;
            }
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public void updateChannelUsers(final long j, final ArrayList<TLRPC$ChannelParticipant> arrayList) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateChannelUsers$112(j, arrayList);
            }
        });
    }

    public void lambda$updateChannelUsers$112(long j, ArrayList arrayList) {
        SQLitePreparedStatement executeFast;
        long j2 = -j;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                this.database.executeFast("DELETE FROM channel_users_v2 WHERE did = " + j2).stepThis().dispose();
                this.database.beginTransaction();
                executeFast = this.database.executeFast("REPLACE INTO channel_users_v2 VALUES(?, ?, ?, ?)");
            } catch (Throwable th) {
                th = th;
            }
        } catch (Exception e) {
            e = e;
        }
        try {
            int currentTimeMillis = (int) (System.currentTimeMillis() / 1000);
            for (int i = 0; i < arrayList.size(); i++) {
                TLRPC$ChannelParticipant tLRPC$ChannelParticipant = (TLRPC$ChannelParticipant) arrayList.get(i);
                executeFast.requery();
                executeFast.bindLong(1, j2);
                executeFast.bindLong(2, MessageObject.getPeerId(tLRPC$ChannelParticipant.peer));
                executeFast.bindInteger(3, currentTimeMillis);
                NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$ChannelParticipant.getObjectSize());
                tLRPC$ChannelParticipant.serializeToStream(nativeByteBuffer);
                executeFast.bindByteBuffer(4, nativeByteBuffer);
                executeFast.step();
                nativeByteBuffer.reuse();
                currentTimeMillis--;
            }
            executeFast.dispose();
            this.database.commitTransaction();
            loadChatInfo(j, true, null, false, true);
            SQLiteDatabase sQLiteDatabase = this.database;
            if (sQLiteDatabase != null) {
                sQLiteDatabase.commitTransaction();
            }
        } catch (Exception e2) {
            e = e2;
            sQLitePreparedStatement = executeFast;
            checkSQLException(e);
            SQLiteDatabase sQLiteDatabase2 = this.database;
            if (sQLiteDatabase2 != null) {
                sQLiteDatabase2.commitTransaction();
            }
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
        } catch (Throwable th2) {
            th = th2;
            sQLitePreparedStatement = executeFast;
            SQLiteDatabase sQLiteDatabase3 = this.database;
            if (sQLiteDatabase3 != null) {
                sQLiteDatabase3.commitTransaction();
            }
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void saveBotCache(final String str, final TLObject tLObject) {
        if (tLObject == null || TextUtils.isEmpty(str)) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$saveBotCache$113(tLObject, str);
            }
        });
    }

    public void lambda$saveBotCache$113(TLObject tLObject, String str) {
        int currentTime;
        int i;
        SQLitePreparedStatement executeFast;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                currentTime = getConnectionsManager().getCurrentTime();
            } catch (Exception e) {
                e = e;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            if (tLObject instanceof TLRPC$TL_messages_botCallbackAnswer) {
                i = ((TLRPC$TL_messages_botCallbackAnswer) tLObject).cache_time;
            } else {
                if (tLObject instanceof TLRPC$TL_messages_botResults) {
                    i = ((TLRPC$TL_messages_botResults) tLObject).cache_time;
                }
                executeFast = this.database.executeFast("REPLACE INTO botcache VALUES(?, ?, ?)");
                NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLObject.getObjectSize());
                tLObject.serializeToStream(nativeByteBuffer);
                executeFast.bindString(1, str);
                executeFast.bindInteger(2, currentTime);
                executeFast.bindByteBuffer(3, nativeByteBuffer);
                executeFast.step();
                executeFast.dispose();
                nativeByteBuffer.reuse();
                return;
            }
            NativeByteBuffer nativeByteBuffer2 = new NativeByteBuffer(tLObject.getObjectSize());
            tLObject.serializeToStream(nativeByteBuffer2);
            executeFast.bindString(1, str);
            executeFast.bindInteger(2, currentTime);
            executeFast.bindByteBuffer(3, nativeByteBuffer2);
            executeFast.step();
            executeFast.dispose();
            nativeByteBuffer2.reuse();
            return;
        } catch (Exception e2) {
            e = e2;
            sQLitePreparedStatement = executeFast;
            checkSQLException(e);
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
                return;
            }
            return;
        } catch (Throwable th2) {
            th = th2;
            sQLitePreparedStatement = executeFast;
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
        currentTime += i;
        executeFast = this.database.executeFast("REPLACE INTO botcache VALUES(?, ?, ?)");
    }

    public void getBotCache(final String str, final RequestDelegate requestDelegate) {
        if (str == null || requestDelegate == null) {
            return;
        }
        final int currentTime = getConnectionsManager().getCurrentTime();
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getBotCache$114(currentTime, str, requestDelegate);
            }
        });
    }

    public void lambda$getBotCache$114(int i, String str, RequestDelegate requestDelegate) {
        SQLiteCursor sQLiteCursor;
        TLObject tLObject;
        NativeByteBuffer byteBufferValue;
        try {
            this.database.executeFast("DELETE FROM botcache WHERE date < " + i).stepThis().dispose();
            sQLiteCursor = this.database.queryFinalized("SELECT data FROM botcache WHERE id = ?", str);
            try {
                try {
                    if (sQLiteCursor.next()) {
                        try {
                            byteBufferValue = sQLiteCursor.byteBufferValue(0);
                        } catch (Exception e) {
                            e = e;
                            tLObject = null;
                        }
                        if (byteBufferValue != null) {
                            int readInt32 = byteBufferValue.readInt32(false);
                            if (readInt32 == 911761060) {
                                tLObject = TLRPC$TL_messages_botCallbackAnswer.TLdeserialize(byteBufferValue, readInt32, false);
                            } else {
                                tLObject = TLRPC$messages_BotResults.TLdeserialize(byteBufferValue, readInt32, false);
                            }
                            try {
                                try {
                                    byteBufferValue.reuse();
                                } catch (Exception e2) {
                                    e = e2;
                                    try {
                                        checkSQLException(e);
                                        sQLiteCursor.dispose();
                                        requestDelegate.run(tLObject, null);
                                    } catch (Exception e3) {
                                        e = e3;
                                        checkSQLException(e);
                                        requestDelegate.run(tLObject, null);
                                        if (sQLiteCursor != null) {
                                            sQLiteCursor.dispose();
                                            return;
                                        }
                                        return;
                                    }
                                }
                                sQLiteCursor.dispose();
                                requestDelegate.run(tLObject, null);
                            } catch (Throwable th) {
                                th = th;
                                requestDelegate.run(tLObject, null);
                                if (sQLiteCursor != null) {
                                }
                                throw th;
                            }
                        }
                    }
                    tLObject = null;
                    sQLiteCursor.dispose();
                    requestDelegate.run(tLObject, null);
                } catch (Throwable th2) {
                    th = th2;
                    tLObject = null;
                    requestDelegate.run(tLObject, null);
                    if (sQLiteCursor != null) {
                        sQLiteCursor.dispose();
                    }
                    throw th;
                }
            } catch (Exception e4) {
                e = e4;
                tLObject = null;
            }
        } catch (Exception e5) {
            e = e5;
            sQLiteCursor = null;
            tLObject = null;
        } catch (Throwable th3) {
            th = th3;
            sQLiteCursor = null;
            tLObject = null;
        }
    }

    public ArrayList<TLRPC$UserFull> loadUserInfos(HashSet<Long> hashSet) {
        ArrayList<TLRPC$UserFull> arrayList = new ArrayList<>();
        try {
            String join = TextUtils.join(",", hashSet);
            SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT info, pinned FROM user_settings WHERE uid IN(" + join + ")", new Object[0]);
            while (queryFinalized.next()) {
                NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(0);
                if (byteBufferValue != null) {
                    TLRPC$UserFull TLdeserialize = TLRPC$UserFull.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                    TLdeserialize.pinned_msg_id = queryFinalized.intValue(1);
                    arrayList.add(TLdeserialize);
                    byteBufferValue.reuse();
                }
            }
            queryFinalized.dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
        return arrayList;
    }

    public void loadUserInfo(final TLRPC$User tLRPC$User, final boolean z, final int i, int i2) {
        if (tLRPC$User == null) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$loadUserInfo$115(tLRPC$User, z, i);
            }
        });
    }

    public void lambda$loadUserInfo$115(TLRPC$User tLRPC$User, boolean z, int i) {
        SQLiteCursor sQLiteCursor;
        int i2;
        boolean z2;
        TLRPC$UserFull tLRPC$UserFull;
        SQLiteCursor queryFinalized;
        TLRPC$UserFull tLRPC$UserFull2;
        int i3;
        boolean z3;
        ArrayList<MessageObject> loadPinnedMessages;
        NativeByteBuffer byteBufferValue;
        HashMap<Integer, MessageObject> hashMap = new HashMap<>();
        ArrayList<Integer> arrayList = new ArrayList<>();
        try {
            queryFinalized = this.database.queryFinalized("SELECT info, pinned FROM user_settings WHERE uid = " + tLRPC$User.id, new Object[0]);
            try {
                if (!queryFinalized.next() || (byteBufferValue = queryFinalized.byteBufferValue(0)) == null) {
                    tLRPC$UserFull2 = null;
                } else {
                    tLRPC$UserFull2 = TLRPC$UserFull.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                    try {
                        tLRPC$UserFull2.pinned_msg_id = queryFinalized.intValue(1);
                        byteBufferValue.reuse();
                    } catch (Exception e) {
                        e = e;
                        i2 = 0;
                        z2 = false;
                        sQLiteCursor = queryFinalized;
                        tLRPC$UserFull = tLRPC$UserFull2;
                        try {
                            checkSQLException(e);
                            getMessagesController().processUserInfo(tLRPC$User, tLRPC$UserFull, true, z, i, arrayList, hashMap, i2, z2);
                            if (sQLiteCursor == null) {
                            }
                        } catch (Throwable th) {
                            th = th;
                            getMessagesController().processUserInfo(tLRPC$User, tLRPC$UserFull, true, z, i, arrayList, hashMap, i2, z2);
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                            }
                            throw th;
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        i2 = 0;
                        z2 = false;
                        sQLiteCursor = queryFinalized;
                        tLRPC$UserFull = tLRPC$UserFull2;
                        getMessagesController().processUserInfo(tLRPC$User, tLRPC$UserFull, true, z, i, arrayList, hashMap, i2, z2);
                        if (sQLiteCursor != null) {
                        }
                        throw th;
                    }
                }
                queryFinalized.dispose();
            } catch (Exception e2) {
                e = e2;
                i2 = 0;
                z2 = false;
                sQLiteCursor = queryFinalized;
                tLRPC$UserFull = null;
            } catch (Throwable th3) {
                th = th3;
                i2 = 0;
                z2 = false;
                sQLiteCursor = queryFinalized;
                tLRPC$UserFull = null;
            }
        } catch (Exception e3) {
            e = e3;
            sQLiteCursor = null;
            i2 = 0;
            z2 = false;
            tLRPC$UserFull = null;
        } catch (Throwable th4) {
            th = th4;
            sQLiteCursor = null;
            i2 = 0;
            z2 = false;
            tLRPC$UserFull = null;
        }
        try {
            SQLiteCursor queryFinalized2 = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT mid FROM chat_pinned_v2 WHERE uid = %d ORDER BY mid DESC", Long.valueOf(tLRPC$User.id)), new Object[0]);
            while (queryFinalized2.next()) {
                int intValue = queryFinalized2.intValue(0);
                arrayList.add(Integer.valueOf(intValue));
                hashMap.put(Integer.valueOf(intValue), null);
            }
            queryFinalized2.dispose();
            queryFinalized = this.database.queryFinalized("SELECT count, end FROM chat_pinned_count WHERE uid = " + tLRPC$User.id, new Object[0]);
            if (queryFinalized.next()) {
                i2 = queryFinalized.intValue(0);
                try {
                    z3 = queryFinalized.intValue(1) != 0;
                    i3 = i2;
                } catch (Exception e4) {
                    e = e4;
                    z2 = false;
                    sQLiteCursor = queryFinalized;
                    tLRPC$UserFull = tLRPC$UserFull2;
                    checkSQLException(e);
                    getMessagesController().processUserInfo(tLRPC$User, tLRPC$UserFull, true, z, i, arrayList, hashMap, i2, z2);
                    if (sQLiteCursor == null) {
                    }
                } catch (Throwable th5) {
                    th = th5;
                    z2 = false;
                    sQLiteCursor = queryFinalized;
                    tLRPC$UserFull = tLRPC$UserFull2;
                    getMessagesController().processUserInfo(tLRPC$User, tLRPC$UserFull, true, z, i, arrayList, hashMap, i2, z2);
                    if (sQLiteCursor != null) {
                    }
                    throw th;
                }
            } else {
                i3 = 0;
                z3 = false;
            }
            try {
                queryFinalized.dispose();
                if (tLRPC$UserFull2 != null) {
                    try {
                        if (tLRPC$UserFull2.pinned_msg_id != 0 && (arrayList.isEmpty() || tLRPC$UserFull2.pinned_msg_id > arrayList.get(0).intValue())) {
                            arrayList.clear();
                            arrayList.add(Integer.valueOf(tLRPC$UserFull2.pinned_msg_id));
                            hashMap.put(Integer.valueOf(tLRPC$UserFull2.pinned_msg_id), null);
                        }
                    } catch (Exception e5) {
                        e = e5;
                        sQLiteCursor = null;
                        tLRPC$UserFull = tLRPC$UserFull2;
                        i2 = i3;
                        z2 = z3;
                        checkSQLException(e);
                        getMessagesController().processUserInfo(tLRPC$User, tLRPC$UserFull, true, z, i, arrayList, hashMap, i2, z2);
                        if (sQLiteCursor == null) {
                            sQLiteCursor.dispose();
                            return;
                        }
                        return;
                    } catch (Throwable th6) {
                        th = th6;
                        sQLiteCursor = null;
                        tLRPC$UserFull = tLRPC$UserFull2;
                        i2 = i3;
                        z2 = z3;
                        getMessagesController().processUserInfo(tLRPC$User, tLRPC$UserFull, true, z, i, arrayList, hashMap, i2, z2);
                        if (sQLiteCursor != null) {
                        }
                        throw th;
                    }
                }
                if (!arrayList.isEmpty() && (loadPinnedMessages = getMediaDataController().loadPinnedMessages(tLRPC$User.id, 0L, arrayList, false)) != null) {
                    int size = loadPinnedMessages.size();
                    for (int i4 = 0; i4 < size; i4++) {
                        MessageObject messageObject = loadPinnedMessages.get(i4);
                        hashMap.put(Integer.valueOf(messageObject.getId()), messageObject);
                    }
                }
                getMessagesController().processUserInfo(tLRPC$User, tLRPC$UserFull2, true, z, i, arrayList, hashMap, i3, z3);
            } catch (Exception e6) {
                e = e6;
                sQLiteCursor = queryFinalized;
            } catch (Throwable th7) {
                th = th7;
                sQLiteCursor = queryFinalized;
            }
        } catch (Exception e7) {
            e = e7;
            sQLiteCursor = null;
            i2 = 0;
            z2 = false;
            tLRPC$UserFull = tLRPC$UserFull2;
            checkSQLException(e);
            getMessagesController().processUserInfo(tLRPC$User, tLRPC$UserFull, true, z, i, arrayList, hashMap, i2, z2);
            if (sQLiteCursor == null) {
            }
        } catch (Throwable th8) {
            th = th8;
            sQLiteCursor = null;
            i2 = 0;
            z2 = false;
            tLRPC$UserFull = tLRPC$UserFull2;
            getMessagesController().processUserInfo(tLRPC$User, tLRPC$UserFull, true, z, i, arrayList, hashMap, i2, z2);
            if (sQLiteCursor != null) {
            }
            throw th;
        }
    }

    public void updateUserInfo(final TLRPC$UserFull tLRPC$UserFull, final boolean z) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateUserInfo$116(tLRPC$UserFull, z);
            }
        });
    }

    public void lambda$updateUserInfo$116(TLRPC$UserFull tLRPC$UserFull, boolean z) {
        SQLiteCursor queryFinalized;
        TLRPC$User tLRPC$User = tLRPC$UserFull.user;
        long j = tLRPC$User != null ? tLRPC$User.id : tLRPC$UserFull.id;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        if (z) {
            try {
                queryFinalized = this.database.queryFinalized("SELECT uid FROM user_settings WHERE uid = " + j, new Object[0]);
                try {
                    try {
                        boolean next = queryFinalized.next();
                        queryFinalized.dispose();
                        if (!next) {
                            return;
                        }
                    } catch (Exception e) {
                        e = e;
                        checkSQLException(e);
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                        if (queryFinalized == null) {
                            queryFinalized.dispose();
                            return;
                        }
                        return;
                    }
                } catch (Throwable th) {
                    th = th;
                    if (sQLitePreparedStatement != null) {
                        sQLitePreparedStatement.dispose();
                    }
                    if (queryFinalized != null) {
                        queryFinalized.dispose();
                    }
                    throw th;
                }
            } catch (Exception e2) {
                e = e2;
                queryFinalized = null;
            } catch (Throwable th2) {
                th = th2;
                queryFinalized = null;
                if (sQLitePreparedStatement != null) {
                }
                if (queryFinalized != null) {
                }
                throw th;
            }
        }
        SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO user_settings VALUES(?, ?, ?)");
        try {
            NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$UserFull.getObjectSize());
            tLRPC$UserFull.serializeToStream(nativeByteBuffer);
            executeFast.bindLong(1, j);
            executeFast.bindByteBuffer(2, nativeByteBuffer);
            executeFast.bindInteger(3, tLRPC$UserFull.pinned_msg_id);
            executeFast.step();
            executeFast.dispose();
            nativeByteBuffer.reuse();
            if ((tLRPC$UserFull.flags & 2048) != 0) {
                SQLitePreparedStatement executeFast2 = this.database.executeFast("UPDATE dialogs SET folder_id = ? WHERE did = ?");
                executeFast2.bindInteger(1, tLRPC$UserFull.folder_id);
                executeFast2.bindLong(2, j);
                executeFast2.step();
                executeFast2.dispose();
                this.unknownDialogsIds.remove(tLRPC$UserFull.user.id);
            }
            if ((tLRPC$UserFull.flags & LiteMode.FLAG_ANIMATED_EMOJI_KEYBOARD_NOT_PREMIUM) != 0) {
                executeFast = this.database.executeFast("UPDATE dialogs SET ttl_period = ? WHERE did = ?");
                executeFast.bindInteger(1, tLRPC$UserFull.ttl_period);
                executeFast.bindLong(2, j);
                executeFast.step();
                executeFast.dispose();
            }
        } catch (Exception e3) {
            e = e3;
            sQLitePreparedStatement = executeFast;
            queryFinalized = null;
            checkSQLException(e);
            if (sQLitePreparedStatement != null) {
            }
            if (queryFinalized == null) {
            }
        } catch (Throwable th3) {
            th = th3;
            sQLitePreparedStatement = executeFast;
            queryFinalized = null;
            if (sQLitePreparedStatement != null) {
            }
            if (queryFinalized != null) {
            }
            throw th;
        }
    }

    public void updateUserInfoPremiumBlocked(final long j, final boolean z) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateUserInfoPremiumBlocked$117(j, z);
            }
        });
    }

    public void lambda$updateUserInfoPremiumBlocked$117(long j, boolean z) {
        SQLiteCursor sQLiteCursor;
        SQLiteCursor sQLiteCursor2;
        TLRPC$UserFull tLRPC$UserFull;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                sQLiteCursor2 = this.database.queryFinalized("SELECT uid, info, pinned FROM user_settings WHERE uid = " + j, new Object[0]);
                try {
                    boolean next = sQLiteCursor2.next();
                    if (next) {
                        NativeByteBuffer byteBufferValue = sQLiteCursor2.byteBufferValue(1);
                        tLRPC$UserFull = TLRPC$UserFull.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(true), true);
                        if (tLRPC$UserFull != null) {
                            tLRPC$UserFull.pinned_msg_id = sQLiteCursor2.intValue(2);
                        }
                        byteBufferValue.reuse();
                    } else {
                        tLRPC$UserFull = null;
                    }
                    sQLiteCursor2.dispose();
                    if (next && tLRPC$UserFull != null && tLRPC$UserFull.contact_require_premium != z) {
                        tLRPC$UserFull.contact_require_premium = z;
                        SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO user_settings VALUES(?, ?, ?)");
                        try {
                            NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$UserFull.getObjectSize());
                            tLRPC$UserFull.serializeToStream(nativeByteBuffer);
                            executeFast.bindLong(1, j);
                            executeFast.bindByteBuffer(2, nativeByteBuffer);
                            executeFast.bindInteger(3, tLRPC$UserFull.pinned_msg_id);
                            executeFast.step();
                            executeFast.dispose();
                            nativeByteBuffer.reuse();
                        } catch (Exception e) {
                            e = e;
                            sQLiteCursor2 = null;
                            sQLitePreparedStatement = executeFast;
                            checkSQLException(e);
                            if (sQLitePreparedStatement != null) {
                                sQLitePreparedStatement.dispose();
                            }
                            if (sQLiteCursor2 != null) {
                                sQLiteCursor2.dispose();
                            }
                        } catch (Throwable th) {
                            th = th;
                            sQLiteCursor = null;
                            sQLitePreparedStatement = executeFast;
                            if (sQLitePreparedStatement != null) {
                                sQLitePreparedStatement.dispose();
                            }
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                            }
                            throw th;
                        }
                    }
                } catch (Exception e2) {
                    e = e2;
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Exception e3) {
            e = e3;
            sQLiteCursor2 = null;
        } catch (Throwable th3) {
            th = th3;
            sQLiteCursor = null;
        }
    }

    public void saveChatInviter(final long j, final long j2) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$saveChatInviter$118(j2, j);
            }
        });
    }

    public void lambda$saveChatInviter$118(long j, long j2) {
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                sQLitePreparedStatement = this.database.executeFast("UPDATE chat_settings_v2 SET inviter = ? WHERE uid = ?");
                sQLitePreparedStatement.requery();
                sQLitePreparedStatement.bindLong(1, j);
                sQLitePreparedStatement.bindLong(2, j2);
                sQLitePreparedStatement.step();
                sQLitePreparedStatement.dispose();
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLitePreparedStatement == null) {
                    return;
                }
            }
            sQLitePreparedStatement.dispose();
        } catch (Throwable th) {
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void saveChatLinksCount(final long j, final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$saveChatLinksCount$119(i, j);
            }
        });
    }

    public void lambda$saveChatLinksCount$119(int i, long j) {
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                sQLitePreparedStatement = this.database.executeFast("UPDATE chat_settings_v2 SET links = ? WHERE uid = ?");
                sQLitePreparedStatement.requery();
                sQLitePreparedStatement.bindInteger(1, i);
                sQLitePreparedStatement.bindLong(2, j);
                sQLitePreparedStatement.step();
                sQLitePreparedStatement.dispose();
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLitePreparedStatement != null) {
                    sQLitePreparedStatement.dispose();
                }
            }
        } catch (Throwable th) {
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void updateChatInfo(final TLRPC$ChatFull tLRPC$ChatFull, final boolean z) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateChatInfo$120(tLRPC$ChatFull, z);
            }
        });
    }

    public void lambda$updateChatInfo$120(TLRPC$ChatFull tLRPC$ChatFull, boolean z) {
        SQLitePreparedStatement sQLitePreparedStatement;
        int i;
        int i2;
        SQLitePreparedStatement sQLitePreparedStatement2;
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT online, inviter, links FROM chat_settings_v2 WHERE uid = " + tLRPC$ChatFull.id, new Object[0]);
                try {
                    if (queryFinalized.next()) {
                        i2 = queryFinalized.intValue(0);
                        tLRPC$ChatFull.inviterId = queryFinalized.longValue(1);
                        i = queryFinalized.intValue(2);
                    } else {
                        i = 0;
                        i2 = -1;
                    }
                    queryFinalized.dispose();
                    if (z == 0 || i2 != -1) {
                        if (i2 >= 0 && (tLRPC$ChatFull.flags & LiteMode.FLAG_ANIMATED_EMOJI_REACTIONS_NOT_PREMIUM) == 0) {
                            tLRPC$ChatFull.online_count = i2;
                        }
                        if (i >= 0) {
                            tLRPC$ChatFull.invitesCount = i;
                        }
                        sQLitePreparedStatement = this.database.executeFast("REPLACE INTO chat_settings_v2 VALUES(?, ?, ?, ?, ?, ?, ?)");
                        try {
                            NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$ChatFull.getObjectSize());
                            tLRPC$ChatFull.serializeToStream(nativeByteBuffer);
                            sQLitePreparedStatement.bindLong(1, tLRPC$ChatFull.id);
                            sQLitePreparedStatement.bindByteBuffer(2, nativeByteBuffer);
                            sQLitePreparedStatement.bindInteger(3, tLRPC$ChatFull.pinned_msg_id);
                            sQLitePreparedStatement.bindInteger(4, tLRPC$ChatFull.online_count);
                            sQLitePreparedStatement.bindLong(5, tLRPC$ChatFull.inviterId);
                            sQLitePreparedStatement.bindInteger(6, tLRPC$ChatFull.invitesCount);
                            sQLitePreparedStatement.bindInteger(7, tLRPC$ChatFull.participants_count);
                            sQLitePreparedStatement.step();
                            sQLitePreparedStatement.dispose();
                            nativeByteBuffer.reuse();
                            if (tLRPC$ChatFull instanceof TLRPC$TL_channelFull) {
                                SQLiteCursor queryFinalized2 = this.database.queryFinalized("SELECT inbox_max, outbox_max FROM dialogs WHERE did = " + (-tLRPC$ChatFull.id), new Object[0]);
                                try {
                                    if (!queryFinalized2.next() || queryFinalized2.intValue(0) >= tLRPC$ChatFull.read_inbox_max_id) {
                                        sQLitePreparedStatement2 = null;
                                    } else {
                                        int intValue = queryFinalized2.intValue(1);
                                        sQLitePreparedStatement2 = this.database.executeFast("UPDATE dialogs SET unread_count = ?, inbox_max = ?, outbox_max = ? WHERE did = ?");
                                        try {
                                            sQLitePreparedStatement2.bindInteger(1, tLRPC$ChatFull.unread_count);
                                            sQLitePreparedStatement2.bindInteger(2, tLRPC$ChatFull.read_inbox_max_id);
                                            sQLitePreparedStatement2.bindInteger(3, Math.max(intValue, tLRPC$ChatFull.read_outbox_max_id));
                                            sQLitePreparedStatement2.bindLong(4, -tLRPC$ChatFull.id);
                                            sQLitePreparedStatement2.step();
                                            sQLitePreparedStatement2.dispose();
                                        } catch (Exception e) {
                                            e = e;
                                            sQLiteCursor = queryFinalized2;
                                            sQLitePreparedStatement = sQLitePreparedStatement2;
                                            checkSQLException(e);
                                            if (sQLiteCursor != null) {
                                            }
                                            if (sQLitePreparedStatement != null) {
                                            }
                                        } catch (Throwable th) {
                                            th = th;
                                            sQLiteCursor = queryFinalized2;
                                            z = sQLitePreparedStatement2;
                                            if (sQLiteCursor != null) {
                                            }
                                            if (z != 0) {
                                            }
                                            throw th;
                                        }
                                    }
                                    queryFinalized2.dispose();
                                } catch (Exception e2) {
                                    e = e2;
                                    sQLiteCursor = queryFinalized2;
                                    sQLitePreparedStatement = null;
                                    checkSQLException(e);
                                    if (sQLiteCursor != null) {
                                    }
                                    if (sQLitePreparedStatement != null) {
                                    }
                                } catch (Throwable th2) {
                                    th = th2;
                                    sQLiteCursor = queryFinalized2;
                                    z = 0;
                                    if (sQLiteCursor != null) {
                                    }
                                    if (z != 0) {
                                    }
                                    throw th;
                                }
                            } else {
                                sQLitePreparedStatement2 = null;
                            }
                        } catch (Exception e3) {
                            e = e3;
                        }
                        try {
                            if ((tLRPC$ChatFull.flags & 2048) != 0) {
                                SQLitePreparedStatement executeFast = this.database.executeFast("UPDATE dialogs SET folder_id = ? WHERE did = ?");
                                executeFast.bindInteger(1, tLRPC$ChatFull.folder_id);
                                executeFast.bindLong(2, -tLRPC$ChatFull.id);
                                executeFast.step();
                                executeFast.dispose();
                                this.unknownDialogsIds.remove(-tLRPC$ChatFull.id);
                                sQLitePreparedStatement2 = null;
                            }
                            sQLitePreparedStatement = this.database.executeFast("UPDATE dialogs SET ttl_period = ? WHERE did = ?");
                            sQLitePreparedStatement.bindInteger(1, tLRPC$ChatFull.ttl_period);
                            sQLitePreparedStatement.bindLong(2, -tLRPC$ChatFull.id);
                            sQLitePreparedStatement.step();
                            sQLitePreparedStatement.dispose();
                        } catch (Exception e4) {
                            e = e4;
                            sQLitePreparedStatement = sQLitePreparedStatement2;
                            checkSQLException(e);
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                            }
                            if (sQLitePreparedStatement != null) {
                                sQLitePreparedStatement.dispose();
                            }
                        } catch (Throwable th3) {
                            th = th3;
                            z = sQLitePreparedStatement2;
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                            }
                            if (z != 0) {
                                z.dispose();
                            }
                            throw th;
                        }
                    }
                } catch (Exception e5) {
                    e = e5;
                    sQLitePreparedStatement = null;
                    sQLiteCursor = queryFinalized;
                } catch (Throwable th4) {
                    th = th4;
                    z = 0;
                    sQLiteCursor = queryFinalized;
                }
            } catch (Throwable th5) {
                th = th5;
            }
        } catch (Exception e6) {
            e = e6;
            sQLitePreparedStatement = null;
        } catch (Throwable th6) {
            th = th6;
            z = 0;
        }
    }

    public void updateChatOnlineCount(final long j, final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateChatOnlineCount$121(i, j);
            }
        });
    }

    public void lambda$updateChatOnlineCount$121(int i, long j) {
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                sQLitePreparedStatement = this.database.executeFast("UPDATE chat_settings_v2 SET online = ? WHERE uid = ?");
                sQLitePreparedStatement.requery();
                sQLitePreparedStatement.bindInteger(1, i);
                sQLitePreparedStatement.bindLong(2, j);
                sQLitePreparedStatement.step();
                sQLitePreparedStatement.dispose();
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLitePreparedStatement != null) {
                    sQLitePreparedStatement.dispose();
                }
            }
        } catch (Throwable th) {
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void updatePinnedMessages(final long j, final ArrayList<Integer> arrayList, final boolean z, final int i, final int i2, final boolean z2, final HashMap<Integer, MessageObject> hashMap) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updatePinnedMessages$124(z, hashMap, i2, j, arrayList, i, z2);
            }
        });
    }

    public void lambda$updatePinnedMessages$124(

    public void lambda$updatePinnedMessages$122(long j, ArrayList arrayList, HashMap hashMap, int i, int i2, boolean z) {
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.didLoadPinnedMessages, Long.valueOf(j), arrayList, Boolean.TRUE, null, hashMap, Integer.valueOf(i), Integer.valueOf(i2), Boolean.valueOf(z));
    }

    public void lambda$updatePinnedMessages$123(long j, ArrayList arrayList, HashMap hashMap, int i, int i2, boolean z) {
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.didLoadPinnedMessages, Long.valueOf(j), arrayList, Boolean.FALSE, null, hashMap, Integer.valueOf(i), Integer.valueOf(i2), Boolean.valueOf(z));
    }

    public void updateChatInfo(final long j, final long j2, final int i, final long j3, final int i2) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateChatInfo$126(j, i, j2, j3, i2);
            }
        });
    }

    public void lambda$updateChatInfo$126(long j, int i, long j2, long j3, int i2) {
        int i3;
        SQLiteCursor queryFinalized;
        final TLRPC$ChatFull tLRPC$ChatFull;
        TLRPC$ChatParticipant tLRPC$TL_chatParticipant;
        NativeByteBuffer byteBufferValue;
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                i3 = 0;
                queryFinalized = this.database.queryFinalized("SELECT info, pinned, online, inviter FROM chat_settings_v2 WHERE uid = " + j, new Object[0]);
            } catch (Exception e) {
                e = e;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            new ArrayList();
            if (!queryFinalized.next() || (byteBufferValue = queryFinalized.byteBufferValue(0)) == null) {
                tLRPC$ChatFull = null;
            } else {
                tLRPC$ChatFull = TLRPC$ChatFull.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                byteBufferValue.reuse();
                tLRPC$ChatFull.pinned_msg_id = queryFinalized.intValue(1);
                tLRPC$ChatFull.online_count = queryFinalized.intValue(2);
                tLRPC$ChatFull.inviterId = queryFinalized.longValue(3);
            }
            queryFinalized.dispose();
            if (tLRPC$ChatFull instanceof TLRPC$TL_chatFull) {
                if (i == 1) {
                    while (true) {
                        if (i3 >= tLRPC$ChatFull.participants.participants.size()) {
                            break;
                        }
                        if (tLRPC$ChatFull.participants.participants.get(i3).user_id == j2) {
                            tLRPC$ChatFull.participants.participants.remove(i3);
                            break;
                        }
                        i3++;
                    }
                } else if (i == 0) {
                    Iterator<TLRPC$ChatParticipant> it = tLRPC$ChatFull.participants.participants.iterator();
                    while (it.hasNext()) {
                        if (it.next().user_id == j2) {
                            return;
                        }
                    }
                    TLRPC$TL_chatParticipant tLRPC$TL_chatParticipant2 = new TLRPC$TL_chatParticipant();
                    tLRPC$TL_chatParticipant2.user_id = j2;
                    tLRPC$TL_chatParticipant2.inviter_id = j3;
                    tLRPC$TL_chatParticipant2.date = getConnectionsManager().getCurrentTime();
                    tLRPC$ChatFull.participants.participants.add(tLRPC$TL_chatParticipant2);
                } else if (i == 2) {
                    while (true) {
                        if (i3 >= tLRPC$ChatFull.participants.participants.size()) {
                            break;
                        }
                        TLRPC$ChatParticipant tLRPC$ChatParticipant = tLRPC$ChatFull.participants.participants.get(i3);
                        if (tLRPC$ChatParticipant.user_id == j2) {
                            if (j3 == 1) {
                                tLRPC$TL_chatParticipant = new TLRPC$TL_chatParticipantAdmin();
                            } else {
                                tLRPC$TL_chatParticipant = new TLRPC$TL_chatParticipant();
                            }
                            tLRPC$TL_chatParticipant.user_id = tLRPC$ChatParticipant.user_id;
                            tLRPC$TL_chatParticipant.date = tLRPC$ChatParticipant.date;
                            tLRPC$TL_chatParticipant.inviter_id = tLRPC$ChatParticipant.inviter_id;
                            tLRPC$ChatFull.participants.participants.set(i3, tLRPC$TL_chatParticipant);
                        } else {
                            i3++;
                        }
                    }
                }
                tLRPC$ChatFull.participants.version = i2;
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$updateChatInfo$125(tLRPC$ChatFull);
                    }
                });
                SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO chat_settings_v2 VALUES(?, ?, ?, ?, ?, ?, ?)");
                NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$ChatFull.getObjectSize());
                tLRPC$ChatFull.serializeToStream(nativeByteBuffer);
                executeFast.bindLong(1, j);
                executeFast.bindByteBuffer(2, nativeByteBuffer);
                executeFast.bindInteger(3, tLRPC$ChatFull.pinned_msg_id);
                executeFast.bindInteger(4, tLRPC$ChatFull.online_count);
                executeFast.bindLong(5, tLRPC$ChatFull.inviterId);
                executeFast.bindInteger(6, tLRPC$ChatFull.invitesCount);
                executeFast.bindInteger(7, tLRPC$ChatFull.participants_count);
                executeFast.step();
                executeFast.dispose();
                nativeByteBuffer.reuse();
            }
        } catch (Exception e2) {
            e = e2;
            sQLiteCursor = queryFinalized;
            checkSQLException(e);
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
        } catch (Throwable th2) {
            th = th2;
            sQLiteCursor = queryFinalized;
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public void lambda$updateChatInfo$125(TLRPC$ChatFull tLRPC$ChatFull) {
        NotificationCenter notificationCenter = getNotificationCenter();
        int i = NotificationCenter.chatInfoDidLoad;
        Boolean bool = Boolean.FALSE;
        notificationCenter.lambda$postNotificationNameOnUIThread$1(i, tLRPC$ChatFull, 0, bool, bool);
    }

    public boolean isMigratedChat(final long j) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        final boolean[] zArr = new boolean[1];
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$isMigratedChat$127(j, zArr, countDownLatch);
            }
        });
        try {
            countDownLatch.await();
        } catch (Exception e) {
            checkSQLException(e);
        }
        return zArr[0];
    }

    public void lambda$isMigratedChat$127(long j, boolean[] zArr, CountDownLatch countDownLatch) {
        SQLiteCursor queryFinalized;
        TLRPC$ChatFull tLRPC$ChatFull;
        NativeByteBuffer byteBufferValue;
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                queryFinalized = this.database.queryFinalized("SELECT info FROM chat_settings_v2 WHERE uid = " + j, new Object[0]);
            } catch (Exception e) {
                e = e;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            new ArrayList();
            if (!queryFinalized.next() || (byteBufferValue = queryFinalized.byteBufferValue(0)) == null) {
                tLRPC$ChatFull = null;
            } else {
                tLRPC$ChatFull = TLRPC$ChatFull.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                byteBufferValue.reuse();
            }
            queryFinalized.dispose();
            zArr[0] = (tLRPC$ChatFull instanceof TLRPC$TL_channelFull) && tLRPC$ChatFull.migrated_from_chat_id != 0;
            countDownLatch.countDown();
        } catch (Exception e2) {
            e = e2;
            sQLiteCursor = queryFinalized;
            checkSQLException(e);
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            countDownLatch.countDown();
        } catch (Throwable th2) {
            th = th2;
            sQLiteCursor = queryFinalized;
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            countDownLatch.countDown();
            throw th;
        }
        countDownLatch.countDown();
    }

    public TLRPC$Message getMessage(final long j, final long j2) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        final AtomicReference atomicReference = new AtomicReference();
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getMessage$128(j, j2, atomicReference, countDownLatch);
            }
        });
        try {
            countDownLatch.await();
        } catch (Exception e) {
            checkSQLException(e);
        }
        return (TLRPC$Message) atomicReference.get();
    }

    public void lambda$getMessage$128(long j, long j2, AtomicReference atomicReference, CountDownLatch countDownLatch) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                sQLiteCursor = this.database.queryFinalized("SELECT data FROM messages_v2 WHERE uid = " + j + " AND mid = " + j2 + " LIMIT 1", new Object[0]);
                while (sQLiteCursor.next()) {
                    NativeByteBuffer byteBufferValue = sQLiteCursor.byteBufferValue(0);
                    if (byteBufferValue != null) {
                        TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                        byteBufferValue.reuse();
                        atomicReference.set(TLdeserialize);
                    }
                }
                sQLiteCursor.dispose();
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLiteCursor != null) {
                    sQLiteCursor.dispose();
                }
            }
            countDownLatch.countDown();
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            countDownLatch.countDown();
            throw th;
        }
    }

    public boolean hasInviteMeMessage(final long j) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        final boolean[] zArr = new boolean[1];
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$hasInviteMeMessage$129(j, zArr, countDownLatch);
            }
        });
        try {
            countDownLatch.await();
        } catch (Exception e) {
            checkSQLException(e);
        }
        return zArr[0];
    }

    public void lambda$hasInviteMeMessage$129(long j, boolean[] zArr, CountDownLatch countDownLatch) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                long clientUserId = getUserConfig().getClientUserId();
                sQLiteCursor = this.database.queryFinalized("SELECT data FROM messages_v2 WHERE uid = " + (-j) + " AND out = 0 ORDER BY mid DESC LIMIT 100", new Object[0]);
                while (true) {
                    if (!sQLiteCursor.next()) {
                        break;
                    }
                    NativeByteBuffer byteBufferValue = sQLiteCursor.byteBufferValue(0);
                    if (byteBufferValue != null) {
                        TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                        byteBufferValue.reuse();
                        TLRPC$MessageAction tLRPC$MessageAction = TLdeserialize.action;
                        if ((tLRPC$MessageAction instanceof TLRPC$TL_messageActionChatAddUser) && tLRPC$MessageAction.users.contains(Long.valueOf(clientUserId))) {
                            zArr[0] = true;
                            break;
                        }
                    }
                }
                sQLiteCursor.dispose();
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLiteCursor != null) {
                    sQLiteCursor.dispose();
                }
            }
            countDownLatch.countDown();
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            countDownLatch.countDown();
            throw th;
        }
    }

    public HashMap<Long, Integer> getSmallGroupsParticipantsCount() {
        HashMap<Long, Integer> hashMap = new HashMap<>();
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                sQLiteCursor = this.database.queryFinalized("SELECT uid, info, participants_count FROM chat_settings_v2 WHERE participants_count > 1", new Object[0]);
                while (sQLiteCursor.next()) {
                    long longValue = sQLiteCursor.longValue(0);
                    NativeByteBuffer byteBufferValue = sQLiteCursor.byteBufferValue(1);
                    int intValue = sQLiteCursor.intValue(2);
                    if (byteBufferValue != null) {
                        TLRPC$ChatFull TLdeserialize = TLRPC$ChatFull.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                        byteBufferValue.reuse();
                        if (TLdeserialize instanceof TLRPC$TL_channelFull) {
                            hashMap.put(Long.valueOf(longValue), Integer.valueOf(intValue));
                        }
                    }
                }
                sQLiteCursor.dispose();
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLiteCursor != null) {
                    sQLiteCursor.dispose();
                }
            }
            return hashMap;
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    private TLRPC$ChatFull loadChatInfoInternal(long j, boolean z, boolean z2, boolean z3, int i) {
        SQLiteCursor sQLiteCursor;
        int i2;
        boolean z4;
        TLRPC$ChatFull tLRPC$ChatFull;
        TLRPC$ChatFull tLRPC$ChatFull2;
        TLRPC$ChatFull tLRPC$ChatFull3;
        ArrayList<Integer> arrayList;
        SQLiteCursor queryFinalized;
        int intValue;
        int i3;
        boolean z5;
        TLRPC$ChatFull tLRPC$ChatFull4;
        int i4;
        ArrayList<Long> arrayList2;
        StringBuilder sb;
        int i5;
        SQLiteDatabase sQLiteDatabase;
        StringBuilder sb2;
        TLRPC$User TLdeserialize;
        TLRPC$ChannelParticipant tLRPC$ChannelParticipant;
        long j2 = j;
        ArrayList<TLRPC$User> arrayList3 = new ArrayList<>();
        HashMap<Integer, MessageObject> hashMap = new HashMap<>();
        ArrayList<Integer> arrayList4 = new ArrayList<>();
        int i6 = 0;
        ?? r5 = 0;
        i6 = 0;
        int i7 = 0;
        try {
            SQLiteCursor queryFinalized2 = this.database.queryFinalized("SELECT info, pinned, online, inviter, links FROM chat_settings_v2 WHERE uid = " + j2, new Object[0]);
            try {
                try {
                    try {
                        try {
                            try {
                                try {
                                    try {
                                        try {
                                            if (queryFinalized2.next()) {
                                                try {
                                                    NativeByteBuffer byteBufferValue = queryFinalized2.byteBufferValue(0);
                                                    if (byteBufferValue != null) {
                                                        TLRPC$ChatFull TLdeserialize2 = TLRPC$ChatFull.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                                                        try {
                                                            byteBufferValue.reuse();
                                                            TLdeserialize2.pinned_msg_id = queryFinalized2.intValue(1);
                                                            TLdeserialize2.online_count = queryFinalized2.intValue(2);
                                                            TLdeserialize2.inviterId = queryFinalized2.longValue(3);
                                                            TLdeserialize2.invitesCount = queryFinalized2.intValue(4);
                                                            tLRPC$ChatFull2 = TLdeserialize2;
                                                            queryFinalized2.dispose();
                                                            if (!(tLRPC$ChatFull2 instanceof TLRPC$TL_chatFull)) {
                                                                try {
                                                                    StringBuilder sb3 = new StringBuilder();
                                                                    for (int i8 = 0; i8 < tLRPC$ChatFull2.participants.participants.size(); i8++) {
                                                                        TLRPC$ChatParticipant tLRPC$ChatParticipant = tLRPC$ChatFull2.participants.participants.get(i8);
                                                                        if (sb3.length() != 0) {
                                                                            sb3.append(",");
                                                                        }
                                                                        sb3.append(tLRPC$ChatParticipant.user_id);
                                                                    }
                                                                    if (sb3.length() != 0) {
                                                                        getUsersInternal(sb3.toString(), arrayList3);
                                                                    }
                                                                } catch (Exception e) {
                                                                    e = e;
                                                                    i2 = 0;
                                                                    z4 = false;
                                                                    sQLiteCursor = null;
                                                                    try {
                                                                        checkSQLException(e);
                                                                        if (sQLiteCursor != null) {
                                                                        }
                                                                        getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                        return tLRPC$ChatFull2;
                                                                    } catch (Throwable th) {
                                                                        th = th;
                                                                        tLRPC$ChatFull = tLRPC$ChatFull2;
                                                                        if (sQLiteCursor != null) {
                                                                            sQLiteCursor.dispose();
                                                                        }
                                                                        getMessagesController().processChatInfo(j, tLRPC$ChatFull, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                        throw th;
                                                                    }
                                                                } catch (Throwable th2) {
                                                                    th = th2;
                                                                    i2 = 0;
                                                                    z4 = false;
                                                                    tLRPC$ChatFull = tLRPC$ChatFull2;
                                                                    sQLiteCursor = null;
                                                                    if (sQLiteCursor != null) {
                                                                    }
                                                                    getMessagesController().processChatInfo(j, tLRPC$ChatFull, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                    throw th;
                                                                }
                                                            } else if (tLRPC$ChatFull2 instanceof TLRPC$TL_channelFull) {
                                                                try {
                                                                    sQLiteDatabase = this.database;
                                                                    sb2 = new StringBuilder();
                                                                    sb2.append("SELECT us.data, us.status, cu.data, cu.date FROM channel_users_v2 as cu LEFT JOIN users as us ON us.uid = cu.uid WHERE cu.did = ");
                                                                    arrayList = arrayList4;
                                                                } catch (Exception e2) {
                                                                    e = e2;
                                                                    sQLiteCursor = null;
                                                                    i2 = 0;
                                                                    z4 = false;
                                                                    checkSQLException(e);
                                                                    if (sQLiteCursor != null) {
                                                                    }
                                                                    getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                    return tLRPC$ChatFull2;
                                                                } catch (Throwable th3) {
                                                                    th = th3;
                                                                    tLRPC$ChatFull = tLRPC$ChatFull2;
                                                                    sQLiteCursor = null;
                                                                    i2 = 0;
                                                                    z4 = false;
                                                                    if (sQLiteCursor != null) {
                                                                    }
                                                                    getMessagesController().processChatInfo(j, tLRPC$ChatFull, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                    throw th;
                                                                }
                                                                try {
                                                                    sb2.append(-j2);
                                                                    sb2.append(" ORDER BY cu.date DESC");
                                                                    SQLiteCursor queryFinalized3 = sQLiteDatabase.queryFinalized(sb2.toString(), new Object[0]);
                                                                    try {
                                                                        try {
                                                                            tLRPC$ChatFull2.participants = new TLRPC$TL_chatParticipants();
                                                                            while (queryFinalized3.next()) {
                                                                                try {
                                                                                    NativeByteBuffer byteBufferValue2 = queryFinalized3.byteBufferValue(r5);
                                                                                    if (byteBufferValue2 != 0) {
                                                                                        try {
                                                                                            TLdeserialize = TLRPC$User.TLdeserialize(byteBufferValue2, byteBufferValue2.readInt32(r5), r5);
                                                                                            byteBufferValue2.reuse();
                                                                                        } catch (Throwable th4) {
                                                                                            th = th4;
                                                                                            arrayList4 = arrayList;
                                                                                            i2 = r5;
                                                                                            z4 = i2;
                                                                                            sQLiteCursor = queryFinalized3;
                                                                                            tLRPC$ChatFull = tLRPC$ChatFull2;
                                                                                            if (sQLiteCursor != null) {
                                                                                            }
                                                                                            getMessagesController().processChatInfo(j, tLRPC$ChatFull, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                                            throw th;
                                                                                        }
                                                                                    } else {
                                                                                        TLdeserialize = null;
                                                                                    }
                                                                                    NativeByteBuffer byteBufferValue3 = queryFinalized3.byteBufferValue(2);
                                                                                    if (byteBufferValue3 != 0) {
                                                                                        tLRPC$ChannelParticipant = TLRPC$ChannelParticipant.TLdeserialize(byteBufferValue3, byteBufferValue3.readInt32(r5), r5);
                                                                                        byteBufferValue3.reuse();
                                                                                    } else {
                                                                                        tLRPC$ChannelParticipant = null;
                                                                                    }
                                                                                    if (tLRPC$ChannelParticipant != null && tLRPC$ChannelParticipant.user_id == getUserConfig().clientUserId) {
                                                                                        TLdeserialize = getUserConfig().getCurrentUser();
                                                                                    }
                                                                                    if (TLdeserialize != null && tLRPC$ChannelParticipant != null) {
                                                                                        TLRPC$UserStatus tLRPC$UserStatus = TLdeserialize.status;
                                                                                        if (tLRPC$UserStatus != null) {
                                                                                            tLRPC$UserStatus.expires = queryFinalized3.intValue(1);
                                                                                        }
                                                                                        arrayList3.add(TLdeserialize);
                                                                                        tLRPC$ChannelParticipant.date = queryFinalized3.intValue(3);
                                                                                        TLRPC$TL_chatChannelParticipant tLRPC$TL_chatChannelParticipant = new TLRPC$TL_chatChannelParticipant();
                                                                                        tLRPC$TL_chatChannelParticipant.user_id = MessageObject.getPeerId(tLRPC$ChannelParticipant.peer);
                                                                                        tLRPC$TL_chatChannelParticipant.date = tLRPC$ChannelParticipant.date;
                                                                                        tLRPC$TL_chatChannelParticipant.inviter_id = tLRPC$ChannelParticipant.inviter_id;
                                                                                        tLRPC$TL_chatChannelParticipant.channelParticipant = tLRPC$ChannelParticipant;
                                                                                        tLRPC$ChatFull2.participants.participants.add(tLRPC$TL_chatChannelParticipant);
                                                                                    }
                                                                                } catch (Exception e3) {
                                                                                    checkSQLException(e3);
                                                                                }
                                                                                j2 = j;
                                                                                r5 = 0;
                                                                            }
                                                                            queryFinalized3.dispose();
                                                                            StringBuilder sb4 = new StringBuilder();
                                                                            for (int i9 = 0; i9 < tLRPC$ChatFull2.bot_info.size(); i9++) {
                                                                                TLRPC$BotInfo tLRPC$BotInfo = tLRPC$ChatFull2.bot_info.get(i9);
                                                                                if (sb4.length() != 0) {
                                                                                    sb4.append(",");
                                                                                }
                                                                                sb4.append(tLRPC$BotInfo.user_id);
                                                                            }
                                                                            if (sb4.length() != 0) {
                                                                                getUsersInternal(sb4.toString(), arrayList3);
                                                                            }
                                                                            if (tLRPC$ChatFull2 != null && tLRPC$ChatFull2.inviterId != 0) {
                                                                                getUsersInternal("" + tLRPC$ChatFull2.inviterId, arrayList3);
                                                                            }
                                                                            if (tLRPC$ChatFull2 != null && (arrayList2 = tLRPC$ChatFull2.recent_requesters) != null && !arrayList2.isEmpty()) {
                                                                                sb = new StringBuilder();
                                                                                for (i5 = 0; i5 < Math.min(3, tLRPC$ChatFull2.recent_requesters.size()); i5++) {
                                                                                    ArrayList<Long> arrayList5 = tLRPC$ChatFull2.recent_requesters;
                                                                                    long longValue = arrayList5.get((arrayList5.size() - 1) - i5).longValue();
                                                                                    if (i5 > 0) {
                                                                                        sb.append(',');
                                                                                    }
                                                                                    sb.append(longValue);
                                                                                }
                                                                                if (sb.length() > 0) {
                                                                                    getUsersInternal(sb.toString(), arrayList3);
                                                                                }
                                                                            }
                                                                            SQLiteDatabase database = getMessagesStorage().getDatabase();
                                                                            Locale locale = Locale.US;
                                                                            Object[] objArr = new Object[1];
                                                                            long j3 = -j;
                                                                            int i10 = 0;
                                                                            objArr[0] = Long.valueOf(j3);
                                                                            queryFinalized = database.queryFinalized(String.format(locale, "SELECT mid FROM chat_pinned_v2 WHERE uid = %d ORDER BY mid DESC", objArr), new Object[0]);
                                                                            while (queryFinalized.next()) {
                                                                                try {
                                                                                    try {
                                                                                        int intValue2 = queryFinalized.intValue(i10);
                                                                                        arrayList4 = arrayList;
                                                                                        try {
                                                                                            arrayList4.add(Integer.valueOf(intValue2));
                                                                                            hashMap.put(Integer.valueOf(intValue2), null);
                                                                                            arrayList = arrayList4;
                                                                                            i10 = 0;
                                                                                        } catch (Exception e4) {
                                                                                            e = e4;
                                                                                            sQLiteCursor = queryFinalized;
                                                                                            i2 = 0;
                                                                                            z4 = false;
                                                                                            checkSQLException(e);
                                                                                            if (sQLiteCursor != null) {
                                                                                            }
                                                                                            getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                                            return tLRPC$ChatFull2;
                                                                                        } catch (Throwable th5) {
                                                                                            th = th5;
                                                                                            sQLiteCursor = queryFinalized;
                                                                                            tLRPC$ChatFull = tLRPC$ChatFull2;
                                                                                            i2 = 0;
                                                                                            z4 = false;
                                                                                            if (sQLiteCursor != null) {
                                                                                            }
                                                                                            getMessagesController().processChatInfo(j, tLRPC$ChatFull, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                                            throw th;
                                                                                        }
                                                                                    } catch (Exception e5) {
                                                                                        e = e5;
                                                                                        arrayList4 = arrayList;
                                                                                    } catch (Throwable th6) {
                                                                                        th = th6;
                                                                                        arrayList4 = arrayList;
                                                                                    }
                                                                                } catch (Exception e6) {
                                                                                    e = e6;
                                                                                    arrayList4 = arrayList;
                                                                                    i6 = i10;
                                                                                } catch (Throwable th7) {
                                                                                    th = th7;
                                                                                    arrayList4 = arrayList;
                                                                                    i7 = i10;
                                                                                    tLRPC$ChatFull3 = tLRPC$ChatFull2;
                                                                                }
                                                                            }
                                                                            arrayList4 = arrayList;
                                                                            queryFinalized.dispose();
                                                                            sQLiteCursor = this.database.queryFinalized("SELECT count, end FROM chat_pinned_count WHERE uid = " + j3, new Object[0]);
                                                                            if (sQLiteCursor.next()) {
                                                                                try {
                                                                                    intValue = sQLiteCursor.intValue(0);
                                                                                } catch (Exception e7) {
                                                                                    e = e7;
                                                                                    i2 = 0;
                                                                                    z4 = false;
                                                                                    checkSQLException(e);
                                                                                    if (sQLiteCursor != null) {
                                                                                    }
                                                                                    getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                                    return tLRPC$ChatFull2;
                                                                                } catch (Throwable th8) {
                                                                                    th = th8;
                                                                                    tLRPC$ChatFull = tLRPC$ChatFull2;
                                                                                    i2 = 0;
                                                                                    z4 = false;
                                                                                    if (sQLiteCursor != null) {
                                                                                    }
                                                                                    getMessagesController().processChatInfo(j, tLRPC$ChatFull, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                                    throw th;
                                                                                }
                                                                                try {
                                                                                    i3 = intValue;
                                                                                    z5 = sQLiteCursor.intValue(1) != 0;
                                                                                } catch (Exception e8) {
                                                                                    e = e8;
                                                                                    i2 = intValue;
                                                                                    z4 = false;
                                                                                    checkSQLException(e);
                                                                                    if (sQLiteCursor != null) {
                                                                                    }
                                                                                    getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                                    return tLRPC$ChatFull2;
                                                                                } catch (Throwable th9) {
                                                                                    th = th9;
                                                                                    i2 = intValue;
                                                                                    tLRPC$ChatFull = tLRPC$ChatFull2;
                                                                                    z4 = false;
                                                                                    if (sQLiteCursor != null) {
                                                                                    }
                                                                                    getMessagesController().processChatInfo(j, tLRPC$ChatFull, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                                    throw th;
                                                                                }
                                                                            } else {
                                                                                i3 = 0;
                                                                                z5 = false;
                                                                            }
                                                                            sQLiteCursor.dispose();
                                                                            if (tLRPC$ChatFull2 != null) {
                                                                                try {
                                                                                    if (tLRPC$ChatFull2.pinned_msg_id != 0) {
                                                                                        try {
                                                                                            if (arrayList4.isEmpty()) {
                                                                                                i4 = 0;
                                                                                            } else {
                                                                                                try {
                                                                                                    i4 = 0;
                                                                                                    if (tLRPC$ChatFull2.pinned_msg_id <= arrayList4.get(0).intValue()) {
                                                                                                        sQLiteCursor = null;
                                                                                                        if (arrayList4.isEmpty()) {
                                                                                                            tLRPC$ChatFull4 = tLRPC$ChatFull2;
                                                                                                        } else {
                                                                                                            tLRPC$ChatFull4 = tLRPC$ChatFull2;
                                                                                                            try {
                                                                                                                ArrayList<MessageObject> loadPinnedMessages = getMediaDataController().loadPinnedMessages(j3, z ? j : 0L, arrayList4, false);
                                                                                                                if (loadPinnedMessages != null) {
                                                                                                                    int size = loadPinnedMessages.size();
                                                                                                                    while (i4 < size) {
                                                                                                                        MessageObject messageObject = loadPinnedMessages.get(i4);
                                                                                                                        hashMap.put(Integer.valueOf(messageObject.getId()), messageObject);
                                                                                                                        i4++;
                                                                                                                    }
                                                                                                                }
                                                                                                            } catch (Exception e9) {
                                                                                                                e = e9;
                                                                                                                tLRPC$ChatFull2 = tLRPC$ChatFull4;
                                                                                                                i2 = i3;
                                                                                                                z4 = z5;
                                                                                                                checkSQLException(e);
                                                                                                                if (sQLiteCursor != null) {
                                                                                                                }
                                                                                                                getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                                                                return tLRPC$ChatFull2;
                                                                                                            } catch (Throwable th10) {
                                                                                                                th = th10;
                                                                                                                tLRPC$ChatFull = tLRPC$ChatFull4;
                                                                                                                i2 = i3;
                                                                                                                z4 = z5;
                                                                                                                if (sQLiteCursor != null) {
                                                                                                                }
                                                                                                                getMessagesController().processChatInfo(j, tLRPC$ChatFull, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                                                                throw th;
                                                                                                            }
                                                                                                        }
                                                                                                        getMessagesController().processChatInfo(j, tLRPC$ChatFull4, arrayList3, true, z2, z3, arrayList4, hashMap, i3, z5);
                                                                                                        return tLRPC$ChatFull4;
                                                                                                    }
                                                                                                } catch (Exception e10) {
                                                                                                    e = e10;
                                                                                                    i2 = i3;
                                                                                                    z4 = z5;
                                                                                                    sQLiteCursor = null;
                                                                                                    checkSQLException(e);
                                                                                                    if (sQLiteCursor != null) {
                                                                                                    }
                                                                                                    getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                                                    return tLRPC$ChatFull2;
                                                                                                } catch (Throwable th11) {
                                                                                                    th = th11;
                                                                                                    tLRPC$ChatFull = tLRPC$ChatFull2;
                                                                                                    i2 = i3;
                                                                                                    z4 = z5;
                                                                                                    sQLiteCursor = null;
                                                                                                    if (sQLiteCursor != null) {
                                                                                                    }
                                                                                                    getMessagesController().processChatInfo(j, tLRPC$ChatFull, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                                                    throw th;
                                                                                                }
                                                                                            }
                                                                                            hashMap.put(Integer.valueOf(tLRPC$ChatFull2.pinned_msg_id), null);
                                                                                            if (arrayList4.isEmpty()) {
                                                                                            }
                                                                                            getMessagesController().processChatInfo(j, tLRPC$ChatFull4, arrayList3, true, z2, z3, arrayList4, hashMap, i3, z5);
                                                                                            return tLRPC$ChatFull4;
                                                                                        } catch (Exception e11) {
                                                                                            e = e11;
                                                                                            i2 = i3;
                                                                                            z4 = z5;
                                                                                            checkSQLException(e);
                                                                                            if (sQLiteCursor != null) {
                                                                                            }
                                                                                            getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                                            return tLRPC$ChatFull2;
                                                                                        } catch (Throwable th12) {
                                                                                            th = th12;
                                                                                            tLRPC$ChatFull = tLRPC$ChatFull2;
                                                                                            i2 = i3;
                                                                                            z4 = z5;
                                                                                            if (sQLiteCursor != null) {
                                                                                            }
                                                                                            getMessagesController().processChatInfo(j, tLRPC$ChatFull, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                                            throw th;
                                                                                        }
                                                                                        arrayList4.clear();
                                                                                        arrayList4.add(Integer.valueOf(tLRPC$ChatFull2.pinned_msg_id));
                                                                                        sQLiteCursor = null;
                                                                                    }
                                                                                } catch (Exception e12) {
                                                                                    e = e12;
                                                                                    sQLiteCursor = null;
                                                                                } catch (Throwable th13) {
                                                                                    th = th13;
                                                                                    sQLiteCursor = null;
                                                                                }
                                                                            }
                                                                            sQLiteCursor = null;
                                                                            i4 = 0;
                                                                            if (arrayList4.isEmpty()) {
                                                                            }
                                                                            getMessagesController().processChatInfo(j, tLRPC$ChatFull4, arrayList3, true, z2, z3, arrayList4, hashMap, i3, z5);
                                                                            return tLRPC$ChatFull4;
                                                                        } catch (Throwable th14) {
                                                                            th = th14;
                                                                            arrayList4 = arrayList;
                                                                            sQLiteCursor = queryFinalized3;
                                                                            tLRPC$ChatFull = tLRPC$ChatFull2;
                                                                            i2 = 0;
                                                                            z4 = false;
                                                                        }
                                                                    } catch (Exception e13) {
                                                                        e = e13;
                                                                        arrayList4 = arrayList;
                                                                        sQLiteCursor = queryFinalized3;
                                                                        i2 = 0;
                                                                        z4 = false;
                                                                        checkSQLException(e);
                                                                        if (sQLiteCursor != null) {
                                                                            sQLiteCursor.dispose();
                                                                        }
                                                                        getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                        return tLRPC$ChatFull2;
                                                                    }
                                                                } catch (Exception e14) {
                                                                    e = e14;
                                                                    arrayList4 = arrayList;
                                                                    sQLiteCursor = null;
                                                                    i2 = 0;
                                                                    z4 = false;
                                                                    checkSQLException(e);
                                                                    if (sQLiteCursor != null) {
                                                                    }
                                                                    getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                    return tLRPC$ChatFull2;
                                                                } catch (Throwable th15) {
                                                                    th = th15;
                                                                    arrayList4 = arrayList;
                                                                    tLRPC$ChatFull = tLRPC$ChatFull2;
                                                                    sQLiteCursor = null;
                                                                    i2 = 0;
                                                                    z4 = false;
                                                                    if (sQLiteCursor != null) {
                                                                    }
                                                                    getMessagesController().processChatInfo(j, tLRPC$ChatFull, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                                    throw th;
                                                                }
                                                            }
                                                            arrayList = arrayList4;
                                                            if (tLRPC$ChatFull2 != null) {
                                                                getUsersInternal("" + tLRPC$ChatFull2.inviterId, arrayList3);
                                                            }
                                                            if (tLRPC$ChatFull2 != null) {
                                                                sb = new StringBuilder();
                                                                while (i5 < Math.min(3, tLRPC$ChatFull2.recent_requesters.size())) {
                                                                }
                                                                if (sb.length() > 0) {
                                                                }
                                                            }
                                                            SQLiteDatabase database2 = getMessagesStorage().getDatabase();
                                                            Locale locale2 = Locale.US;
                                                            Object[] objArr2 = new Object[1];
                                                            long j32 = -j;
                                                            int i102 = 0;
                                                            objArr2[0] = Long.valueOf(j32);
                                                            queryFinalized = database2.queryFinalized(String.format(locale2, "SELECT mid FROM chat_pinned_v2 WHERE uid = %d ORDER BY mid DESC", objArr2), new Object[0]);
                                                            while (queryFinalized.next()) {
                                                            }
                                                            arrayList4 = arrayList;
                                                            queryFinalized.dispose();
                                                            sQLiteCursor = this.database.queryFinalized("SELECT count, end FROM chat_pinned_count WHERE uid = " + j32, new Object[0]);
                                                            if (sQLiteCursor.next()) {
                                                            }
                                                            sQLiteCursor.dispose();
                                                            if (tLRPC$ChatFull2 != null) {
                                                            }
                                                            sQLiteCursor = null;
                                                            i4 = 0;
                                                            if (arrayList4.isEmpty()) {
                                                            }
                                                            getMessagesController().processChatInfo(j, tLRPC$ChatFull4, arrayList3, true, z2, z3, arrayList4, hashMap, i3, z5);
                                                            return tLRPC$ChatFull4;
                                                        } catch (Exception e15) {
                                                            e = e15;
                                                            i2 = 0;
                                                            sQLiteCursor = queryFinalized2;
                                                            tLRPC$ChatFull2 = TLdeserialize2;
                                                            z4 = i2;
                                                            checkSQLException(e);
                                                            if (sQLiteCursor != null) {
                                                            }
                                                            getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                            return tLRPC$ChatFull2;
                                                        } catch (Throwable th16) {
                                                            th = th16;
                                                            i2 = 0;
                                                            sQLiteCursor = queryFinalized2;
                                                            tLRPC$ChatFull = TLdeserialize2;
                                                            z4 = false;
                                                            if (sQLiteCursor != null) {
                                                            }
                                                            getMessagesController().processChatInfo(j, tLRPC$ChatFull, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                            throw th;
                                                        }
                                                    }
                                                } catch (Exception e16) {
                                                    e = e16;
                                                    i2 = 0;
                                                    z4 = false;
                                                    sQLiteCursor = queryFinalized2;
                                                    tLRPC$ChatFull2 = null;
                                                    checkSQLException(e);
                                                    if (sQLiteCursor != null) {
                                                    }
                                                    getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                                    return tLRPC$ChatFull2;
                                                } catch (Throwable th17) {
                                                    th = th17;
                                                    i2 = 0;
                                                    z4 = false;
                                                    sQLiteCursor = queryFinalized2;
                                                    tLRPC$ChatFull = null;
                                                }
                                            }
                                            sQLiteCursor.dispose();
                                            if (tLRPC$ChatFull2 != null) {
                                            }
                                            sQLiteCursor = null;
                                            i4 = 0;
                                            if (arrayList4.isEmpty()) {
                                            }
                                            getMessagesController().processChatInfo(j, tLRPC$ChatFull4, arrayList3, true, z2, z3, arrayList4, hashMap, i3, z5);
                                            return tLRPC$ChatFull4;
                                        } catch (Exception e17) {
                                            e = e17;
                                        } catch (Throwable th18) {
                                            th = th18;
                                            tLRPC$ChatFull4 = tLRPC$ChatFull2;
                                        }
                                        if (sQLiteCursor.next()) {
                                        }
                                    } catch (Exception e18) {
                                        e = e18;
                                        i6 = 0;
                                        i2 = i6;
                                        z4 = i2;
                                        checkSQLException(e);
                                        if (sQLiteCursor != null) {
                                        }
                                        getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                        return tLRPC$ChatFull2;
                                    } catch (Throwable th19) {
                                        th = th19;
                                        i7 = 0;
                                        tLRPC$ChatFull3 = tLRPC$ChatFull2;
                                        i2 = i7;
                                        z4 = i2;
                                        tLRPC$ChatFull = tLRPC$ChatFull3;
                                        if (sQLiteCursor != null) {
                                        }
                                        getMessagesController().processChatInfo(j, tLRPC$ChatFull, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                        throw th;
                                    }
                                    sQLiteCursor = this.database.queryFinalized("SELECT count, end FROM chat_pinned_count WHERE uid = " + j32, new Object[0]);
                                } catch (Exception e19) {
                                    e = e19;
                                    i6 = 0;
                                    sQLiteCursor = null;
                                    i2 = i6;
                                    z4 = i2;
                                    checkSQLException(e);
                                    if (sQLiteCursor != null) {
                                    }
                                    getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                    return tLRPC$ChatFull2;
                                } catch (Throwable th20) {
                                    th = th20;
                                    i7 = 0;
                                    tLRPC$ChatFull3 = tLRPC$ChatFull2;
                                    sQLiteCursor = null;
                                    i2 = i7;
                                    z4 = i2;
                                    tLRPC$ChatFull = tLRPC$ChatFull3;
                                    if (sQLiteCursor != null) {
                                    }
                                    getMessagesController().processChatInfo(j, tLRPC$ChatFull, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                                    throw th;
                                }
                                objArr2[0] = Long.valueOf(j32);
                                queryFinalized = database2.queryFinalized(String.format(locale2, "SELECT mid FROM chat_pinned_v2 WHERE uid = %d ORDER BY mid DESC", objArr2), new Object[0]);
                                while (queryFinalized.next()) {
                                }
                                arrayList4 = arrayList;
                                queryFinalized.dispose();
                            } catch (Exception e20) {
                                e = e20;
                                arrayList4 = arrayList;
                                i6 = 0;
                            } catch (Throwable th21) {
                                th = th21;
                                arrayList4 = arrayList;
                                i7 = 0;
                            }
                            int i1022 = 0;
                        } catch (Exception e21) {
                            e = e21;
                            arrayList4 = arrayList;
                            sQLiteCursor = null;
                            i6 = 0;
                            i2 = i6;
                            z4 = i2;
                            checkSQLException(e);
                            if (sQLiteCursor != null) {
                            }
                            getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                            return tLRPC$ChatFull2;
                        } catch (Throwable th22) {
                            th = th22;
                            arrayList4 = arrayList;
                            tLRPC$ChatFull3 = tLRPC$ChatFull2;
                            sQLiteCursor = null;
                            i7 = 0;
                            i2 = i7;
                            z4 = i2;
                            tLRPC$ChatFull = tLRPC$ChatFull3;
                            if (sQLiteCursor != null) {
                            }
                            getMessagesController().processChatInfo(j, tLRPC$ChatFull, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                            throw th;
                        }
                        if (!(tLRPC$ChatFull2 instanceof TLRPC$TL_chatFull)) {
                        }
                        arrayList = arrayList4;
                        if (tLRPC$ChatFull2 != null) {
                        }
                        if (tLRPC$ChatFull2 != null) {
                        }
                        SQLiteDatabase database22 = getMessagesStorage().getDatabase();
                        Locale locale22 = Locale.US;
                        Object[] objArr22 = new Object[1];
                        long j322 = -j;
                    } catch (Exception e22) {
                        e = e22;
                    } catch (Throwable th23) {
                        th = th23;
                    }
                    queryFinalized2.dispose();
                } catch (Exception e23) {
                    e = e23;
                    i2 = 0;
                    z4 = false;
                    sQLiteCursor = queryFinalized2;
                    checkSQLException(e);
                    if (sQLiteCursor != null) {
                    }
                    getMessagesController().processChatInfo(j, tLRPC$ChatFull2, arrayList3, true, z2, z3, arrayList4, hashMap, i2, z4);
                    return tLRPC$ChatFull2;
                } catch (Throwable th24) {
                    th = th24;
                    tLRPC$ChatFull3 = tLRPC$ChatFull2;
                    i2 = 0;
                    z4 = false;
                    sQLiteCursor = queryFinalized2;
                }
                tLRPC$ChatFull2 = null;
            } catch (Exception e24) {
                e = e24;
                tLRPC$ChatFull2 = null;
            } catch (Throwable th25) {
                th = th25;
                i2 = 0;
                z4 = false;
                tLRPC$ChatFull = null;
                sQLiteCursor = queryFinalized2;
            }
        } catch (Exception e25) {
            e = e25;
            sQLiteCursor = null;
            tLRPC$ChatFull2 = null;
        } catch (Throwable th26) {
            th = th26;
            sQLiteCursor = null;
            i2 = 0;
            z4 = false;
            tLRPC$ChatFull = null;
        }
    }

    public TLRPC$ChatFull loadChatInfo(long j, boolean z, CountDownLatch countDownLatch, boolean z2, boolean z3) {
        return loadChatInfo(j, z, countDownLatch, z2, z3, 0);
    }

    public TLRPC$ChatFull loadChatInfo(final long j, final boolean z, final CountDownLatch countDownLatch, final boolean z2, final boolean z3, final int i) {
        final TLRPC$ChatFull[] tLRPC$ChatFullArr = new TLRPC$ChatFull[1];
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$loadChatInfo$130(tLRPC$ChatFullArr, j, z, z2, z3, i, countDownLatch);
            }
        });
        if (countDownLatch != null) {
            try {
                countDownLatch.await();
            } catch (Throwable unused) {
            }
        }
        return tLRPC$ChatFullArr[0];
    }

    public void lambda$loadChatInfo$130(TLRPC$ChatFull[] tLRPC$ChatFullArr, long j, boolean z, boolean z2, boolean z3, int i, CountDownLatch countDownLatch) {
        tLRPC$ChatFullArr[0] = loadChatInfoInternal(j, z, z2, z3, i);
        if (countDownLatch != null) {
            countDownLatch.countDown();
        }
    }

    public TLRPC$ChatFull loadChatInfoInQueue(long j, boolean z, boolean z2, boolean z3, int i) {
        return loadChatInfoInternal(j, z, z2, z3, i);
    }

    public void processPendingRead(final long j, final int i, final int i2, final int i3) {
        final int i4 = this.lastSavedDate;
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$processPendingRead$131(j, i, i3, i4, i2);
            }
        });
    }

    public void lambda$processPendingRead$131(long j, int i, int i2, int i3, int i4) {
        SQLiteCursor sQLiteCursor;
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLiteDatabase sQLiteDatabase;
        SQLiteDatabase sQLiteDatabase2;
        int intValue;
        int i5;
        int intValue2;
        int intValue3;
        SQLitePreparedStatement sQLitePreparedStatement2;
        SQLitePreparedStatement sQLitePreparedStatement3;
        int i6 = i4;
        long j2 = 0;
        try {
            int i7 = 0;
            SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT unread_count, inbox_max, last_mid FROM dialogs WHERE did = " + j, new Object[0]);
            try {
                if (queryFinalized.next()) {
                    try {
                        intValue = queryFinalized.intValue(0);
                        i5 = intValue;
                        intValue2 = queryFinalized.intValue(1);
                        j2 = queryFinalized.longValue(2);
                    } catch (Exception e) {
                        e = e;
                        sQLiteCursor = queryFinalized;
                        sQLitePreparedStatement = null;
                        try {
                            checkSQLException(e);
                            if (sQLitePreparedStatement != null) {
                            }
                            if (sQLiteCursor != null) {
                            }
                            sQLiteDatabase2 = this.database;
                            if (sQLiteDatabase2 == null) {
                            }
                            sQLiteDatabase2.commitTransaction();
                        } catch (Throwable th) {
                            th = th;
                            if (sQLitePreparedStatement != null) {
                                sQLitePreparedStatement.dispose();
                            }
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                            }
                            sQLiteDatabase = this.database;
                            if (sQLiteDatabase != null) {
                                sQLiteDatabase.commitTransaction();
                            }
                            throw th;
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        sQLiteCursor = queryFinalized;
                        sQLitePreparedStatement = null;
                        if (sQLitePreparedStatement != null) {
                        }
                        if (sQLiteCursor != null) {
                        }
                        sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                        }
                        throw th;
                    }
                } else {
                    intValue = 0;
                    i5 = 0;
                    intValue2 = 0;
                }
                queryFinalized.dispose();
                this.database.beginTransaction();
                if (!DialogObject.isEncryptedDialog(j)) {
                    try {
                        i6 = Math.max(intValue2, i);
                        SQLitePreparedStatement executeFast = this.database.executeFast("UPDATE messages_v2 SET read_state = read_state | 1 WHERE uid = ? AND mid <= ? AND read_state IN(0,2) AND out = 0");
                        try {
                            executeFast.requery();
                            executeFast.bindLong(1, j);
                            executeFast.bindInteger(2, i6);
                            executeFast.step();
                            executeFast.dispose();
                            sQLitePreparedStatement3 = executeFast;
                            if (i6 >= j2) {
                                sQLiteCursor = null;
                            } else {
                                try {
                                    sQLiteCursor = this.database.queryFinalized("SELECT changes()", new Object[0]);
                                    try {
                                        int intValue4 = sQLiteCursor.next() ? sQLiteCursor.intValue(0) + i2 : 0;
                                        sQLiteCursor.dispose();
                                        i7 = Math.max(0, intValue - intValue4);
                                    } catch (Exception e2) {
                                        e = e2;
                                        sQLitePreparedStatement = sQLitePreparedStatement3;
                                        checkSQLException(e);
                                        if (sQLitePreparedStatement != null) {
                                        }
                                        if (sQLiteCursor != null) {
                                        }
                                        sQLiteDatabase2 = this.database;
                                        if (sQLiteDatabase2 == null) {
                                        }
                                        sQLiteDatabase2.commitTransaction();
                                    } catch (Throwable th3) {
                                        th = th3;
                                        sQLitePreparedStatement = sQLitePreparedStatement3;
                                        if (sQLitePreparedStatement != null) {
                                        }
                                        if (sQLiteCursor != null) {
                                        }
                                        sQLiteDatabase = this.database;
                                        if (sQLiteDatabase != null) {
                                        }
                                        throw th;
                                    }
                                } catch (Exception e3) {
                                    e = e3;
                                    sQLitePreparedStatement = sQLitePreparedStatement3;
                                    sQLiteCursor = null;
                                    checkSQLException(e);
                                    if (sQLitePreparedStatement != null) {
                                    }
                                    if (sQLiteCursor != null) {
                                    }
                                    sQLiteDatabase2 = this.database;
                                    if (sQLiteDatabase2 == null) {
                                    }
                                    sQLiteDatabase2.commitTransaction();
                                } catch (Throwable th4) {
                                    th = th4;
                                    sQLitePreparedStatement = sQLitePreparedStatement3;
                                    sQLiteCursor = null;
                                    if (sQLitePreparedStatement != null) {
                                    }
                                    if (sQLiteCursor != null) {
                                    }
                                    sQLiteDatabase = this.database;
                                    if (sQLiteDatabase != null) {
                                    }
                                    throw th;
                                }
                            }
                            sQLitePreparedStatement2 = this.database.executeFast("DELETE FROM unread_push_messages WHERE uid = ? AND mid <= ?");
                            try {
                                sQLitePreparedStatement2.requery();
                                sQLitePreparedStatement2.bindLong(1, j);
                                sQLitePreparedStatement2.bindInteger(2, i6);
                                sQLitePreparedStatement2.step();
                                sQLitePreparedStatement2.dispose();
                                SQLitePreparedStatement executeFast2 = this.database.executeFast("DELETE FROM unread_push_messages WHERE uid = ? AND date <= ?");
                                executeFast2.requery();
                                executeFast2.bindLong(1, j);
                                executeFast2.bindInteger(2, i3);
                                executeFast2.step();
                                executeFast2.dispose();
                            } catch (Exception e4) {
                                e = e4;
                                sQLitePreparedStatement = sQLitePreparedStatement2;
                                checkSQLException(e);
                                if (sQLitePreparedStatement != null) {
                                }
                                if (sQLiteCursor != null) {
                                }
                                sQLiteDatabase2 = this.database;
                                if (sQLiteDatabase2 == null) {
                                }
                                sQLiteDatabase2.commitTransaction();
                            } catch (Throwable th5) {
                                th = th5;
                                sQLitePreparedStatement = sQLitePreparedStatement2;
                                if (sQLitePreparedStatement != null) {
                                }
                                if (sQLiteCursor != null) {
                                }
                                sQLiteDatabase = this.database;
                                if (sQLiteDatabase != null) {
                                }
                                throw th;
                            }
                        } catch (Exception e5) {
                            e = e5;
                            sQLitePreparedStatement3 = executeFast;
                        } catch (Throwable th6) {
                            th = th6;
                            sQLitePreparedStatement3 = executeFast;
                        }
                    } catch (Exception e6) {
                        e = e6;
                        sQLiteCursor = null;
                        sQLitePreparedStatement = null;
                        checkSQLException(e);
                        if (sQLitePreparedStatement != null) {
                        }
                        if (sQLiteCursor != null) {
                        }
                        sQLiteDatabase2 = this.database;
                        if (sQLiteDatabase2 == null) {
                        }
                        sQLiteDatabase2.commitTransaction();
                    } catch (Throwable th7) {
                        th = th7;
                        sQLiteCursor = null;
                        sQLitePreparedStatement = null;
                        if (sQLitePreparedStatement != null) {
                        }
                        if (sQLiteCursor != null) {
                        }
                        sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                        }
                        throw th;
                    }
                } else {
                    sQLitePreparedStatement = this.database.executeFast("UPDATE messages_v2 SET read_state = read_state | 1 WHERE uid = ? AND mid >= ? AND read_state IN(0,2) AND out = 0");
                    try {
                        sQLitePreparedStatement.requery();
                        sQLitePreparedStatement.bindLong(1, j);
                        sQLitePreparedStatement.bindInteger(2, i6);
                        sQLitePreparedStatement.step();
                        sQLitePreparedStatement.dispose();
                        if (i6 <= j2) {
                            sQLiteCursor = null;
                        } else {
                            sQLiteCursor = this.database.queryFinalized("SELECT changes()", new Object[0]);
                            try {
                                if (sQLiteCursor.next()) {
                                    try {
                                        intValue3 = sQLiteCursor.intValue(0) + i2;
                                    } catch (Exception e7) {
                                        e = e7;
                                        sQLitePreparedStatement = null;
                                        checkSQLException(e);
                                        if (sQLitePreparedStatement != null) {
                                        }
                                        if (sQLiteCursor != null) {
                                        }
                                        sQLiteDatabase2 = this.database;
                                        if (sQLiteDatabase2 == null) {
                                        }
                                        sQLiteDatabase2.commitTransaction();
                                    } catch (Throwable th8) {
                                        th = th8;
                                        sQLitePreparedStatement = null;
                                        if (sQLitePreparedStatement != null) {
                                        }
                                        if (sQLiteCursor != null) {
                                        }
                                        sQLiteDatabase = this.database;
                                        if (sQLiteDatabase != null) {
                                        }
                                        throw th;
                                    }
                                } else {
                                    intValue3 = 0;
                                }
                                sQLiteCursor.dispose();
                                i7 = Math.max(0, intValue - intValue3);
                            } catch (Exception e8) {
                                e = e8;
                                sQLitePreparedStatement2 = null;
                                sQLitePreparedStatement = sQLitePreparedStatement2;
                                checkSQLException(e);
                                if (sQLitePreparedStatement != null) {
                                    sQLitePreparedStatement.dispose();
                                }
                                if (sQLiteCursor != null) {
                                    sQLiteCursor.dispose();
                                }
                                sQLiteDatabase2 = this.database;
                                if (sQLiteDatabase2 == null) {
                                    return;
                                }
                                sQLiteDatabase2.commitTransaction();
                            } catch (Throwable th9) {
                                th = th9;
                                sQLitePreparedStatement2 = null;
                                sQLitePreparedStatement = sQLitePreparedStatement2;
                                if (sQLitePreparedStatement != null) {
                                }
                                if (sQLiteCursor != null) {
                                }
                                sQLiteDatabase = this.database;
                                if (sQLiteDatabase != null) {
                                }
                                throw th;
                            }
                        }
                    } catch (Exception e9) {
                        e = e9;
                        sQLiteCursor = null;
                        checkSQLException(e);
                        if (sQLitePreparedStatement != null) {
                        }
                        if (sQLiteCursor != null) {
                        }
                        sQLiteDatabase2 = this.database;
                        if (sQLiteDatabase2 == null) {
                        }
                        sQLiteDatabase2.commitTransaction();
                    } catch (Throwable th10) {
                        th = th10;
                        sQLiteCursor = null;
                        if (sQLitePreparedStatement != null) {
                        }
                        if (sQLiteCursor != null) {
                        }
                        sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                        }
                        throw th;
                    }
                }
                SQLitePreparedStatement executeFast3 = this.database.executeFast("UPDATE dialogs SET unread_count = ?, inbox_max = ? WHERE did = ?");
                executeFast3.requery();
                executeFast3.bindInteger(1, i7);
                executeFast3.bindInteger(2, i6);
                executeFast3.bindLong(3, j);
                executeFast3.step();
                executeFast3.dispose();
                this.database.commitTransaction();
                if (i5 == 0 || i7 != 0 || isForum(j)) {
                    sQLitePreparedStatement2 = null;
                } else {
                    LongSparseIntArray longSparseIntArray = new LongSparseIntArray();
                    longSparseIntArray.put(j, i7);
                    sQLitePreparedStatement2 = null;
                    updateFiltersReadCounter(longSparseIntArray, null, true);
                }
                updateWidgets(j);
                if (sQLiteCursor != null) {
                    sQLiteCursor.dispose();
                }
                sQLiteDatabase2 = this.database;
                if (sQLiteDatabase2 == null) {
                    return;
                }
            } catch (Exception e10) {
                e = e10;
                sQLitePreparedStatement = null;
                sQLiteCursor = queryFinalized;
            } catch (Throwable th11) {
                th = th11;
                sQLitePreparedStatement = null;
                sQLiteCursor = queryFinalized;
            }
        } catch (Exception e11) {
            e = e11;
            sQLiteCursor = null;
            sQLitePreparedStatement = null;
        } catch (Throwable th12) {
            th = th12;
            sQLiteCursor = null;
            sQLitePreparedStatement = null;
        }
        sQLiteDatabase2.commitTransaction();
    }

    public void putContacts(ArrayList<TLRPC$TL_contact> arrayList, final boolean z) {
        if (!arrayList.isEmpty() || z) {
            final ArrayList arrayList2 = new ArrayList(arrayList);
            this.storageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$putContacts$132(z, arrayList2);
                }
            });
        }
    }

    public void lambda$putContacts$132(boolean z, ArrayList arrayList) {
        Throwable th;
        SQLiteDatabase sQLiteDatabase;
        Exception e;
        SQLiteDatabase sQLiteDatabase2;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        if (z) {
            try {
                try {
                    this.database.executeFast("DELETE FROM contacts WHERE 1").stepThis().dispose();
                } catch (Exception e2) {
                    e = e2;
                    checkSQLException(e);
                    if (sQLitePreparedStatement != null) {
                        sQLitePreparedStatement.dispose();
                    }
                    sQLiteDatabase2 = this.database;
                    if (sQLiteDatabase2 == null) {
                        return;
                    }
                    sQLiteDatabase2.commitTransaction();
                }
            } catch (Throwable th2) {
                th = th2;
                if (sQLitePreparedStatement != null) {
                    sQLitePreparedStatement.dispose();
                }
                sQLiteDatabase = this.database;
                if (sQLiteDatabase != null) {
                    sQLiteDatabase.commitTransaction();
                }
                throw th;
            }
        }
        this.database.beginTransaction();
        SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO contacts VALUES(?, ?)");
        for (int i = 0; i < arrayList.size(); i++) {
            try {
                TLRPC$TL_contact tLRPC$TL_contact = (TLRPC$TL_contact) arrayList.get(i);
                executeFast.requery();
                int i2 = 1;
                executeFast.bindLong(1, tLRPC$TL_contact.user_id);
                if (!tLRPC$TL_contact.mutual) {
                    i2 = 0;
                }
                executeFast.bindInteger(2, i2);
                executeFast.step();
            } catch (Exception e3) {
                sQLitePreparedStatement = executeFast;
                e = e3;
                checkSQLException(e);
                if (sQLitePreparedStatement != null) {
                }
                sQLiteDatabase2 = this.database;
                if (sQLiteDatabase2 == null) {
                }
                sQLiteDatabase2.commitTransaction();
            } catch (Throwable th3) {
                sQLitePreparedStatement = executeFast;
                th = th3;
                if (sQLitePreparedStatement != null) {
                }
                sQLiteDatabase = this.database;
                if (sQLiteDatabase != null) {
                }
                throw th;
            }
        }
        executeFast.dispose();
        this.database.commitTransaction();
        sQLiteDatabase2 = this.database;
        if (sQLiteDatabase2 == null) {
            return;
        }
        sQLiteDatabase2.commitTransaction();
    }

    public void deleteContacts(final ArrayList<Long> arrayList) {
        if (arrayList == null || arrayList.isEmpty()) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$deleteContacts$133(arrayList);
            }
        });
    }

    public void lambda$deleteContacts$133(ArrayList arrayList) {
        try {
            String join = TextUtils.join(",", arrayList);
            this.database.executeFast("DELETE FROM contacts WHERE uid IN(" + join + ")").stepThis().dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void applyPhoneBookUpdates(final String str, final String str2) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$applyPhoneBookUpdates$134(str, str2);
            }
        });
    }

    public void lambda$applyPhoneBookUpdates$134(String str, String str2) {
        try {
            if (str.length() != 0) {
                this.database.executeFast(String.format(Locale.US, "UPDATE user_phones_v7 SET deleted = 0 WHERE sphone IN(%s)", str)).stepThis().dispose();
            }
            if (str2.length() != 0) {
                this.database.executeFast(String.format(Locale.US, "UPDATE user_phones_v7 SET deleted = 1 WHERE sphone IN(%s)", str2)).stepThis().dispose();
            }
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void putCachedPhoneBook(final HashMap<String, ContactsController.Contact> hashMap, final boolean z, boolean z2) {
        if (hashMap != null) {
            if (!hashMap.isEmpty() || z || z2) {
                this.storageQueue.postRunnable(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$putCachedPhoneBook$135(hashMap, z);
                    }
                });
            }
        }
    }

    public void lambda$putCachedPhoneBook$135(HashMap hashMap, boolean z) {
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLiteDatabase sQLiteDatabase;
        SQLitePreparedStatement executeFast;
        SQLitePreparedStatement sQLitePreparedStatement2 = null;
        try {
            try {
                if (BuildVars.LOGS_ENABLED) {
                    FileLog.d(this.currentAccount + " save contacts to db " + hashMap.size());
                }
                this.database.executeFast("DELETE FROM user_contacts_v7 WHERE 1").stepThis().dispose();
                this.database.executeFast("DELETE FROM user_phones_v7 WHERE 1").stepThis().dispose();
                this.database.beginTransaction();
                executeFast = this.database.executeFast("REPLACE INTO user_contacts_v7 VALUES(?, ?, ?, ?, ?)");
                try {
                    sQLitePreparedStatement = this.database.executeFast("REPLACE INTO user_phones_v7 VALUES(?, ?, ?, ?)");
                } catch (Exception e) {
                    e = e;
                    sQLitePreparedStatement = null;
                } catch (Throwable th) {
                    th = th;
                    sQLitePreparedStatement = null;
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Exception e2) {
            e = e2;
            sQLitePreparedStatement = null;
        } catch (Throwable th3) {
            th = th3;
            sQLitePreparedStatement = null;
        }
        try {
            Iterator it = hashMap.entrySet().iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                ContactsController.Contact contact = (ContactsController.Contact) ((Map.Entry) it.next()).getValue();
                if (!contact.phones.isEmpty() && !contact.shortPhones.isEmpty()) {
                    executeFast.requery();
                    executeFast.bindString(1, contact.key);
                    executeFast.bindInteger(2, contact.contact_id);
                    executeFast.bindString(3, contact.first_name);
                    executeFast.bindString(4, contact.last_name);
                    executeFast.bindInteger(5, contact.imported);
                    executeFast.step();
                    for (int i = 0; i < contact.phones.size(); i++) {
                        sQLitePreparedStatement.requery();
                        sQLitePreparedStatement.bindString(1, contact.key);
                        sQLitePreparedStatement.bindString(2, contact.phones.get(i));
                        sQLitePreparedStatement.bindString(3, contact.shortPhones.get(i));
                        sQLitePreparedStatement.bindInteger(4, contact.phoneDeleted.get(i).intValue());
                        sQLitePreparedStatement.step();
                    }
                }
            }
            executeFast.dispose();
            try {
                sQLitePreparedStatement.dispose();
                this.database.commitTransaction();
                if (z) {
                    this.database.executeFast("DROP TABLE IF EXISTS user_contacts_v6;").stepThis().dispose();
                    this.database.executeFast("DROP TABLE IF EXISTS user_phones_v6;").stepThis().dispose();
                    getCachedPhoneBook(false);
                }
                sQLiteDatabase = this.database;
            } catch (Exception e3) {
                e = e3;
                checkSQLException(e);
                if (sQLitePreparedStatement2 != null) {
                    sQLitePreparedStatement2.dispose();
                }
                if (sQLitePreparedStatement != null) {
                    sQLitePreparedStatement.dispose();
                }
                sQLiteDatabase = this.database;
            }
        } catch (Exception e4) {
            e = e4;
            sQLitePreparedStatement2 = executeFast;
            checkSQLException(e);
            if (sQLitePreparedStatement2 != null) {
            }
            if (sQLitePreparedStatement != null) {
            }
            sQLiteDatabase = this.database;
        } catch (Throwable th4) {
            th = th4;
            sQLitePreparedStatement2 = executeFast;
            if (sQLitePreparedStatement2 != null) {
                sQLitePreparedStatement2.dispose();
            }
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            SQLiteDatabase sQLiteDatabase2 = this.database;
            if (sQLiteDatabase2 != null) {
                sQLiteDatabase2.commitTransaction();
            }
            throw th;
        }
    }

    public void getCachedPhoneBook(final boolean z) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getCachedPhoneBook$136(z);
            }
        });
    }

    public void lambda$getCachedPhoneBook$136(boolean z) {
        SQLiteCursor sQLiteCursor;
        int i;
        int i2;
        int i3;
        HashMap<String, ContactsController.Contact> hashMap;
        SQLiteCursor queryFinalized;
        SQLiteCursor queryFinalized2;
        boolean next;
        int i4 = 6;
        try {
            queryFinalized2 = this.database.queryFinalized("SELECT name FROM sqlite_master WHERE type='table' AND name='user_contacts_v6'", new Object[0]);
            try {
                next = queryFinalized2.next();
                queryFinalized2.dispose();
            } catch (Throwable th) {
                th = th;
                sQLiteCursor = queryFinalized2;
            }
        } catch (Throwable th2) {
            th = th2;
            sQLiteCursor = null;
        }
        if (next) {
            queryFinalized2 = this.database.queryFinalized("SELECT COUNT(uid) FROM user_contacts_v6 WHERE 1", new Object[0]);
            int min = queryFinalized2.next() ? Math.min(5000, queryFinalized2.intValue(0)) : 16;
            queryFinalized2.dispose();
            SparseArray<ContactsController.Contact> sparseArray = new SparseArray<>(min);
            SQLiteCursor queryFinalized3 = this.database.queryFinalized("SELECT us.uid, us.fname, us.sname, up.phone, up.sphone, up.deleted, us.imported FROM user_contacts_v6 as us LEFT JOIN user_phones_v6 as up ON us.uid = up.uid WHERE 1", new Object[0]);
            while (queryFinalized3.next()) {
                try {
                    int intValue = queryFinalized3.intValue(0);
                    ContactsController.Contact contact = sparseArray.get(intValue);
                    if (contact == null) {
                        contact = new ContactsController.Contact();
                        contact.first_name = queryFinalized3.stringValue(1);
                        contact.last_name = queryFinalized3.stringValue(2);
                        contact.imported = queryFinalized3.intValue(i4);
                        if (contact.first_name == null) {
                            contact.first_name = "";
                        }
                        if (contact.last_name == null) {
                            contact.last_name = "";
                        }
                        contact.contact_id = intValue;
                        sparseArray.put(intValue, contact);
                    }
                    String stringValue = queryFinalized3.stringValue(3);
                    if (stringValue != null) {
                        contact.phones.add(stringValue);
                        String stringValue2 = queryFinalized3.stringValue(4);
                        if (stringValue2 != null) {
                            if (stringValue2.length() == 8 && stringValue.length() != 8) {
                                stringValue2 = PhoneFormat.stripExceptNumbers(stringValue);
                            }
                            contact.shortPhones.add(stringValue2);
                            contact.phoneDeleted.add(Integer.valueOf(queryFinalized3.intValue(5)));
                            contact.phoneTypes.add("");
                            if (sparseArray.size() == 5000) {
                                break;
                            } else {
                                i4 = 6;
                            }
                        }
                    }
                } catch (Throwable th3) {
                    th = th3;
                    sQLiteCursor = queryFinalized3;
                    try {
                        checkSQLException(th);
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        try {
                            sQLiteCursor = this.database.queryFinalized("SELECT COUNT(key) FROM user_contacts_v7 WHERE 1", new Object[0]);
                            if (sQLiteCursor.next()) {
                            }
                        } catch (Throwable th4) {
                            th = th4;
                            i = 0;
                            i2 = 0;
                        }
                        sQLiteCursor.dispose();
                        hashMap = new HashMap<>(i3);
                        try {
                            try {
                                if (i2 == 0) {
                                }
                                sQLiteCursor = queryFinalized;
                                while (sQLiteCursor.next()) {
                                }
                                sQLiteCursor.dispose();
                            } catch (Exception e) {
                                hashMap.clear();
                                checkSQLException(e);
                                if (sQLiteCursor != null) {
                                    sQLiteCursor.dispose();
                                }
                            }
                            getContactsController().performSyncPhoneBook(hashMap, true, true, false, false, !z, false);
                        } catch (Throwable th5) {
                        }
                    } finally {
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        throw th;
                    }
                }
            }
            queryFinalized3.dispose();
            getContactsController().migratePhoneBookToV7(sparseArray);
            return;
        }
        sQLiteCursor = null;
        sQLiteCursor = this.database.queryFinalized("SELECT COUNT(key) FROM user_contacts_v7 WHERE 1", new Object[0]);
        if (sQLiteCursor.next()) {
            i = 0;
            i2 = 0;
            i3 = 16;
        } else {
            i = sQLiteCursor.intValue(0);
            try {
                i3 = Math.min(5000, i);
                i2 = i > 5000 ? i - 5000 : 0;
                try {
                    if (BuildVars.LOGS_ENABLED) {
                        FileLog.d(this.currentAccount + " current cached contacts count = " + i);
                    }
                } catch (Throwable th6) {
                    th = th6;
                    try {
                        checkSQLException(th);
                    } finally {
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        throw th5;
                    }
                }
            } catch (Throwable th7) {
                th = th7;
                i2 = 0;
                i3 = 16;
                checkSQLException(th);
            }
        }
        sQLiteCursor.dispose();
        hashMap = new HashMap<>(i3);
        if (i2 == 0) {
            queryFinalized = this.database.queryFinalized("SELECT us.key, us.uid, us.fname, us.sname, up.phone, up.sphone, up.deleted, us.imported FROM user_contacts_v7 as us LEFT JOIN user_phones_v7 as up ON us.key = up.key WHERE 1 LIMIT 0," + i, new Object[0]);
        } else {
            queryFinalized = this.database.queryFinalized("SELECT us.key, us.uid, us.fname, us.sname, up.phone, up.sphone, up.deleted, us.imported FROM user_contacts_v7 as us LEFT JOIN user_phones_v7 as up ON us.key = up.key WHERE 1", new Object[0]);
        }
        sQLiteCursor = queryFinalized;
        while (sQLiteCursor.next()) {
            String stringValue3 = sQLiteCursor.stringValue(0);
            ContactsController.Contact contact2 = hashMap.get(stringValue3);
            if (contact2 == null) {
                contact2 = new ContactsController.Contact();
                contact2.contact_id = sQLiteCursor.intValue(1);
                contact2.first_name = sQLiteCursor.stringValue(2);
                contact2.last_name = sQLiteCursor.stringValue(3);
                contact2.imported = sQLiteCursor.intValue(7);
                if (contact2.first_name == null) {
                    contact2.first_name = "";
                }
                if (contact2.last_name == null) {
                    contact2.last_name = "";
                }
                hashMap.put(stringValue3, contact2);
            }
            String stringValue4 = sQLiteCursor.stringValue(4);
            if (stringValue4 != null) {
                contact2.phones.add(stringValue4);
                String stringValue5 = sQLiteCursor.stringValue(5);
                if (stringValue5 != null) {
                    if (stringValue5.length() == 8 && stringValue4.length() != 8) {
                        stringValue5 = PhoneFormat.stripExceptNumbers(stringValue4);
                    }
                    contact2.shortPhones.add(stringValue5);
                    contact2.phoneDeleted.add(Integer.valueOf(sQLiteCursor.intValue(6)));
                    contact2.phoneTypes.add("");
                    if (hashMap.size() == 5000) {
                        break;
                    }
                }
            }
        }
        sQLiteCursor.dispose();
        getContactsController().performSyncPhoneBook(hashMap, true, true, false, false, !z, false);
    }

    public void getContacts() {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getContacts$137();
            }
        });
    }

    public void lambda$getContacts$137() {
        SQLiteCursor sQLiteCursor;
        Exception e;
        ArrayList<TLRPC$TL_contact> arrayList = new ArrayList<>();
        ArrayList<TLRPC$User> arrayList2 = new ArrayList<>();
        SQLiteCursor sQLiteCursor2 = null;
        try {
            sQLiteCursor = this.database.queryFinalized("SELECT * FROM contacts WHERE 1", new Object[0]);
            try {
                try {
                    StringBuilder sb = new StringBuilder();
                    while (sQLiteCursor.next()) {
                        long intValue = sQLiteCursor.intValue(0);
                        TLRPC$TL_contact tLRPC$TL_contact = new TLRPC$TL_contact();
                        tLRPC$TL_contact.user_id = intValue;
                        tLRPC$TL_contact.mutual = sQLiteCursor.intValue(1) == 1;
                        if (sb.length() != 0) {
                            sb.append(",");
                        }
                        arrayList.add(tLRPC$TL_contact);
                        sb.append(tLRPC$TL_contact.user_id);
                    }
                    sQLiteCursor.dispose();
                    if (sb.length() != 0) {
                        getUsersInternal(sb.toString(), arrayList2);
                    }
                } catch (Exception e2) {
                    e = e2;
                    arrayList.clear();
                    arrayList2.clear();
                    checkSQLException(e);
                    if (sQLiteCursor != null) {
                        sQLiteCursor.dispose();
                    }
                    getContactsController().processLoadedContacts(arrayList, arrayList2, 1);
                }
            } catch (Throwable th) {
                th = th;
                sQLiteCursor2 = sQLiteCursor;
                if (sQLiteCursor2 != null) {
                    sQLiteCursor2.dispose();
                }
                throw th;
            }
        } catch (Exception e3) {
            sQLiteCursor = null;
            e = e3;
        } catch (Throwable th2) {
            th = th2;
            if (sQLiteCursor2 != null) {
            }
            throw th;
        }
        getContactsController().processLoadedContacts(arrayList, arrayList2, 1);
    }

    public void getUnsentMessages(final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getUnsentMessages$138(i);
            }
        });
    }

    public void lambda$getUnsentMessages$138(int i) {
        SQLiteCursor sQLiteCursor;
        int i2;
        ArrayList<Long> arrayList;
        ArrayList<Long> arrayList2;
        boolean z;
        ArrayList<Long> arrayList3;
        try {
            SparseArray sparseArray = new SparseArray();
            ArrayList arrayList4 = new ArrayList();
            ArrayList arrayList5 = new ArrayList();
            ArrayList<TLRPC$User> arrayList6 = new ArrayList<>();
            ArrayList<TLRPC$Chat> arrayList7 = new ArrayList<>();
            ArrayList<TLRPC$EncryptedChat> arrayList8 = new ArrayList<>();
            ArrayList<Long> arrayList9 = new ArrayList<>();
            ArrayList arrayList10 = new ArrayList();
            ArrayList arrayList11 = new ArrayList();
            ?? r13 = 0;
            ?? queryFinalized = this.database.queryFinalized("SELECT m.read_state, m.data, m.send_state, m.mid, m.date, r.random_id, m.uid, s.seq_in, s.seq_out, m.ttl FROM messages_v2 as m LEFT JOIN randoms_v2 as r ON r.mid = m.mid AND r.uid = m.uid LEFT JOIN messages_seq as s ON m.mid = s.mid WHERE (m.mid < 0 AND m.send_state = 1) OR (m.mid > 0 AND m.send_state = 3) ORDER BY m.mid DESC LIMIT " + i, new Object[0]);
            while (true) {
                try {
                    i2 = 1;
                    if (!queryFinalized.next()) {
                        break;
                    }
                    NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(1);
                    if (byteBufferValue != null) {
                        TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                        TLdeserialize.send_state = queryFinalized.intValue(2);
                        TLdeserialize.readAttachPath(byteBufferValue, getUserConfig().clientUserId);
                        byteBufferValue.reuse();
                        if (sparseArray.indexOfKey(TLdeserialize.id) < 0) {
                            MessageObject.setUnreadFlags(TLdeserialize, queryFinalized.intValue(0));
                            TLdeserialize.id = queryFinalized.intValue(3);
                            TLdeserialize.date = queryFinalized.intValue(4);
                            if (!queryFinalized.isNull(5)) {
                                TLdeserialize.random_id = queryFinalized.longValue(5);
                            }
                            TLdeserialize.dialog_id = queryFinalized.longValue(6);
                            TLdeserialize.seq_in = queryFinalized.intValue(7);
                            TLdeserialize.seq_out = queryFinalized.intValue(8);
                            TLdeserialize.ttl = queryFinalized.intValue(9);
                            arrayList4.add(TLdeserialize);
                            sparseArray.put(TLdeserialize.id, TLdeserialize);
                            if (DialogObject.isEncryptedDialog(TLdeserialize.dialog_id)) {
                                int encryptedChatId = DialogObject.getEncryptedChatId(TLdeserialize.dialog_id);
                                if (!arrayList11.contains(Integer.valueOf(encryptedChatId))) {
                                    arrayList11.add(Integer.valueOf(encryptedChatId));
                                }
                            } else if (DialogObject.isUserDialog(TLdeserialize.dialog_id)) {
                                if (!arrayList9.contains(Long.valueOf(TLdeserialize.dialog_id))) {
                                    arrayList9.add(Long.valueOf(TLdeserialize.dialog_id));
                                }
                            } else if (!arrayList10.contains(Long.valueOf(-TLdeserialize.dialog_id))) {
                                arrayList10.add(Long.valueOf(-TLdeserialize.dialog_id));
                            }
                            addUsersAndChatsFromMessage(TLdeserialize, arrayList9, arrayList10, null);
                            if (TLdeserialize.send_state != 3 && ((TLdeserialize.peer_id.channel_id == 0 && !MessageObject.isUnread(TLdeserialize) && !DialogObject.isEncryptedDialog(TLdeserialize.dialog_id)) || TLdeserialize.id > 0)) {
                                TLdeserialize.send_state = 0;
                            }
                        }
                    }
                } catch (Exception e) {
                    e = e;
                    sQLiteCursor = queryFinalized;
                    try {
                        checkSQLException(e);
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                            return;
                        }
                        return;
                    } catch (Throwable th) {
                        th = th;
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        throw th;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    sQLiteCursor = queryFinalized;
                    if (sQLiteCursor != null) {
                    }
                    throw th;
                }
            }
            queryFinalized.dispose();
            queryFinalized = this.database.queryFinalized("SELECT m.data, m.send_state, m.mid, m.date, r.random_id, m.uid, m.ttl FROM scheduled_messages_v2 as m LEFT JOIN randoms_v2 as r ON r.mid = m.mid AND r.uid = m.uid WHERE (m.mid < 0 AND m.send_state = 1) OR (m.mid > 0 AND m.send_state = 3) ORDER BY date ASC", new Object[0]);
            while (queryFinalized.next()) {
                ?? byteBufferValue2 = queryFinalized.byteBufferValue(r13);
                if (byteBufferValue2 != 0) {
                    ?? TLdeserialize2 = TLRPC$Message.TLdeserialize(byteBufferValue2, byteBufferValue2.readInt32(r13), r13);
                    TLdeserialize2.send_state = queryFinalized.intValue(i2);
                    arrayList2 = arrayList9;
                    TLdeserialize2.readAttachPath(byteBufferValue2, getUserConfig().clientUserId);
                    byteBufferValue2.reuse();
                    if (sparseArray.indexOfKey(TLdeserialize2.id) < 0) {
                        TLdeserialize2.id = queryFinalized.intValue(2);
                        TLdeserialize2.date = queryFinalized.intValue(3);
                        if (!queryFinalized.isNull(4)) {
                            TLdeserialize2.random_id = queryFinalized.longValue(4);
                        }
                        TLdeserialize2.dialog_id = queryFinalized.longValue(5);
                        TLdeserialize2.ttl = queryFinalized.intValue(6);
                        arrayList5.add(TLdeserialize2);
                        sparseArray.put(TLdeserialize2.id, TLdeserialize2);
                        if (DialogObject.isEncryptedDialog(TLdeserialize2.dialog_id)) {
                            int encryptedChatId2 = DialogObject.getEncryptedChatId(TLdeserialize2.dialog_id);
                            if (!arrayList11.contains(Integer.valueOf(encryptedChatId2))) {
                                arrayList11.add(Integer.valueOf(encryptedChatId2));
                            }
                            arrayList3 = arrayList2;
                        } else if (DialogObject.isUserDialog(TLdeserialize2.dialog_id)) {
                            arrayList3 = arrayList2;
                            if (!arrayList3.contains(Long.valueOf(TLdeserialize2.dialog_id))) {
                                arrayList3.add(Long.valueOf(TLdeserialize2.dialog_id));
                            }
                        } else {
                            arrayList3 = arrayList2;
                            if (!arrayList10.contains(Long.valueOf(-TLdeserialize2.dialog_id))) {
                                arrayList10.add(Long.valueOf(-TLdeserialize2.dialog_id));
                            }
                        }
                        addUsersAndChatsFromMessage(TLdeserialize2, arrayList3, arrayList10, null);
                        if (TLdeserialize2.send_state != 3) {
                            arrayList2 = arrayList3;
                            if ((TLdeserialize2.peer_id.channel_id != 0 || MessageObject.isUnread(TLdeserialize2) || DialogObject.isEncryptedDialog(TLdeserialize2.dialog_id)) && TLdeserialize2.id <= 0) {
                                z = false;
                            } else {
                                z = false;
                                TLdeserialize2.send_state = 0;
                            }
                            r13 = z;
                            arrayList9 = arrayList2;
                            i2 = 1;
                        } else {
                            arrayList2 = arrayList3;
                            z = false;
                            r13 = z;
                            arrayList9 = arrayList2;
                            i2 = 1;
                        }
                    } else {
                        z = false;
                    }
                } else {
                    arrayList2 = arrayList9;
                    z = r13;
                }
                r13 = z;
                arrayList9 = arrayList2;
                i2 = 1;
            }
            ArrayList<Long> arrayList12 = arrayList9;
            int i3 = r13;
            queryFinalized.dispose();
            if (arrayList11.isEmpty()) {
                arrayList = arrayList12;
            } else {
                String join = TextUtils.join(",", arrayList11);
                arrayList = arrayList12;
                getEncryptedChatsInternal(join, arrayList8, arrayList);
            }
            if (!arrayList.isEmpty()) {
                getUsersInternal(TextUtils.join(",", arrayList), arrayList6);
            }
            if (!arrayList10.isEmpty()) {
                StringBuilder sb = new StringBuilder();
                for (int i4 = i3; i4 < arrayList10.size(); i4++) {
                    Long l = (Long) arrayList10.get(i4);
                    if (sb.length() != 0) {
                        sb.append(",");
                    }
                    sb.append(l);
                }
                getChatsInternal(sb.toString(), arrayList7);
            }
            getSendMessagesHelper().processUnsentMessages(arrayList4, arrayList5, arrayList6, arrayList7, arrayList8);
        } catch (Exception e2) {
            e = e2;
            sQLiteCursor = null;
        } catch (Throwable th3) {
            th = th3;
            sQLiteCursor = null;
        }
    }

    public boolean checkMessageByRandomId(final long j) {
        final boolean[] zArr = new boolean[1];
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$checkMessageByRandomId$139(j, zArr, countDownLatch);
            }
        });
        try {
            countDownLatch.await();
        } catch (Exception e) {
            checkSQLException(e);
        }
        return zArr[0];
    }

    public void lambda$checkMessageByRandomId$139(long j, boolean[] zArr, CountDownLatch countDownLatch) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                sQLiteCursor = this.database.queryFinalized(String.format(Locale.US, "SELECT random_id FROM randoms_v2 WHERE random_id = %d", Long.valueOf(j)), new Object[0]);
                if (sQLiteCursor.next()) {
                    zArr[0] = true;
                }
            } catch (Exception e) {
                checkSQLException(e);
            }
            sQLiteCursor.dispose();
            countDownLatch.countDown();
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public boolean checkMessageId(final long j, final int i) {
        final boolean[] zArr = new boolean[1];
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$checkMessageId$140(j, i, zArr, countDownLatch);
            }
        });
        try {
            countDownLatch.await();
        } catch (Exception e) {
            checkSQLException(e);
        }
        return zArr[0];
    }

    public void lambda$checkMessageId$140(long j, int i, boolean[] zArr, CountDownLatch countDownLatch) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                sQLiteCursor = this.database.queryFinalized(String.format(Locale.US, "SELECT mid FROM messages_v2 WHERE uid = %d AND mid = %d", Long.valueOf(j), Integer.valueOf(i)), new Object[0]);
                if (sQLiteCursor.next()) {
                    zArr[0] = true;
                }
            } catch (Exception e) {
                checkSQLException(e);
            }
            sQLiteCursor.dispose();
            countDownLatch.countDown();
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public void getUnreadMention(final long j, final long j2, final IntCallback intCallback) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getUnreadMention$142(j2, j, intCallback);
            }
        });
    }

    public void lambda$getUnreadMention$142(long j, long j2, final IntCallback intCallback) {
        SQLiteCursor queryFinalized;
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                if (j != 0) {
                    queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT MIN(mid) FROM messages_topics WHERE uid = %d AND topic_id = %d AND mention = 1 AND read_state IN(0, 1)", Long.valueOf(j2), Long.valueOf(j)), new Object[0]);
                } else {
                    queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT MIN(mid) FROM messages_v2 WHERE uid = %d AND mention = 1 AND read_state IN(0, 1)", Long.valueOf(j2)), new Object[0]);
                }
                sQLiteCursor = queryFinalized;
                final int intValue = sQLiteCursor.next() ? sQLiteCursor.intValue(0) : 0;
                sQLiteCursor.dispose();
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.IntCallback.this.run(intValue);
                    }
                });
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLiteCursor == null) {
                    return;
                }
            }
            sQLiteCursor.dispose();
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public void getMessagesCount(final long j, final IntCallback intCallback) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getMessagesCount$144(j, intCallback);
            }
        });
    }

    public void lambda$getMessagesCount$144(long j, final IntCallback intCallback) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                sQLiteCursor = this.database.queryFinalized(String.format(Locale.US, "SELECT COUNT(mid) FROM messages_v2 WHERE uid = %d", Long.valueOf(j)), new Object[0]);
                final int intValue = sQLiteCursor.next() ? sQLiteCursor.intValue(0) : 0;
                sQLiteCursor.dispose();
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.IntCallback.this.run(intValue);
                    }
                });
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLiteCursor == null) {
                    return;
                }
            }
            sQLiteCursor.dispose();
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public java.lang.Runnable getMessagesInternal(long r55, long r57, int r59, int r60, int r61, int r62, int r63, int r64, int r65, long r66, int r68, boolean r69, boolean r70, org.telegram.messenger.MessageLoaderLogger r71) {
        throw new UnsupportedOperationException("Method not decompiled: org.telegram.messenger.MessagesStorage.getMessagesInternal(long, long, int, int, int, int, int, int, int, long, int, boolean, boolean, org.telegram.messenger.MessageLoaderLogger):java.lang.Runnable");
    }

    public static int lambda$getMessagesInternal$145(TLRPC$Message tLRPC$Message, TLRPC$Message tLRPC$Message2) {
        int i;
        int i2;
        int i3 = tLRPC$Message.id;
        if (i3 > 0 && (i2 = tLRPC$Message2.id) > 0) {
            if (i3 > i2) {
                return -1;
            }
            return i3 < i2 ? 1 : 0;
        }
        if (i3 < 0 && (i = tLRPC$Message2.id) < 0) {
            if (i3 < i) {
                return -1;
            }
            return i3 > i ? 1 : 0;
        }
        int i4 = tLRPC$Message.date;
        int i5 = tLRPC$Message2.date;
        if (i4 > i5) {
            return -1;
        }
        return i4 < i5 ? 1 : 0;
    }

    public void lambda$getMessagesInternal$146(TLRPC$TL_messages_messages tLRPC$TL_messages_messages, int i, long j, long j2, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10, boolean z, int i11, long j3, int i12, boolean z2, int i13, boolean z3, boolean z4, MessageLoaderLogger messageLoaderLogger) {
        getMessagesController().processLoadedMessages(tLRPC$TL_messages_messages, i, j, j2, i2, i3, i4, true, i5, i6, i7, i8, i9, i10, z, i11, j3, i12, z2, i13, z3, z4, messageLoaderLogger);
    }

    public void getAnimatedEmoji(String str, ArrayList<TLRPC$Document> arrayList) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                sQLiteCursor = this.database.queryFinalized(String.format(Locale.US, "SELECT data FROM animated_emoji WHERE document_id IN (%s)", str), new Object[0]);
                while (sQLiteCursor.next()) {
                    NativeByteBuffer byteBufferValue = sQLiteCursor.byteBufferValue(0);
                    try {
                        TLRPC$Document TLdeserialize = TLRPC$Document.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(true), true);
                        if (TLdeserialize != null && TLdeserialize.id != 0) {
                            arrayList.add(TLdeserialize);
                        }
                    } catch (Exception e) {
                        checkSQLException(e);
                    }
                    if (byteBufferValue != null) {
                        byteBufferValue.reuse();
                    }
                }
            } catch (Throwable th) {
                if (sQLiteCursor != null) {
                    sQLiteCursor.dispose();
                }
                throw th;
            }
        } catch (SQLiteException e2) {
            e2.printStackTrace();
            if (sQLiteCursor == null) {
                return;
            }
        }
        sQLiteCursor.dispose();
    }

    public void getMessages(final long j, final long j2, boolean z, final int i, final int i2, final int i3, final int i4, final int i5, final int i6, final int i7, final long j3, final int i8, final boolean z2, final boolean z3, final MessageLoaderLogger messageLoaderLogger) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getMessages$148(messageLoaderLogger, j, j2, i, i2, i3, i4, i5, i6, i7, j3, i8, z2, z3);
            }
        });
    }

    public void lambda$getMessages$148(final MessageLoaderLogger messageLoaderLogger, long j, long j2, int i, int i2, int i3, int i4, int i5, int i6, int i7, long j3, int i8, boolean z, boolean z2) {
        if (messageLoaderLogger != null) {
            messageLoaderLogger.logStorageQueuePost();
        }
        final Runnable messagesInternal = getMessagesInternal(j, j2, i, i2, i3, i4, i5, i6, i7, j3, i8, z, z2, messageLoaderLogger);
        if (messageLoaderLogger != null) {
            messageLoaderLogger.logStorageProccessing();
        }
        Utilities.stageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.lambda$getMessages$147(MessageLoaderLogger.this, messagesInternal);
            }
        });
    }

    public static void lambda$getMessages$147(MessageLoaderLogger messageLoaderLogger, Runnable runnable) {
        if (messageLoaderLogger != null) {
            messageLoaderLogger.logStageQueuePost();
        }
        runnable.run();
    }

    public void clearSentMedia() {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$clearSentMedia$149();
            }
        });
    }

    public void lambda$clearSentMedia$149() {
        try {
            this.database.executeFast("DELETE FROM sent_files_v2 WHERE 1").stepThis().dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public Object[] getSentFile(final String str, final int i) {
        if (str == null || str.toLowerCase().endsWith("attheme")) {
            return null;
        }
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        final Object[] objArr = new Object[2];
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getSentFile$150(str, i, objArr, countDownLatch);
            }
        });
        try {
            countDownLatch.await();
        } catch (Exception e) {
            checkSQLException(e);
        }
        if (objArr[0] != null) {
            return objArr;
        }
        return null;
    }

    public void lambda$getSentFile$150(String str, int i, Object[] objArr, CountDownLatch countDownLatch) {
        NativeByteBuffer byteBufferValue;
        try {
            try {
                String MD5 = Utilities.MD5(str);
                if (MD5 != null) {
                    SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT data, parent FROM sent_files_v2 WHERE uid = '%s' AND type = %d", MD5, Integer.valueOf(i)), new Object[0]);
                    if (queryFinalized.next() && (byteBufferValue = queryFinalized.byteBufferValue(0)) != null) {
                        TLRPC$MessageMedia TLdeserialize = TLRPC$MessageMedia.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                        byteBufferValue.reuse();
                        if (TLdeserialize instanceof TLRPC$TL_messageMediaDocument) {
                            objArr[0] = ((TLRPC$TL_messageMediaDocument) TLdeserialize).document;
                        } else if (TLdeserialize instanceof TLRPC$TL_messageMediaPhoto) {
                            objArr[0] = ((TLRPC$TL_messageMediaPhoto) TLdeserialize).photo;
                        }
                        if (objArr[0] != null) {
                            objArr[1] = queryFinalized.stringValue(1);
                        }
                    }
                    queryFinalized.dispose();
                }
            } catch (Exception e) {
                checkSQLException(e);
            }
        } finally {
            countDownLatch.countDown();
        }
    }

    private void updateWidgets(long j) {
        ArrayList<Long> arrayList = new ArrayList<>();
        arrayList.add(Long.valueOf(j));
        updateWidgets(arrayList);
    }

    private void updateWidgets(ArrayList<Long> arrayList) {
        if (arrayList.isEmpty()) {
            return;
        }
        AppWidgetManager appWidgetManager = null;
        try {
            TextUtils.join(",", arrayList);
            SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT DISTINCT id FROM shortcut_widget WHERE did IN(%s,-1)", TextUtils.join(",", arrayList)), new Object[0]);
            while (queryFinalized.next()) {
                if (appWidgetManager == null) {
                    appWidgetManager = AppWidgetManager.getInstance(ApplicationLoader.applicationContext);
                }
                appWidgetManager.notifyAppWidgetViewDataChanged(queryFinalized.intValue(0), R.id.list_view);
            }
            queryFinalized.dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void putWidgetDialogs(final int i, final ArrayList<TopicKey> arrayList) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$putWidgetDialogs$151(i, arrayList);
            }
        });
    }

    public void lambda$putWidgetDialogs$151(int i, ArrayList arrayList) {
        try {
            this.database.beginTransaction();
            this.database.executeFast("DELETE FROM shortcut_widget WHERE id = " + i).stepThis().dispose();
            SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO shortcut_widget VALUES(?, ?, ?)");
            if (arrayList.isEmpty()) {
                executeFast.requery();
                executeFast.bindInteger(1, i);
                executeFast.bindLong(2, -1L);
                executeFast.bindInteger(3, 0);
                executeFast.step();
            } else {
                int size = arrayList.size();
                for (int i2 = 0; i2 < size; i2++) {
                    long j = ((TopicKey) arrayList.get(i2)).dialogId;
                    executeFast.requery();
                    executeFast.bindInteger(1, i);
                    executeFast.bindLong(2, j);
                    executeFast.bindInteger(3, i2);
                    executeFast.step();
                }
            }
            executeFast.dispose();
            this.database.commitTransaction();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void clearWidgetDialogs(final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$clearWidgetDialogs$152(i);
            }
        });
    }

    public void lambda$clearWidgetDialogs$152(int i) {
        try {
            this.database.executeFast("DELETE FROM shortcut_widget WHERE id = " + i).stepThis().dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void getWidgetDialogIds(final int i, final int i2, final ArrayList<Long> arrayList, final ArrayList<TLRPC$User> arrayList2, final ArrayList<TLRPC$Chat> arrayList3, final boolean z) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getWidgetDialogIds$153(i, arrayList, arrayList2, arrayList3, z, i2, countDownLatch);
            }
        });
        try {
            countDownLatch.await();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void lambda$getWidgetDialogIds$153(int i, ArrayList arrayList, ArrayList arrayList2, ArrayList arrayList3, boolean z, int i2, CountDownLatch countDownLatch) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                ArrayList arrayList4 = new ArrayList();
                ArrayList arrayList5 = new ArrayList();
                SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT did FROM shortcut_widget WHERE id = %d ORDER BY ord ASC", Integer.valueOf(i)), new Object[0]);
                while (queryFinalized.next()) {
                    try {
                        long longValue = queryFinalized.longValue(0);
                        if (longValue != -1) {
                            arrayList.add(Long.valueOf(longValue));
                            if (arrayList2 != null && arrayList3 != null) {
                                if (DialogObject.isUserDialog(longValue)) {
                                    arrayList4.add(Long.valueOf(longValue));
                                } else {
                                    arrayList5.add(Long.valueOf(-longValue));
                                }
                            }
                        }
                    } catch (Exception e) {
                        e = e;
                        sQLiteCursor = queryFinalized;
                        checkSQLException(e);
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        countDownLatch.countDown();
                    } catch (Throwable th) {
                        th = th;
                        sQLiteCursor = queryFinalized;
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        countDownLatch.countDown();
                        throw th;
                    }
                }
                queryFinalized.dispose();
                if (!z && arrayList.isEmpty()) {
                    if (i2 == 0) {
                        SQLiteCursor queryFinalized2 = this.database.queryFinalized("SELECT did FROM dialogs WHERE folder_id = 0 ORDER BY pinned DESC, date DESC LIMIT 0,10", new Object[0]);
                        while (queryFinalized2.next()) {
                            long longValue2 = queryFinalized2.longValue(0);
                            if (!DialogObject.isFolderDialogId(longValue2)) {
                                arrayList.add(Long.valueOf(longValue2));
                                if (arrayList2 != null && arrayList3 != null) {
                                    if (DialogObject.isUserDialog(longValue2)) {
                                        arrayList4.add(Long.valueOf(longValue2));
                                    } else {
                                        arrayList5.add(Long.valueOf(-longValue2));
                                    }
                                }
                            }
                        }
                        queryFinalized2.dispose();
                    } else {
                        SQLiteCursor queryFinalized3 = getMessagesStorage().getDatabase().queryFinalized("SELECT did FROM chat_hints WHERE type = 0 ORDER BY rating DESC LIMIT 4", new Object[0]);
                        while (queryFinalized3.next()) {
                            long longValue3 = queryFinalized3.longValue(0);
                            arrayList.add(Long.valueOf(longValue3));
                            if (arrayList2 != null && arrayList3 != null) {
                                if (DialogObject.isUserDialog(longValue3)) {
                                    arrayList4.add(Long.valueOf(longValue3));
                                } else {
                                    arrayList5.add(Long.valueOf(-longValue3));
                                }
                            }
                        }
                        queryFinalized3.dispose();
                    }
                }
                if (arrayList2 != null && arrayList3 != null) {
                    if (!arrayList5.isEmpty()) {
                        getChatsInternal(TextUtils.join(",", arrayList5), arrayList3);
                    }
                    if (!arrayList4.isEmpty()) {
                        getUsersInternal(TextUtils.join(",", arrayList4), arrayList2);
                    }
                }
            } catch (Exception e2) {
                e = e2;
            }
            countDownLatch.countDown();
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public void getWidgetDialogs(final int i, final int i2, final ArrayList<Long> arrayList, final LongSparseArray<TLRPC$Dialog> longSparseArray, final LongSparseArray<TLRPC$Message> longSparseArray2, final ArrayList<TLRPC$User> arrayList2, final ArrayList<TLRPC$Chat> arrayList3) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getWidgetDialogs$154(i, arrayList, i2, longSparseArray, longSparseArray2, arrayList3, arrayList2, countDownLatch);
            }
        });
        try {
            countDownLatch.await();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void lambda$getWidgetDialogs$154(int i, ArrayList arrayList, int i2, LongSparseArray longSparseArray, LongSparseArray longSparseArray2, ArrayList arrayList2, ArrayList arrayList3, CountDownLatch countDownLatch) {
        boolean z;
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                ArrayList arrayList4 = new ArrayList();
                ArrayList arrayList5 = new ArrayList();
                SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT did FROM shortcut_widget WHERE id = %d ORDER BY ord ASC", Integer.valueOf(i)), new Object[0]);
                while (queryFinalized.next()) {
                    try {
                        long longValue = queryFinalized.longValue(0);
                        if (longValue != -1) {
                            arrayList.add(Long.valueOf(longValue));
                            if (DialogObject.isUserDialog(longValue)) {
                                arrayList4.add(Long.valueOf(longValue));
                            } else {
                                arrayList5.add(Long.valueOf(-longValue));
                            }
                        }
                    } catch (Exception e) {
                        e = e;
                        sQLiteCursor = queryFinalized;
                        checkSQLException(e);
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        countDownLatch.countDown();
                    } catch (Throwable th) {
                        th = th;
                        sQLiteCursor = queryFinalized;
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        countDownLatch.countDown();
                        throw th;
                    }
                }
                queryFinalized.dispose();
                if (arrayList.isEmpty() && i2 == 1) {
                    SQLiteCursor queryFinalized2 = getMessagesStorage().getDatabase().queryFinalized("SELECT did FROM chat_hints WHERE type = 0 ORDER BY rating DESC LIMIT 4", new Object[0]);
                    while (queryFinalized2.next()) {
                        long longValue2 = queryFinalized2.longValue(0);
                        arrayList.add(Long.valueOf(longValue2));
                        if (DialogObject.isUserDialog(longValue2)) {
                            arrayList4.add(Long.valueOf(longValue2));
                        } else {
                            arrayList5.add(Long.valueOf(-longValue2));
                        }
                    }
                    queryFinalized2.dispose();
                }
                if (arrayList.isEmpty()) {
                    queryFinalized = this.database.queryFinalized("SELECT d.did, d.last_mid, d.unread_count, d.date, m.data, m.read_state, m.mid, m.send_state, m.date FROM dialogs as d LEFT JOIN messages_v2 as m ON d.last_mid = m.mid AND d.did = m.uid WHERE d.folder_id = 0 ORDER BY d.pinned DESC, d.date DESC LIMIT 0,10", new Object[0]);
                    z = true;
                } else {
                    queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT d.did, d.last_mid, d.unread_count, d.date, m.data, m.read_state, m.mid, m.send_state, m.date FROM dialogs as d LEFT JOIN messages_v2 as m ON d.last_mid = m.mid AND d.did = m.uid WHERE d.did IN(%s)", TextUtils.join(",", arrayList)), new Object[0]);
                    z = false;
                }
                while (queryFinalized.next()) {
                    long longValue3 = queryFinalized.longValue(0);
                    if (!DialogObject.isFolderDialogId(longValue3)) {
                        if (z) {
                            arrayList.add(Long.valueOf(longValue3));
                        }
                        TLRPC$TL_dialog tLRPC$TL_dialog = new TLRPC$TL_dialog();
                        tLRPC$TL_dialog.id = longValue3;
                        tLRPC$TL_dialog.top_message = queryFinalized.intValue(1);
                        tLRPC$TL_dialog.unread_count = queryFinalized.intValue(2);
                        tLRPC$TL_dialog.last_message_date = queryFinalized.intValue(3);
                        longSparseArray.put(tLRPC$TL_dialog.id, tLRPC$TL_dialog);
                        NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(4);
                        if (byteBufferValue != null) {
                            TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                            TLdeserialize.readAttachPath(byteBufferValue, getUserConfig().clientUserId);
                            byteBufferValue.reuse();
                            MessageObject.setUnreadFlags(TLdeserialize, queryFinalized.intValue(5));
                            TLdeserialize.id = queryFinalized.intValue(6);
                            TLdeserialize.send_state = queryFinalized.intValue(7);
                            int intValue = queryFinalized.intValue(8);
                            if (intValue != 0) {
                                tLRPC$TL_dialog.last_message_date = intValue;
                            }
                            long j = tLRPC$TL_dialog.id;
                            TLdeserialize.dialog_id = j;
                            longSparseArray2.put(j, TLdeserialize);
                            addUsersAndChatsFromMessage(TLdeserialize, arrayList4, arrayList5, null);
                        }
                    }
                }
                queryFinalized.dispose();
                if (!z && arrayList.size() > longSparseArray.size()) {
                    int size = arrayList.size();
                    for (int i3 = 0; i3 < size; i3++) {
                        long longValue4 = ((Long) arrayList.get(i3)).longValue();
                        if (longSparseArray.get(((Long) arrayList.get(i3)).longValue()) == null) {
                            TLRPC$TL_dialog tLRPC$TL_dialog2 = new TLRPC$TL_dialog();
                            tLRPC$TL_dialog2.id = longValue4;
                            longSparseArray.put(longValue4, tLRPC$TL_dialog2);
                            if (DialogObject.isChatDialog(longValue4)) {
                                long j2 = -longValue4;
                                if (arrayList5.contains(Long.valueOf(j2))) {
                                    arrayList5.add(Long.valueOf(j2));
                                }
                            } else if (arrayList4.contains(Long.valueOf(longValue4))) {
                                arrayList4.add(Long.valueOf(longValue4));
                            }
                        }
                    }
                }
                if (!arrayList5.isEmpty()) {
                    getChatsInternal(TextUtils.join(",", arrayList5), arrayList2);
                }
                if (!arrayList4.isEmpty()) {
                    getUsersInternal(TextUtils.join(",", arrayList4), arrayList3);
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Exception e2) {
            e = e2;
        }
        countDownLatch.countDown();
    }

    public void putSentFile(final String str, final TLObject tLObject, final int i, final String str2) {
        if (str == null || tLObject == null || str2 == null) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$putSentFile$155(str, tLObject, i, str2);
            }
        });
    }

    public void lambda$putSentFile$155(String str, TLObject tLObject, int i, String str2) {
        TLRPC$MessageMedia tLRPC$MessageMedia;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                String MD5 = Utilities.MD5(str);
                if (MD5 != null) {
                    if (tLObject instanceof TLRPC$Photo) {
                        tLRPC$MessageMedia = new TLRPC$TL_messageMediaPhoto();
                        tLRPC$MessageMedia.photo = (TLRPC$Photo) tLObject;
                        tLRPC$MessageMedia.flags |= 1;
                    } else if (tLObject instanceof TLRPC$Document) {
                        tLRPC$MessageMedia = new TLRPC$TL_messageMediaDocument();
                        tLRPC$MessageMedia.document = (TLRPC$Document) tLObject;
                        tLRPC$MessageMedia.flags |= 1;
                    } else {
                        tLRPC$MessageMedia = null;
                    }
                    if (tLRPC$MessageMedia == null) {
                        return;
                    }
                    sQLitePreparedStatement = this.database.executeFast("REPLACE INTO sent_files_v2 VALUES(?, ?, ?, ?)");
                    sQLitePreparedStatement.requery();
                    NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$MessageMedia.getObjectSize());
                    tLRPC$MessageMedia.serializeToStream(nativeByteBuffer);
                    sQLitePreparedStatement.bindString(1, MD5);
                    sQLitePreparedStatement.bindInteger(2, i);
                    sQLitePreparedStatement.bindByteBuffer(3, nativeByteBuffer);
                    sQLitePreparedStatement.bindString(4, str2);
                    sQLitePreparedStatement.step();
                    nativeByteBuffer.reuse();
                }
                if (sQLitePreparedStatement == null) {
                    return;
                }
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLitePreparedStatement == null) {
                    return;
                }
            }
            sQLitePreparedStatement.dispose();
        } catch (Throwable th) {
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void updateEncryptedChatSeq(final TLRPC$EncryptedChat tLRPC$EncryptedChat, final boolean z) {
        if (tLRPC$EncryptedChat == null) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateEncryptedChatSeq$156(tLRPC$EncryptedChat, z);
            }
        });
    }

    public void lambda$updateEncryptedChatSeq$156(TLRPC$EncryptedChat tLRPC$EncryptedChat, boolean z) {
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                sQLitePreparedStatement = this.database.executeFast("UPDATE enc_chats SET seq_in = ?, seq_out = ?, use_count = ?, in_seq_no = ?, mtproto_seq = ? WHERE uid = ?");
                sQLitePreparedStatement.bindInteger(1, tLRPC$EncryptedChat.seq_in);
                sQLitePreparedStatement.bindInteger(2, tLRPC$EncryptedChat.seq_out);
                sQLitePreparedStatement.bindInteger(3, (tLRPC$EncryptedChat.key_use_count_in << 16) | tLRPC$EncryptedChat.key_use_count_out);
                sQLitePreparedStatement.bindInteger(4, tLRPC$EncryptedChat.in_seq_no);
                sQLitePreparedStatement.bindInteger(5, tLRPC$EncryptedChat.mtproto_seq);
                sQLitePreparedStatement.bindInteger(6, tLRPC$EncryptedChat.id);
                sQLitePreparedStatement.step();
                if (z && tLRPC$EncryptedChat.in_seq_no != 0) {
                    long encryptedChatId = DialogObject.getEncryptedChatId(tLRPC$EncryptedChat.id);
                    this.database.executeFast(String.format(Locale.US, "DELETE FROM messages_v2 WHERE mid IN (SELECT m.mid FROM messages_v2 as m LEFT JOIN messages_seq as s ON m.mid = s.mid WHERE m.uid = %d AND m.date = 0 AND m.mid < 0 AND s.seq_out <= %d) AND uid = %d", Long.valueOf(encryptedChatId), Integer.valueOf(tLRPC$EncryptedChat.in_seq_no), Long.valueOf(encryptedChatId))).stepThis().dispose();
                }
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLitePreparedStatement == null) {
                    return;
                }
            }
            sQLitePreparedStatement.dispose();
        } catch (Throwable th) {
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void updateEncryptedChatTTL(final TLRPC$EncryptedChat tLRPC$EncryptedChat) {
        if (tLRPC$EncryptedChat == null) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateEncryptedChatTTL$157(tLRPC$EncryptedChat);
            }
        });
    }

    public void lambda$updateEncryptedChatTTL$157(TLRPC$EncryptedChat tLRPC$EncryptedChat) {
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                sQLitePreparedStatement = this.database.executeFast("UPDATE enc_chats SET ttl = ? WHERE uid = ?");
                sQLitePreparedStatement.bindInteger(1, tLRPC$EncryptedChat.ttl);
                sQLitePreparedStatement.bindInteger(2, tLRPC$EncryptedChat.id);
                sQLitePreparedStatement.step();
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLitePreparedStatement == null) {
                    return;
                }
            }
            sQLitePreparedStatement.dispose();
        } catch (Throwable th) {
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void updateEncryptedChatLayer(final TLRPC$EncryptedChat tLRPC$EncryptedChat) {
        if (tLRPC$EncryptedChat == null) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateEncryptedChatLayer$158(tLRPC$EncryptedChat);
            }
        });
    }

    public void lambda$updateEncryptedChatLayer$158(TLRPC$EncryptedChat tLRPC$EncryptedChat) {
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                sQLitePreparedStatement = this.database.executeFast("UPDATE enc_chats SET layer = ? WHERE uid = ?");
                sQLitePreparedStatement.bindInteger(1, tLRPC$EncryptedChat.layer);
                sQLitePreparedStatement.bindInteger(2, tLRPC$EncryptedChat.id);
                sQLitePreparedStatement.step();
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLitePreparedStatement == null) {
                    return;
                }
            }
            sQLitePreparedStatement.dispose();
        } catch (Throwable th) {
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void updateEncryptedChat(final TLRPC$EncryptedChat tLRPC$EncryptedChat) {
        if (tLRPC$EncryptedChat == null) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateEncryptedChat$159(tLRPC$EncryptedChat);
            }
        });
    }

    public void lambda$updateEncryptedChat$159(TLRPC$EncryptedChat tLRPC$EncryptedChat) {
        byte[] bArr;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                byte[] bArr2 = tLRPC$EncryptedChat.key_hash;
                if ((bArr2 == null || bArr2.length < 16) && (bArr = tLRPC$EncryptedChat.auth_key) != null) {
                    tLRPC$EncryptedChat.key_hash = AndroidUtilities.calcAuthKeyHash(bArr);
                }
                sQLitePreparedStatement = this.database.executeFast("UPDATE enc_chats SET data = ?, g = ?, authkey = ?, ttl = ?, layer = ?, seq_in = ?, seq_out = ?, use_count = ?, exchange_id = ?, key_date = ?, fprint = ?, fauthkey = ?, khash = ?, in_seq_no = ?, admin_id = ?, mtproto_seq = ? WHERE uid = ?");
                NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$EncryptedChat.getObjectSize());
                byte[] bArr3 = tLRPC$EncryptedChat.a_or_b;
                NativeByteBuffer nativeByteBuffer2 = new NativeByteBuffer(bArr3 != null ? bArr3.length : 1);
                byte[] bArr4 = tLRPC$EncryptedChat.auth_key;
                NativeByteBuffer nativeByteBuffer3 = new NativeByteBuffer(bArr4 != null ? bArr4.length : 1);
                byte[] bArr5 = tLRPC$EncryptedChat.future_auth_key;
                NativeByteBuffer nativeByteBuffer4 = new NativeByteBuffer(bArr5 != null ? bArr5.length : 1);
                byte[] bArr6 = tLRPC$EncryptedChat.key_hash;
                NativeByteBuffer nativeByteBuffer5 = new NativeByteBuffer(bArr6 != null ? bArr6.length : 1);
                tLRPC$EncryptedChat.serializeToStream(nativeByteBuffer);
                sQLitePreparedStatement.bindByteBuffer(1, nativeByteBuffer);
                byte[] bArr7 = tLRPC$EncryptedChat.a_or_b;
                if (bArr7 != null) {
                    nativeByteBuffer2.writeBytes(bArr7);
                }
                byte[] bArr8 = tLRPC$EncryptedChat.auth_key;
                if (bArr8 != null) {
                    nativeByteBuffer3.writeBytes(bArr8);
                }
                byte[] bArr9 = tLRPC$EncryptedChat.future_auth_key;
                if (bArr9 != null) {
                    nativeByteBuffer4.writeBytes(bArr9);
                }
                byte[] bArr10 = tLRPC$EncryptedChat.key_hash;
                if (bArr10 != null) {
                    nativeByteBuffer5.writeBytes(bArr10);
                }
                sQLitePreparedStatement.bindByteBuffer(2, nativeByteBuffer2);
                sQLitePreparedStatement.bindByteBuffer(3, nativeByteBuffer3);
                sQLitePreparedStatement.bindInteger(4, tLRPC$EncryptedChat.ttl);
                sQLitePreparedStatement.bindInteger(5, tLRPC$EncryptedChat.layer);
                sQLitePreparedStatement.bindInteger(6, tLRPC$EncryptedChat.seq_in);
                sQLitePreparedStatement.bindInteger(7, tLRPC$EncryptedChat.seq_out);
                sQLitePreparedStatement.bindInteger(8, (tLRPC$EncryptedChat.key_use_count_in << 16) | tLRPC$EncryptedChat.key_use_count_out);
                sQLitePreparedStatement.bindLong(9, tLRPC$EncryptedChat.exchange_id);
                sQLitePreparedStatement.bindInteger(10, tLRPC$EncryptedChat.key_create_date);
                sQLitePreparedStatement.bindLong(11, tLRPC$EncryptedChat.future_key_fingerprint);
                sQLitePreparedStatement.bindByteBuffer(12, nativeByteBuffer4);
                sQLitePreparedStatement.bindByteBuffer(13, nativeByteBuffer5);
                sQLitePreparedStatement.bindInteger(14, tLRPC$EncryptedChat.in_seq_no);
                sQLitePreparedStatement.bindLong(15, tLRPC$EncryptedChat.admin_id);
                sQLitePreparedStatement.bindInteger(16, tLRPC$EncryptedChat.mtproto_seq);
                sQLitePreparedStatement.bindInteger(17, tLRPC$EncryptedChat.id);
                sQLitePreparedStatement.step();
                nativeByteBuffer.reuse();
                nativeByteBuffer2.reuse();
                nativeByteBuffer3.reuse();
                nativeByteBuffer4.reuse();
                nativeByteBuffer5.reuse();
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLitePreparedStatement == null) {
                    return;
                }
            }
            sQLitePreparedStatement.dispose();
        } catch (Throwable th) {
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void isDialogHasTopMessage(final long j, final Runnable runnable) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$isDialogHasTopMessage$160(j, runnable);
            }
        });
    }

    public void lambda$isDialogHasTopMessage$160(long j, Runnable runnable) {
        boolean z = false;
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                sQLiteCursor = this.database.queryFinalized(String.format(Locale.US, "SELECT last_mid FROM dialogs WHERE did = %d", Long.valueOf(j)), new Object[0]);
                if (sQLiteCursor.next()) {
                    if (sQLiteCursor.intValue(0) != 0) {
                        z = true;
                    }
                }
            } catch (Exception e) {
                checkSQLException(e);
            }
            sQLiteCursor.dispose();
            if (z) {
                return;
            }
            AndroidUtilities.runOnUIThread(runnable);
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public boolean hasAuthMessage(final int i) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        final boolean[] zArr = new boolean[1];
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$hasAuthMessage$161(i, zArr, countDownLatch);
            }
        });
        try {
            countDownLatch.await();
        } catch (Exception e) {
            checkSQLException(e);
        }
        return zArr[0];
    }

    public void lambda$hasAuthMessage$161(int i, boolean[] zArr, CountDownLatch countDownLatch) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                sQLiteCursor = this.database.queryFinalized(String.format(Locale.US, "SELECT mid FROM messages_v2 WHERE uid = 777000 AND date = %d AND mid < 0 LIMIT 1", Integer.valueOf(i)), new Object[0]);
                zArr[0] = sQLiteCursor.next();
            } catch (Exception e) {
                checkSQLException(e);
            }
            sQLiteCursor.dispose();
            countDownLatch.countDown();
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            countDownLatch.countDown();
            throw th;
        }
    }

    public void getEncryptedChat(final long j, final CountDownLatch countDownLatch, final ArrayList<TLObject> arrayList) {
        if (countDownLatch == null || arrayList == null) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getEncryptedChat$162(j, arrayList, countDownLatch);
            }
        });
    }

    public void lambda$getEncryptedChat$162(long j, ArrayList arrayList, CountDownLatch countDownLatch) {
        try {
            try {
                ArrayList<Long> arrayList2 = new ArrayList<>();
                ArrayList<TLRPC$EncryptedChat> arrayList3 = new ArrayList<>();
                getEncryptedChatsInternal("" + j, arrayList3, arrayList2);
                if (!arrayList3.isEmpty() && !arrayList2.isEmpty()) {
                    ArrayList<TLRPC$User> arrayList4 = new ArrayList<>();
                    getUsersInternal(TextUtils.join(",", arrayList2), arrayList4);
                    if (!arrayList4.isEmpty()) {
                        arrayList.add(arrayList3.get(0));
                        arrayList.add(arrayList4.get(0));
                    }
                }
            } catch (Exception e) {
                checkSQLException(e);
            }
        } finally {
            countDownLatch.countDown();
        }
    }

    public void putEncryptedChat(final TLRPC$EncryptedChat tLRPC$EncryptedChat, final TLRPC$User tLRPC$User, final TLRPC$Dialog tLRPC$Dialog) {
        if (tLRPC$EncryptedChat == null) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$putEncryptedChat$163(tLRPC$EncryptedChat, tLRPC$User, tLRPC$Dialog);
            }
        });
    }

    public void lambda$putEncryptedChat$163(TLRPC$EncryptedChat tLRPC$EncryptedChat, TLRPC$User tLRPC$User, TLRPC$Dialog tLRPC$Dialog) {
        SQLitePreparedStatement sQLitePreparedStatement;
        byte[] bArr;
        try {
            byte[] bArr2 = tLRPC$EncryptedChat.key_hash;
            if ((bArr2 == null || bArr2.length < 16) && (bArr = tLRPC$EncryptedChat.auth_key) != null) {
                tLRPC$EncryptedChat.key_hash = AndroidUtilities.calcAuthKeyHash(bArr);
            }
            SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO enc_chats VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            try {
                NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$EncryptedChat.getObjectSize());
                byte[] bArr3 = tLRPC$EncryptedChat.a_or_b;
                NativeByteBuffer nativeByteBuffer2 = new NativeByteBuffer(bArr3 != null ? bArr3.length : 1);
                byte[] bArr4 = tLRPC$EncryptedChat.auth_key;
                NativeByteBuffer nativeByteBuffer3 = new NativeByteBuffer(bArr4 != null ? bArr4.length : 1);
                byte[] bArr5 = tLRPC$EncryptedChat.future_auth_key;
                NativeByteBuffer nativeByteBuffer4 = new NativeByteBuffer(bArr5 != null ? bArr5.length : 1);
                byte[] bArr6 = tLRPC$EncryptedChat.key_hash;
                NativeByteBuffer nativeByteBuffer5 = new NativeByteBuffer(bArr6 != null ? bArr6.length : 1);
                tLRPC$EncryptedChat.serializeToStream(nativeByteBuffer);
                executeFast.bindInteger(1, tLRPC$EncryptedChat.id);
                executeFast.bindLong(2, tLRPC$User.id);
                executeFast.bindString(3, formatUserSearchName(tLRPC$User));
                executeFast.bindByteBuffer(4, nativeByteBuffer);
                byte[] bArr7 = tLRPC$EncryptedChat.a_or_b;
                if (bArr7 != null) {
                    nativeByteBuffer2.writeBytes(bArr7);
                }
                byte[] bArr8 = tLRPC$EncryptedChat.auth_key;
                if (bArr8 != null) {
                    nativeByteBuffer3.writeBytes(bArr8);
                }
                byte[] bArr9 = tLRPC$EncryptedChat.future_auth_key;
                if (bArr9 != null) {
                    nativeByteBuffer4.writeBytes(bArr9);
                }
                byte[] bArr10 = tLRPC$EncryptedChat.key_hash;
                if (bArr10 != null) {
                    nativeByteBuffer5.writeBytes(bArr10);
                }
                executeFast.bindByteBuffer(5, nativeByteBuffer2);
                executeFast.bindByteBuffer(6, nativeByteBuffer3);
                executeFast.bindInteger(7, tLRPC$EncryptedChat.ttl);
                executeFast.bindInteger(8, tLRPC$EncryptedChat.layer);
                executeFast.bindInteger(9, tLRPC$EncryptedChat.seq_in);
                executeFast.bindInteger(10, tLRPC$EncryptedChat.seq_out);
                executeFast.bindInteger(11, (tLRPC$EncryptedChat.key_use_count_in << 16) | tLRPC$EncryptedChat.key_use_count_out);
                executeFast.bindLong(12, tLRPC$EncryptedChat.exchange_id);
                executeFast.bindInteger(13, tLRPC$EncryptedChat.key_create_date);
                executeFast.bindLong(14, tLRPC$EncryptedChat.future_key_fingerprint);
                executeFast.bindByteBuffer(15, nativeByteBuffer4);
                executeFast.bindByteBuffer(16, nativeByteBuffer5);
                executeFast.bindInteger(17, tLRPC$EncryptedChat.in_seq_no);
                executeFast.bindLong(18, tLRPC$EncryptedChat.admin_id);
                executeFast.bindInteger(19, tLRPC$EncryptedChat.mtproto_seq);
                executeFast.step();
                executeFast.dispose();
                nativeByteBuffer.reuse();
                nativeByteBuffer2.reuse();
                nativeByteBuffer3.reuse();
                nativeByteBuffer4.reuse();
                nativeByteBuffer5.reuse();
                if (tLRPC$Dialog != null) {
                    executeFast = this.database.executeFast("REPLACE INTO dialogs VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                    executeFast.bindLong(1, tLRPC$Dialog.id);
                    executeFast.bindInteger(2, tLRPC$Dialog.last_message_date);
                    executeFast.bindInteger(3, tLRPC$Dialog.unread_count);
                    executeFast.bindInteger(4, tLRPC$Dialog.top_message);
                    executeFast.bindInteger(5, tLRPC$Dialog.read_inbox_max_id);
                    executeFast.bindInteger(6, tLRPC$Dialog.read_outbox_max_id);
                    executeFast.bindInteger(7, 0);
                    executeFast.bindInteger(8, tLRPC$Dialog.unread_mentions_count);
                    executeFast.bindInteger(9, tLRPC$Dialog.pts);
                    executeFast.bindInteger(10, 0);
                    executeFast.bindInteger(11, tLRPC$Dialog.pinnedNum);
                    executeFast.bindInteger(12, tLRPC$Dialog.flags);
                    executeFast.bindInteger(13, tLRPC$Dialog.folder_id);
                    executeFast.bindNull(14);
                    executeFast.bindInteger(15, tLRPC$Dialog.unread_reactions_count);
                    executeFast.bindInteger(16, 0);
                    executeFast.bindInteger(17, tLRPC$Dialog.ttl_period);
                    executeFast.step();
                    executeFast.dispose();
                }
            } catch (Exception e) {
                e = e;
                sQLitePreparedStatement = executeFast;
                try {
                    checkSQLException(e);
                    if (sQLitePreparedStatement != null) {
                        sQLitePreparedStatement.dispose();
                    }
                } catch (Throwable th) {
                    th = th;
                    if (sQLitePreparedStatement != null) {
                        sQLitePreparedStatement.dispose();
                    }
                    throw th;
                }
            } catch (Throwable th2) {
                th = th2;
                sQLitePreparedStatement = executeFast;
                if (sQLitePreparedStatement != null) {
                }
                throw th;
            }
        } catch (Exception e2) {
            e = e2;
            sQLitePreparedStatement = null;
        } catch (Throwable th3) {
            th = th3;
            sQLitePreparedStatement = null;
        }
    }

    private String formatUserSearchName(TLRPC$User tLRPC$User) {
        StringBuilder sb = new StringBuilder();
        String str = tLRPC$User.first_name;
        if (str != null && str.length() > 0) {
            sb.append(tLRPC$User.first_name);
        }
        String str2 = tLRPC$User.last_name;
        if (str2 != null && str2.length() > 0) {
            if (sb.length() > 0) {
                sb.append(" ");
            }
            sb.append(tLRPC$User.last_name);
        }
        sb.append(";;;");
        String str3 = tLRPC$User.username;
        if (str3 != null && str3.length() > 0) {
            sb.append(tLRPC$User.username);
        } else {
            ArrayList<TLRPC$TL_username> arrayList = tLRPC$User.usernames;
            if (arrayList != null && arrayList.size() > 0) {
                for (int i = 0; i < tLRPC$User.usernames.size(); i++) {
                    TLRPC$TL_username tLRPC$TL_username = tLRPC$User.usernames.get(i);
                    if (tLRPC$TL_username != null && tLRPC$TL_username.active) {
                        sb.append(tLRPC$TL_username.username);
                        sb.append(";;");
                    }
                }
            }
        }
        return sb.toString().toLowerCase();
    }

    public boolean containsLocalDialog(final long j) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        final Boolean[] boolArr = {Boolean.FALSE};
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$containsLocalDialog$164(j, boolArr, countDownLatch);
            }
        });
        try {
            countDownLatch.await();
        } catch (Exception e) {
            checkSQLException(e);
        }
        return boolArr[0].booleanValue();
    }

    public void lambda$containsLocalDialog$164(long j, Boolean[] boolArr, CountDownLatch countDownLatch) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                sQLiteCursor = this.database.queryFinalized("SELECT date FROM dialogs WHERE did = " + j, new Object[0]);
                boolArr[0] = Boolean.valueOf(sQLiteCursor.next());
            } catch (Exception e) {
                checkSQLException(e);
            }
            sQLiteCursor.dispose();
            countDownLatch.countDown();
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    private void putUsersInternal(List<TLRPC$User> list) throws Exception {
        if (list == null || list.isEmpty()) {
            return;
        }
        SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO users VALUES(?, ?, ?, ?)");
        for (int i = 0; i < list.size(); i++) {
            TLRPC$User tLRPC$User = list.get(i);
            if (tLRPC$User != null) {
                if (tLRPC$User.min) {
                    SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT data FROM users WHERE uid = %d", Long.valueOf(tLRPC$User.id)), new Object[0]);
                    if (queryFinalized.next()) {
                        try {
                            NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(0);
                            if (byteBufferValue != null) {
                                TLRPC$User TLdeserialize = TLRPC$User.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                                byteBufferValue.reuse();
                                if (TLdeserialize != null) {
                                    String str = tLRPC$User.username;
                                    if (str != null) {
                                        TLdeserialize.username = str;
                                        TLdeserialize.flags |= 8;
                                    } else {
                                        TLdeserialize.username = null;
                                        TLdeserialize.flags &= -9;
                                    }
                                    if (tLRPC$User.apply_min_photo) {
                                        TLRPC$UserProfilePhoto tLRPC$UserProfilePhoto = tLRPC$User.photo;
                                        if (tLRPC$UserProfilePhoto != null) {
                                            TLdeserialize.photo = tLRPC$UserProfilePhoto;
                                            TLdeserialize.flags |= 32;
                                        } else {
                                            TLdeserialize.photo = null;
                                            TLdeserialize.flags &= -33;
                                        }
                                    }
                                    tLRPC$User = TLdeserialize;
                                }
                            }
                        } catch (Exception e) {
                            checkSQLException(e);
                        }
                    }
                    queryFinalized.dispose();
                }
                executeFast.requery();
                NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$User.getObjectSize());
                tLRPC$User.serializeToStream(nativeByteBuffer);
                executeFast.bindLong(1, tLRPC$User.id);
                executeFast.bindString(2, formatUserSearchName(tLRPC$User));
                TLRPC$UserStatus tLRPC$UserStatus = tLRPC$User.status;
                if (tLRPC$UserStatus != null) {
                    if (tLRPC$UserStatus instanceof TLRPC$TL_userStatusRecently) {
                        tLRPC$UserStatus.expires = tLRPC$UserStatus.by_me ? -1000 : -100;
                    } else if (tLRPC$UserStatus instanceof TLRPC$TL_userStatusLastWeek) {
                        tLRPC$UserStatus.expires = tLRPC$UserStatus.by_me ? -1001 : -101;
                    } else if (tLRPC$UserStatus instanceof TLRPC$TL_userStatusLastMonth) {
                        tLRPC$UserStatus.expires = tLRPC$UserStatus.by_me ? -1002 : -102;
                    }
                    executeFast.bindInteger(3, tLRPC$UserStatus.expires);
                } else {
                    executeFast.bindInteger(3, 0);
                }
                executeFast.bindByteBuffer(4, nativeByteBuffer);
                executeFast.step();
                nativeByteBuffer.reuse();
            }
        }
        executeFast.dispose();
    }

    public void updateChatDefaultBannedRights(final long j, final TLRPC$TL_chatBannedRights tLRPC$TL_chatBannedRights, final int i) {
        if (tLRPC$TL_chatBannedRights == null || j == 0) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateChatDefaultBannedRights$165(j, i, tLRPC$TL_chatBannedRights);
            }
        });
    }

    public void lambda$updateChatDefaultBannedRights$165(long j, int i, TLRPC$TL_chatBannedRights tLRPC$TL_chatBannedRights) {
        SQLiteCursor queryFinalized;
        TLRPC$Chat tLRPC$Chat;
        NativeByteBuffer byteBufferValue;
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT data FROM chats WHERE uid = %d", Long.valueOf(j)), new Object[0]);
            } catch (Throwable th) {
                th = th;
            }
        } catch (Exception e) {
            e = e;
            j = 0;
        } catch (Throwable th2) {
            th = th2;
            j = 0;
        }
        try {
            if (!queryFinalized.next() || (byteBufferValue = queryFinalized.byteBufferValue(0)) == null) {
                tLRPC$Chat = null;
            } else {
                tLRPC$Chat = TLRPC$Chat.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                byteBufferValue.reuse();
            }
            queryFinalized.dispose();
        } catch (Exception e2) {
            e = e2;
            sQLiteCursor = queryFinalized;
            j = 0;
        } catch (Throwable th3) {
            th = th3;
            sQLiteCursor = queryFinalized;
            j = 0;
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            if (j != 0) {
                j.dispose();
            }
            throw th;
        }
        if (tLRPC$Chat != null) {
            if (tLRPC$Chat.default_banned_rights == null || i >= tLRPC$Chat.version) {
                tLRPC$Chat.default_banned_rights = tLRPC$TL_chatBannedRights;
                tLRPC$Chat.flags |= 262144;
                tLRPC$Chat.version = i;
                j = this.database.executeFast("UPDATE chats SET data = ? WHERE uid = ?");
                try {
                    NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$Chat.getObjectSize());
                    tLRPC$Chat.serializeToStream(nativeByteBuffer);
                    j.bindByteBuffer(1, nativeByteBuffer);
                    j.bindLong(2, tLRPC$Chat.id);
                    j.step();
                    nativeByteBuffer.reuse();
                    j.dispose();
                } catch (Exception e3) {
                    e = e3;
                    checkSQLException(e);
                    if (sQLiteCursor != null) {
                        sQLiteCursor.dispose();
                    }
                    if (j != 0) {
                        j.dispose();
                    }
                }
            }
        }
    }

    private void putChatsInternal(List<TLRPC$Chat> list) throws Exception {
        if (list == null || list.isEmpty()) {
            return;
        }
        SQLitePreparedStatement executeFast = this.database.executeFast("REPLACE INTO chats VALUES(?, ?, ?)");
        for (int i = 0; i < list.size(); i++) {
            TLRPC$Chat tLRPC$Chat = list.get(i);
            if (tLRPC$Chat.min) {
                SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT data FROM chats WHERE uid = %d", Long.valueOf(tLRPC$Chat.id)), new Object[0]);
                if (queryFinalized.next()) {
                    try {
                        NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(0);
                        if (byteBufferValue != null) {
                            TLRPC$Chat TLdeserialize = TLRPC$Chat.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                            byteBufferValue.reuse();
                            if (TLdeserialize != null) {
                                TLdeserialize.title = tLRPC$Chat.title;
                                TLdeserialize.photo = tLRPC$Chat.photo;
                                TLdeserialize.broadcast = tLRPC$Chat.broadcast;
                                TLdeserialize.verified = tLRPC$Chat.verified;
                                TLdeserialize.megagroup = tLRPC$Chat.megagroup;
                                TLdeserialize.call_not_empty = tLRPC$Chat.call_not_empty;
                                TLdeserialize.call_active = tLRPC$Chat.call_active;
                                TLRPC$TL_chatBannedRights tLRPC$TL_chatBannedRights = tLRPC$Chat.default_banned_rights;
                                if (tLRPC$TL_chatBannedRights != null) {
                                    TLdeserialize.default_banned_rights = tLRPC$TL_chatBannedRights;
                                    TLdeserialize.flags |= 262144;
                                }
                                TLRPC$TL_chatAdminRights tLRPC$TL_chatAdminRights = tLRPC$Chat.admin_rights;
                                if (tLRPC$TL_chatAdminRights != null) {
                                    TLdeserialize.admin_rights = tLRPC$TL_chatAdminRights;
                                    TLdeserialize.flags |= LiteMode.FLAG_ANIMATED_EMOJI_KEYBOARD_NOT_PREMIUM;
                                }
                                TLRPC$TL_chatBannedRights tLRPC$TL_chatBannedRights2 = tLRPC$Chat.banned_rights;
                                if (tLRPC$TL_chatBannedRights2 != null) {
                                    TLdeserialize.banned_rights = tLRPC$TL_chatBannedRights2;
                                    TLdeserialize.flags |= LiteMode.FLAG_CHAT_SCALE;
                                }
                                String str = tLRPC$Chat.username;
                                if (str != null) {
                                    TLdeserialize.username = str;
                                    TLdeserialize.flags |= 64;
                                } else {
                                    TLdeserialize.username = null;
                                    TLdeserialize.flags &= -65;
                                }
                                int i2 = tLRPC$Chat.participants_count;
                                if (i2 > 0) {
                                    TLdeserialize.participants_count = i2;
                                }
                                tLRPC$Chat = TLdeserialize;
                            }
                        }
                    } catch (Exception e) {
                        FileLog.e(e);
                    }
                }
                queryFinalized.dispose();
            }
            executeFast.requery();
            tLRPC$Chat.flags |= 131072;
            NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$Chat.getObjectSize());
            tLRPC$Chat.serializeToStream(nativeByteBuffer);
            executeFast.bindLong(1, tLRPC$Chat.id);
            String str2 = tLRPC$Chat.title;
            if (str2 != null) {
                executeFast.bindString(2, str2.toLowerCase());
            } else {
                executeFast.bindString(2, "");
            }
            executeFast.bindByteBuffer(3, nativeByteBuffer);
            executeFast.step();
            nativeByteBuffer.reuse();
            this.dialogIsForum.put(-tLRPC$Chat.id, tLRPC$Chat.forum ? 1 : 0);
        }
        executeFast.dispose();
    }

    public void getUsersInternal(String str, ArrayList<TLRPC$User> arrayList) throws Exception {
        if (str == null || str.length() == 0 || arrayList == null) {
            return;
        }
        SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN(%s)", str), new Object[0]);
        while (queryFinalized.next()) {
            try {
                NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(0);
                if (byteBufferValue != null) {
                    TLRPC$User TLdeserialize = TLRPC$User.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                    byteBufferValue.reuse();
                    if (TLdeserialize != null) {
                        TLRPC$UserStatus tLRPC$UserStatus = TLdeserialize.status;
                        if (tLRPC$UserStatus != null) {
                            tLRPC$UserStatus.expires = queryFinalized.intValue(1);
                        }
                        arrayList.add(TLdeserialize);
                    }
                }
            } catch (Exception e) {
                checkSQLException(e);
            }
        }
        queryFinalized.dispose();
    }

    public void getChatsInternal(String str, ArrayList<TLRPC$Chat> arrayList) throws Exception {
        getChatsInternal(str, arrayList, true);
    }

    public void getChatsInternal(String str, ArrayList<TLRPC$Chat> arrayList, boolean z) throws Exception {
        if (str == null || str.length() == 0 || arrayList == null) {
            return;
        }
        SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT data FROM chats WHERE uid IN(%s)", str), new Object[0]);
        while (queryFinalized.next()) {
            try {
                NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(0);
                if (byteBufferValue != null) {
                    TLRPC$Chat TLdeserialize = TLRPC$Chat.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false, z);
                    byteBufferValue.reuse();
                    if (TLdeserialize != null) {
                        arrayList.add(TLdeserialize);
                    }
                }
            } catch (Exception e) {
                checkSQLException(e);
            }
        }
        queryFinalized.dispose();
    }

    public void getEncryptedChatsInternal(String str, ArrayList<TLRPC$EncryptedChat> arrayList, ArrayList<Long> arrayList2) throws Exception {
        if (str == null || str.length() == 0 || arrayList == null) {
            return;
        }
        SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT data, user, g, authkey, ttl, layer, seq_in, seq_out, use_count, exchange_id, key_date, fprint, fauthkey, khash, in_seq_no, admin_id, mtproto_seq FROM enc_chats WHERE uid IN(%s)", str), new Object[0]);
        while (queryFinalized.next()) {
            try {
                NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(0);
                if (byteBufferValue != null) {
                    TLRPC$EncryptedChat TLdeserialize = TLRPC$EncryptedChat.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                    byteBufferValue.reuse();
                    if (TLdeserialize != null) {
                        long longValue = queryFinalized.longValue(1);
                        TLdeserialize.user_id = longValue;
                        if (arrayList2 != null && !arrayList2.contains(Long.valueOf(longValue))) {
                            arrayList2.add(Long.valueOf(TLdeserialize.user_id));
                        }
                        TLdeserialize.a_or_b = queryFinalized.byteArrayValue(2);
                        TLdeserialize.auth_key = queryFinalized.byteArrayValue(3);
                        TLdeserialize.ttl = queryFinalized.intValue(4);
                        TLdeserialize.layer = queryFinalized.intValue(5);
                        TLdeserialize.seq_in = queryFinalized.intValue(6);
                        TLdeserialize.seq_out = queryFinalized.intValue(7);
                        int intValue = queryFinalized.intValue(8);
                        TLdeserialize.key_use_count_in = (short) (intValue >> 16);
                        TLdeserialize.key_use_count_out = (short) intValue;
                        TLdeserialize.exchange_id = queryFinalized.longValue(9);
                        TLdeserialize.key_create_date = queryFinalized.intValue(10);
                        TLdeserialize.future_key_fingerprint = queryFinalized.longValue(11);
                        TLdeserialize.future_auth_key = queryFinalized.byteArrayValue(12);
                        TLdeserialize.key_hash = queryFinalized.byteArrayValue(13);
                        TLdeserialize.in_seq_no = queryFinalized.intValue(14);
                        long longValue2 = queryFinalized.longValue(15);
                        if (longValue2 != 0) {
                            TLdeserialize.admin_id = longValue2;
                        }
                        TLdeserialize.mtproto_seq = queryFinalized.intValue(16);
                        arrayList.add(TLdeserialize);
                    }
                }
            } catch (Exception e) {
                checkSQLException(e);
            }
        }
        queryFinalized.dispose();
    }

    public void lambda$putUsersAndChats$166(List<TLRPC$User> list, List<TLRPC$Chat> list2, boolean z) {
        SQLiteDatabase sQLiteDatabase;
        try {
            if (z) {
                try {
                    this.database.beginTransaction();
                } catch (Exception e) {
                    checkSQLException(e);
                    sQLiteDatabase = this.database;
                    if (sQLiteDatabase == null) {
                        return;
                    }
                }
            }
            putUsersInternal(list);
            putChatsInternal(list2);
            sQLiteDatabase = this.database;
            if (sQLiteDatabase == null) {
                return;
            }
            sQLiteDatabase.commitTransaction();
        } catch (Throwable th) {
            SQLiteDatabase sQLiteDatabase2 = this.database;
            if (sQLiteDatabase2 != null) {
                sQLiteDatabase2.commitTransaction();
            }
            throw th;
        }
    }

    public void putUsersAndChats(final List<TLRPC$User> list, final List<TLRPC$Chat> list2, final boolean z, boolean z2) {
        if (list == null || !list.isEmpty() || list2 == null || !list2.isEmpty()) {
            if (z2) {
                this.storageQueue.postRunnable(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$putUsersAndChats$166(list, list2, z);
                    }
                });
            } else {
                lambda$putUsersAndChats$166(list, list2, z);
            }
        }
    }

    public void removeFromDownloadQueue(final long j, final int i, final boolean z) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$removeFromDownloadQueue$167(z, i, j);
            }
        });
    }

    public void lambda$removeFromDownloadQueue$167(boolean z, int i, long j) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                if (z) {
                    SQLiteDatabase sQLiteDatabase = this.database;
                    Locale locale = Locale.US;
                    SQLiteCursor queryFinalized = sQLiteDatabase.queryFinalized(String.format(locale, "SELECT min(date) FROM download_queue WHERE type = %d", Integer.valueOf(i)), new Object[0]);
                    try {
                        int intValue = queryFinalized.next() ? queryFinalized.intValue(0) : -1;
                        queryFinalized.dispose();
                        if (intValue != -1) {
                            this.database.executeFast(String.format(locale, "UPDATE download_queue SET date = %d WHERE uid = %d AND type = %d", Integer.valueOf(intValue - 1), Long.valueOf(j), Integer.valueOf(i))).stepThis().dispose();
                        }
                    } catch (Exception e) {
                        e = e;
                        sQLiteCursor = queryFinalized;
                        checkSQLException(e);
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                    } catch (Throwable th) {
                        th = th;
                        sQLiteCursor = queryFinalized;
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        throw th;
                    }
                } else {
                    this.database.executeFast(String.format(Locale.US, "DELETE FROM download_queue WHERE uid = %d AND type = %d", Long.valueOf(j), Integer.valueOf(i))).stepThis().dispose();
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Exception e2) {
            e = e2;
        }
    }

    private void deleteFromDownloadQueue(final ArrayList<Pair<Long, Integer>> arrayList, boolean z) {
        Throwable th;
        SQLiteDatabase sQLiteDatabase;
        Exception e;
        SQLiteDatabase sQLiteDatabase2;
        if (arrayList == null || arrayList.isEmpty()) {
            return;
        }
        SQLitePreparedStatement sQLitePreparedStatement = null;
        if (z) {
            try {
                try {
                    this.database.beginTransaction();
                } catch (Exception e2) {
                    e = e2;
                    checkSQLException(e);
                    if (sQLitePreparedStatement != null) {
                        sQLitePreparedStatement.dispose();
                    }
                    if (z || (sQLiteDatabase2 = this.database) == null) {
                        return;
                    }
                    sQLiteDatabase2.commitTransaction();
                }
            } catch (Throwable th2) {
                th = th2;
                if (sQLitePreparedStatement != null) {
                    sQLitePreparedStatement.dispose();
                }
                if (z && (sQLiteDatabase = this.database) != null) {
                    sQLiteDatabase.commitTransaction();
                }
                throw th;
            }
        }
        SQLitePreparedStatement executeFast = this.database.executeFast("DELETE FROM download_queue WHERE uid = ? AND type = ?");
        try {
            int size = arrayList.size();
            for (int i = 0; i < size; i++) {
                Pair<Long, Integer> pair = arrayList.get(i);
                executeFast.requery();
                executeFast.bindLong(1, ((Long) pair.first).longValue());
                executeFast.bindInteger(2, ((Integer) pair.second).intValue());
                executeFast.step();
            }
            executeFast.dispose();
            if (z) {
                this.database.commitTransaction();
            }
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$deleteFromDownloadQueue$168(arrayList);
                }
            });
            if (!z || (sQLiteDatabase2 = this.database) == null) {
                return;
            }
        } catch (Exception e3) {
            e = e3;
            sQLitePreparedStatement = executeFast;
            checkSQLException(e);
            if (sQLitePreparedStatement != null) {
            }
            if (z) {
                return;
            } else {
                return;
            }
        } catch (Throwable th3) {
            th = th3;
            sQLitePreparedStatement = executeFast;
            if (sQLitePreparedStatement != null) {
            }
            if (z) {
                sQLiteDatabase.commitTransaction();
            }
            throw th;
        }
        sQLiteDatabase2.commitTransaction();
    }

    public void lambda$deleteFromDownloadQueue$168(ArrayList arrayList) {
        getDownloadController().cancelDownloading(arrayList);
    }

    public void clearDownloadQueue(final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$clearDownloadQueue$169(i);
            }
        });
    }

    public void lambda$clearDownloadQueue$169(int i) {
        try {
            if (i == 0) {
                this.database.executeFast("DELETE FROM download_queue WHERE 1").stepThis().dispose();
            } else {
                this.database.executeFast(String.format(Locale.US, "DELETE FROM download_queue WHERE type = %d", Integer.valueOf(i))).stepThis().dispose();
            }
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void getDownloadQueue(final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getDownloadQueue$171(i);
            }
        });
    }

    public void lambda$getDownloadQueue$171(final int i) {
        int i2;
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                final ArrayList arrayList = new ArrayList();
                SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT uid, type, data, parent FROM download_queue WHERE type = %d ORDER BY date DESC LIMIT 3", Integer.valueOf(i)), new Object[0]);
                while (queryFinalized.next()) {
                    try {
                        DownloadObject downloadObject = new DownloadObject();
                        downloadObject.type = queryFinalized.intValue(1);
                        downloadObject.id = queryFinalized.longValue(0);
                        downloadObject.parent = queryFinalized.stringValue(3);
                        NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(2);
                        if (byteBufferValue != null) {
                            TLRPC$MessageMedia TLdeserialize = TLRPC$MessageMedia.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                            byteBufferValue.reuse();
                            TLRPC$Document tLRPC$Document = TLdeserialize.document;
                            if (tLRPC$Document != null) {
                                downloadObject.object = tLRPC$Document;
                                downloadObject.secret = (MessageObject.isVideoDocument(tLRPC$Document) || MessageObject.isVoiceDocument(TLdeserialize.document) || MessageObject.isRoundVideoDocument(TLdeserialize.document)) && (((i2 = TLdeserialize.ttl_seconds) > 0 && i2 <= 60) || i2 == Integer.MAX_VALUE);
                            } else {
                                TLRPC$Photo tLRPC$Photo = TLdeserialize.photo;
                                if (tLRPC$Photo != null) {
                                    downloadObject.object = tLRPC$Photo;
                                    int i3 = TLdeserialize.ttl_seconds;
                                    downloadObject.secret = (i3 > 0 && i3 <= 60) || i3 == Integer.MAX_VALUE;
                                }
                            }
                            downloadObject.forceCache = (TLdeserialize.flags & Integer.MIN_VALUE) != 0;
                        }
                        arrayList.add(downloadObject);
                    } catch (Exception e) {
                        e = e;
                        sQLiteCursor = queryFinalized;
                        checkSQLException(e);
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                    } catch (Throwable th) {
                        th = th;
                        sQLiteCursor = queryFinalized;
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        throw th;
                    }
                }
                queryFinalized.dispose();
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$getDownloadQueue$170(i, arrayList);
                    }
                });
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Exception e2) {
            e = e2;
        }
    }

    public void lambda$getDownloadQueue$170(int i, ArrayList arrayList) {
        getDownloadController().processDownloadObjects(i, arrayList);
    }

    public int getMessageMediaType(TLRPC$Message tLRPC$Message) {
        if (tLRPC$Message instanceof TLRPC$TL_message_secret) {
            if (!(tLRPC$Message.media instanceof TLRPC$TL_messageMediaPhoto) && !MessageObject.isGifMessage(tLRPC$Message) && !MessageObject.isVoiceMessage(tLRPC$Message) && !MessageObject.isVideoMessage(tLRPC$Message) && !MessageObject.isRoundVideoMessage(tLRPC$Message)) {
                return -1;
            }
            int i = tLRPC$Message.ttl;
            return (i <= 0 || i > 60) ? 0 : 1;
        }
        if (tLRPC$Message instanceof TLRPC$TL_message) {
            TLRPC$MessageMedia tLRPC$MessageMedia = tLRPC$Message.media;
            if (((tLRPC$MessageMedia instanceof TLRPC$TL_messageMediaPhoto) || (tLRPC$MessageMedia instanceof TLRPC$TL_messageMediaDocument)) && tLRPC$MessageMedia.ttl_seconds != 0) {
                return 1;
            }
        }
        return ((tLRPC$Message.media instanceof TLRPC$TL_messageMediaPhoto) || MessageObject.isVideoMessage(tLRPC$Message)) ? 0 : -1;
    }

    public void putWebPages(final LongSparseArray<TLRPC$WebPage> longSparseArray) {
        if (isEmpty(longSparseArray)) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$putWebPages$173(longSparseArray);
            }
        });
    }

    public void lambda$putWebPages$173(LongSparseArray longSparseArray) {
        SQLiteCursor sQLiteCursor;
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLitePreparedStatement sQLitePreparedStatement2;
        SQLiteDatabase sQLiteDatabase;
        SQLiteDatabase sQLiteDatabase2;
        final ArrayList arrayList;
        int i;
        try {
            arrayList = new ArrayList();
            int size = longSparseArray.size();
            int i2 = 0;
            int i3 = 0;
            while (true) {
                int i4 = 2;
                if (i3 >= size) {
                    break;
                }
                SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT mid, uid FROM webpage_pending_v2 WHERE id = " + longSparseArray.keyAt(i3), new Object[i2]);
                try {
                    LongSparseArray longSparseArray2 = new LongSparseArray();
                    while (queryFinalized.next()) {
                        long longValue = queryFinalized.longValue(1);
                        ArrayList arrayList2 = (ArrayList) longSparseArray2.get(longValue);
                        if (arrayList2 == null) {
                            arrayList2 = new ArrayList();
                            longSparseArray2.put(longValue, arrayList2);
                        }
                        arrayList2.add(Integer.valueOf(queryFinalized.intValue(i2)));
                    }
                    queryFinalized.dispose();
                    if (!longSparseArray2.isEmpty()) {
                        int size2 = longSparseArray2.size();
                        int i5 = i2;
                        while (i5 < size2) {
                            long keyAt = longSparseArray2.keyAt(i5);
                            ArrayList arrayList3 = (ArrayList) longSparseArray2.valueAt(i5);
                            SQLiteDatabase sQLiteDatabase3 = this.database;
                            Locale locale = Locale.US;
                            Object[] objArr = new Object[i4];
                            objArr[0] = TextUtils.join(",", arrayList3);
                            objArr[1] = Long.valueOf(keyAt);
                            sQLiteCursor = sQLiteDatabase3.queryFinalized(String.format(locale, "SELECT mid, data FROM messages_v2 WHERE mid IN (%s) AND uid = %d", objArr), new Object[0]);
                            while (sQLiteCursor.next()) {
                                try {
                                    int intValue = sQLiteCursor.intValue(0);
                                    NativeByteBuffer byteBufferValue = sQLiteCursor.byteBufferValue(1);
                                    if (byteBufferValue != null) {
                                        TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                                        TLdeserialize.readAttachPath(byteBufferValue, getUserConfig().clientUserId);
                                        byteBufferValue.reuse();
                                        TLRPC$MessageMedia tLRPC$MessageMedia = TLdeserialize.media;
                                        if (tLRPC$MessageMedia instanceof TLRPC$TL_messageMediaWebPage) {
                                            TLdeserialize.id = intValue;
                                            tLRPC$MessageMedia.webpage = (TLRPC$WebPage) longSparseArray.valueAt(i3);
                                            arrayList.add(TLdeserialize);
                                        }
                                    }
                                } catch (Exception e) {
                                    e = e;
                                    sQLitePreparedStatement = null;
                                    sQLitePreparedStatement2 = null;
                                    try {
                                        checkSQLException(e);
                                        if (sQLiteCursor != null) {
                                        }
                                        if (sQLitePreparedStatement2 != null) {
                                        }
                                        if (sQLitePreparedStatement != null) {
                                        }
                                        sQLiteDatabase2 = this.database;
                                        if (sQLiteDatabase2 == null) {
                                        }
                                        sQLiteDatabase2.commitTransaction();
                                    } catch (Throwable th) {
                                        th = th;
                                        if (sQLiteCursor != null) {
                                            sQLiteCursor.dispose();
                                        }
                                        if (sQLitePreparedStatement2 != null) {
                                            sQLitePreparedStatement2.dispose();
                                        }
                                        if (sQLitePreparedStatement != null) {
                                            sQLitePreparedStatement.dispose();
                                        }
                                        sQLiteDatabase = this.database;
                                        if (sQLiteDatabase != null) {
                                            sQLiteDatabase.commitTransaction();
                                        }
                                        throw th;
                                    }
                                } catch (Throwable th2) {
                                    th = th2;
                                    sQLitePreparedStatement = null;
                                    sQLitePreparedStatement2 = null;
                                    if (sQLiteCursor != null) {
                                    }
                                    if (sQLitePreparedStatement2 != null) {
                                    }
                                    if (sQLitePreparedStatement != null) {
                                    }
                                    sQLiteDatabase = this.database;
                                    if (sQLiteDatabase != null) {
                                    }
                                    throw th;
                                }
                            }
                            sQLiteCursor.dispose();
                            i5++;
                            i2 = 0;
                            i4 = 2;
                        }
                    }
                    i3++;
                    i2 = i2;
                } catch (Exception e2) {
                    e = e2;
                    sQLiteCursor = queryFinalized;
                } catch (Throwable th3) {
                    th = th3;
                    sQLiteCursor = queryFinalized;
                }
            }
            i = i2;
        } catch (Exception e3) {
            e = e3;
            sQLiteCursor = null;
        } catch (Throwable th4) {
            th = th4;
            sQLiteCursor = null;
        }
        if (arrayList.isEmpty()) {
            SQLiteDatabase sQLiteDatabase4 = this.database;
            if (sQLiteDatabase4 != null) {
                sQLiteDatabase4.commitTransaction();
                return;
            }
            return;
        }
        this.database.beginTransaction();
        SQLitePreparedStatement executeFast = this.database.executeFast("UPDATE messages_v2 SET data = ? WHERE mid = ? AND uid = ?");
        try {
            sQLitePreparedStatement = this.database.executeFast("UPDATE media_v4 SET data = ? WHERE mid = ? AND uid = ?");
            for (int i6 = i; i6 < arrayList.size(); i6++) {
                try {
                    TLRPC$Message tLRPC$Message = (TLRPC$Message) arrayList.get(i6);
                    MessageObject.normalizeFlags(tLRPC$Message);
                    NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$Message.getObjectSize());
                    tLRPC$Message.serializeToStream(nativeByteBuffer);
                    executeFast.requery();
                    executeFast.bindByteBuffer(1, nativeByteBuffer);
                    executeFast.bindInteger(2, tLRPC$Message.id);
                    executeFast.bindLong(3, MessageObject.getDialogId(tLRPC$Message));
                    executeFast.step();
                    sQLitePreparedStatement.requery();
                    sQLitePreparedStatement.bindByteBuffer(1, nativeByteBuffer);
                    sQLitePreparedStatement.bindInteger(2, tLRPC$Message.id);
                    sQLitePreparedStatement.bindLong(3, MessageObject.getDialogId(tLRPC$Message));
                    sQLitePreparedStatement.step();
                    nativeByteBuffer.reuse();
                } catch (Exception e4) {
                    e = e4;
                    sQLitePreparedStatement2 = executeFast;
                    sQLiteCursor = null;
                    checkSQLException(e);
                    if (sQLiteCursor != null) {
                        sQLiteCursor.dispose();
                    }
                    if (sQLitePreparedStatement2 != null) {
                        sQLitePreparedStatement2.dispose();
                    }
                    if (sQLitePreparedStatement != null) {
                        sQLitePreparedStatement.dispose();
                    }
                    sQLiteDatabase2 = this.database;
                    if (sQLiteDatabase2 == null) {
                        return;
                    }
                    sQLiteDatabase2.commitTransaction();
                } catch (Throwable th5) {
                    th = th5;
                    sQLitePreparedStatement2 = executeFast;
                    sQLiteCursor = null;
                    if (sQLiteCursor != null) {
                    }
                    if (sQLitePreparedStatement2 != null) {
                    }
                    if (sQLitePreparedStatement != null) {
                    }
                    sQLiteDatabase = this.database;
                    if (sQLiteDatabase != null) {
                    }
                    throw th;
                }
            }
            executeFast.dispose();
        } catch (Exception e5) {
            e = e5;
            sQLitePreparedStatement2 = executeFast;
            sQLiteCursor = null;
            sQLitePreparedStatement = null;
        } catch (Throwable th6) {
            th = th6;
            sQLitePreparedStatement2 = executeFast;
            sQLiteCursor = null;
            sQLitePreparedStatement = null;
        }
        try {
            sQLitePreparedStatement.dispose();
            this.database.commitTransaction();
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$putWebPages$172(arrayList);
                }
            });
            sQLiteDatabase2 = this.database;
            if (sQLiteDatabase2 == null) {
                return;
            }
        } catch (Exception e6) {
            e = e6;
            sQLiteCursor = null;
            sQLitePreparedStatement2 = null;
            checkSQLException(e);
            if (sQLiteCursor != null) {
            }
            if (sQLitePreparedStatement2 != null) {
            }
            if (sQLitePreparedStatement != null) {
            }
            sQLiteDatabase2 = this.database;
            if (sQLiteDatabase2 == null) {
            }
            sQLiteDatabase2.commitTransaction();
        } catch (Throwable th7) {
            th = th7;
            sQLiteCursor = null;
            sQLitePreparedStatement2 = null;
            if (sQLiteCursor != null) {
            }
            if (sQLitePreparedStatement2 != null) {
            }
            if (sQLitePreparedStatement != null) {
            }
            sQLiteDatabase = this.database;
            if (sQLiteDatabase != null) {
            }
            throw th;
        }
        sQLiteDatabase2.commitTransaction();
    }

    public void lambda$putWebPages$172(ArrayList arrayList) {
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.didReceivedWebpages, arrayList);
    }

    public void overwriteChannel(final long j, final TLRPC$TL_updates_channelDifferenceTooLong tLRPC$TL_updates_channelDifferenceTooLong, final int i, final Runnable runnable) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$overwriteChannel$175(j, i, tLRPC$TL_updates_channelDifferenceTooLong, runnable);
            }
        });
    }

    public void lambda$overwriteChannel$175(long j, int i, final TLRPC$TL_updates_channelDifferenceTooLong tLRPC$TL_updates_channelDifferenceTooLong, Runnable runnable) {
        SQLiteCursor sQLiteCursor;
        SQLiteCursor queryFinalized;
        int intValue;
        boolean z;
        final long j2 = -j;
        try {
            queryFinalized = this.database.queryFinalized("SELECT pinned FROM dialogs WHERE did = " + j2, new Object[0]);
        } catch (Exception e) {
            e = e;
            sQLiteCursor = null;
        } catch (Throwable th) {
            th = th;
            sQLiteCursor = null;
        }
        try {
            if (queryFinalized.next()) {
                intValue = queryFinalized.intValue(0);
                z = false;
            } else if (i != 0) {
                z = true;
                intValue = 0;
            } else {
                intValue = 0;
                z = false;
            }
            queryFinalized.dispose();
            this.database.executeFast("DELETE FROM chat_pinned_count WHERE uid = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM chat_pinned_v2 WHERE uid = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM messages_v2 WHERE uid = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM bot_keyboard WHERE uid = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM bot_keyboard_topics WHERE uid = " + j2).stepThis().dispose();
            this.database.executeFast("UPDATE media_counts_v2 SET old = 1 WHERE uid = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM media_v4 WHERE uid = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM messages_holes WHERE uid = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM media_holes_v2 WHERE uid = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM topics WHERE did = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM media_topics WHERE uid = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM media_holes_topics WHERE uid = " + j2).stepThis().dispose();
            this.database.executeFast("UPDATE media_counts_topics SET old = 1 WHERE uid = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM messages_topics WHERE uid = " + j2).stepThis().dispose();
            this.database.executeFast("DELETE FROM messages_holes_topics WHERE uid = " + j2).stepThis().dispose();
            getMediaDataController().clearBotKeyboard(j2);
            TLRPC$TL_messages_dialogs tLRPC$TL_messages_dialogs = new TLRPC$TL_messages_dialogs();
            tLRPC$TL_messages_dialogs.chats.addAll(tLRPC$TL_updates_channelDifferenceTooLong.chats);
            tLRPC$TL_messages_dialogs.users.addAll(tLRPC$TL_updates_channelDifferenceTooLong.users);
            tLRPC$TL_messages_dialogs.messages.addAll(tLRPC$TL_updates_channelDifferenceTooLong.messages);
            TLRPC$Dialog tLRPC$Dialog = tLRPC$TL_updates_channelDifferenceTooLong.dialog;
            tLRPC$Dialog.id = j2;
            tLRPC$Dialog.flags = 1;
            tLRPC$Dialog.notify_settings = null;
            tLRPC$Dialog.pinned = intValue != 0;
            tLRPC$Dialog.pinnedNum = intValue;
            tLRPC$TL_messages_dialogs.dialogs.add(tLRPC$Dialog);
            putDialogsInternal(tLRPC$TL_messages_dialogs, 0);
            updateDialogsWithDeletedMessages(j2, j, new ArrayList<>(), null, false);
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$overwriteChannel$174(j2, tLRPC$TL_updates_channelDifferenceTooLong);
                }
            });
            if (z) {
                if (i == 1) {
                    getMessagesController().checkChatInviter(j, true);
                } else {
                    getMessagesController().generateJoinMessage(j, false);
                }
            }
            if (i != 1) {
                getMessagesController().getTopicsController().reloadTopics(j);
            }
        } catch (Exception e2) {
            e = e2;
            sQLiteCursor = queryFinalized;
            try {
                checkSQLException(e);
                if (sQLiteCursor != null) {
                    sQLiteCursor.dispose();
                }
                if (runnable == null) {
                }
            } catch (Throwable th2) {
                th = th2;
                if (sQLiteCursor != null) {
                    sQLiteCursor.dispose();
                }
                throw th;
            }
        } catch (Throwable th3) {
            th = th3;
            sQLiteCursor = queryFinalized;
            if (sQLiteCursor != null) {
            }
            throw th;
        }
        if (runnable == null) {
            runnable.run();
        }
    }

    public void lambda$overwriteChannel$174(long j, TLRPC$TL_updates_channelDifferenceTooLong tLRPC$TL_updates_channelDifferenceTooLong) {
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.removeAllMessagesFromDialog, Long.valueOf(j), Boolean.TRUE, tLRPC$TL_updates_channelDifferenceTooLong);
    }

    public void putChannelViews(final LongSparseArray<SparseIntArray> longSparseArray, final LongSparseArray<SparseIntArray> longSparseArray2, final LongSparseArray<SparseArray<TLRPC$MessageReplies>> longSparseArray3, final boolean z) {
        if (isEmpty(longSparseArray) && isEmpty(longSparseArray2) && isEmpty(longSparseArray3)) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$putChannelViews$176(longSparseArray, longSparseArray2, longSparseArray3, z);
            }
        });
    }

    public void lambda$putChannelViews$176(LongSparseArray longSparseArray, LongSparseArray longSparseArray2, LongSparseArray longSparseArray3, boolean z) {
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLiteDatabase sQLiteDatabase;
        SQLiteDatabase sQLiteDatabase2;
        char c;
        TLRPC$MessageReplies TLdeserialize;
        char c2;
        int i;
        int i2;
        LongSparseArray longSparseArray4 = longSparseArray3;
        char c3 = 1;
        try {
            this.database.beginTransaction();
            try {
                char c4 = 3;
                int i3 = 2;
                if (!isEmpty((LongSparseArray<?>) longSparseArray)) {
                    try {
                        SQLitePreparedStatement executeFast = this.database.executeFast("UPDATE messages_v2 SET media = max((SELECT media FROM messages_v2 WHERE mid = ? AND uid = ?), ?) WHERE mid = ? AND uid = ?");
                        for (int i4 = 0; i4 < longSparseArray.size(); i4++) {
                            try {
                                long keyAt = longSparseArray.keyAt(i4);
                                SparseIntArray sparseIntArray = (SparseIntArray) longSparseArray.valueAt(i4);
                                int size = sparseIntArray.size();
                                for (int i5 = 0; i5 < size; i5++) {
                                    int valueAt = sparseIntArray.valueAt(i5);
                                    int keyAt2 = sparseIntArray.keyAt(i5);
                                    executeFast.requery();
                                    executeFast.bindInteger(1, keyAt2);
                                    executeFast.bindLong(2, keyAt);
                                    executeFast.bindInteger(3, valueAt);
                                    executeFast.bindInteger(4, keyAt2);
                                    executeFast.bindLong(5, keyAt);
                                    executeFast.step();
                                }
                            } catch (Exception e) {
                                e = e;
                                sQLitePreparedStatement = executeFast;
                                checkSQLException(e);
                                if (c3 != 0) {
                                }
                                if (sQLitePreparedStatement == null) {
                                }
                            } catch (Throwable th) {
                                th = th;
                                sQLitePreparedStatement = executeFast;
                                if (c3 != 0) {
                                }
                                if (sQLitePreparedStatement != null) {
                                }
                                throw th;
                            }
                        }
                        executeFast.dispose();
                    } catch (Exception e2) {
                        e = e2;
                        sQLitePreparedStatement = null;
                        checkSQLException(e);
                        if (c3 != 0) {
                        }
                        if (sQLitePreparedStatement == null) {
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        sQLitePreparedStatement = null;
                        if (c3 != 0) {
                        }
                        if (sQLitePreparedStatement != null) {
                        }
                        throw th;
                    }
                }
                if (!isEmpty((LongSparseArray<?>) longSparseArray2)) {
                    sQLitePreparedStatement = this.database.executeFast("UPDATE messages_v2 SET forwards = max((SELECT forwards FROM messages_v2 WHERE mid = ? AND uid = ?), ?) WHERE mid = ? AND uid = ?");
                    for (int i6 = 0; i6 < longSparseArray2.size(); i6++) {
                        try {
                            try {
                                long keyAt3 = longSparseArray2.keyAt(i6);
                                SparseIntArray sparseIntArray2 = (SparseIntArray) longSparseArray2.valueAt(i6);
                                int size2 = sparseIntArray2.size();
                                for (int i7 = 0; i7 < size2; i7++) {
                                    int valueAt2 = sparseIntArray2.valueAt(i7);
                                    int keyAt4 = sparseIntArray2.keyAt(i7);
                                    sQLitePreparedStatement.requery();
                                    sQLitePreparedStatement.bindInteger(1, keyAt4);
                                    sQLitePreparedStatement.bindLong(2, keyAt3);
                                    sQLitePreparedStatement.bindInteger(3, valueAt2);
                                    sQLitePreparedStatement.bindInteger(4, keyAt4);
                                    sQLitePreparedStatement.bindLong(5, keyAt3);
                                    sQLitePreparedStatement.step();
                                }
                            } catch (Exception e3) {
                                e = e3;
                                checkSQLException(e);
                                if (c3 != 0 && (sQLiteDatabase2 = this.database) != null) {
                                    sQLiteDatabase2.commitTransaction();
                                }
                                if (sQLitePreparedStatement == null) {
                                    sQLitePreparedStatement.dispose();
                                    return;
                                }
                                return;
                            }
                        } catch (Throwable th3) {
                            th = th3;
                            if (c3 != 0 && (sQLiteDatabase = this.database) != null) {
                                sQLiteDatabase.commitTransaction();
                            }
                            if (sQLitePreparedStatement != null) {
                                sQLitePreparedStatement.dispose();
                            }
                            throw th;
                        }
                    }
                    sQLitePreparedStatement.dispose();
                }
                if (isEmpty((LongSparseArray<?>) longSparseArray3)) {
                    c = 1;
                } else {
                    SQLitePreparedStatement executeFast2 = this.database.executeFast("UPDATE messages_v2 SET replies_data = ? WHERE mid = ? AND uid = ?");
                    int i8 = 0;
                    while (i8 < longSparseArray3.size()) {
                        try {
                            long keyAt5 = longSparseArray4.keyAt(i8);
                            SparseArray sparseArray = (SparseArray) longSparseArray4.valueAt(i8);
                            int size3 = sparseArray.size();
                            int i9 = 0;
                            while (i9 < size3) {
                                int keyAt6 = sparseArray.keyAt(i9);
                                SQLiteDatabase sQLiteDatabase3 = this.database;
                                Locale locale = Locale.US;
                                Object[] objArr = new Object[i3];
                                objArr[0] = Integer.valueOf(keyAt6);
                                objArr[c3] = Long.valueOf(keyAt5);
                                SQLiteCursor queryFinalized = sQLiteDatabase3.queryFinalized(String.format(locale, "SELECT replies_data FROM messages_v2 WHERE mid = %d AND uid = %d", objArr), new Object[0]);
                                boolean next = queryFinalized.next();
                                if (next) {
                                    try {
                                        NativeByteBuffer byteBufferValue = queryFinalized.byteBufferValue(0);
                                        if (byteBufferValue != null) {
                                            TLdeserialize = TLRPC$MessageReplies.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                                            byteBufferValue.reuse();
                                            queryFinalized.dispose();
                                            if (next) {
                                                TLRPC$MessageReplies tLRPC$MessageReplies = (TLRPC$MessageReplies) sparseArray.get(sparseArray.keyAt(i9));
                                                if (z || TLdeserialize == null || (i2 = TLdeserialize.replies_pts) == 0 || tLRPC$MessageReplies.replies_pts > i2 || tLRPC$MessageReplies.read_max_id > TLdeserialize.read_max_id || tLRPC$MessageReplies.max_id > TLdeserialize.max_id) {
                                                    if (z) {
                                                        if (TLdeserialize == null) {
                                                            TLRPC$TL_messageReplies tLRPC$TL_messageReplies = new TLRPC$TL_messageReplies();
                                                            tLRPC$TL_messageReplies.flags |= 2;
                                                            TLdeserialize = tLRPC$TL_messageReplies;
                                                        }
                                                        try {
                                                            TLdeserialize.replies += tLRPC$MessageReplies.replies;
                                                            int size4 = tLRPC$MessageReplies.recent_repliers.size();
                                                            for (int i10 = 0; i10 < size4; i10++) {
                                                                long peerId = MessageObject.getPeerId(tLRPC$MessageReplies.recent_repliers.get(i10));
                                                                int size5 = TLdeserialize.recent_repliers.size();
                                                                int i11 = 0;
                                                                while (i11 < size5) {
                                                                    if (peerId == MessageObject.getPeerId(TLdeserialize.recent_repliers.get(i11))) {
                                                                        TLdeserialize.recent_repliers.remove(i11);
                                                                        i11--;
                                                                        size5--;
                                                                    }
                                                                    i11++;
                                                                }
                                                            }
                                                            TLdeserialize.recent_repliers.addAll(0, tLRPC$MessageReplies.recent_repliers);
                                                            while (TLdeserialize.recent_repliers.size() > 3) {
                                                                TLdeserialize.recent_repliers.remove(0);
                                                            }
                                                            tLRPC$MessageReplies = TLdeserialize;
                                                        } catch (Exception e4) {
                                                            e = e4;
                                                            sQLitePreparedStatement = executeFast2;
                                                            c3 = 1;
                                                            checkSQLException(e);
                                                            if (c3 != 0) {
                                                            }
                                                            if (sQLitePreparedStatement == null) {
                                                            }
                                                        } catch (Throwable th4) {
                                                            th = th4;
                                                            sQLitePreparedStatement = executeFast2;
                                                            c3 = 1;
                                                            if (c3 != 0) {
                                                            }
                                                            if (sQLitePreparedStatement != null) {
                                                            }
                                                            throw th;
                                                        }
                                                    }
                                                    if (TLdeserialize != null && (i = TLdeserialize.read_max_id) > tLRPC$MessageReplies.read_max_id) {
                                                        tLRPC$MessageReplies.read_max_id = i;
                                                    }
                                                    try {
                                                        executeFast2.requery();
                                                        NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$MessageReplies.getObjectSize());
                                                        tLRPC$MessageReplies.serializeToStream(nativeByteBuffer);
                                                        c = 1;
                                                        try {
                                                            executeFast2.bindByteBuffer(1, nativeByteBuffer);
                                                            i3 = 2;
                                                            executeFast2.bindInteger(2, keyAt6);
                                                            c2 = 3;
                                                            executeFast2.bindLong(3, keyAt5);
                                                            executeFast2.step();
                                                            nativeByteBuffer.reuse();
                                                            i9++;
                                                            c3 = c;
                                                            c4 = c2;
                                                        } catch (Exception e5) {
                                                            e = e5;
                                                            sQLitePreparedStatement = executeFast2;
                                                            c3 = c;
                                                            checkSQLException(e);
                                                            if (c3 != 0) {
                                                                sQLiteDatabase2.commitTransaction();
                                                            }
                                                            if (sQLitePreparedStatement == null) {
                                                            }
                                                        } catch (Throwable th5) {
                                                            th = th5;
                                                            sQLitePreparedStatement = executeFast2;
                                                            c3 = c;
                                                            if (c3 != 0) {
                                                                sQLiteDatabase.commitTransaction();
                                                            }
                                                            if (sQLitePreparedStatement != null) {
                                                            }
                                                            throw th;
                                                        }
                                                    } catch (Exception e6) {
                                                        e = e6;
                                                        c = 1;
                                                        sQLitePreparedStatement = executeFast2;
                                                        c3 = c;
                                                        checkSQLException(e);
                                                        if (c3 != 0) {
                                                        }
                                                        if (sQLitePreparedStatement == null) {
                                                        }
                                                    } catch (Throwable th6) {
                                                        th = th6;
                                                        c = 1;
                                                        sQLitePreparedStatement = executeFast2;
                                                        c3 = c;
                                                        if (c3 != 0) {
                                                        }
                                                        if (sQLitePreparedStatement != null) {
                                                        }
                                                        throw th;
                                                    }
                                                }
                                            }
                                            c = c3;
                                            i3 = 2;
                                            c2 = 3;
                                            i9++;
                                            c3 = c;
                                            c4 = c2;
                                        }
                                    } catch (Exception e7) {
                                        e = e7;
                                        sQLitePreparedStatement = executeFast2;
                                        checkSQLException(e);
                                        if (c3 != 0) {
                                        }
                                        if (sQLitePreparedStatement == null) {
                                        }
                                    } catch (Throwable th7) {
                                        th = th7;
                                        sQLitePreparedStatement = executeFast2;
                                        if (c3 != 0) {
                                        }
                                        if (sQLitePreparedStatement != null) {
                                        }
                                        throw th;
                                    }
                                }
                                TLdeserialize = null;
                                queryFinalized.dispose();
                                if (next) {
                                }
                                c = c3;
                                i3 = 2;
                                c2 = 3;
                                i9++;
                                c3 = c;
                                c4 = c2;
                            }
                            i8++;
                            longSparseArray4 = longSparseArray3;
                            c3 = c3;
                            c4 = c4;
                        } catch (Exception e8) {
                            e = e8;
                        } catch (Throwable th8) {
                            th = th8;
                        }
                    }
                    c = c3;
                    executeFast2.dispose();
                }
            } catch (Exception e9) {
                e = e9;
            } catch (Throwable th9) {
                th = th9;
            }
        } catch (Exception e10) {
            e = e10;
            c3 = 0;
        } catch (Throwable th10) {
            th = th10;
            c3 = 0;
        }
        try {
            this.database.commitTransaction();
        } catch (Exception e11) {
            e = e11;
            c3 = c;
            sQLitePreparedStatement = null;
            checkSQLException(e);
            if (c3 != 0) {
            }
            if (sQLitePreparedStatement == null) {
            }
        } catch (Throwable th11) {
            th = th11;
            c3 = c;
            sQLitePreparedStatement = null;
            if (c3 != 0) {
            }
            if (sQLitePreparedStatement != null) {
            }
            throw th;
        }
    }

    public void lambda$updateRepliesMaxReadId$178(final long j, final int i, final int i2, int i3) {
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLiteCursor sQLiteCursor;
        final int i4;
        int i5;
        SQLiteCursor queryFinalized;
        TLRPC$MessageReplies tLRPC$MessageReplies;
        NativeByteBuffer byteBufferValue;
        long j2 = -j;
        try {
            if (!isForum(j2)) {
                SQLitePreparedStatement executeFast = this.database.executeFast("UPDATE messages_v2 SET replies_data = ? WHERE mid = ? AND uid = ?");
                try {
                    queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT replies_data FROM messages_v2 WHERE mid = %d AND uid = %d", Integer.valueOf(i), Long.valueOf(j2)), new Object[0]);
                } catch (Exception e) {
                    e = e;
                    sQLitePreparedStatement = executeFast;
                    sQLiteCursor = null;
                    try {
                        checkSQLException(e);
                        if (sQLitePreparedStatement != null) {
                        }
                        if (sQLiteCursor != null) {
                        }
                    } catch (Throwable th) {
                        th = th;
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        throw th;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    sQLitePreparedStatement = executeFast;
                    sQLiteCursor = null;
                    if (sQLitePreparedStatement != null) {
                    }
                    if (sQLiteCursor != null) {
                    }
                    throw th;
                }
                try {
                    if (!queryFinalized.next() || (byteBufferValue = queryFinalized.byteBufferValue(0)) == null) {
                        tLRPC$MessageReplies = null;
                    } else {
                        tLRPC$MessageReplies = TLRPC$MessageReplies.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                        byteBufferValue.reuse();
                    }
                    queryFinalized.dispose();
                    if (tLRPC$MessageReplies != null) {
                        tLRPC$MessageReplies.read_max_id = i2;
                        executeFast.requery();
                        NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$MessageReplies.getObjectSize());
                        tLRPC$MessageReplies.serializeToStream(nativeByteBuffer);
                        executeFast.bindByteBuffer(1, nativeByteBuffer);
                        executeFast.bindInteger(2, i);
                        executeFast.bindLong(3, j2);
                        executeFast.step();
                        nativeByteBuffer.reuse();
                    }
                    executeFast.dispose();
                } catch (Exception e2) {
                    e = e2;
                    sQLiteCursor = queryFinalized;
                    sQLitePreparedStatement = executeFast;
                    checkSQLException(e);
                    if (sQLitePreparedStatement != null) {
                    }
                    if (sQLiteCursor != null) {
                    }
                } catch (Throwable th3) {
                    th = th3;
                    sQLiteCursor = queryFinalized;
                    sQLitePreparedStatement = executeFast;
                    if (sQLitePreparedStatement != null) {
                    }
                    if (sQLiteCursor != null) {
                    }
                    throw th;
                }
            }
            SQLiteDatabase sQLiteDatabase = this.database;
            Locale locale = Locale.US;
            SQLiteCursor queryFinalized2 = sQLiteDatabase.queryFinalized(String.format(locale, "SELECT max_read_id FROM topics WHERE did = %d AND topic_id = %d", Long.valueOf(j2), Integer.valueOf(i)), new Object[0]);
            try {
                boolean z = queryFinalized2.next() && i2 >= queryFinalized2.intValue(0);
                queryFinalized2.dispose();
                this.database.executeFast(String.format(locale, "UPDATE messages_topics SET read_state = read_state | 1 WHERE uid = %d AND topic_id = %d AND mid <= %d AND read_state IN(0, 2) AND out = 0", Long.valueOf(j2), Integer.valueOf(i), Integer.valueOf(i2))).stepThis().dispose();
                this.database.executeFast(String.format(locale, "UPDATE messages_topics SET read_state = read_state | 2 WHERE uid = %d AND topic_id = %d AND mid <= %d AND read_state IN(0, 1) AND out = 0", Long.valueOf(j2), Integer.valueOf(i), Integer.valueOf(i2))).stepThis().dispose();
                if (i3 < 0) {
                    SQLiteCursor queryFinalized3 = this.database.queryFinalized(String.format(locale, "SELECT count(mid) FROM messages_topics WHERE uid = %d AND topic_id = %d AND mid > %d AND read_state IN(0, 2) AND out = 0", Long.valueOf(j2), Integer.valueOf(i), Integer.valueOf(i2)), new Object[0]);
                    int intValue = queryFinalized3.next() ? queryFinalized3.intValue(0) : 0;
                    queryFinalized3.dispose();
                    if (intValue == 0) {
                        i5 = 0;
                    } else {
                        SQLiteCursor queryFinalized4 = this.database.queryFinalized(String.format(locale, "SELECT count(mid) FROM messages_topics WHERE uid = %d AND topic_id = %d AND mid > %d AND read_state IN(0, 1) AND out = 0", Long.valueOf(j2), Integer.valueOf(i), Integer.valueOf(i2)), new Object[0]);
                        int intValue2 = queryFinalized4.next() ? queryFinalized4.intValue(0) : -1;
                        queryFinalized4.dispose();
                        queryFinalized2 = this.database.queryFinalized(String.format(locale, "SELECT unread_mentions FROM topics WHERE did = %d AND topic_id = %d", Long.valueOf(j2), Integer.valueOf(i)), new Object[0]);
                        int intValue3 = queryFinalized2.next() ? queryFinalized2.intValue(0) : 0;
                        queryFinalized2.dispose();
                        if (intValue2 > intValue3) {
                            intValue2 = intValue3;
                        }
                        i5 = intValue2;
                    }
                    i4 = intValue;
                } else {
                    i4 = i3;
                    i5 = i3 == 0 ? 0 : -1;
                }
                if (z) {
                    if (i5 >= 0) {
                        if (BuildVars.DEBUG_PRIVATE_VERSION && i5 > 0) {
                            FileLog.d("(updateRepliesMaxReadIdInternal) new unread mentions " + i5 + " for dialog_id=" + j2 + " topic_id=" + i);
                        }
                        this.database.executeFast(String.format(Locale.ENGLISH, "UPDATE topics SET max_read_id = %d, unread_count = %d, unread_mentions = %d WHERE did = %d AND topic_id = %d", Integer.valueOf(i2), Integer.valueOf(i4), Integer.valueOf(i5), Long.valueOf(j2), Integer.valueOf(i))).stepThis().dispose();
                    } else {
                        this.database.executeFast(String.format(Locale.ENGLISH, "UPDATE topics SET max_read_id = %d, unread_count = %d WHERE did = %d AND topic_id = %d", Integer.valueOf(i2), Integer.valueOf(i4), Long.valueOf(j2), Integer.valueOf(i))).stepThis().dispose();
                    }
                    final int i6 = i5;
                    AndroidUtilities.runOnUIThread(new Runnable() {
                        @Override
                        public final void run() {
                            MessagesStorage.this.lambda$updateRepliesMaxReadIdInternal$177(j, i, i2, i4, i6);
                        }
                    });
                    resetForumBadgeIfNeed(j2);
                }
            } catch (Exception e3) {
                e = e3;
                sQLiteCursor = queryFinalized2;
                sQLitePreparedStatement = null;
                checkSQLException(e);
                if (sQLitePreparedStatement != null) {
                    sQLitePreparedStatement.dispose();
                }
                if (sQLiteCursor != null) {
                    sQLiteCursor.dispose();
                }
            } catch (Throwable th4) {
                th = th4;
                sQLiteCursor = queryFinalized2;
                sQLitePreparedStatement = null;
                if (sQLitePreparedStatement != null) {
                }
                if (sQLiteCursor != null) {
                }
                throw th;
            }
        } catch (Exception e4) {
            e = e4;
            sQLitePreparedStatement = null;
        } catch (Throwable th5) {
            th = th5;
            sQLitePreparedStatement = null;
        }
    }

    public void lambda$updateRepliesMaxReadIdInternal$177(long j, int i, int i2, int i3, int i4) {
        getMessagesController().getTopicsController().updateMaxReadId(j, i, i2, i3, i4);
    }

    private void resetForumBadgeIfNeed(long j) {
        LongSparseIntArray longSparseIntArray;
        SQLiteCursor sQLiteCursor = null;
        try {
            SQLiteDatabase sQLiteDatabase = this.database;
            Locale locale = Locale.ENGLISH;
            SQLiteCursor queryFinalized = sQLiteDatabase.queryFinalized(String.format(locale, "SELECT topic_id FROM topics WHERE did = %d AND unread_count > 0", Long.valueOf(j)), new Object[0]);
            try {
                if (queryFinalized.next()) {
                    longSparseIntArray = null;
                } else {
                    longSparseIntArray = new LongSparseIntArray();
                    longSparseIntArray.put(j, 0);
                }
                queryFinalized.dispose();
                if (longSparseIntArray != null) {
                    this.database.executeFast(String.format(locale, "UPDATE dialogs SET unread_count = 0, unread_count_i = 0 WHERE did = %d", Long.valueOf(j))).stepThis().dispose();
                }
                updateFiltersReadCounter(longSparseIntArray, null, true);
                getMessagesController().processDialogsUpdateRead(longSparseIntArray, null);
            } catch (Throwable th) {
                th = th;
                sQLiteCursor = queryFinalized;
                try {
                    checkSQLException(th);
                } finally {
                    if (sQLiteCursor != null) {
                        sQLiteCursor.dispose();
                    }
                }
            }
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public void updateRepliesMaxReadId(final long j, final int i, final int i2, final int i3, boolean z) {
        if (z) {
            this.storageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$updateRepliesMaxReadId$178(j, i, i2, i3);
                }
            });
        } else {
            lambda$updateRepliesMaxReadId$178(j, i, i2, i3);
        }
    }

    public void updateRepliesCount(final long j, final int i, final ArrayList<TLRPC$Peer> arrayList, final int i2, final int i3) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateRepliesCount$179(i, j, i3, arrayList, i2);
            }
        });
    }

    public void lambda$updateRepliesCount$179(int i, long j, int i2, ArrayList arrayList, int i3) {
        SQLiteCursor sQLiteCursor;
        long j2;
        TLRPC$MessageReplies tLRPC$MessageReplies;
        NativeByteBuffer byteBufferValue;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            SQLitePreparedStatement executeFast = this.database.executeFast("UPDATE messages_v2 SET replies_data = ? WHERE mid = ? AND uid = ?");
            try {
                j2 = -j;
                sQLiteCursor = this.database.queryFinalized(String.format(Locale.ENGLISH, "SELECT replies_data FROM messages_v2 WHERE mid = %d AND uid = %d", Integer.valueOf(i), Long.valueOf(j2)), new Object[0]);
            } catch (Exception e) {
                e = e;
                sQLiteCursor = null;
            } catch (Throwable th) {
                th = th;
                sQLiteCursor = null;
            }
            try {
                if (!sQLiteCursor.next() || (byteBufferValue = sQLiteCursor.byteBufferValue(0)) == null) {
                    tLRPC$MessageReplies = null;
                } else {
                    tLRPC$MessageReplies = TLRPC$MessageReplies.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                    byteBufferValue.reuse();
                }
                sQLiteCursor.dispose();
                if (tLRPC$MessageReplies != null) {
                    int i4 = tLRPC$MessageReplies.replies + i2;
                    tLRPC$MessageReplies.replies = i4;
                    if (i4 < 0) {
                        tLRPC$MessageReplies.replies = 0;
                    }
                    if (arrayList != null) {
                        tLRPC$MessageReplies.recent_repliers = arrayList;
                        tLRPC$MessageReplies.flags |= 2;
                    }
                    if (i3 != 0) {
                        tLRPC$MessageReplies.max_id = i3;
                    }
                    executeFast.requery();
                    NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$MessageReplies.getObjectSize());
                    tLRPC$MessageReplies.serializeToStream(nativeByteBuffer);
                    executeFast.bindByteBuffer(1, nativeByteBuffer);
                    executeFast.bindInteger(2, i);
                    executeFast.bindLong(3, j2);
                    executeFast.step();
                    nativeByteBuffer.reuse();
                }
                executeFast.dispose();
            } catch (Exception e2) {
                e = e2;
                sQLitePreparedStatement = executeFast;
                try {
                    checkSQLException(e);
                    if (sQLitePreparedStatement != null) {
                        sQLitePreparedStatement.dispose();
                    }
                    if (sQLiteCursor != null) {
                        sQLiteCursor.dispose();
                    }
                } catch (Throwable th2) {
                    th = th2;
                    if (sQLitePreparedStatement != null) {
                        sQLitePreparedStatement.dispose();
                    }
                    if (sQLiteCursor != null) {
                        sQLiteCursor.dispose();
                    }
                    throw th;
                }
            } catch (Throwable th3) {
                th = th3;
                sQLitePreparedStatement = executeFast;
                if (sQLitePreparedStatement != null) {
                }
                if (sQLiteCursor != null) {
                }
                throw th;
            }
        } catch (Exception e3) {
            e = e3;
            sQLiteCursor = null;
        } catch (Throwable th4) {
            th = th4;
            sQLiteCursor = null;
        }
    }

    private boolean isValidKeyboardToSave(TLRPC$Message tLRPC$Message) {
        TLRPC$ReplyMarkup tLRPC$ReplyMarkup = tLRPC$Message.reply_markup;
        return (tLRPC$ReplyMarkup == null || (tLRPC$ReplyMarkup instanceof TLRPC$TL_replyInlineMarkup) || (tLRPC$ReplyMarkup.selective && !tLRPC$Message.mentioned)) ? false : true;
    }

    public void updateMessageVerifyFlags(final ArrayList<TLRPC$Message> arrayList) {
        Utilities.stageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateMessageVerifyFlags$180(arrayList);
            }
        });
    }

    public void lambda$updateMessageVerifyFlags$180(ArrayList arrayList) {
        SQLiteDatabase sQLiteDatabase;
        SQLiteDatabase sQLiteDatabase2;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        boolean z = false;
        try {
            try {
                this.database.beginTransaction();
                try {
                    SQLitePreparedStatement executeFast = this.database.executeFast("UPDATE messages_v2 SET imp = ? WHERE mid = ? AND uid = ?");
                    try {
                        int size = arrayList.size();
                        for (int i = 0; i < size; i++) {
                            TLRPC$Message tLRPC$Message = (TLRPC$Message) arrayList.get(i);
                            executeFast.requery();
                            int i2 = tLRPC$Message.stickerVerified;
                            executeFast.bindInteger(1, i2 == 0 ? 1 : i2 == 2 ? 2 : 0);
                            executeFast.bindInteger(2, tLRPC$Message.id);
                            executeFast.bindLong(3, MessageObject.getDialogId(tLRPC$Message));
                            executeFast.step();
                        }
                        executeFast.dispose();
                        this.database.commitTransaction();
                    } catch (Exception e) {
                        e = e;
                        z = true;
                        sQLitePreparedStatement = executeFast;
                        checkSQLException(e);
                        if (z && (sQLiteDatabase2 = this.database) != null) {
                            sQLiteDatabase2.commitTransaction();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                    } catch (Throwable th) {
                        th = th;
                        z = true;
                        sQLitePreparedStatement = executeFast;
                        if (z && (sQLiteDatabase = this.database) != null) {
                            sQLiteDatabase.commitTransaction();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                        throw th;
                    }
                } catch (Exception e2) {
                    e = e2;
                    z = true;
                } catch (Throwable th2) {
                    th = th2;
                    z = true;
                }
            } catch (Exception e3) {
                e = e3;
            }
        } catch (Throwable th3) {
            th = th3;
        }
    }

    public void lambda$putMessages$185(java.util.ArrayList<org.telegram.tgnet.TLRPC$Message> r55, boolean r56, boolean r57, int r58, boolean r59, int r60, long r61) {
        throw new UnsupportedOperationException("Method not decompiled: org.telegram.messenger.MessagesStorage.lambda$putMessages$185(java.util.ArrayList, boolean, boolean, int, boolean, int, long):void");
    }

    public void lambda$putMessagesInternal$181(int i) {
        getDownloadController().newDownloadObjectsAvailable(i);
    }

    public void lambda$putMessagesInternal$182(ArrayList arrayList) {
        if (getMessagesController().getSavedMessagesController().updateSavedDialogs(arrayList)) {
            getMessagesController().getSavedMessagesController().update();
        }
    }

    private void createOrEditTopic(final long j, TLRPC$Message tLRPC$Message) {
        final TLRPC$TL_forumTopic tLRPC$TL_forumTopic = new TLRPC$TL_forumTopic();
        tLRPC$TL_forumTopic.topicStartMessage = tLRPC$Message;
        tLRPC$TL_forumTopic.top_message = tLRPC$Message.id;
        tLRPC$TL_forumTopic.topMessage = tLRPC$Message;
        tLRPC$TL_forumTopic.from_id = tLRPC$Message.from_id;
        tLRPC$TL_forumTopic.notify_settings = new TLRPC$TL_peerNotifySettings();
        tLRPC$TL_forumTopic.unread_count = 0;
        TLRPC$MessageAction tLRPC$MessageAction = tLRPC$Message.action;
        if (tLRPC$MessageAction instanceof TLRPC$TL_messageActionTopicCreate) {
            TLRPC$TL_messageActionTopicCreate tLRPC$TL_messageActionTopicCreate = (TLRPC$TL_messageActionTopicCreate) tLRPC$MessageAction;
            tLRPC$TL_forumTopic.id = tLRPC$Message.id;
            long j2 = tLRPC$TL_messageActionTopicCreate.icon_emoji_id;
            tLRPC$TL_forumTopic.icon_emoji_id = j2;
            tLRPC$TL_forumTopic.title = tLRPC$TL_messageActionTopicCreate.title;
            tLRPC$TL_forumTopic.icon_color = tLRPC$TL_messageActionTopicCreate.icon_color;
            if (j2 != 0) {
                tLRPC$TL_forumTopic.flags |= 1;
            }
            ArrayList arrayList = new ArrayList();
            arrayList.add(tLRPC$TL_forumTopic);
            saveTopics(j, arrayList, false, false);
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$createOrEditTopic$183(j, tLRPC$TL_forumTopic);
                }
            });
            return;
        }
        if (tLRPC$MessageAction instanceof TLRPC$TL_messageActionTopicEdit) {
            TLRPC$TL_messageActionTopicEdit tLRPC$TL_messageActionTopicEdit = (TLRPC$TL_messageActionTopicEdit) tLRPC$MessageAction;
            tLRPC$TL_forumTopic.id = (int) MessageObject.getTopicId(this.currentAccount, tLRPC$Message, true);
            tLRPC$TL_forumTopic.icon_emoji_id = tLRPC$TL_messageActionTopicEdit.icon_emoji_id;
            tLRPC$TL_forumTopic.title = tLRPC$TL_messageActionTopicEdit.title;
            tLRPC$TL_forumTopic.closed = tLRPC$TL_messageActionTopicEdit.closed;
            tLRPC$TL_forumTopic.hidden = tLRPC$TL_messageActionTopicEdit.hidden;
            int i = tLRPC$TL_messageActionTopicEdit.flags;
            int i2 = (i & 1) != 0 ? 1 : 0;
            if ((i & 2) != 0) {
                i2 += 2;
            }
            if ((i & 4) != 0) {
                i2 += 8;
            }
            if ((i & 8) != 0) {
                i2 += 32;
            }
            final int i3 = i2;
            updateTopicData(j, tLRPC$TL_forumTopic, i3);
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$createOrEditTopic$184(j, tLRPC$TL_forumTopic, i3);
                }
            });
        }
    }

    public void lambda$createOrEditTopic$183(long j, TLRPC$TL_forumTopic tLRPC$TL_forumTopic) {
        getMessagesController().getTopicsController().onTopicCreated(j, tLRPC$TL_forumTopic, false);
    }

    public void lambda$createOrEditTopic$184(long j, TLRPC$TL_forumTopic tLRPC$TL_forumTopic, int i) {
        getMessagesController().getTopicsController().updateTopicInUi(j, tLRPC$TL_forumTopic, i);
    }

    public void putMessages(ArrayList<TLRPC$Message> arrayList, boolean z, boolean z2, boolean z3, int i, int i2, long j) {
        putMessages(arrayList, z, z2, z3, i, false, i2, j);
    }

    public void putMessages(final ArrayList<TLRPC$Message> arrayList, final boolean z, boolean z2, final boolean z3, final int i, final boolean z4, final int i2, final long j) {
        if (arrayList.size() == 0) {
            return;
        }
        if (z2) {
            this.storageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$putMessages$185(arrayList, z, z3, i, z4, i2, j);
                }
            });
        } else {
            lambda$putMessages$185(arrayList, z, z3, i, z4, i2, j);
        }
    }

    public void markMessageAsSendError(final TLRPC$Message tLRPC$Message, final boolean z) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$markMessageAsSendError$186(tLRPC$Message, z);
            }
        });
    }

    public void lambda$markMessageAsSendError$186(TLRPC$Message tLRPC$Message, boolean z) {
        try {
            long j = tLRPC$Message.id;
            if (z) {
                this.database.executeFast(String.format(Locale.US, "UPDATE scheduled_messages_v2 SET send_state = 2 WHERE mid = %d AND uid = %d", Long.valueOf(j), Long.valueOf(MessageObject.getDialogId(tLRPC$Message)))).stepThis().dispose();
            } else {
                SQLiteDatabase sQLiteDatabase = this.database;
                Locale locale = Locale.US;
                sQLiteDatabase.executeFast(String.format(locale, "UPDATE messages_v2 SET send_state = 2 WHERE mid = %d AND uid = %d", Long.valueOf(j), Long.valueOf(MessageObject.getDialogId(tLRPC$Message)))).stepThis().dispose();
                this.database.executeFast(String.format(locale, "UPDATE messages_topics SET send_state = 2 WHERE mid = %d AND uid = %d", Long.valueOf(j), Long.valueOf(MessageObject.getDialogId(tLRPC$Message)))).stepThis().dispose();
            }
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void setMessageSeq(final int i, final int i2, final int i3) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$setMessageSeq$187(i, i2, i3);
            }
        });
    }

    public void lambda$setMessageSeq$187(int i, int i2, int i3) {
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                sQLitePreparedStatement = this.database.executeFast("REPLACE INTO messages_seq VALUES(?, ?, ?)");
                sQLitePreparedStatement.requery();
                sQLitePreparedStatement.bindInteger(1, i);
                sQLitePreparedStatement.bindInteger(2, i2);
                sQLitePreparedStatement.bindInteger(3, i3);
                sQLitePreparedStatement.step();
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLitePreparedStatement == null) {
                    return;
                }
            }
            sQLitePreparedStatement.dispose();
        } catch (Throwable th) {
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public long[] lambda$updateMessageStateAndId$189(long j, long j2, Integer num, int i, int i2, int i3) {
        Integer num2;
        SQLiteCursor sQLiteCursor;
        ?? r10;
        long j3;
        int intValue;
        SQLiteCursor sQLiteCursor2;
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLiteCursor sQLiteCursor3;
        long j4;
        long j5;
        long j6;
        SQLitePreparedStatement sQLitePreparedStatement2;
        SQLitePreparedStatement sQLitePreparedStatement3;
        SQLitePreparedStatement sQLitePreparedStatement4;
        SQLitePreparedStatement executeFast;
        int i4 = i3;
        SQLiteCursor sQLiteCursor4 = null;
        r5 = null;
        r5 = null;
        SQLitePreparedStatement sQLitePreparedStatement5 = null;
        r5 = null;
        SQLitePreparedStatement sQLitePreparedStatement6 = null;
        r5 = null;
        r5 = null;
        r5 = null;
        SQLitePreparedStatement sQLitePreparedStatement7 = null;
        SQLitePreparedStatement sQLitePreparedStatement8 = null;
        SQLitePreparedStatement sQLitePreparedStatement9 = null;
        SQLitePreparedStatement sQLitePreparedStatement10 = null;
        if (num == null) {
            try {
                sQLiteCursor = this.database.queryFinalized(String.format(Locale.US, "SELECT mid, uid FROM randoms_v2 WHERE random_id = %d LIMIT 1", Long.valueOf(j)), new Object[0]);
                try {
                    try {
                        if (sQLiteCursor.next()) {
                            num2 = Integer.valueOf(sQLiteCursor.intValue(0));
                            try {
                                j3 = sQLiteCursor.longValue(1);
                            } catch (Exception e) {
                                e = e;
                                checkSQLException(e);
                                if (sQLiteCursor != null) {
                                    sQLiteCursor.dispose();
                                }
                                r10 = j2;
                                if (num2 == null) {
                                }
                                getUserConfig().getClientUserId();
                                intValue = num2.intValue();
                                if (num2.intValue() < 0) {
                                }
                                sQLiteCursor2 = sQLiteCursor;
                                if (num2.intValue() > 0) {
                                }
                                if (i4 != -1) {
                                }
                                sQLiteCursor3 = this.database.queryFinalized(String.format(Locale.US, "SELECT uid FROM messages_v2 WHERE mid = %d LIMIT 1", Integer.valueOf(intValue)), new Object[0]);
                                try {
                                    if (sQLiteCursor3.next()) {
                                    }
                                    sQLiteCursor3.dispose();
                                    j5 = j4;
                                } catch (Exception e2) {
                                    e = e2;
                                    checkSQLException(e);
                                    if (sQLiteCursor3 != null) {
                                        sQLiteCursor3.dispose();
                                    }
                                    j5 = 0;
                                    if (i4 != -1) {
                                    }
                                    SQLiteDatabase sQLiteDatabase = this.database;
                                    Locale locale = Locale.US;
                                    Object[] objArr = new Object[r7];
                                    objArr[0] = Integer.valueOf(intValue);
                                    sQLiteCursor3 = sQLiteDatabase.queryFinalized(String.format(locale, "SELECT uid FROM scheduled_messages_v2 WHERE mid = %d LIMIT 1", objArr), new Object[0]);
                                    if (sQLiteCursor3.next()) {
                                    }
                                    sQLiteCursor3.dispose();
                                    j6 = j5;
                                    if (j6 == 0) {
                                    }
                                }
                                if (i4 != -1) {
                                }
                                try {
                                    SQLiteDatabase sQLiteDatabase2 = this.database;
                                    Locale locale2 = Locale.US;
                                    Object[] objArr2 = new Object[r7];
                                    objArr2[0] = Integer.valueOf(intValue);
                                    sQLiteCursor3 = sQLiteDatabase2.queryFinalized(String.format(locale2, "SELECT uid FROM scheduled_messages_v2 WHERE mid = %d LIMIT 1", objArr2), new Object[0]);
                                    if (sQLiteCursor3.next()) {
                                    }
                                } catch (Exception e3) {
                                    checkSQLException(e3);
                                }
                                sQLiteCursor3.dispose();
                                j6 = j5;
                                if (j6 == 0) {
                                }
                            }
                        } else {
                            j3 = j2;
                            num2 = num;
                        }
                        sQLiteCursor.dispose();
                        r10 = j3;
                    } catch (Throwable th) {
                        th = th;
                        sQLiteCursor4 = sQLiteCursor;
                        if (sQLiteCursor4 != null) {
                            sQLiteCursor4.dispose();
                        }
                        throw th;
                    }
                } catch (Exception e4) {
                    e = e4;
                    num2 = num;
                }
            } catch (Exception e5) {
                e = e5;
                num2 = num;
                sQLiteCursor = null;
            } catch (Throwable th2) {
                th = th2;
                if (sQLiteCursor4 != null) {
                }
                throw th;
            }
            if (num2 == null) {
                return null;
            }
        } else {
            r10 = j2;
            num2 = num;
            sQLiteCursor = null;
        }
        getUserConfig().getClientUserId();
        intValue = num2.intValue();
        try {
            try {
                if (num2.intValue() < 0) {
                    try {
                        if (i4 == 1) {
                            try {
                                sQLitePreparedStatement = this.database.executeFast("UPDATE randoms_v2 SET mid = ? WHERE random_id = ? AND mid = ?");
                                try {
                                    sQLitePreparedStatement.bindInteger(1, i);
                                    sQLiteCursor2 = sQLiteCursor;
                                } catch (Exception e6) {
                                    e = e6;
                                    sQLiteCursor2 = sQLiteCursor;
                                }
                                try {
                                    sQLitePreparedStatement.bindLong(2, j);
                                    sQLitePreparedStatement.bindInteger(3, intValue);
                                    sQLitePreparedStatement.step();
                                    r10 = sQLitePreparedStatement;
                                } catch (Exception e7) {
                                    e = e7;
                                    checkSQLException(e);
                                    r10 = sQLitePreparedStatement;
                                }
                            } catch (Exception e8) {
                                e = e8;
                                sQLiteCursor2 = sQLiteCursor;
                                sQLitePreparedStatement = null;
                            } catch (Throwable th3) {
                                th = th3;
                                throw th;
                            }
                            r10.dispose();
                            if (i4 != -1 || i4 == 0) {
                                sQLiteCursor3 = this.database.queryFinalized(String.format(Locale.US, "SELECT uid FROM messages_v2 WHERE mid = %d LIMIT 1", Integer.valueOf(intValue)), new Object[0]);
                                if (sQLiteCursor3.next()) {
                                    j4 = sQLiteCursor3.longValue(0);
                                    i4 = 0;
                                } else {
                                    j4 = 0;
                                }
                                sQLiteCursor3.dispose();
                                j5 = j4;
                                int i5 = i4 != -1 ? 1 : 1;
                                SQLiteDatabase sQLiteDatabase22 = this.database;
                                Locale locale22 = Locale.US;
                                Object[] objArr22 = new Object[i5];
                                objArr22[0] = Integer.valueOf(intValue);
                                sQLiteCursor3 = sQLiteDatabase22.queryFinalized(String.format(locale22, "SELECT uid FROM scheduled_messages_v2 WHERE mid = %d LIMIT 1", objArr22), new Object[0]);
                                if (sQLiteCursor3.next()) {
                                    j5 = sQLiteCursor3.longValue(0);
                                    i4 = 1;
                                }
                                sQLiteCursor3.dispose();
                                j6 = j5;
                                if (j6 == 0) {
                                    return null;
                                }
                                if (intValue == i && i2 != 0) {
                                    try {
                                        if (i4 == 0) {
                                            executeFast = this.database.executeFast("UPDATE messages_v2 SET send_state = 0, date = ? WHERE mid = ? AND uid = ?");
                                        } else {
                                            executeFast = this.database.executeFast("UPDATE scheduled_messages_v2 SET send_state = 0, date = ? WHERE mid = ? AND uid = ?");
                                        }
                                        SQLitePreparedStatement sQLitePreparedStatement11 = executeFast;
                                        try {
                                            sQLitePreparedStatement11.bindInteger(1, i2);
                                            sQLitePreparedStatement11.bindInteger(2, i);
                                            sQLitePreparedStatement11.bindLong(3, j6);
                                            sQLitePreparedStatement11.step();
                                            if (i4 == 0) {
                                                sQLitePreparedStatement5 = this.database.executeFast("UPDATE messages_topics SET send_state = 0, date = ? WHERE mid = ? AND uid = ?");
                                                sQLitePreparedStatement5.bindInteger(1, i2);
                                                sQLitePreparedStatement5.bindInteger(2, i);
                                                sQLitePreparedStatement5.bindLong(3, j6);
                                                sQLitePreparedStatement5.step();
                                            }
                                            sQLitePreparedStatement11.dispose();
                                        } catch (Exception e9) {
                                            e = e9;
                                            sQLitePreparedStatement4 = sQLitePreparedStatement5;
                                            sQLitePreparedStatement6 = sQLitePreparedStatement11;
                                            try {
                                                checkSQLException(e);
                                                if (sQLitePreparedStatement6 != null) {
                                                    sQLitePreparedStatement6.dispose();
                                                }
                                                if (sQLitePreparedStatement4 != null) {
                                                    sQLitePreparedStatement4.dispose();
                                                }
                                                return new long[]{j6, i};
                                            } catch (Throwable th4) {
                                                th = th4;
                                                if (sQLitePreparedStatement6 != null) {
                                                    sQLitePreparedStatement6.dispose();
                                                }
                                                if (sQLitePreparedStatement4 != null) {
                                                    sQLitePreparedStatement4.dispose();
                                                }
                                                throw th;
                                            }
                                        } catch (Throwable th5) {
                                            th = th5;
                                            sQLitePreparedStatement4 = sQLitePreparedStatement5;
                                            sQLitePreparedStatement6 = sQLitePreparedStatement11;
                                            if (sQLitePreparedStatement6 != null) {
                                            }
                                            if (sQLitePreparedStatement4 != null) {
                                            }
                                            throw th;
                                        }
                                    } catch (Exception e10) {
                                        e = e10;
                                        sQLitePreparedStatement4 = null;
                                    } catch (Throwable th6) {
                                        th = th6;
                                        sQLitePreparedStatement4 = null;
                                    }
                                    return new long[]{j6, i};
                                }
                                try {
                                    if (i4 == 0) {
                                        try {
                                            sQLitePreparedStatement3 = this.database.executeFast("UPDATE messages_v2 SET mid = ?, send_state = 0 WHERE mid = ? AND uid = ?");
                                            try {
                                                sQLitePreparedStatement3.bindInteger(1, i);
                                                sQLitePreparedStatement3.bindInteger(2, intValue);
                                                sQLitePreparedStatement3.bindLong(3, j6);
                                                sQLitePreparedStatement3.step();
                                                sQLitePreparedStatement2 = this.database.executeFast("UPDATE messages_topics SET mid = ?, send_state = 0 WHERE mid = ? AND uid = ?");
                                                try {
                                                    try {
                                                        sQLitePreparedStatement2.bindInteger(1, i);
                                                        sQLitePreparedStatement2.bindInteger(2, intValue);
                                                        sQLitePreparedStatement2.bindLong(3, j6);
                                                        sQLitePreparedStatement2.step();
                                                        sQLitePreparedStatement3.dispose();
                                                        sQLitePreparedStatement2.dispose();
                                                        sQLitePreparedStatement3 = null;
                                                    } catch (Exception unused) {
                                                        try {
                                                            try {
                                                                SQLiteDatabase sQLiteDatabase3 = this.database;
                                                                Locale locale3 = Locale.US;
                                                                sQLiteDatabase3.executeFast(String.format(locale3, "DELETE FROM messages_v2 WHERE mid = %d AND uid = %d", Integer.valueOf(intValue), Long.valueOf(j6))).stepThis().dispose();
                                                                this.database.executeFast(String.format(locale3, "DELETE FROM messages_seq WHERE mid = %d", Integer.valueOf(intValue))).stepThis().dispose();
                                                                this.database.executeFast(String.format(locale3, "DELETE FROM messages_topics WHERE mid = %d AND uid = %d", Integer.valueOf(intValue), Long.valueOf(j6))).stepThis().dispose();
                                                            } catch (Exception e11) {
                                                                checkSQLException(e11);
                                                                if (sQLitePreparedStatement3 != null) {
                                                                }
                                                                if (sQLitePreparedStatement2 != null) {
                                                                }
                                                                sQLitePreparedStatement3 = this.database.executeFast("UPDATE media_v4 SET mid = ? WHERE mid = ? AND uid = ?");
                                                                sQLitePreparedStatement3.bindInteger(1, i);
                                                                sQLitePreparedStatement3.bindInteger(2, intValue);
                                                                sQLitePreparedStatement3.bindLong(3, j6);
                                                                sQLitePreparedStatement3.step();
                                                                sQLitePreparedStatement3.dispose();
                                                                sQLitePreparedStatement3 = null;
                                                                sQLitePreparedStatement3 = this.database.executeFast("UPDATE media_topics SET mid = ? WHERE mid = ? AND uid = ?");
                                                                sQLitePreparedStatement3.bindInteger(1, i);
                                                                sQLitePreparedStatement3.bindInteger(2, intValue);
                                                                sQLitePreparedStatement3.bindLong(3, j6);
                                                                sQLitePreparedStatement3.step();
                                                                sQLitePreparedStatement3.dispose();
                                                                sQLitePreparedStatement7 = this.database.executeFast("UPDATE dialogs SET last_mid = ? WHERE last_mid = ?");
                                                                sQLitePreparedStatement7.bindInteger(1, i);
                                                                sQLitePreparedStatement7.bindInteger(2, intValue);
                                                                sQLitePreparedStatement7.step();
                                                                sQLitePreparedStatement7.dispose();
                                                                return new long[]{j6, num2.intValue()};
                                                            }
                                                            sQLitePreparedStatement3 = this.database.executeFast("UPDATE media_v4 SET mid = ? WHERE mid = ? AND uid = ?");
                                                            sQLitePreparedStatement3.bindInteger(1, i);
                                                            sQLitePreparedStatement3.bindInteger(2, intValue);
                                                            sQLitePreparedStatement3.bindLong(3, j6);
                                                            sQLitePreparedStatement3.step();
                                                            sQLitePreparedStatement3.dispose();
                                                            sQLitePreparedStatement3 = null;
                                                            sQLitePreparedStatement3 = this.database.executeFast("UPDATE media_topics SET mid = ? WHERE mid = ? AND uid = ?");
                                                            sQLitePreparedStatement3.bindInteger(1, i);
                                                            sQLitePreparedStatement3.bindInteger(2, intValue);
                                                            sQLitePreparedStatement3.bindLong(3, j6);
                                                            sQLitePreparedStatement3.step();
                                                            sQLitePreparedStatement3.dispose();
                                                            try {
                                                                sQLitePreparedStatement7 = this.database.executeFast("UPDATE dialogs SET last_mid = ? WHERE last_mid = ?");
                                                                sQLitePreparedStatement7.bindInteger(1, i);
                                                                sQLitePreparedStatement7.bindInteger(2, intValue);
                                                                sQLitePreparedStatement7.step();
                                                            } catch (Exception e12) {
                                                                checkSQLException(e12);
                                                            }
                                                            sQLitePreparedStatement7.dispose();
                                                            return new long[]{j6, num2.intValue()};
                                                        } finally {
                                                            if (sQLitePreparedStatement3 != null) {
                                                                sQLitePreparedStatement3.dispose();
                                                            }
                                                        }
                                                        if (sQLitePreparedStatement3 != null) {
                                                            sQLitePreparedStatement3.dispose();
                                                            sQLitePreparedStatement3 = null;
                                                        }
                                                        if (sQLitePreparedStatement2 != null) {
                                                            sQLitePreparedStatement2.dispose();
                                                        }
                                                    }
                                                } catch (Throwable th7) {
                                                    th = th7;
                                                    sQLitePreparedStatement8 = sQLitePreparedStatement3;
                                                    if (sQLitePreparedStatement8 != null) {
                                                        sQLitePreparedStatement8.dispose();
                                                    }
                                                    if (sQLitePreparedStatement2 != null) {
                                                        sQLitePreparedStatement2.dispose();
                                                    }
                                                    throw th;
                                                }
                                            } catch (Exception unused2) {
                                                sQLitePreparedStatement2 = null;
                                            } catch (Throwable th8) {
                                                th = th8;
                                                sQLitePreparedStatement2 = null;
                                                sQLitePreparedStatement8 = sQLitePreparedStatement3;
                                                if (sQLitePreparedStatement8 != null) {
                                                }
                                                if (sQLitePreparedStatement2 != null) {
                                                }
                                                throw th;
                                            }
                                        } catch (Exception unused3) {
                                            sQLitePreparedStatement3 = null;
                                            sQLitePreparedStatement2 = null;
                                        } catch (Throwable th9) {
                                            th = th9;
                                            sQLitePreparedStatement2 = null;
                                            if (sQLitePreparedStatement8 != null) {
                                            }
                                            if (sQLitePreparedStatement2 != null) {
                                            }
                                            throw th;
                                        }
                                        try {
                                            sQLitePreparedStatement3 = this.database.executeFast("UPDATE media_v4 SET mid = ? WHERE mid = ? AND uid = ?");
                                            sQLitePreparedStatement3.bindInteger(1, i);
                                            sQLitePreparedStatement3.bindInteger(2, intValue);
                                            sQLitePreparedStatement3.bindLong(3, j6);
                                            sQLitePreparedStatement3.step();
                                        } catch (Exception unused4) {
                                            this.database.executeFast(String.format(Locale.US, "DELETE FROM media_v4 WHERE mid = %d AND uid = %d", Integer.valueOf(intValue), Long.valueOf(j6))).stepThis().dispose();
                                        }
                                        sQLitePreparedStatement3.dispose();
                                        sQLitePreparedStatement3 = null;
                                        try {
                                            sQLitePreparedStatement3 = this.database.executeFast("UPDATE media_topics SET mid = ? WHERE mid = ? AND uid = ?");
                                            sQLitePreparedStatement3.bindInteger(1, i);
                                            sQLitePreparedStatement3.bindInteger(2, intValue);
                                            sQLitePreparedStatement3.bindLong(3, j6);
                                            sQLitePreparedStatement3.step();
                                            sQLitePreparedStatement3.dispose();
                                            try {
                                                sQLitePreparedStatement7 = this.database.executeFast("UPDATE dialogs SET last_mid = ? WHERE last_mid = ?");
                                                sQLitePreparedStatement7.bindInteger(1, i);
                                                sQLitePreparedStatement7.bindInteger(2, intValue);
                                                sQLitePreparedStatement7.step();
                                                sQLitePreparedStatement7.dispose();
                                            } catch (Throwable th10) {
                                                if (sQLitePreparedStatement7 != null) {
                                                    sQLitePreparedStatement7.dispose();
                                                }
                                                throw th10;
                                            }
                                        } finally {
                                            if (sQLitePreparedStatement3 != null) {
                                                sQLitePreparedStatement3.dispose();
                                            }
                                        }
                                    } else {
                                        try {
                                            try {
                                                sQLitePreparedStatement9 = this.database.executeFast("UPDATE scheduled_messages_v2 SET mid = ?, send_state = 0 WHERE mid = ? AND uid = ?");
                                                sQLitePreparedStatement9.bindInteger(1, i);
                                                sQLitePreparedStatement9.bindInteger(2, intValue);
                                                sQLitePreparedStatement9.bindLong(3, j6);
                                                sQLitePreparedStatement9.step();
                                            } catch (Exception unused5) {
                                                this.database.executeFast(String.format(Locale.US, "DELETE FROM scheduled_messages_v2 WHERE mid = %d AND uid = %d", Integer.valueOf(intValue), Long.valueOf(j6))).stepThis().dispose();
                                            }
                                        } catch (Exception e13) {
                                            checkSQLException(e13);
                                        }
                                        sQLitePreparedStatement9.dispose();
                                    }
                                    return new long[]{j6, num2.intValue()};
                                } finally {
                                    if (sQLitePreparedStatement9 != null) {
                                        sQLitePreparedStatement9.dispose();
                                    }
                                }
                            }
                            sQLiteCursor3 = sQLiteCursor2;
                            j5 = 0;
                            if (i4 != -1) {
                            }
                            SQLiteDatabase sQLiteDatabase222 = this.database;
                            Locale locale222 = Locale.US;
                            Object[] objArr222 = new Object[i5];
                            objArr222[0] = Integer.valueOf(intValue);
                            sQLiteCursor3 = sQLiteDatabase222.queryFinalized(String.format(locale222, "SELECT uid FROM scheduled_messages_v2 WHERE mid = %d LIMIT 1", objArr222), new Object[0]);
                            if (sQLiteCursor3.next()) {
                            }
                            sQLiteCursor3.dispose();
                            j6 = j5;
                            if (j6 == 0) {
                            }
                        }
                    } catch (Throwable th11) {
                        th = th11;
                        sQLitePreparedStatement10 = r10;
                    }
                }
                SQLiteDatabase sQLiteDatabase2222 = this.database;
                Locale locale2222 = Locale.US;
                Object[] objArr2222 = new Object[i5];
                objArr2222[0] = Integer.valueOf(intValue);
                sQLiteCursor3 = sQLiteDatabase2222.queryFinalized(String.format(locale2222, "SELECT uid FROM scheduled_messages_v2 WHERE mid = %d LIMIT 1", objArr2222), new Object[0]);
                if (sQLiteCursor3.next()) {
                }
                sQLiteCursor3.dispose();
                j6 = j5;
                if (j6 == 0) {
                }
            } catch (Throwable th12) {
                if (sQLiteCursor3 != null) {
                    sQLiteCursor3.dispose();
                }
                throw th12;
            }
            if (sQLiteCursor3.next()) {
            }
            sQLiteCursor3.dispose();
            j5 = j4;
            if (i4 != -1) {
            }
        } catch (Throwable th13) {
            th = th13;
            if (sQLiteCursor3 != null) {
                sQLiteCursor3.dispose();
            }
            throw th;
        }
        sQLiteCursor2 = sQLiteCursor;
        if (num2.intValue() > 0) {
            TLRPC$TL_updateDeleteScheduledMessages tLRPC$TL_updateDeleteScheduledMessages = new TLRPC$TL_updateDeleteScheduledMessages();
            tLRPC$TL_updateDeleteScheduledMessages.messages.add(Integer.valueOf(intValue));
            if (DialogObject.isChatDialog(r10)) {
                TLRPC$TL_peerChannel tLRPC$TL_peerChannel = new TLRPC$TL_peerChannel();
                tLRPC$TL_updateDeleteScheduledMessages.peer = tLRPC$TL_peerChannel;
                tLRPC$TL_peerChannel.channel_id = -r10;
            } else {
                TLRPC$TL_peerUser tLRPC$TL_peerUser = new TLRPC$TL_peerUser();
                tLRPC$TL_updateDeleteScheduledMessages.peer = tLRPC$TL_peerUser;
                tLRPC$TL_peerUser.user_id = r10;
            }
            final TLRPC$TL_updates tLRPC$TL_updates = new TLRPC$TL_updates();
            tLRPC$TL_updates.updates.add(tLRPC$TL_updateDeleteScheduledMessages);
            Utilities.stageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$updateMessageStateAndIdInternal$188(tLRPC$TL_updates);
                }
            });
            try {
                this.database.executeFast(String.format(Locale.US, "DELETE FROM randoms_v2 WHERE random_id = %d AND mid = %d AND uid = %d", Long.valueOf(j), num2, Long.valueOf((long) r10))).stepThis().dispose();
            } catch (Exception e14) {
                checkSQLException(e14);
            }
            return null;
        }
        if (i4 != -1) {
        }
        sQLiteCursor3 = this.database.queryFinalized(String.format(Locale.US, "SELECT uid FROM messages_v2 WHERE mid = %d LIMIT 1", Integer.valueOf(intValue)), new Object[0]);
    }

    public void lambda$updateMessageStateAndIdInternal$188(TLRPC$TL_updates tLRPC$TL_updates) {
        getMessagesController().processUpdates(tLRPC$TL_updates, false);
    }

    public long[] updateMessageStateAndId(final long j, final long j2, final Integer num, final int i, final int i2, boolean z, final int i3) {
        if (z) {
            this.storageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$updateMessageStateAndId$189(j, j2, num, i, i2, i3);
                }
            });
            return null;
        }
        return lambda$updateMessageStateAndId$189(j, j2, num, i, i2, i3);
    }

    public void lambda$updateUsers$190(ArrayList<TLRPC$User> arrayList, boolean z, boolean z2) {
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                if (z) {
                    if (z2) {
                        this.database.beginTransaction();
                    }
                    SQLitePreparedStatement executeFast = this.database.executeFast("UPDATE users SET status = ? WHERE uid = ?");
                    try {
                        int size = arrayList.size();
                        for (int i = 0; i < size; i++) {
                            TLRPC$User tLRPC$User = arrayList.get(i);
                            executeFast.requery();
                            TLRPC$UserStatus tLRPC$UserStatus = tLRPC$User.status;
                            if (tLRPC$UserStatus != null) {
                                executeFast.bindInteger(1, tLRPC$UserStatus.expires);
                            } else {
                                executeFast.bindInteger(1, 0);
                            }
                            executeFast.bindLong(2, tLRPC$User.id);
                            executeFast.step();
                        }
                        executeFast.dispose();
                        if (z2) {
                            this.database.commitTransaction();
                        }
                    } catch (Exception e) {
                        e = e;
                        sQLitePreparedStatement = executeFast;
                        checkSQLException(e);
                        SQLiteDatabase sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                            sQLiteDatabase.commitTransaction();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                            return;
                        }
                        return;
                    } catch (Throwable th) {
                        th = th;
                        sQLitePreparedStatement = executeFast;
                        SQLiteDatabase sQLiteDatabase2 = this.database;
                        if (sQLiteDatabase2 != null) {
                            sQLiteDatabase2.commitTransaction();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                        throw th;
                    }
                } else {
                    StringBuilder sb = new StringBuilder();
                    LongSparseArray longSparseArray = new LongSparseArray();
                    int size2 = arrayList.size();
                    for (int i2 = 0; i2 < size2; i2++) {
                        TLRPC$User tLRPC$User2 = arrayList.get(i2);
                        if (sb.length() != 0) {
                            sb.append(",");
                        }
                        sb.append(tLRPC$User2.id);
                        longSparseArray.put(tLRPC$User2.id, tLRPC$User2);
                    }
                    ArrayList<TLRPC$User> arrayList2 = new ArrayList<>();
                    getUsersInternal(sb.toString(), arrayList2);
                    int size3 = arrayList2.size();
                    for (int i3 = 0; i3 < size3; i3++) {
                        TLRPC$User tLRPC$User3 = arrayList2.get(i3);
                        TLRPC$User tLRPC$User4 = (TLRPC$User) longSparseArray.get(tLRPC$User3.id);
                        if (tLRPC$User4 != null) {
                            if (tLRPC$User4.first_name != null && tLRPC$User4.last_name != null) {
                                if (!UserObject.isContact(tLRPC$User3)) {
                                    tLRPC$User3.first_name = tLRPC$User4.first_name;
                                    tLRPC$User3.last_name = tLRPC$User4.last_name;
                                }
                                tLRPC$User3.username = tLRPC$User4.username;
                            } else {
                                TLRPC$UserProfilePhoto tLRPC$UserProfilePhoto = tLRPC$User4.photo;
                                if (tLRPC$UserProfilePhoto != null) {
                                    tLRPC$User3.photo = tLRPC$UserProfilePhoto;
                                } else {
                                    String str = tLRPC$User4.phone;
                                    if (str != null) {
                                        tLRPC$User3.phone = str;
                                    }
                                }
                            }
                        }
                    }
                    if (!arrayList2.isEmpty()) {
                        if (z2) {
                            this.database.beginTransaction();
                        }
                        putUsersInternal(arrayList2);
                        if (z2) {
                            this.database.commitTransaction();
                        }
                    }
                }
                SQLiteDatabase sQLiteDatabase3 = this.database;
                if (sQLiteDatabase3 != null) {
                    sQLiteDatabase3.commitTransaction();
                }
            } catch (Exception e2) {
                e = e2;
            }
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public void updateUsers(final ArrayList<TLRPC$User> arrayList, final boolean z, final boolean z2, boolean z3) {
        if (arrayList == null || arrayList.isEmpty()) {
            return;
        }
        if (z3) {
            this.storageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$updateUsers$190(arrayList, z, z2);
                }
            });
        } else {
            lambda$updateUsers$190(arrayList, z, z2);
        }
    }

    public void lambda$markMessagesAsRead$192(LongSparseIntArray longSparseIntArray, LongSparseIntArray longSparseIntArray2, SparseIntArray sparseIntArray) {
        SQLitePreparedStatement sQLitePreparedStatement;
        try {
            if (!isEmpty(longSparseIntArray)) {
                SQLitePreparedStatement executeFast = this.database.executeFast("DELETE FROM unread_push_messages WHERE uid = ? AND mid <= ?");
                for (int i = 0; i < longSparseIntArray.size(); i++) {
                    try {
                        long keyAt = longSparseIntArray.keyAt(i);
                        int i2 = longSparseIntArray.get(keyAt);
                        this.database.executeFast(String.format(Locale.US, "UPDATE messages_v2 SET read_state = read_state | 1 WHERE uid = %d AND mid > 0 AND mid <= %d AND read_state IN(0,2) AND out = 0", Long.valueOf(keyAt), Integer.valueOf(i2))).stepThis().dispose();
                        executeFast.requery();
                        executeFast.bindLong(1, keyAt);
                        executeFast.bindInteger(2, i2);
                        executeFast.step();
                    } catch (Exception e) {
                        e = e;
                        sQLitePreparedStatement = executeFast;
                        try {
                            checkSQLException(e);
                            if (sQLitePreparedStatement == null) {
                            }
                        } catch (Throwable th) {
                            th = th;
                            if (sQLitePreparedStatement != null) {
                                sQLitePreparedStatement.dispose();
                            }
                            throw th;
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        sQLitePreparedStatement = executeFast;
                        if (sQLitePreparedStatement != null) {
                        }
                        throw th;
                    }
                }
                executeFast.dispose();
            }
            if (!isEmpty(longSparseIntArray2)) {
                for (int i3 = 0; i3 < longSparseIntArray2.size(); i3++) {
                    long keyAt2 = longSparseIntArray2.keyAt(i3);
                    this.database.executeFast(String.format(Locale.US, "UPDATE messages_v2 SET read_state = read_state | 1 WHERE uid = %d AND mid > 0 AND mid <= %d AND read_state IN(0,2) AND out = 1", Long.valueOf(keyAt2), Integer.valueOf(longSparseIntArray2.get(keyAt2)))).stepThis().dispose();
                }
            }
            if (sparseIntArray == null || isEmpty(sparseIntArray)) {
                return;
            }
            for (int i4 = 0; i4 < sparseIntArray.size(); i4++) {
                long makeEncryptedDialogId = DialogObject.makeEncryptedDialogId(sparseIntArray.keyAt(i4));
                int valueAt = sparseIntArray.valueAt(i4);
                SQLitePreparedStatement executeFast2 = this.database.executeFast("UPDATE messages_v2 SET read_state = read_state | 1 WHERE uid = ? AND date <= ? AND read_state IN(0,2) AND out = 1");
                try {
                    executeFast2.requery();
                    executeFast2.bindLong(1, makeEncryptedDialogId);
                    executeFast2.bindInteger(2, valueAt);
                    executeFast2.step();
                    executeFast2.dispose();
                } catch (Exception e2) {
                    e = e2;
                    sQLitePreparedStatement = executeFast2;
                    checkSQLException(e);
                    if (sQLitePreparedStatement == null) {
                        sQLitePreparedStatement.dispose();
                        return;
                    }
                    return;
                } catch (Throwable th3) {
                    th = th3;
                    sQLitePreparedStatement = executeFast2;
                    if (sQLitePreparedStatement != null) {
                    }
                    throw th;
                }
            }
        } catch (Exception e3) {
            e = e3;
            sQLitePreparedStatement = null;
        } catch (Throwable th4) {
            th = th4;
            sQLitePreparedStatement = null;
        }
    }

    private void markMessagesContentAsReadInternal(long j, ArrayList<Integer> arrayList, int i) {
        SQLiteCursor sQLiteCursor = null;
        ArrayList<Integer> arrayList2 = null;
        sQLiteCursor = null;
        try {
            try {
                String join = TextUtils.join(",", arrayList);
                SQLiteDatabase sQLiteDatabase = this.database;
                Locale locale = Locale.US;
                sQLiteDatabase.executeFast(String.format(locale, "UPDATE messages_v2 SET read_state = read_state | 2 WHERE mid IN (%s) AND uid = %d", join, Long.valueOf(j))).stepThis().dispose();
                if (i != 0) {
                    SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(locale, "SELECT mid, ttl FROM messages_v2 WHERE mid IN (%s) AND uid = %d AND ttl > 0", join, Long.valueOf(j)), new Object[0]);
                    while (queryFinalized.next()) {
                        try {
                            if (arrayList2 == null) {
                                arrayList2 = new ArrayList<>();
                            }
                            arrayList2.add(Integer.valueOf(queryFinalized.intValue(0)));
                        } catch (Exception e) {
                            e = e;
                            sQLiteCursor = queryFinalized;
                            checkSQLException(e);
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                                return;
                            }
                            return;
                        } catch (Throwable th) {
                            th = th;
                            sQLiteCursor = queryFinalized;
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                            }
                            throw th;
                        }
                    }
                    if (arrayList2 != null) {
                        emptyMessagesMedia(j, arrayList2);
                    }
                    queryFinalized.dispose();
                }
            } catch (Exception e2) {
                e = e2;
            }
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public void markMessagesContentAsRead(final long j, final ArrayList<Integer> arrayList, final int i, final int i2) {
        if (isEmpty(arrayList)) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$markMessagesContentAsRead$191(j, arrayList, i2, i);
            }
        });
    }

    public void lambda$markMessagesContentAsRead$191(long j, ArrayList arrayList, int i, int i2) {
        SQLiteCursor sQLiteCursor;
        Throwable th;
        Exception e;
        int i3;
        if (j == 0) {
            try {
                LongSparseArray longSparseArray = new LongSparseArray();
                LongSparseArray longSparseArray2 = new LongSparseArray();
                sQLiteCursor = this.database.queryFinalized(String.format(Locale.US, "SELECT uid, mid, ttl FROM messages_v2 WHERE mid IN (%s) AND is_channel = 0", TextUtils.join(",", arrayList)), new Object[0]);
                while (sQLiteCursor.next()) {
                    try {
                        try {
                            long longValue = sQLiteCursor.longValue(0);
                            int intValue = sQLiteCursor.intValue(1);
                            int intValue2 = sQLiteCursor.intValue(2);
                            if (intValue2 > 0 && intValue2 != Integer.MAX_VALUE && i != 0 && (i3 = intValue2 + i) >= i2) {
                                SparseArray sparseArray = (SparseArray) longSparseArray2.get(longValue);
                                if (sparseArray == null) {
                                    sparseArray = new SparseArray();
                                    longSparseArray2.put(longValue, sparseArray);
                                }
                                ArrayList arrayList2 = (ArrayList) sparseArray.get(i3);
                                if (arrayList2 == null) {
                                    arrayList2 = new ArrayList();
                                    sparseArray.put(i3, arrayList2);
                                }
                                arrayList2.add(Integer.valueOf(intValue));
                            }
                            ArrayList arrayList3 = (ArrayList) longSparseArray.get(longValue);
                            if (arrayList3 == null) {
                                arrayList3 = new ArrayList();
                                longSparseArray.put(longValue, arrayList3);
                            }
                            arrayList3.add(Integer.valueOf(intValue));
                        } catch (Exception e2) {
                            e = e2;
                            checkSQLException(e);
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                                return;
                            }
                            return;
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        throw th;
                    }
                }
                sQLiteCursor.dispose();
                int size = longSparseArray.size();
                for (int i4 = 0; i4 < size; i4++) {
                    markMessagesContentAsReadInternal(longSparseArray.keyAt(i4), (ArrayList) longSparseArray.valueAt(i4), i2);
                }
                int size2 = longSparseArray2.size();
                for (int i5 = 0; i5 < size2; i5++) {
                    createTaskForSecretMedia(longSparseArray2.keyAt(i5), (SparseArray) longSparseArray2.valueAt(i5));
                }
            } catch (Exception e3) {
                sQLiteCursor = null;
                e = e3;
            } catch (Throwable th3) {
                sQLiteCursor = null;
                th = th3;
                if (sQLiteCursor != null) {
                }
                throw th;
            }
        } else {
            markMessagesContentAsReadInternal(j, arrayList, i2);
        }
    }

    public void markMessagesAsRead(final LongSparseIntArray longSparseIntArray, final LongSparseIntArray longSparseIntArray2, final SparseIntArray sparseIntArray, boolean z) {
        if (z) {
            this.storageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$markMessagesAsRead$192(longSparseIntArray, longSparseIntArray2, sparseIntArray);
                }
            });
        } else {
            lambda$markMessagesAsRead$192(longSparseIntArray, longSparseIntArray2, sparseIntArray);
        }
    }

    public void markMessagesAsDeletedByRandoms(final ArrayList<Long> arrayList) {
        if (arrayList.isEmpty()) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$markMessagesAsDeletedByRandoms$194(arrayList);
            }
        });
    }

    public void lambda$markMessagesAsDeletedByRandoms$194(ArrayList arrayList) {
        SQLiteCursor queryFinalized;
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT mid, uid FROM randoms_v2 WHERE random_id IN(%s)", TextUtils.join(",", arrayList)), new Object[0]);
            } catch (Throwable th) {
                th = th;
            }
        } catch (Exception e) {
            e = e;
        }
        try {
            LongSparseArray longSparseArray = new LongSparseArray();
            while (queryFinalized.next()) {
                long longValue = queryFinalized.longValue(1);
                ArrayList arrayList2 = (ArrayList) longSparseArray.get(longValue);
                if (arrayList2 == null) {
                    arrayList2 = new ArrayList();
                    longSparseArray.put(longValue, arrayList2);
                }
                arrayList2.add(Integer.valueOf(queryFinalized.intValue(0)));
            }
            queryFinalized.dispose();
            if (longSparseArray.isEmpty()) {
                return;
            }
            int size = longSparseArray.size();
            for (int i = 0; i < size; i++) {
                long keyAt = longSparseArray.keyAt(i);
                final ArrayList<Integer> arrayList3 = (ArrayList) longSparseArray.valueAt(i);
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$markMessagesAsDeletedByRandoms$193(arrayList3);
                    }
                });
                updateDialogsWithReadMessagesInternal(arrayList3, null, null, null, null);
                lambda$markMessagesAsDeleted$199(keyAt, arrayList3, true, false);
                lambda$updateDialogsWithDeletedMessages$198(keyAt, 0L, arrayList3, null);
            }
        } catch (Exception e2) {
            e = e2;
            sQLiteCursor = queryFinalized;
            checkSQLException(e);
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
        } catch (Throwable th2) {
            th = th2;
            sQLiteCursor = queryFinalized;
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public void lambda$markMessagesAsDeletedByRandoms$193(ArrayList arrayList) {
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.messagesDeleted, arrayList, 0L, Boolean.FALSE);
    }

    public void deletePushMessages(long j, ArrayList<Integer> arrayList) {
        try {
            this.database.executeFast(String.format(Locale.US, "DELETE FROM unread_push_messages WHERE uid = %d AND mid IN(%s)", Long.valueOf(j), TextUtils.join(",", arrayList))).stepThis().dispose();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    private void broadcastScheduledMessagesChange(final Long l) {
        SQLiteCursor queryFinalized;
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT COUNT(mid) FROM scheduled_messages_v2 WHERE uid = %d", l), new Object[0]);
            } catch (Throwable th) {
                th = th;
            }
        } catch (Exception e) {
            e = e;
        }
        try {
            final int intValue = queryFinalized.next() ? queryFinalized.intValue(0) : 0;
            queryFinalized.dispose();
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$broadcastScheduledMessagesChange$195(l, intValue);
                }
            });
        } catch (Exception e2) {
            e = e2;
            sQLiteCursor = queryFinalized;
            checkSQLException(e);
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
        } catch (Throwable th2) {
            th = th2;
            sQLiteCursor = queryFinalized;
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public void lambda$broadcastScheduledMessagesChange$195(Long l, int i) {
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.scheduledMessagesUpdated, l, Integer.valueOf(i), Boolean.TRUE);
    }

    public ArrayList<Long> lambda$markMessagesAsDeleted$199(long j, ArrayList<Integer> arrayList, boolean z, boolean z2) {
        Throwable th;
        SQLiteCursor sQLiteCursor;
        Exception exc;
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLitePreparedStatement sQLitePreparedStatement2;
        SQLiteDatabase sQLiteDatabase;
        Throwable th2;
        SQLiteCursor sQLiteCursor2;
        SQLiteDatabase sQLiteDatabase2;
        SQLiteCursor sQLiteCursor3;
        HashMap hashMap;
        LongSparseArray longSparseArray;
        ArrayList arrayList2;
        int i;
        SQLiteCursor queryFinalized;
        HashMap hashMap2;
        ArrayList<File> arrayList3;
        LongSparseArray longSparseArray2;
        ArrayList arrayList4;
        String str;
        LongSparseArray longSparseArray3;
        ArrayList arrayList5;
        LongSparseArray longSparseArray4;
        ArrayList arrayList6;
        int i2;
        int i3;
        long j2;
        ArrayList arrayList7;
        HashMap hashMap3;
        int i4;
        ArrayList<TopicKey> arrayList8;
        int i5;
        int i6;
        ArrayList<TopicKey> arrayList9;
        ArrayList<Long> arrayList10;
        LongSparseArray longSparseArray5;
        ArrayList arrayList11;
        int size;
        int i7;
        LongSparseArray longSparseArray6;
        SQLitePreparedStatement sQLitePreparedStatement3;
        ArrayList<TopicKey> arrayList12;
        SQLiteCursor sQLiteCursor4;
        ArrayList arrayList13;
        ArrayList<Long> arrayList14;
        SQLiteCursor sQLiteCursor5;
        Throwable th3;
        Exception e;
        long keyAt;
        CharSequence charSequence;
        int i8;
        ArrayList arrayList15;
        int i9;
        Locale locale;
        SQLitePreparedStatement sQLitePreparedStatement4;
        LongSparseArray longSparseArray7;
        ArrayList<TopicKey> arrayList16;
        ArrayList arrayList17;
        long j3;
        CharSequence charSequence2;
        SQLiteCursor sQLiteCursor6;
        SQLiteCursor sQLiteCursor7;
        SQLiteCursor sQLiteCursor8;
        int i10;
        int i11;
        Integer num;
        SQLitePreparedStatement executeFast;
        int i12;
        int i13;
        LongSparseArray longSparseArray8;
        Integer num2;
        int i14;
        int i15;
        int i16;
        int i17;
        int i18;
        String str2;
        Exception exc2;
        long topicId;
        TopicKey of;
        int intValue;
        int i19;
        Exception exc3;
        ArrayList arrayList18;
        LongSparseArray longSparseArray9;
        NativeByteBuffer byteBufferValue;
        TLRPC$Message TLdeserialize;
        TLRPC$Message tLRPC$Message;
        Integer[] numArr;
        SQLiteDatabase sQLiteDatabase3;
        try {
            ArrayList<Long> arrayList19 = new ArrayList<>();
            try {
                try {
                    if (z2) {
                        String join = TextUtils.join(",", arrayList);
                        ArrayList arrayList20 = new ArrayList();
                        SQLiteCursor queryFinalized2 = this.database.queryFinalized(String.format(Locale.US, "SELECT uid FROM scheduled_messages_v2 WHERE mid IN(%s) AND uid = %d", join, Long.valueOf(j)), new Object[0]);
                        while (queryFinalized2.next()) {
                            try {
                                long longValue = queryFinalized2.longValue(0);
                                if (!arrayList20.contains(Long.valueOf(longValue))) {
                                    arrayList20.add(Long.valueOf(longValue));
                                }
                            } catch (Exception e2) {
                                checkSQLException(e2);
                            }
                        }
                        queryFinalized2.dispose();
                        this.database.executeFast(String.format(Locale.US, "DELETE FROM scheduled_messages_v2 WHERE mid IN(%s) AND uid = %d", join, Long.valueOf(j))).stepThis().dispose();
                        int size2 = arrayList20.size();
                        for (int i20 = 0; i20 < size2; i20++) {
                            broadcastScheduledMessagesChange((Long) arrayList20.get(i20));
                        }
                        arrayList14 = arrayList19;
                        sQLiteCursor5 = null;
                        sQLitePreparedStatement3 = null;
                    } else {
                        ArrayList arrayList21 = new ArrayList(arrayList);
                        ArrayList arrayList22 = new ArrayList(arrayList);
                        LongSparseArray longSparseArray10 = new LongSparseArray();
                        LongSparseArray longSparseArray11 = new LongSparseArray();
                        HashMap hashMap4 = new HashMap();
                        LongSparseArray longSparseArray12 = new LongSparseArray();
                        String join2 = TextUtils.join(",", arrayList);
                        ArrayList<File> arrayList23 = new ArrayList<>();
                        ArrayList arrayList24 = new ArrayList();
                        ArrayList arrayList25 = new ArrayList();
                        ArrayList<Long> arrayList26 = arrayList19;
                        CharSequence charSequence3 = ",";
                        long clientUserId = getUserConfig().getClientUserId();
                        if (j != 0) {
                            hashMap = hashMap4;
                            longSparseArray = longSparseArray11;
                            arrayList2 = arrayList22;
                            queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT uid, data, read_state, out, mention, mid FROM messages_v2 WHERE mid IN(%s) AND uid = %d", join2, Long.valueOf(j)), new Object[0]);
                            i = 0;
                        } else {
                            hashMap = hashMap4;
                            longSparseArray = longSparseArray11;
                            arrayList2 = arrayList22;
                            i = 0;
                            queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT uid, data, read_state, out, mention, mid FROM messages_v2 WHERE mid IN(%s) AND is_channel = 0", join2), new Object[0]);
                        }
                        while (queryFinalized.next()) {
                            try {
                                try {
                                    LongSparseArray longSparseArray13 = longSparseArray10;
                                    try {
                                        long longValue2 = queryFinalized.longValue(i);
                                        try {
                                            int intValue2 = queryFinalized.intValue(5);
                                            arrayList21.remove(Integer.valueOf(intValue2));
                                            ArrayList arrayList27 = (ArrayList) longSparseArray12.get(longValue2);
                                            if (arrayList27 == null) {
                                                try {
                                                    arrayList27 = new ArrayList();
                                                    longSparseArray12.put(longValue2, arrayList27);
                                                } catch (Exception e3) {
                                                    hashMap2 = hashMap;
                                                    exc3 = e3;
                                                    longSparseArray2 = longSparseArray12;
                                                    arrayList4 = arrayList24;
                                                    str = join2;
                                                    longSparseArray3 = longSparseArray;
                                                    arrayList5 = arrayList2;
                                                    longSparseArray4 = longSparseArray13;
                                                    i19 = 0;
                                                    arrayList6 = arrayList21;
                                                    arrayList3 = arrayList23;
                                                    try {
                                                        checkSQLException(exc3);
                                                        i3 = i19;
                                                        queryFinalized.dispose();
                                                        if (j < 0) {
                                                        }
                                                        this.database.beginTransaction();
                                                        i5 = i3;
                                                        while (i5 < i4) {
                                                        }
                                                        String str3 = str;
                                                        deleteFromDownloadQueue(arrayList25, true);
                                                        final ArrayList arrayList28 = arrayList4;
                                                        AndroidUtilities.runOnUIThread(new Runnable() {
                                                            @Override
                                                            public final void run() {
                                                                MessagesStorage.this.lambda$markMessagesAsDeletedInternal$196(arrayList28);
                                                            }
                                                        });
                                                        getFileLoader().deleteFiles(arrayList3, i3);
                                                        i6 = i3;
                                                        while (i6 < longSparseArray4.size()) {
                                                        }
                                                        ArrayList<Long> arrayList29 = arrayList26;
                                                        LongSparseArray longSparseArray14 = longSparseArray4;
                                                        if (hashMap3.isEmpty()) {
                                                        }
                                                        size = longSparseArray2.size();
                                                        SQLitePreparedStatement sQLitePreparedStatement5 = null;
                                                        SQLiteCursor sQLiteCursor9 = null;
                                                        i7 = 0;
                                                        while (i7 < size) {
                                                        }
                                                        longSparseArray6 = longSparseArray14;
                                                        sQLitePreparedStatement3 = sQLitePreparedStatement5;
                                                        arrayList12 = arrayList9;
                                                        sQLiteCursor4 = sQLiteCursor9;
                                                        arrayList13 = arrayList11;
                                                        if (!longSparseArray5.isEmpty()) {
                                                        }
                                                        SQLiteDatabase sQLiteDatabase4 = this.database;
                                                        Locale locale2 = Locale.US;
                                                        sQLiteDatabase4.executeFast(String.format(locale2, "DELETE FROM messages_seq WHERE mid IN(%s)", str3)).stepThis().dispose();
                                                        if (!arrayList6.isEmpty()) {
                                                        }
                                                        if (!arrayList7.isEmpty()) {
                                                        }
                                                        getMediaDataController().clearBotKeyboard(null, arrayList);
                                                        if (longSparseArray6.size() != 0) {
                                                        }
                                                        arrayList14 = arrayList10;
                                                        updateWidgets(arrayList14);
                                                        if (arrayList12 != null) {
                                                        }
                                                        if (arrayList13 != null) {
                                                        }
                                                        sQLiteCursor5 = sQLiteCursor4;
                                                        sQLiteDatabase3 = this.database;
                                                        if (sQLiteDatabase3 != null) {
                                                        }
                                                        if (sQLiteCursor5 != null) {
                                                        }
                                                        if (sQLitePreparedStatement3 != null) {
                                                        }
                                                        return arrayList14;
                                                    } catch (Exception e4) {
                                                        exc = e4;
                                                        sQLiteCursor2 = queryFinalized;
                                                        sQLitePreparedStatement = null;
                                                        try {
                                                            checkSQLException(exc);
                                                            sQLiteDatabase2 = this.database;
                                                            if (sQLiteDatabase2 != null) {
                                                                sQLiteDatabase2.commitTransaction();
                                                            }
                                                            if (sQLiteCursor2 != null) {
                                                                sQLiteCursor2.dispose();
                                                            }
                                                            if (sQLitePreparedStatement == null) {
                                                                return null;
                                                            }
                                                            sQLitePreparedStatement.dispose();
                                                            return null;
                                                        } catch (Throwable th4) {
                                                            th2 = th4;
                                                            sQLitePreparedStatement2 = sQLitePreparedStatement;
                                                            sQLiteCursor = sQLiteCursor2;
                                                            th = th2;
                                                            sQLiteDatabase = this.database;
                                                            if (sQLiteDatabase != null) {
                                                                sQLiteDatabase.commitTransaction();
                                                            }
                                                            if (sQLiteCursor != null) {
                                                                sQLiteCursor.dispose();
                                                            }
                                                            if (sQLitePreparedStatement2 == null) {
                                                                sQLitePreparedStatement2.dispose();
                                                                throw th;
                                                            }
                                                            throw th;
                                                        }
                                                    }
                                                }
                                            }
                                            arrayList27.add(Integer.valueOf(intValue2));
                                            try {
                                                try {
                                                    if (longValue2 != clientUserId) {
                                                        try {
                                                            int intValue3 = queryFinalized.intValue(2);
                                                            if (queryFinalized.intValue(3) == 0) {
                                                                arrayList18 = arrayList21;
                                                                longSparseArray9 = longSparseArray13;
                                                                try {
                                                                    Integer[] numArr2 = (Integer[]) longSparseArray9.get(longValue2);
                                                                    if (numArr2 == null) {
                                                                        longSparseArray2 = longSparseArray12;
                                                                        try {
                                                                            numArr = new Integer[2];
                                                                        } catch (Exception e5) {
                                                                            e = e5;
                                                                            hashMap2 = hashMap;
                                                                            exc3 = e;
                                                                            arrayList3 = arrayList23;
                                                                            str = join2;
                                                                            longSparseArray3 = longSparseArray;
                                                                            arrayList5 = arrayList2;
                                                                            arrayList6 = arrayList18;
                                                                            longSparseArray4 = longSparseArray9;
                                                                            arrayList4 = arrayList24;
                                                                            i19 = 0;
                                                                            checkSQLException(exc3);
                                                                            i3 = i19;
                                                                            queryFinalized.dispose();
                                                                            if (j < 0) {
                                                                            }
                                                                            this.database.beginTransaction();
                                                                            i5 = i3;
                                                                            while (i5 < i4) {
                                                                            }
                                                                            String str32 = str;
                                                                            deleteFromDownloadQueue(arrayList25, true);
                                                                            final ArrayList arrayList282 = arrayList4;
                                                                            AndroidUtilities.runOnUIThread(new Runnable() {
                                                                                @Override
                                                                                public final void run() {
                                                                                    MessagesStorage.this.lambda$markMessagesAsDeletedInternal$196(arrayList282);
                                                                                }
                                                                            });
                                                                            getFileLoader().deleteFiles(arrayList3, i3);
                                                                            i6 = i3;
                                                                            while (i6 < longSparseArray4.size()) {
                                                                            }
                                                                            ArrayList<Long> arrayList292 = arrayList26;
                                                                            LongSparseArray longSparseArray142 = longSparseArray4;
                                                                            if (hashMap3.isEmpty()) {
                                                                            }
                                                                            size = longSparseArray2.size();
                                                                            SQLitePreparedStatement sQLitePreparedStatement52 = null;
                                                                            SQLiteCursor sQLiteCursor92 = null;
                                                                            i7 = 0;
                                                                            while (i7 < size) {
                                                                            }
                                                                            longSparseArray6 = longSparseArray142;
                                                                            sQLitePreparedStatement3 = sQLitePreparedStatement52;
                                                                            arrayList12 = arrayList9;
                                                                            sQLiteCursor4 = sQLiteCursor92;
                                                                            arrayList13 = arrayList11;
                                                                            if (!longSparseArray5.isEmpty()) {
                                                                            }
                                                                            SQLiteDatabase sQLiteDatabase42 = this.database;
                                                                            Locale locale22 = Locale.US;
                                                                            sQLiteDatabase42.executeFast(String.format(locale22, "DELETE FROM messages_seq WHERE mid IN(%s)", str32)).stepThis().dispose();
                                                                            if (!arrayList6.isEmpty()) {
                                                                            }
                                                                            if (!arrayList7.isEmpty()) {
                                                                            }
                                                                            getMediaDataController().clearBotKeyboard(null, arrayList);
                                                                            if (longSparseArray6.size() != 0) {
                                                                            }
                                                                            arrayList14 = arrayList10;
                                                                            updateWidgets(arrayList14);
                                                                            if (arrayList12 != null) {
                                                                            }
                                                                            if (arrayList13 != null) {
                                                                            }
                                                                            sQLiteCursor5 = sQLiteCursor4;
                                                                            sQLiteDatabase3 = this.database;
                                                                            if (sQLiteDatabase3 != null) {
                                                                            }
                                                                            if (sQLiteCursor5 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement3 != null) {
                                                                            }
                                                                            return arrayList14;
                                                                        }
                                                                        try {
                                                                            numArr[0] = 0;
                                                                            numArr[1] = 0;
                                                                            longSparseArray9.put(longValue2, numArr);
                                                                            numArr2 = numArr;
                                                                        } catch (Exception e6) {
                                                                            e = e6;
                                                                            hashMap2 = hashMap;
                                                                            arrayList3 = arrayList23;
                                                                            str = join2;
                                                                            longSparseArray3 = longSparseArray;
                                                                            arrayList5 = arrayList2;
                                                                            arrayList6 = arrayList18;
                                                                            longSparseArray4 = longSparseArray9;
                                                                            arrayList4 = arrayList24;
                                                                            i2 = 0;
                                                                            exc3 = e;
                                                                            i19 = i2;
                                                                            checkSQLException(exc3);
                                                                            i3 = i19;
                                                                            queryFinalized.dispose();
                                                                            if (j < 0) {
                                                                            }
                                                                            this.database.beginTransaction();
                                                                            i5 = i3;
                                                                            while (i5 < i4) {
                                                                            }
                                                                            String str322 = str;
                                                                            deleteFromDownloadQueue(arrayList25, true);
                                                                            final ArrayList arrayList2822 = arrayList4;
                                                                            AndroidUtilities.runOnUIThread(new Runnable() {
                                                                                @Override
                                                                                public final void run() {
                                                                                    MessagesStorage.this.lambda$markMessagesAsDeletedInternal$196(arrayList2822);
                                                                                }
                                                                            });
                                                                            getFileLoader().deleteFiles(arrayList3, i3);
                                                                            i6 = i3;
                                                                            while (i6 < longSparseArray4.size()) {
                                                                            }
                                                                            ArrayList<Long> arrayList2922 = arrayList26;
                                                                            LongSparseArray longSparseArray1422 = longSparseArray4;
                                                                            if (hashMap3.isEmpty()) {
                                                                            }
                                                                            size = longSparseArray2.size();
                                                                            SQLitePreparedStatement sQLitePreparedStatement522 = null;
                                                                            SQLiteCursor sQLiteCursor922 = null;
                                                                            i7 = 0;
                                                                            while (i7 < size) {
                                                                            }
                                                                            longSparseArray6 = longSparseArray1422;
                                                                            sQLitePreparedStatement3 = sQLitePreparedStatement522;
                                                                            arrayList12 = arrayList9;
                                                                            sQLiteCursor4 = sQLiteCursor922;
                                                                            arrayList13 = arrayList11;
                                                                            if (!longSparseArray5.isEmpty()) {
                                                                            }
                                                                            SQLiteDatabase sQLiteDatabase422 = this.database;
                                                                            Locale locale222 = Locale.US;
                                                                            sQLiteDatabase422.executeFast(String.format(locale222, "DELETE FROM messages_seq WHERE mid IN(%s)", str322)).stepThis().dispose();
                                                                            if (!arrayList6.isEmpty()) {
                                                                            }
                                                                            if (!arrayList7.isEmpty()) {
                                                                            }
                                                                            getMediaDataController().clearBotKeyboard(null, arrayList);
                                                                            if (longSparseArray6.size() != 0) {
                                                                            }
                                                                            arrayList14 = arrayList10;
                                                                            updateWidgets(arrayList14);
                                                                            if (arrayList12 != null) {
                                                                            }
                                                                            if (arrayList13 != null) {
                                                                            }
                                                                            sQLiteCursor5 = sQLiteCursor4;
                                                                            sQLiteDatabase3 = this.database;
                                                                            if (sQLiteDatabase3 != null) {
                                                                            }
                                                                            if (sQLiteCursor5 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement3 != null) {
                                                                            }
                                                                            return arrayList14;
                                                                        }
                                                                    } else {
                                                                        longSparseArray2 = longSparseArray12;
                                                                    }
                                                                    if (intValue3 < 2) {
                                                                        Integer num3 = numArr2[1];
                                                                        numArr2[1] = Integer.valueOf(numArr2[1].intValue() + 1);
                                                                    }
                                                                    if (intValue3 == 0 || intValue3 == 2) {
                                                                        Integer num4 = numArr2[0];
                                                                        numArr2[0] = Integer.valueOf(numArr2[0].intValue() + 1);
                                                                    }
                                                                    if ((!DialogObject.isEncryptedDialog(longValue2) || z || longValue2 == clientUserId) && (byteBufferValue = queryFinalized.byteBufferValue(1)) != null) {
                                                                        TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                                                                        TLdeserialize.readAttachPath(byteBufferValue, clientUserId);
                                                                        byteBufferValue.reuse();
                                                                        if (!DialogObject.isEncryptedDialog(longValue2) || z) {
                                                                            HashMap hashMap5 = hashMap;
                                                                            longSparseArray3 = longSparseArray;
                                                                            str = join2;
                                                                            tLRPC$Message = TLdeserialize;
                                                                            hashMap2 = hashMap5;
                                                                            arrayList5 = arrayList2;
                                                                            longSparseArray4 = longSparseArray9;
                                                                            arrayList6 = arrayList18;
                                                                            ArrayList arrayList30 = arrayList24;
                                                                            arrayList3 = arrayList23;
                                                                            arrayList4 = arrayList24;
                                                                            i2 = 0;
                                                                            addFilesToDelete(TLdeserialize, arrayList23, arrayList25, arrayList30, false);
                                                                        } else {
                                                                            hashMap2 = hashMap;
                                                                            arrayList3 = arrayList23;
                                                                            str = join2;
                                                                            arrayList5 = arrayList2;
                                                                            tLRPC$Message = TLdeserialize;
                                                                            longSparseArray4 = longSparseArray9;
                                                                            ArrayList arrayList31 = arrayList24;
                                                                            i2 = 0;
                                                                            longSparseArray3 = longSparseArray;
                                                                            arrayList6 = arrayList18;
                                                                            arrayList4 = arrayList31;
                                                                        }
                                                                        if (longValue2 != clientUserId) {
                                                                            try {
                                                                                long savedDialogId = MessageObject.getSavedDialogId(clientUserId, tLRPC$Message);
                                                                                if (savedDialogId != 0) {
                                                                                    ArrayList arrayList32 = (ArrayList) longSparseArray3.get(savedDialogId);
                                                                                    if (arrayList32 == null) {
                                                                                        arrayList32 = new ArrayList();
                                                                                        longSparseArray3.put(savedDialogId, arrayList32);
                                                                                    }
                                                                                    arrayList32.add(Integer.valueOf(intValue2));
                                                                                }
                                                                            } catch (Exception e7) {
                                                                                e = e7;
                                                                                exc3 = e;
                                                                                i19 = i2;
                                                                                checkSQLException(exc3);
                                                                                i3 = i19;
                                                                                queryFinalized.dispose();
                                                                                if (j < 0) {
                                                                                }
                                                                                this.database.beginTransaction();
                                                                                i5 = i3;
                                                                                while (i5 < i4) {
                                                                                }
                                                                                String str3222 = str;
                                                                                deleteFromDownloadQueue(arrayList25, true);
                                                                                final ArrayList arrayList28222 = arrayList4;
                                                                                AndroidUtilities.runOnUIThread(new Runnable() {
                                                                                    @Override
                                                                                    public final void run() {
                                                                                        MessagesStorage.this.lambda$markMessagesAsDeletedInternal$196(arrayList28222);
                                                                                    }
                                                                                });
                                                                                getFileLoader().deleteFiles(arrayList3, i3);
                                                                                i6 = i3;
                                                                                while (i6 < longSparseArray4.size()) {
                                                                                }
                                                                                ArrayList<Long> arrayList29222 = arrayList26;
                                                                                LongSparseArray longSparseArray14222 = longSparseArray4;
                                                                                if (hashMap3.isEmpty()) {
                                                                                }
                                                                                size = longSparseArray2.size();
                                                                                SQLitePreparedStatement sQLitePreparedStatement5222 = null;
                                                                                SQLiteCursor sQLiteCursor9222 = null;
                                                                                i7 = 0;
                                                                                while (i7 < size) {
                                                                                }
                                                                                longSparseArray6 = longSparseArray14222;
                                                                                sQLitePreparedStatement3 = sQLitePreparedStatement5222;
                                                                                arrayList12 = arrayList9;
                                                                                sQLiteCursor4 = sQLiteCursor9222;
                                                                                arrayList13 = arrayList11;
                                                                                if (!longSparseArray5.isEmpty()) {
                                                                                }
                                                                                SQLiteDatabase sQLiteDatabase4222 = this.database;
                                                                                Locale locale2222 = Locale.US;
                                                                                sQLiteDatabase4222.executeFast(String.format(locale2222, "DELETE FROM messages_seq WHERE mid IN(%s)", str3222)).stepThis().dispose();
                                                                                if (!arrayList6.isEmpty()) {
                                                                                }
                                                                                if (!arrayList7.isEmpty()) {
                                                                                }
                                                                                getMediaDataController().clearBotKeyboard(null, arrayList);
                                                                                if (longSparseArray6.size() != 0) {
                                                                                }
                                                                                arrayList14 = arrayList10;
                                                                                updateWidgets(arrayList14);
                                                                                if (arrayList12 != null) {
                                                                                }
                                                                                if (arrayList13 != null) {
                                                                                }
                                                                                sQLiteCursor5 = sQLiteCursor4;
                                                                                sQLiteDatabase3 = this.database;
                                                                                if (sQLiteDatabase3 != null) {
                                                                                }
                                                                                if (sQLiteCursor5 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement3 != null) {
                                                                                }
                                                                                return arrayList14;
                                                                            }
                                                                        } else {
                                                                            continue;
                                                                        }
                                                                    } else {
                                                                        hashMap2 = hashMap;
                                                                        arrayList3 = arrayList23;
                                                                        str = join2;
                                                                        longSparseArray3 = longSparseArray;
                                                                        arrayList5 = arrayList2;
                                                                        arrayList6 = arrayList18;
                                                                        longSparseArray4 = longSparseArray9;
                                                                        arrayList4 = arrayList24;
                                                                        i2 = 0;
                                                                    }
                                                                    join2 = str;
                                                                    i = i2;
                                                                    arrayList21 = arrayList6;
                                                                    longSparseArray10 = longSparseArray4;
                                                                    arrayList23 = arrayList3;
                                                                    arrayList24 = arrayList4;
                                                                    arrayList2 = arrayList5;
                                                                    hashMap = hashMap2;
                                                                    longSparseArray = longSparseArray3;
                                                                    longSparseArray12 = longSparseArray2;
                                                                } catch (Exception e8) {
                                                                    e = e8;
                                                                    longSparseArray2 = longSparseArray12;
                                                                }
                                                            }
                                                        } catch (Exception e9) {
                                                            longSparseArray2 = longSparseArray12;
                                                            hashMap2 = hashMap;
                                                            exc3 = e9;
                                                            arrayList4 = arrayList24;
                                                            str = join2;
                                                            longSparseArray3 = longSparseArray;
                                                            arrayList5 = arrayList2;
                                                            longSparseArray4 = longSparseArray13;
                                                            i19 = 0;
                                                            arrayList6 = arrayList21;
                                                            arrayList3 = arrayList23;
                                                            checkSQLException(exc3);
                                                            i3 = i19;
                                                            queryFinalized.dispose();
                                                            if (j < 0) {
                                                            }
                                                            this.database.beginTransaction();
                                                            i5 = i3;
                                                            while (i5 < i4) {
                                                            }
                                                            String str32222 = str;
                                                            deleteFromDownloadQueue(arrayList25, true);
                                                            final ArrayList arrayList282222 = arrayList4;
                                                            AndroidUtilities.runOnUIThread(new Runnable() {
                                                                @Override
                                                                public final void run() {
                                                                    MessagesStorage.this.lambda$markMessagesAsDeletedInternal$196(arrayList282222);
                                                                }
                                                            });
                                                            getFileLoader().deleteFiles(arrayList3, i3);
                                                            i6 = i3;
                                                            while (i6 < longSparseArray4.size()) {
                                                            }
                                                            ArrayList<Long> arrayList292222 = arrayList26;
                                                            LongSparseArray longSparseArray142222 = longSparseArray4;
                                                            if (hashMap3.isEmpty()) {
                                                            }
                                                            size = longSparseArray2.size();
                                                            SQLitePreparedStatement sQLitePreparedStatement52222 = null;
                                                            SQLiteCursor sQLiteCursor92222 = null;
                                                            i7 = 0;
                                                            while (i7 < size) {
                                                            }
                                                            longSparseArray6 = longSparseArray142222;
                                                            sQLitePreparedStatement3 = sQLitePreparedStatement52222;
                                                            arrayList12 = arrayList9;
                                                            sQLiteCursor4 = sQLiteCursor92222;
                                                            arrayList13 = arrayList11;
                                                            if (!longSparseArray5.isEmpty()) {
                                                            }
                                                            SQLiteDatabase sQLiteDatabase42222 = this.database;
                                                            Locale locale22222 = Locale.US;
                                                            sQLiteDatabase42222.executeFast(String.format(locale22222, "DELETE FROM messages_seq WHERE mid IN(%s)", str32222)).stepThis().dispose();
                                                            if (!arrayList6.isEmpty()) {
                                                            }
                                                            if (!arrayList7.isEmpty()) {
                                                            }
                                                            getMediaDataController().clearBotKeyboard(null, arrayList);
                                                            if (longSparseArray6.size() != 0) {
                                                            }
                                                            arrayList14 = arrayList10;
                                                            updateWidgets(arrayList14);
                                                            if (arrayList12 != null) {
                                                            }
                                                            if (arrayList13 != null) {
                                                            }
                                                            sQLiteCursor5 = sQLiteCursor4;
                                                            sQLiteDatabase3 = this.database;
                                                            if (sQLiteDatabase3 != null) {
                                                            }
                                                            if (sQLiteCursor5 != null) {
                                                            }
                                                            if (sQLitePreparedStatement3 != null) {
                                                            }
                                                            return arrayList14;
                                                        }
                                                    }
                                                    TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                                                    TLdeserialize.readAttachPath(byteBufferValue, clientUserId);
                                                    byteBufferValue.reuse();
                                                    if (DialogObject.isEncryptedDialog(longValue2)) {
                                                    }
                                                    HashMap hashMap52 = hashMap;
                                                    longSparseArray3 = longSparseArray;
                                                    str = join2;
                                                    tLRPC$Message = TLdeserialize;
                                                    hashMap2 = hashMap52;
                                                    arrayList5 = arrayList2;
                                                    longSparseArray4 = longSparseArray9;
                                                    arrayList6 = arrayList18;
                                                    ArrayList arrayList302 = arrayList24;
                                                    arrayList3 = arrayList23;
                                                    arrayList4 = arrayList24;
                                                    i2 = 0;
                                                    addFilesToDelete(TLdeserialize, arrayList23, arrayList25, arrayList302, false);
                                                    if (longValue2 != clientUserId) {
                                                    }
                                                    join2 = str;
                                                    i = i2;
                                                    arrayList21 = arrayList6;
                                                    longSparseArray10 = longSparseArray4;
                                                    arrayList23 = arrayList3;
                                                    arrayList24 = arrayList4;
                                                    arrayList2 = arrayList5;
                                                    hashMap = hashMap2;
                                                    longSparseArray = longSparseArray3;
                                                    longSparseArray12 = longSparseArray2;
                                                } catch (Exception e10) {
                                                    e = e10;
                                                    hashMap2 = hashMap;
                                                    arrayList3 = arrayList23;
                                                    str = join2;
                                                    arrayList5 = arrayList2;
                                                    longSparseArray4 = longSparseArray9;
                                                    ArrayList arrayList33 = arrayList24;
                                                    i2 = 0;
                                                    longSparseArray3 = longSparseArray;
                                                    arrayList6 = arrayList18;
                                                    arrayList4 = arrayList33;
                                                }
                                                if (DialogObject.isEncryptedDialog(longValue2)) {
                                                }
                                            } catch (Exception e11) {
                                                e = e11;
                                                hashMap2 = hashMap;
                                                arrayList3 = arrayList23;
                                                str = join2;
                                                longSparseArray3 = longSparseArray;
                                                arrayList5 = arrayList2;
                                                arrayList6 = arrayList18;
                                                longSparseArray4 = longSparseArray9;
                                                arrayList4 = arrayList24;
                                                i2 = 0;
                                            }
                                            arrayList18 = arrayList21;
                                            longSparseArray2 = longSparseArray12;
                                            longSparseArray9 = longSparseArray13;
                                        } catch (Exception e12) {
                                            e = e12;
                                            hashMap2 = hashMap;
                                            longSparseArray2 = longSparseArray12;
                                            arrayList4 = arrayList24;
                                            str = join2;
                                            longSparseArray3 = longSparseArray;
                                            arrayList5 = arrayList2;
                                            longSparseArray4 = longSparseArray13;
                                            i2 = 0;
                                            arrayList6 = arrayList21;
                                            arrayList3 = arrayList23;
                                        }
                                    } catch (Exception e13) {
                                        e = e13;
                                        hashMap2 = hashMap;
                                        longSparseArray2 = longSparseArray12;
                                        arrayList4 = arrayList24;
                                        str = join2;
                                        longSparseArray3 = longSparseArray;
                                        arrayList5 = arrayList2;
                                        longSparseArray4 = longSparseArray13;
                                        arrayList6 = arrayList21;
                                        arrayList3 = arrayList23;
                                        i2 = i;
                                        exc3 = e;
                                        i19 = i2;
                                        checkSQLException(exc3);
                                        i3 = i19;
                                        queryFinalized.dispose();
                                        if (j < 0) {
                                        }
                                        this.database.beginTransaction();
                                        i5 = i3;
                                        while (i5 < i4) {
                                        }
                                        String str322222 = str;
                                        deleteFromDownloadQueue(arrayList25, true);
                                        final ArrayList arrayList2822222 = arrayList4;
                                        AndroidUtilities.runOnUIThread(new Runnable() {
                                            @Override
                                            public final void run() {
                                                MessagesStorage.this.lambda$markMessagesAsDeletedInternal$196(arrayList2822222);
                                            }
                                        });
                                        getFileLoader().deleteFiles(arrayList3, i3);
                                        i6 = i3;
                                        while (i6 < longSparseArray4.size()) {
                                        }
                                        ArrayList<Long> arrayList2922222 = arrayList26;
                                        LongSparseArray longSparseArray1422222 = longSparseArray4;
                                        if (hashMap3.isEmpty()) {
                                        }
                                        size = longSparseArray2.size();
                                        SQLitePreparedStatement sQLitePreparedStatement522222 = null;
                                        SQLiteCursor sQLiteCursor922222 = null;
                                        i7 = 0;
                                        while (i7 < size) {
                                        }
                                        longSparseArray6 = longSparseArray1422222;
                                        sQLitePreparedStatement3 = sQLitePreparedStatement522222;
                                        arrayList12 = arrayList9;
                                        sQLiteCursor4 = sQLiteCursor922222;
                                        arrayList13 = arrayList11;
                                        if (!longSparseArray5.isEmpty()) {
                                        }
                                        SQLiteDatabase sQLiteDatabase422222 = this.database;
                                        Locale locale222222 = Locale.US;
                                        sQLiteDatabase422222.executeFast(String.format(locale222222, "DELETE FROM messages_seq WHERE mid IN(%s)", str322222)).stepThis().dispose();
                                        if (!arrayList6.isEmpty()) {
                                        }
                                        if (!arrayList7.isEmpty()) {
                                        }
                                        getMediaDataController().clearBotKeyboard(null, arrayList);
                                        if (longSparseArray6.size() != 0) {
                                        }
                                        arrayList14 = arrayList10;
                                        updateWidgets(arrayList14);
                                        if (arrayList12 != null) {
                                        }
                                        if (arrayList13 != null) {
                                        }
                                        sQLiteCursor5 = sQLiteCursor4;
                                        sQLiteDatabase3 = this.database;
                                        if (sQLiteDatabase3 != null) {
                                        }
                                        if (sQLiteCursor5 != null) {
                                        }
                                        if (sQLitePreparedStatement3 != null) {
                                        }
                                        return arrayList14;
                                    }
                                } catch (Exception e14) {
                                    e = e14;
                                    hashMap2 = hashMap;
                                    arrayList3 = arrayList23;
                                    longSparseArray2 = longSparseArray12;
                                    arrayList4 = arrayList24;
                                    str = join2;
                                    longSparseArray3 = longSparseArray;
                                    arrayList5 = arrayList2;
                                    longSparseArray4 = longSparseArray10;
                                    arrayList6 = arrayList21;
                                }
                            } catch (Throwable th5) {
                                th = th5;
                                sQLiteCursor = queryFinalized;
                                sQLitePreparedStatement2 = null;
                                sQLiteDatabase = this.database;
                                if (sQLiteDatabase != null) {
                                }
                                if (sQLiteCursor != null) {
                                }
                                if (sQLitePreparedStatement2 == null) {
                                }
                            }
                        }
                        hashMap2 = hashMap;
                        arrayList3 = arrayList23;
                        longSparseArray2 = longSparseArray12;
                        arrayList4 = arrayList24;
                        str = join2;
                        longSparseArray3 = longSparseArray;
                        arrayList5 = arrayList2;
                        longSparseArray4 = longSparseArray10;
                        arrayList6 = arrayList21;
                        i3 = i;
                        queryFinalized.dispose();
                        if (j < 0) {
                            SQLiteDatabase sQLiteDatabase5 = this.database;
                            Locale locale3 = Locale.US;
                            Object[] objArr = new Object[2];
                            objArr[i3] = str;
                            objArr[1] = Long.valueOf(j);
                            SQLiteCursor queryFinalized3 = sQLiteDatabase5.queryFinalized(String.format(locale3, "SELECT uid, data, read_state, out, mention, mid FROM messages_topics WHERE mid IN(%s) AND uid = %d", objArr), new Object[i3]);
                            arrayList8 = null;
                            while (queryFinalized3.next()) {
                                try {
                                    try {
                                        long longValue3 = queryFinalized3.longValue(i3);
                                        ArrayList arrayList34 = arrayList5;
                                        try {
                                            arrayList34.remove(Integer.valueOf(queryFinalized3.intValue(5)));
                                            NativeByteBuffer byteBufferValue2 = queryFinalized3.byteBufferValue(1);
                                            if (byteBufferValue2 != null) {
                                                try {
                                                    TLRPC$Message TLdeserialize2 = TLRPC$Message.TLdeserialize(byteBufferValue2, byteBufferValue2.readInt32(i3), i3);
                                                    TLdeserialize2.readAttachPath(byteBufferValue2, getUserConfig().clientUserId);
                                                    byteBufferValue2.reuse();
                                                    arrayList7 = arrayList34;
                                                    try {
                                                        addFilesToDelete(TLdeserialize2, arrayList3, arrayList25, arrayList4, false);
                                                        if (TLdeserialize2.action instanceof TLRPC$TL_messageActionTopicCreate) {
                                                            if (arrayList8 == null) {
                                                                arrayList8 = new ArrayList<>();
                                                            }
                                                            arrayList8.add(TopicKey.of(longValue3, TLdeserialize2.id));
                                                        }
                                                        topicId = MessageObject.getTopicId(this.currentAccount, TLdeserialize2, isForum(longValue3));
                                                    } catch (Exception e15) {
                                                        e = e15;
                                                        j2 = j;
                                                        exc2 = e;
                                                        hashMap3 = hashMap2;
                                                        i4 = 3;
                                                        try {
                                                            checkSQLException(exc2);
                                                            queryFinalized3.dispose();
                                                            this.database.beginTransaction();
                                                            i5 = i3;
                                                            while (i5 < i4) {
                                                            }
                                                            String str3222222 = str;
                                                            deleteFromDownloadQueue(arrayList25, true);
                                                            final ArrayList arrayList28222222 = arrayList4;
                                                            AndroidUtilities.runOnUIThread(new Runnable() {
                                                                @Override
                                                                public final void run() {
                                                                    MessagesStorage.this.lambda$markMessagesAsDeletedInternal$196(arrayList28222222);
                                                                }
                                                            });
                                                            getFileLoader().deleteFiles(arrayList3, i3);
                                                            i6 = i3;
                                                            while (i6 < longSparseArray4.size()) {
                                                            }
                                                            ArrayList<Long> arrayList29222222 = arrayList26;
                                                            LongSparseArray longSparseArray14222222 = longSparseArray4;
                                                            if (hashMap3.isEmpty()) {
                                                            }
                                                            size = longSparseArray2.size();
                                                            SQLitePreparedStatement sQLitePreparedStatement5222222 = null;
                                                            SQLiteCursor sQLiteCursor9222222 = null;
                                                            i7 = 0;
                                                            while (i7 < size) {
                                                            }
                                                            longSparseArray6 = longSparseArray14222222;
                                                            sQLitePreparedStatement3 = sQLitePreparedStatement5222222;
                                                            arrayList12 = arrayList9;
                                                            sQLiteCursor4 = sQLiteCursor9222222;
                                                            arrayList13 = arrayList11;
                                                            if (!longSparseArray5.isEmpty()) {
                                                            }
                                                            SQLiteDatabase sQLiteDatabase4222222 = this.database;
                                                            Locale locale2222222 = Locale.US;
                                                            sQLiteDatabase4222222.executeFast(String.format(locale2222222, "DELETE FROM messages_seq WHERE mid IN(%s)", str3222222)).stepThis().dispose();
                                                            if (!arrayList6.isEmpty()) {
                                                            }
                                                            if (!arrayList7.isEmpty()) {
                                                            }
                                                            getMediaDataController().clearBotKeyboard(null, arrayList);
                                                            if (longSparseArray6.size() != 0) {
                                                            }
                                                            arrayList14 = arrayList10;
                                                            updateWidgets(arrayList14);
                                                            if (arrayList12 != null) {
                                                            }
                                                            if (arrayList13 != null) {
                                                            }
                                                            sQLiteCursor5 = sQLiteCursor4;
                                                            sQLiteDatabase3 = this.database;
                                                            if (sQLiteDatabase3 != null) {
                                                            }
                                                            if (sQLiteCursor5 != null) {
                                                            }
                                                            if (sQLitePreparedStatement3 != null) {
                                                            }
                                                            return arrayList14;
                                                        } catch (Exception e16) {
                                                            exc = e16;
                                                            sQLiteCursor2 = queryFinalized3;
                                                            sQLitePreparedStatement = null;
                                                            checkSQLException(exc);
                                                            sQLiteDatabase2 = this.database;
                                                            if (sQLiteDatabase2 != null) {
                                                            }
                                                            if (sQLiteCursor2 != null) {
                                                            }
                                                            if (sQLitePreparedStatement == null) {
                                                            }
                                                        }
                                                    }
                                                } catch (Exception e17) {
                                                    e = e17;
                                                    arrayList7 = arrayList34;
                                                }
                                            } else {
                                                arrayList7 = arrayList34;
                                                topicId = 0;
                                            }
                                            if (topicId != 0) {
                                                j2 = j;
                                                try {
                                                    of = TopicKey.of(j2, topicId);
                                                    intValue = queryFinalized3.intValue(2);
                                                    hashMap3 = hashMap2;
                                                } catch (Exception e18) {
                                                    e = e18;
                                                    hashMap3 = hashMap2;
                                                    i4 = 3;
                                                    exc2 = e;
                                                    checkSQLException(exc2);
                                                    queryFinalized3.dispose();
                                                    this.database.beginTransaction();
                                                    i5 = i3;
                                                    while (i5 < i4) {
                                                    }
                                                    String str32222222 = str;
                                                    deleteFromDownloadQueue(arrayList25, true);
                                                    final ArrayList arrayList282222222 = arrayList4;
                                                    AndroidUtilities.runOnUIThread(new Runnable() {
                                                        @Override
                                                        public final void run() {
                                                            MessagesStorage.this.lambda$markMessagesAsDeletedInternal$196(arrayList282222222);
                                                        }
                                                    });
                                                    getFileLoader().deleteFiles(arrayList3, i3);
                                                    i6 = i3;
                                                    while (i6 < longSparseArray4.size()) {
                                                    }
                                                    ArrayList<Long> arrayList292222222 = arrayList26;
                                                    LongSparseArray longSparseArray142222222 = longSparseArray4;
                                                    if (hashMap3.isEmpty()) {
                                                    }
                                                    size = longSparseArray2.size();
                                                    SQLitePreparedStatement sQLitePreparedStatement52222222 = null;
                                                    SQLiteCursor sQLiteCursor92222222 = null;
                                                    i7 = 0;
                                                    while (i7 < size) {
                                                    }
                                                    longSparseArray6 = longSparseArray142222222;
                                                    sQLitePreparedStatement3 = sQLitePreparedStatement52222222;
                                                    arrayList12 = arrayList9;
                                                    sQLiteCursor4 = sQLiteCursor92222222;
                                                    arrayList13 = arrayList11;
                                                    if (!longSparseArray5.isEmpty()) {
                                                    }
                                                    SQLiteDatabase sQLiteDatabase42222222 = this.database;
                                                    Locale locale22222222 = Locale.US;
                                                    sQLiteDatabase42222222.executeFast(String.format(locale22222222, "DELETE FROM messages_seq WHERE mid IN(%s)", str32222222)).stepThis().dispose();
                                                    if (!arrayList6.isEmpty()) {
                                                    }
                                                    if (!arrayList7.isEmpty()) {
                                                    }
                                                    getMediaDataController().clearBotKeyboard(null, arrayList);
                                                    if (longSparseArray6.size() != 0) {
                                                    }
                                                    arrayList14 = arrayList10;
                                                    updateWidgets(arrayList14);
                                                    if (arrayList12 != null) {
                                                    }
                                                    if (arrayList13 != null) {
                                                    }
                                                    sQLiteCursor5 = sQLiteCursor4;
                                                    sQLiteDatabase3 = this.database;
                                                    if (sQLiteDatabase3 != null) {
                                                    }
                                                    if (sQLiteCursor5 != null) {
                                                    }
                                                    if (sQLitePreparedStatement3 != null) {
                                                    }
                                                    return arrayList14;
                                                }
                                                try {
                                                    int[] iArr = (int[]) hashMap3.get(of);
                                                    if (iArr == null) {
                                                        i4 = 3;
                                                        try {
                                                            iArr = new int[3];
                                                            hashMap3.put(of, iArr);
                                                        } catch (Exception e19) {
                                                            e = e19;
                                                            exc2 = e;
                                                            checkSQLException(exc2);
                                                            queryFinalized3.dispose();
                                                            this.database.beginTransaction();
                                                            i5 = i3;
                                                            while (i5 < i4) {
                                                            }
                                                            String str322222222 = str;
                                                            deleteFromDownloadQueue(arrayList25, true);
                                                            final ArrayList arrayList2822222222 = arrayList4;
                                                            AndroidUtilities.runOnUIThread(new Runnable() {
                                                                @Override
                                                                public final void run() {
                                                                    MessagesStorage.this.lambda$markMessagesAsDeletedInternal$196(arrayList2822222222);
                                                                }
                                                            });
                                                            getFileLoader().deleteFiles(arrayList3, i3);
                                                            i6 = i3;
                                                            while (i6 < longSparseArray4.size()) {
                                                            }
                                                            ArrayList<Long> arrayList2922222222 = arrayList26;
                                                            LongSparseArray longSparseArray1422222222 = longSparseArray4;
                                                            if (hashMap3.isEmpty()) {
                                                            }
                                                            size = longSparseArray2.size();
                                                            SQLitePreparedStatement sQLitePreparedStatement522222222 = null;
                                                            SQLiteCursor sQLiteCursor922222222 = null;
                                                            i7 = 0;
                                                            while (i7 < size) {
                                                            }
                                                            longSparseArray6 = longSparseArray1422222222;
                                                            sQLitePreparedStatement3 = sQLitePreparedStatement522222222;
                                                            arrayList12 = arrayList9;
                                                            sQLiteCursor4 = sQLiteCursor922222222;
                                                            arrayList13 = arrayList11;
                                                            if (!longSparseArray5.isEmpty()) {
                                                            }
                                                            SQLiteDatabase sQLiteDatabase422222222 = this.database;
                                                            Locale locale222222222 = Locale.US;
                                                            sQLiteDatabase422222222.executeFast(String.format(locale222222222, "DELETE FROM messages_seq WHERE mid IN(%s)", str322222222)).stepThis().dispose();
                                                            if (!arrayList6.isEmpty()) {
                                                            }
                                                            if (!arrayList7.isEmpty()) {
                                                            }
                                                            getMediaDataController().clearBotKeyboard(null, arrayList);
                                                            if (longSparseArray6.size() != 0) {
                                                            }
                                                            arrayList14 = arrayList10;
                                                            updateWidgets(arrayList14);
                                                            if (arrayList12 != null) {
                                                            }
                                                            if (arrayList13 != null) {
                                                            }
                                                            sQLiteCursor5 = sQLiteCursor4;
                                                            sQLiteDatabase3 = this.database;
                                                            if (sQLiteDatabase3 != null) {
                                                            }
                                                            if (sQLiteCursor5 != null) {
                                                            }
                                                            if (sQLitePreparedStatement3 != null) {
                                                            }
                                                            return arrayList14;
                                                        }
                                                    } else {
                                                        i4 = 3;
                                                    }
                                                    iArr[2] = iArr[2] + 1;
                                                    if (queryFinalized3.intValue(i4) == 0) {
                                                        if (intValue < 2) {
                                                            iArr[1] = iArr[1] + 1;
                                                        }
                                                        if (intValue == 0 || intValue == 2) {
                                                            iArr[i3] = iArr[i3] + 1;
                                                        }
                                                    }
                                                } catch (Exception e20) {
                                                    e = e20;
                                                    i4 = 3;
                                                    exc2 = e;
                                                    checkSQLException(exc2);
                                                    queryFinalized3.dispose();
                                                    this.database.beginTransaction();
                                                    i5 = i3;
                                                    while (i5 < i4) {
                                                    }
                                                    String str3222222222 = str;
                                                    deleteFromDownloadQueue(arrayList25, true);
                                                    final ArrayList arrayList28222222222 = arrayList4;
                                                    AndroidUtilities.runOnUIThread(new Runnable() {
                                                        @Override
                                                        public final void run() {
                                                            MessagesStorage.this.lambda$markMessagesAsDeletedInternal$196(arrayList28222222222);
                                                        }
                                                    });
                                                    getFileLoader().deleteFiles(arrayList3, i3);
                                                    i6 = i3;
                                                    while (i6 < longSparseArray4.size()) {
                                                    }
                                                    ArrayList<Long> arrayList29222222222 = arrayList26;
                                                    LongSparseArray longSparseArray14222222222 = longSparseArray4;
                                                    if (hashMap3.isEmpty()) {
                                                    }
                                                    size = longSparseArray2.size();
                                                    SQLitePreparedStatement sQLitePreparedStatement5222222222 = null;
                                                    SQLiteCursor sQLiteCursor9222222222 = null;
                                                    i7 = 0;
                                                    while (i7 < size) {
                                                    }
                                                    longSparseArray6 = longSparseArray14222222222;
                                                    sQLitePreparedStatement3 = sQLitePreparedStatement5222222222;
                                                    arrayList12 = arrayList9;
                                                    sQLiteCursor4 = sQLiteCursor9222222222;
                                                    arrayList13 = arrayList11;
                                                    if (!longSparseArray5.isEmpty()) {
                                                    }
                                                    SQLiteDatabase sQLiteDatabase4222222222 = this.database;
                                                    Locale locale2222222222 = Locale.US;
                                                    sQLiteDatabase4222222222.executeFast(String.format(locale2222222222, "DELETE FROM messages_seq WHERE mid IN(%s)", str3222222222)).stepThis().dispose();
                                                    if (!arrayList6.isEmpty()) {
                                                    }
                                                    if (!arrayList7.isEmpty()) {
                                                    }
                                                    getMediaDataController().clearBotKeyboard(null, arrayList);
                                                    if (longSparseArray6.size() != 0) {
                                                    }
                                                    arrayList14 = arrayList10;
                                                    updateWidgets(arrayList14);
                                                    if (arrayList12 != null) {
                                                    }
                                                    if (arrayList13 != null) {
                                                    }
                                                    sQLiteCursor5 = sQLiteCursor4;
                                                    sQLiteDatabase3 = this.database;
                                                    if (sQLiteDatabase3 != null) {
                                                    }
                                                    if (sQLiteCursor5 != null) {
                                                    }
                                                    if (sQLitePreparedStatement3 != null) {
                                                    }
                                                    return arrayList14;
                                                }
                                            } else {
                                                hashMap3 = hashMap2;
                                            }
                                            hashMap2 = hashMap3;
                                            arrayList5 = arrayList7;
                                        } catch (Exception e21) {
                                            e = e21;
                                            j2 = j;
                                            arrayList7 = arrayList34;
                                        }
                                    } catch (Throwable th6) {
                                        th = th6;
                                        sQLiteCursor = queryFinalized3;
                                        sQLitePreparedStatement2 = null;
                                        sQLiteDatabase = this.database;
                                        if (sQLiteDatabase != null) {
                                        }
                                        if (sQLiteCursor != null) {
                                        }
                                        if (sQLitePreparedStatement2 == null) {
                                        }
                                    }
                                } catch (Exception e22) {
                                    e = e22;
                                    j2 = j;
                                    arrayList7 = arrayList5;
                                }
                            }
                            j2 = j;
                            arrayList7 = arrayList5;
                            hashMap3 = hashMap2;
                            i4 = 3;
                            queryFinalized3.dispose();
                        } else {
                            j2 = j;
                            arrayList7 = arrayList5;
                            hashMap3 = hashMap2;
                            i4 = 3;
                            arrayList8 = null;
                        }
                        this.database.beginTransaction();
                        i5 = i3;
                        while (i5 < i4) {
                            try {
                                if (i5 == 0) {
                                    if (j != 0) {
                                        sQLitePreparedStatement = getMessagesStorage().getDatabase().executeFast("UPDATE messages_v2 SET replydata = ? WHERE reply_to_message_id IN(?) AND uid = ?");
                                    } else {
                                        sQLitePreparedStatement = getMessagesStorage().getDatabase().executeFast("UPDATE messages_v2 SET replydata = ? WHERE reply_to_message_id IN(?) AND is_channel = 0");
                                    }
                                } else if (i5 == 1) {
                                    if (j != 0) {
                                        sQLitePreparedStatement = getMessagesStorage().getDatabase().executeFast("UPDATE scheduled_messages_v2 SET replydata = ? WHERE reply_to_message_id IN(?) AND uid = ?");
                                    } else {
                                        sQLitePreparedStatement = getMessagesStorage().getDatabase().executeFast("UPDATE scheduled_messages_v2 SET replydata = ? WHERE reply_to_message_id IN(?)");
                                    }
                                } else if (j == 0) {
                                    str2 = str;
                                    i5++;
                                    str = str2;
                                } else {
                                    sQLitePreparedStatement = getMessagesStorage().getDatabase().executeFast("UPDATE messages_topics SET replydata = ? WHERE reply_to_message_id IN(?) AND uid = ?");
                                }
                                TLRPC$TL_messageEmpty tLRPC$TL_messageEmpty = new TLRPC$TL_messageEmpty();
                                NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$TL_messageEmpty.getObjectSize());
                                tLRPC$TL_messageEmpty.serializeToStream(nativeByteBuffer);
                                sQLitePreparedStatement.requery();
                                sQLitePreparedStatement.bindByteBuffer(1, nativeByteBuffer);
                                str2 = str;
                                sQLitePreparedStatement.bindString(2, str2);
                                if (j != 0) {
                                    sQLitePreparedStatement.bindLong(i4, j2);
                                }
                                sQLitePreparedStatement.step();
                                sQLitePreparedStatement.dispose();
                                this.database.commitTransaction();
                                nativeByteBuffer.reuse();
                                i5++;
                                str = str2;
                            } catch (Exception e23) {
                                exc = e23;
                                sQLiteCursor2 = null;
                                checkSQLException(exc);
                                sQLiteDatabase2 = this.database;
                                if (sQLiteDatabase2 != null) {
                                }
                                if (sQLiteCursor2 != null) {
                                }
                                if (sQLitePreparedStatement == null) {
                                }
                            } catch (Throwable th7) {
                                th2 = th7;
                                sQLitePreparedStatement2 = sQLitePreparedStatement;
                                sQLiteCursor = null;
                                th = th2;
                                sQLiteDatabase = this.database;
                                if (sQLiteDatabase != null) {
                                }
                                if (sQLiteCursor != null) {
                                }
                                if (sQLitePreparedStatement2 == null) {
                                }
                            }
                        }
                        String str32222222222 = str;
                        deleteFromDownloadQueue(arrayList25, true);
                        final ArrayList arrayList282222222222 = arrayList4;
                        AndroidUtilities.runOnUIThread(new Runnable() {
                            @Override
                            public final void run() {
                                MessagesStorage.this.lambda$markMessagesAsDeletedInternal$196(arrayList282222222222);
                            }
                        });
                        getFileLoader().deleteFiles(arrayList3, i3);
                        i6 = i3;
                        while (i6 < longSparseArray4.size()) {
                            LongSparseArray longSparseArray15 = longSparseArray4;
                            long keyAt2 = longSparseArray15.keyAt(i6);
                            Integer[] numArr3 = (Integer[]) longSparseArray15.valueAt(i6);
                            SQLiteCursor queryFinalized4 = this.database.queryFinalized("SELECT unread_count, unread_count_i FROM dialogs WHERE did = " + keyAt2, new Object[0]);
                            if (queryFinalized4.next()) {
                                i17 = queryFinalized4.intValue(0);
                                i18 = queryFinalized4.intValue(1);
                            } else {
                                i17 = 0;
                                i18 = 0;
                            }
                            queryFinalized4.dispose();
                            ArrayList<Long> arrayList35 = arrayList26;
                            arrayList35.add(Long.valueOf(keyAt2));
                            executeFast = this.database.executeFast("UPDATE dialogs SET unread_count = ?, unread_count_i = ? WHERE did = ?");
                            try {
                                executeFast.requery();
                                executeFast.bindInteger(1, Math.max(0, i17 - numArr3[0].intValue()));
                                executeFast.bindInteger(2, Math.max(0, i18 - numArr3[1].intValue()));
                                executeFast.bindLong(3, keyAt2);
                                executeFast.step();
                                executeFast.dispose();
                                i6++;
                                longSparseArray4 = longSparseArray15;
                                arrayList26 = arrayList35;
                            } catch (Exception e24) {
                                exc = e24;
                                sQLitePreparedStatement = executeFast;
                                sQLiteCursor2 = null;
                                checkSQLException(exc);
                                sQLiteDatabase2 = this.database;
                                if (sQLiteDatabase2 != null) {
                                }
                                if (sQLiteCursor2 != null) {
                                }
                                if (sQLitePreparedStatement == null) {
                                }
                            } catch (Throwable th8) {
                                th = th8;
                                sQLitePreparedStatement2 = executeFast;
                                sQLiteCursor = null;
                                sQLiteDatabase = this.database;
                                if (sQLiteDatabase != null) {
                                }
                                if (sQLiteCursor != null) {
                                }
                                if (sQLitePreparedStatement2 == null) {
                                }
                            }
                        }
                        ArrayList<Long> arrayList292222222222 = arrayList26;
                        LongSparseArray longSparseArray142222222222 = longSparseArray4;
                        if (hashMap3.isEmpty()) {
                            arrayList9 = arrayList8;
                            arrayList10 = arrayList292222222222;
                            longSparseArray5 = longSparseArray3;
                            arrayList11 = null;
                        } else {
                            Iterator it = hashMap3.keySet().iterator();
                            HashSet hashSet = null;
                            arrayList11 = null;
                            while (it.hasNext()) {
                                TopicKey topicKey = (TopicKey) it.next();
                                Iterator it2 = it;
                                ArrayList<TopicKey> arrayList36 = arrayList8;
                                ArrayList<Long> arrayList37 = arrayList292222222222;
                                LongSparseArray longSparseArray16 = longSparseArray3;
                                SQLiteCursor queryFinalized5 = this.database.queryFinalized(String.format(Locale.US, "SELECT unread_count, unread_mentions, total_messages_count FROM topics WHERE did = %d AND topic_id = %d", Long.valueOf(topicKey.dialogId), Long.valueOf(topicKey.topicId)), new Object[0]);
                                if (queryFinalized5.next()) {
                                    i14 = queryFinalized5.intValue(0);
                                    i15 = queryFinalized5.intValue(1);
                                    i16 = queryFinalized5.intValue(2);
                                } else {
                                    i14 = 0;
                                    i15 = 0;
                                    i16 = 0;
                                }
                                queryFinalized5.dispose();
                                int[] iArr2 = (int[]) hashMap3.get(topicKey);
                                int max = Math.max(0, i14 - iArr2[0]);
                                int max2 = Math.max(0, i15 - iArr2[1]);
                                int max3 = Math.max(0, i16 - iArr2[2]);
                                if (BuildVars.DEBUG_PRIVATE_VERSION && max2 > 0) {
                                    FileLog.d("(markMessagesAsDeletedInternal) new unread mentions " + max2 + " for dialog_id=" + topicKey.dialogId + " topic_id=" + topicKey.topicId);
                                }
                                SQLitePreparedStatement executeFast2 = this.database.executeFast("UPDATE topics SET unread_count = ?, unread_mentions = ?, total_messages_count = ? WHERE did = ? AND topic_id = ?");
                                try {
                                    executeFast2.requery();
                                    executeFast2.bindInteger(1, max);
                                    executeFast2.bindInteger(2, max2);
                                    executeFast2.bindLong(3, max3);
                                    executeFast2.bindLong(4, topicKey.dialogId);
                                    executeFast2.bindLong(5, topicKey.topicId);
                                    executeFast2.step();
                                    executeFast2.dispose();
                                    if (max == 0) {
                                        if (hashSet == null) {
                                            hashSet = new HashSet();
                                        }
                                        hashSet.add(Long.valueOf(topicKey.dialogId));
                                    }
                                    TopicsController.TopicUpdate topicUpdate = new TopicsController.TopicUpdate();
                                    topicUpdate.dialogId = topicKey.dialogId;
                                    topicUpdate.topicId = topicKey.topicId;
                                    topicUpdate.unreadCount = max;
                                    topicUpdate.totalMessagesCount = max3;
                                    topicUpdate.onlyCounters = true;
                                    if (arrayList11 == null) {
                                        arrayList11 = new ArrayList();
                                    }
                                    arrayList11.add(topicUpdate);
                                    it = it2;
                                    arrayList8 = arrayList36;
                                    arrayList292222222222 = arrayList37;
                                    longSparseArray3 = longSparseArray16;
                                } catch (Exception e25) {
                                    exc = e25;
                                    sQLitePreparedStatement = executeFast2;
                                    sQLiteCursor2 = null;
                                    checkSQLException(exc);
                                    sQLiteDatabase2 = this.database;
                                    if (sQLiteDatabase2 != null) {
                                    }
                                    if (sQLiteCursor2 != null) {
                                    }
                                    if (sQLitePreparedStatement == null) {
                                    }
                                } catch (Throwable th9) {
                                    th = th9;
                                    sQLitePreparedStatement2 = executeFast2;
                                    sQLiteCursor = null;
                                    sQLiteDatabase = this.database;
                                    if (sQLiteDatabase != null) {
                                    }
                                    if (sQLiteCursor != null) {
                                    }
                                    if (sQLitePreparedStatement2 == null) {
                                    }
                                }
                            }
                            arrayList9 = arrayList8;
                            arrayList10 = arrayList292222222222;
                            longSparseArray5 = longSparseArray3;
                            if (hashSet != null) {
                                Iterator it3 = hashSet.iterator();
                                while (it3.hasNext()) {
                                    resetForumBadgeIfNeed(((Long) it3.next()).longValue());
                                }
                            }
                        }
                        size = longSparseArray2.size();
                        SQLitePreparedStatement sQLitePreparedStatement52222222222 = null;
                        SQLiteCursor sQLiteCursor92222222222 = null;
                        i7 = 0;
                        while (i7 < size) {
                            LongSparseArray longSparseArray17 = longSparseArray2;
                            try {
                                keyAt = longSparseArray17.keyAt(i7);
                                charSequence = charSequence3;
                                String join3 = TextUtils.join(charSequence, (ArrayList) longSparseArray17.valueAt(i7));
                                if (DialogObject.isEncryptedDialog(keyAt)) {
                                    i8 = size;
                                    sQLitePreparedStatement3 = sQLitePreparedStatement52222222222;
                                    arrayList15 = arrayList11;
                                    i9 = i7;
                                    longSparseArray2 = longSparseArray17;
                                } else if (DialogObject.isChatDialog(keyAt)) {
                                    i8 = size;
                                    sQLitePreparedStatement3 = sQLitePreparedStatement52222222222;
                                    sQLiteCursor4 = sQLiteCursor92222222222;
                                    longSparseArray2 = longSparseArray17;
                                    try {
                                        arrayList15 = arrayList11;
                                        i9 = i7;
                                        this.database.executeFast(String.format(Locale.US, "UPDATE chat_settings_v2 SET pinned = 0 WHERE uid = %d AND pinned IN (%s)", Long.valueOf(-keyAt), join3)).stepThis().dispose();
                                    } catch (Exception e26) {
                                        e = e26;
                                        exc = e;
                                        sQLitePreparedStatement = sQLitePreparedStatement3;
                                        sQLiteCursor2 = sQLiteCursor4;
                                        checkSQLException(exc);
                                        sQLiteDatabase2 = this.database;
                                        if (sQLiteDatabase2 != null) {
                                        }
                                        if (sQLiteCursor2 != null) {
                                        }
                                        if (sQLitePreparedStatement == null) {
                                        }
                                    } catch (Throwable th10) {
                                        th3 = th10;
                                        th = th3;
                                        sQLitePreparedStatement2 = sQLitePreparedStatement3;
                                        sQLiteCursor = sQLiteCursor4;
                                        sQLiteDatabase = this.database;
                                        if (sQLiteDatabase != null) {
                                        }
                                        if (sQLiteCursor != null) {
                                        }
                                        if (sQLitePreparedStatement2 == null) {
                                        }
                                    }
                                } else {
                                    i8 = size;
                                    sQLitePreparedStatement3 = sQLitePreparedStatement52222222222;
                                    arrayList15 = arrayList11;
                                    i9 = i7;
                                    longSparseArray2 = longSparseArray17;
                                    this.database.executeFast(String.format(Locale.US, "UPDATE user_settings SET pinned = 0 WHERE uid = %d AND pinned IN (%s)", Long.valueOf(keyAt), join3)).stepThis().dispose();
                                }
                                SQLiteDatabase sQLiteDatabase6 = this.database;
                                locale = Locale.US;
                                sQLiteDatabase6.executeFast(String.format(locale, "DELETE FROM chat_pinned_v2 WHERE uid = %d AND mid IN(%s)", Long.valueOf(keyAt), join3)).stepThis().dispose();
                                SQLiteCursor queryFinalized6 = this.database.queryFinalized("SELECT changes()", new Object[0]);
                                try {
                                    int intValue4 = queryFinalized6.next() ? queryFinalized6.intValue(0) : 0;
                                    queryFinalized6.dispose();
                                    if (intValue4 > 0) {
                                        try {
                                            SQLiteCursor queryFinalized7 = this.database.queryFinalized(String.format(locale, "SELECT count FROM chat_pinned_count WHERE uid = %d", Long.valueOf(keyAt)), new Object[0]);
                                            if (queryFinalized7.next()) {
                                                int intValue5 = queryFinalized7.intValue(0);
                                                sQLitePreparedStatement4 = this.database.executeFast("UPDATE chat_pinned_count SET count = ? WHERE uid = ?");
                                                try {
                                                    sQLitePreparedStatement4.requery();
                                                    sQLitePreparedStatement4.bindInteger(1, Math.max(0, intValue5 - intValue4));
                                                    sQLitePreparedStatement4.bindLong(2, keyAt);
                                                    sQLitePreparedStatement4.step();
                                                    sQLitePreparedStatement4.dispose();
                                                } catch (Exception e27) {
                                                    e = e27;
                                                    sQLiteCursor2 = queryFinalized7;
                                                    sQLitePreparedStatement = sQLitePreparedStatement4;
                                                    exc = e;
                                                    checkSQLException(exc);
                                                    sQLiteDatabase2 = this.database;
                                                    if (sQLiteDatabase2 != null) {
                                                    }
                                                    if (sQLiteCursor2 != null) {
                                                    }
                                                    if (sQLitePreparedStatement == null) {
                                                    }
                                                } catch (Throwable th11) {
                                                    th = th11;
                                                    sQLiteCursor = queryFinalized7;
                                                    sQLitePreparedStatement2 = sQLitePreparedStatement4;
                                                    sQLiteDatabase = this.database;
                                                    if (sQLiteDatabase != null) {
                                                    }
                                                    if (sQLiteCursor != null) {
                                                    }
                                                    if (sQLitePreparedStatement2 == null) {
                                                    }
                                                }
                                            } else {
                                                sQLitePreparedStatement4 = sQLitePreparedStatement3;
                                            }
                                            queryFinalized7.dispose();
                                        } catch (Exception e28) {
                                            exc = e28;
                                            sQLitePreparedStatement = sQLitePreparedStatement3;
                                            sQLiteCursor2 = null;
                                            checkSQLException(exc);
                                            sQLiteDatabase2 = this.database;
                                            if (sQLiteDatabase2 != null) {
                                            }
                                            if (sQLiteCursor2 != null) {
                                            }
                                            if (sQLitePreparedStatement == null) {
                                            }
                                        } catch (Throwable th12) {
                                            th = th12;
                                            sQLitePreparedStatement2 = sQLitePreparedStatement3;
                                            sQLiteCursor = null;
                                            sQLiteDatabase = this.database;
                                            if (sQLiteDatabase != null) {
                                            }
                                            if (sQLiteCursor != null) {
                                            }
                                            if (sQLitePreparedStatement2 == null) {
                                            }
                                        }
                                    } else {
                                        sQLitePreparedStatement4 = sQLitePreparedStatement3;
                                    }
                                    try {
                                        this.database.executeFast(String.format(locale, "DELETE FROM messages_v2 WHERE mid IN(%s) AND uid = %d", str32222222222, Long.valueOf(keyAt))).stepThis().dispose();
                                        this.database.executeFast(String.format(locale, "DELETE FROM messages_topics WHERE mid IN(%s) AND uid = %d", str32222222222, Long.valueOf(keyAt))).stepThis().dispose();
                                        this.database.executeFast(String.format(locale, "DELETE FROM polls_v2 WHERE mid IN(%s) AND uid = %d", str32222222222, Long.valueOf(keyAt))).stepThis().dispose();
                                        this.database.executeFast(String.format(locale, "DELETE FROM bot_keyboard WHERE mid IN(%s) AND uid = %d", str32222222222, Long.valueOf(keyAt))).stepThis().dispose();
                                        this.database.executeFast(String.format(locale, "DELETE FROM bot_keyboard_topics WHERE mid IN(%s) AND uid = %d", str32222222222, Long.valueOf(keyAt))).stepThis().dispose();
                                    } catch (Exception e29) {
                                        exc = e29;
                                        sQLitePreparedStatement = sQLitePreparedStatement4;
                                        sQLiteCursor2 = null;
                                        checkSQLException(exc);
                                        sQLiteDatabase2 = this.database;
                                        if (sQLiteDatabase2 != null) {
                                        }
                                        if (sQLiteCursor2 != null) {
                                        }
                                        if (sQLitePreparedStatement == null) {
                                        }
                                    } catch (Throwable th13) {
                                        th = th13;
                                        sQLitePreparedStatement2 = sQLitePreparedStatement4;
                                    }
                                } catch (Exception e30) {
                                    e = e30;
                                    sQLiteCursor2 = queryFinalized6;
                                    sQLitePreparedStatement = sQLitePreparedStatement3;
                                } catch (Throwable th14) {
                                    th = th14;
                                    sQLiteCursor = queryFinalized6;
                                    sQLitePreparedStatement2 = sQLitePreparedStatement3;
                                }
                            } catch (Exception e31) {
                                e = e31;
                                sQLitePreparedStatement3 = sQLitePreparedStatement52222222222;
                                sQLiteCursor4 = sQLiteCursor92222222222;
                                exc = e;
                                sQLitePreparedStatement = sQLitePreparedStatement3;
                                sQLiteCursor2 = sQLiteCursor4;
                                checkSQLException(exc);
                                sQLiteDatabase2 = this.database;
                                if (sQLiteDatabase2 != null) {
                                }
                                if (sQLiteCursor2 != null) {
                                }
                                if (sQLitePreparedStatement == null) {
                                }
                            } catch (Throwable th15) {
                                th3 = th15;
                                sQLitePreparedStatement3 = sQLitePreparedStatement52222222222;
                                sQLiteCursor4 = sQLiteCursor92222222222;
                                th = th3;
                                sQLitePreparedStatement2 = sQLitePreparedStatement3;
                                sQLiteCursor = sQLiteCursor4;
                                sQLiteDatabase = this.database;
                                if (sQLiteDatabase != null) {
                                }
                                if (sQLiteCursor != null) {
                                }
                                if (sQLitePreparedStatement2 == null) {
                                }
                            }
                            try {
                                if (arrayList6.isEmpty()) {
                                    int i21 = 0;
                                    sQLiteCursor2 = this.database.queryFinalized(String.format(locale, "SELECT uid, type FROM media_v4 WHERE mid IN(%s) AND uid = %d", str32222222222, Long.valueOf(keyAt)), new Object[0]);
                                    SparseArray sparseArray = null;
                                    while (sQLiteCursor2.next()) {
                                        try {
                                            long longValue4 = sQLiteCursor2.longValue(i21);
                                            int intValue6 = sQLiteCursor2.intValue(1);
                                            if (sparseArray == null) {
                                                sparseArray = new SparseArray();
                                            }
                                            LongSparseArray longSparseArray18 = (LongSparseArray) sparseArray.get(intValue6);
                                            if (longSparseArray18 == null) {
                                                longSparseArray18 = new LongSparseArray();
                                                num2 = 0;
                                                sparseArray.put(intValue6, longSparseArray18);
                                            } else {
                                                num2 = (Integer) longSparseArray18.get(longValue4);
                                            }
                                            if (num2 == null) {
                                                num2 = 0;
                                            }
                                            longSparseArray18.put(longValue4, Integer.valueOf(num2.intValue() + 1));
                                            i21 = 0;
                                        } catch (Exception e32) {
                                            exc = e32;
                                            sQLitePreparedStatement = sQLitePreparedStatement4;
                                        } catch (Throwable th16) {
                                            th = th16;
                                            sQLiteCursor = sQLiteCursor2;
                                            sQLitePreparedStatement2 = sQLitePreparedStatement4;
                                            sQLiteDatabase = this.database;
                                            if (sQLiteDatabase != null) {
                                            }
                                            if (sQLiteCursor != null) {
                                            }
                                            if (sQLitePreparedStatement2 == null) {
                                            }
                                        }
                                    }
                                    sQLiteCursor2.dispose();
                                    if (sparseArray != null) {
                                        executeFast = this.database.executeFast("REPLACE INTO media_counts_v2 VALUES(?, ?, ?, ?)");
                                        int size3 = sparseArray.size();
                                        SQLiteCursor sQLiteCursor10 = null;
                                        int i22 = 0;
                                        while (i22 < size3) {
                                            try {
                                                int keyAt3 = sparseArray.keyAt(i22);
                                                LongSparseArray longSparseArray19 = (LongSparseArray) sparseArray.valueAt(i22);
                                                SparseArray sparseArray2 = sparseArray;
                                                int size4 = longSparseArray19.size();
                                                sQLiteCursor8 = sQLiteCursor10;
                                                int i23 = 0;
                                                while (i23 < size4) {
                                                    LongSparseArray longSparseArray20 = longSparseArray142222222222;
                                                    int i24 = size4;
                                                    try {
                                                        long keyAt4 = longSparseArray19.keyAt(i23);
                                                        int i25 = size3;
                                                        CharSequence charSequence4 = charSequence;
                                                        LongSparseArray longSparseArray21 = longSparseArray19;
                                                        ArrayList<TopicKey> arrayList38 = arrayList9;
                                                        ArrayList arrayList39 = arrayList15;
                                                        long j4 = keyAt;
                                                        sQLiteCursor7 = this.database.queryFinalized(String.format(Locale.US, "SELECT count, old FROM media_counts_v2 WHERE uid = %d AND type = %d LIMIT 1", Long.valueOf(keyAt4), Integer.valueOf(keyAt3)), new Object[0]);
                                                        try {
                                                            if (sQLiteCursor7.next()) {
                                                                int intValue7 = sQLiteCursor7.intValue(0);
                                                                i12 = sQLiteCursor7.intValue(1);
                                                                i13 = intValue7;
                                                            } else {
                                                                i12 = 0;
                                                                i13 = -1;
                                                            }
                                                            sQLiteCursor7.dispose();
                                                            if (i13 != -1) {
                                                                longSparseArray8 = longSparseArray21;
                                                                executeFast.requery();
                                                                int max4 = Math.max(0, i13 - ((Integer) longSparseArray8.valueAt(i23)).intValue());
                                                                executeFast.bindLong(1, keyAt4);
                                                                executeFast.bindInteger(2, keyAt3);
                                                                executeFast.bindInteger(3, max4);
                                                                executeFast.bindInteger(4, i12);
                                                                executeFast.step();
                                                            } else {
                                                                longSparseArray8 = longSparseArray21;
                                                            }
                                                            i23++;
                                                            sQLiteCursor8 = sQLiteCursor7;
                                                            longSparseArray19 = longSparseArray8;
                                                            longSparseArray142222222222 = longSparseArray20;
                                                            size4 = i24;
                                                            size3 = i25;
                                                            charSequence = charSequence4;
                                                            arrayList15 = arrayList39;
                                                            arrayList9 = arrayList38;
                                                            keyAt = j4;
                                                        } catch (Exception e33) {
                                                            exc = e33;
                                                            sQLitePreparedStatement = executeFast;
                                                            sQLiteCursor2 = sQLiteCursor7;
                                                            checkSQLException(exc);
                                                            sQLiteDatabase2 = this.database;
                                                            if (sQLiteDatabase2 != null) {
                                                            }
                                                            if (sQLiteCursor2 != null) {
                                                            }
                                                            if (sQLitePreparedStatement == null) {
                                                            }
                                                        } catch (Throwable th17) {
                                                            th = th17;
                                                            sQLitePreparedStatement2 = executeFast;
                                                            sQLiteCursor = sQLiteCursor7;
                                                            sQLiteDatabase = this.database;
                                                            if (sQLiteDatabase != null) {
                                                            }
                                                            if (sQLiteCursor != null) {
                                                            }
                                                            if (sQLitePreparedStatement2 == null) {
                                                            }
                                                        }
                                                    } catch (Exception e34) {
                                                        exc = e34;
                                                        sQLitePreparedStatement = executeFast;
                                                        sQLiteCursor2 = sQLiteCursor8;
                                                    } catch (Throwable th18) {
                                                        th = th18;
                                                        sQLitePreparedStatement2 = executeFast;
                                                        sQLiteCursor = sQLiteCursor8;
                                                        sQLiteDatabase = this.database;
                                                        if (sQLiteDatabase != null) {
                                                        }
                                                        if (sQLiteCursor != null) {
                                                        }
                                                        if (sQLitePreparedStatement2 == null) {
                                                        }
                                                    }
                                                }
                                                i22++;
                                                sparseArray = sparseArray2;
                                                sQLiteCursor10 = sQLiteCursor8;
                                            } catch (Exception e35) {
                                                exc = e35;
                                                sQLitePreparedStatement = executeFast;
                                                sQLiteCursor2 = sQLiteCursor10;
                                            } catch (Throwable th19) {
                                                th = th19;
                                                sQLitePreparedStatement2 = executeFast;
                                                sQLiteCursor = sQLiteCursor10;
                                                sQLiteDatabase = this.database;
                                                if (sQLiteDatabase != null) {
                                                }
                                                if (sQLiteCursor != null) {
                                                }
                                                if (sQLitePreparedStatement2 == null) {
                                                }
                                            }
                                        }
                                        longSparseArray7 = longSparseArray142222222222;
                                        arrayList16 = arrayList9;
                                        arrayList17 = arrayList15;
                                        j3 = keyAt;
                                        charSequence2 = charSequence;
                                        executeFast.dispose();
                                        sQLiteCursor6 = sQLiteCursor10;
                                        sQLitePreparedStatement4 = null;
                                        if (arrayList7.isEmpty()) {
                                            sQLiteCursor92222222222 = sQLiteCursor6;
                                            sQLitePreparedStatement52222222222 = sQLitePreparedStatement4;
                                        } else {
                                            int i26 = 0;
                                            SQLiteCursor queryFinalized8 = this.database.queryFinalized(String.format(Locale.US, "SELECT uid, topic_id, type FROM media_topics WHERE mid IN(%s) AND uid = %d", str32222222222, Long.valueOf(j3)), new Object[0]);
                                            SparseArray sparseArray3 = null;
                                            while (queryFinalized8.next()) {
                                                long longValue5 = queryFinalized8.longValue(i26);
                                                int intValue8 = queryFinalized8.intValue(1);
                                                int intValue9 = queryFinalized8.intValue(2);
                                                TopicKey of2 = TopicKey.of(longValue5, intValue8);
                                                if (sparseArray3 == null) {
                                                    sparseArray3 = new SparseArray();
                                                }
                                                HashMap hashMap6 = (HashMap) sparseArray3.get(intValue9);
                                                if (hashMap6 == null) {
                                                    hashMap6 = new HashMap();
                                                    num = 0;
                                                    sparseArray3.put(intValue9, hashMap6);
                                                } else {
                                                    num = (Integer) hashMap6.get(of2);
                                                }
                                                if (num == null) {
                                                    num = 0;
                                                }
                                                hashMap6.put(of2, Integer.valueOf(num.intValue() + 1));
                                                i26 = 0;
                                            }
                                            queryFinalized8.dispose();
                                            if (sparseArray3 != null) {
                                                SQLitePreparedStatement executeFast3 = this.database.executeFast("REPLACE INTO media_counts_topics VALUES(?, ?, ?, ?, ?)");
                                                try {
                                                    int size5 = sparseArray3.size();
                                                    sQLiteCursor7 = null;
                                                    int i27 = 0;
                                                    while (i27 < size5) {
                                                        try {
                                                            int keyAt5 = sparseArray3.keyAt(i27);
                                                            HashMap hashMap7 = (HashMap) sparseArray3.valueAt(i27);
                                                            for (TopicKey topicKey2 : hashMap7.keySet()) {
                                                                try {
                                                                    SQLiteDatabase sQLiteDatabase7 = this.database;
                                                                    Locale locale4 = Locale.US;
                                                                    SparseArray sparseArray4 = sparseArray3;
                                                                    Object[] objArr2 = new Object[3];
                                                                    int i28 = size5;
                                                                    sQLiteCursor8 = sQLiteCursor7;
                                                                    try {
                                                                        objArr2[0] = Long.valueOf(topicKey2.dialogId);
                                                                        objArr2[1] = Long.valueOf(topicKey2.topicId);
                                                                        objArr2[2] = Integer.valueOf(keyAt5);
                                                                        sQLiteCursor7 = sQLiteDatabase7.queryFinalized(String.format(locale4, "SELECT count, old FROM media_counts_topics WHERE uid = %d AND topic_id = %d AND type = %d LIMIT 1", objArr2), new Object[0]);
                                                                        if (sQLiteCursor7.next()) {
                                                                            i10 = sQLiteCursor7.intValue(0);
                                                                            i11 = sQLiteCursor7.intValue(1);
                                                                        } else {
                                                                            i10 = -1;
                                                                            i11 = 0;
                                                                        }
                                                                        sQLiteCursor7.dispose();
                                                                        if (i10 != -1) {
                                                                            executeFast3.requery();
                                                                            int max5 = Math.max(0, i10 - ((Integer) hashMap7.get(topicKey2)).intValue());
                                                                            executeFast3.bindLong(1, topicKey2.dialogId);
                                                                            executeFast3.bindLong(2, topicKey2.topicId);
                                                                            executeFast3.bindInteger(3, keyAt5);
                                                                            executeFast3.bindInteger(4, max5);
                                                                            executeFast3.bindInteger(5, i11);
                                                                            executeFast3.step();
                                                                        }
                                                                        size5 = i28;
                                                                        sparseArray3 = sparseArray4;
                                                                    } catch (Exception e36) {
                                                                        e = e36;
                                                                        sQLitePreparedStatement = executeFast3;
                                                                        sQLiteCursor2 = sQLiteCursor8;
                                                                        exc = e;
                                                                        checkSQLException(exc);
                                                                        sQLiteDatabase2 = this.database;
                                                                        if (sQLiteDatabase2 != null) {
                                                                        }
                                                                        if (sQLiteCursor2 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement == null) {
                                                                        }
                                                                    } catch (Throwable th20) {
                                                                        th = th20;
                                                                        th = th;
                                                                        sQLitePreparedStatement2 = executeFast3;
                                                                        sQLiteCursor = sQLiteCursor8;
                                                                        sQLiteDatabase = this.database;
                                                                        if (sQLiteDatabase != null) {
                                                                        }
                                                                        if (sQLiteCursor != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement2 == null) {
                                                                        }
                                                                    }
                                                                } catch (Exception e37) {
                                                                    e = e37;
                                                                    sQLiteCursor8 = sQLiteCursor7;
                                                                } catch (Throwable th21) {
                                                                    th = th21;
                                                                    sQLiteCursor8 = sQLiteCursor7;
                                                                }
                                                            }
                                                            i27++;
                                                            sparseArray3 = sparseArray3;
                                                        } catch (Exception e38) {
                                                            e = e38;
                                                            sQLitePreparedStatement = executeFast3;
                                                            sQLiteCursor2 = sQLiteCursor7;
                                                        } catch (Throwable th22) {
                                                            th = th22;
                                                            sQLitePreparedStatement2 = executeFast3;
                                                            sQLiteCursor = sQLiteCursor7;
                                                            sQLiteDatabase = this.database;
                                                            if (sQLiteDatabase != null) {
                                                            }
                                                            if (sQLiteCursor != null) {
                                                            }
                                                            if (sQLitePreparedStatement2 == null) {
                                                            }
                                                        }
                                                    }
                                                    executeFast3.dispose();
                                                    sQLiteCursor92222222222 = sQLiteCursor7;
                                                    sQLitePreparedStatement52222222222 = null;
                                                } catch (Exception e39) {
                                                    e = e39;
                                                    sQLitePreparedStatement = executeFast3;
                                                    sQLiteCursor2 = null;
                                                } catch (Throwable th23) {
                                                    th = th23;
                                                    sQLitePreparedStatement2 = executeFast3;
                                                    sQLiteCursor = null;
                                                    sQLiteDatabase = this.database;
                                                    if (sQLiteDatabase != null) {
                                                    }
                                                    if (sQLiteCursor != null) {
                                                    }
                                                    if (sQLitePreparedStatement2 == null) {
                                                    }
                                                }
                                            } else {
                                                sQLitePreparedStatement52222222222 = sQLitePreparedStatement4;
                                                sQLiteCursor92222222222 = null;
                                            }
                                        }
                                        SQLiteDatabase sQLiteDatabase8 = this.database;
                                        Locale locale5 = Locale.US;
                                        sQLiteDatabase8.executeFast(String.format(locale5, "DELETE FROM media_v4 WHERE mid IN(%s) AND uid = %d", str32222222222, Long.valueOf(j3))).stepThis().dispose();
                                        this.database.executeFast(String.format(locale5, "DELETE FROM media_topics WHERE mid IN(%s) AND uid = %d", str32222222222, Long.valueOf(j3))).stepThis().dispose();
                                        i7 = i9 + 1;
                                        size = i8;
                                        longSparseArray142222222222 = longSparseArray7;
                                        charSequence3 = charSequence2;
                                        arrayList11 = arrayList17;
                                        arrayList9 = arrayList16;
                                    }
                                }
                                SQLiteDatabase sQLiteDatabase82 = this.database;
                                Locale locale52 = Locale.US;
                                sQLiteDatabase82.executeFast(String.format(locale52, "DELETE FROM media_v4 WHERE mid IN(%s) AND uid = %d", str32222222222, Long.valueOf(j3))).stepThis().dispose();
                                this.database.executeFast(String.format(locale52, "DELETE FROM media_topics WHERE mid IN(%s) AND uid = %d", str32222222222, Long.valueOf(j3))).stepThis().dispose();
                                i7 = i9 + 1;
                                size = i8;
                                longSparseArray142222222222 = longSparseArray7;
                                charSequence3 = charSequence2;
                                arrayList11 = arrayList17;
                                arrayList9 = arrayList16;
                            } catch (Exception e40) {
                                exc = e40;
                                sQLitePreparedStatement = sQLitePreparedStatement52222222222;
                                sQLiteCursor2 = sQLiteCursor92222222222;
                                checkSQLException(exc);
                                sQLiteDatabase2 = this.database;
                                if (sQLiteDatabase2 != null) {
                                }
                                if (sQLiteCursor2 != null) {
                                }
                                if (sQLitePreparedStatement == null) {
                                }
                            } catch (Throwable th24) {
                                th = th24;
                                sQLitePreparedStatement2 = sQLitePreparedStatement52222222222;
                                sQLiteCursor = sQLiteCursor92222222222;
                                sQLiteDatabase = this.database;
                                if (sQLiteDatabase != null) {
                                }
                                if (sQLiteCursor != null) {
                                }
                                if (sQLitePreparedStatement2 == null) {
                                }
                            }
                            longSparseArray7 = longSparseArray142222222222;
                            arrayList16 = arrayList9;
                            arrayList17 = arrayList15;
                            j3 = keyAt;
                            charSequence2 = charSequence;
                            sQLiteCursor6 = null;
                            if (arrayList7.isEmpty()) {
                            }
                        }
                        longSparseArray6 = longSparseArray142222222222;
                        sQLitePreparedStatement3 = sQLitePreparedStatement52222222222;
                        arrayList12 = arrayList9;
                        sQLiteCursor4 = sQLiteCursor92222222222;
                        arrayList13 = arrayList11;
                        if (!longSparseArray5.isEmpty()) {
                            final LongSparseArray longSparseArray22 = longSparseArray5;
                            AndroidUtilities.runOnUIThread(new Runnable() {
                                @Override
                                public final void run() {
                                    MessagesStorage.this.lambda$markMessagesAsDeletedInternal$197(longSparseArray22);
                                }
                            });
                        }
                        SQLiteDatabase sQLiteDatabase42222222222 = this.database;
                        Locale locale22222222222 = Locale.US;
                        sQLiteDatabase42222222222.executeFast(String.format(locale22222222222, "DELETE FROM messages_seq WHERE mid IN(%s)", str32222222222)).stepThis().dispose();
                        if (!arrayList6.isEmpty()) {
                            if (j == 0) {
                                this.database.executeFast("UPDATE media_counts_v2 SET old = 1 WHERE 1").stepThis().dispose();
                            } else {
                                this.database.executeFast(String.format(locale22222222222, "UPDATE media_counts_v2 SET old = 1 WHERE uid = %d", Long.valueOf(j))).stepThis().dispose();
                            }
                        }
                        if (!arrayList7.isEmpty()) {
                            if (j == 0) {
                                this.database.executeFast("UPDATE media_counts_topics SET old = 1 WHERE 1").stepThis().dispose();
                            } else {
                                this.database.executeFast(String.format(locale22222222222, "UPDATE media_counts_topics SET old = 1 WHERE uid = %d", Long.valueOf(j))).stepThis().dispose();
                            }
                        }
                        getMediaDataController().clearBotKeyboard(null, arrayList);
                        if (longSparseArray6.size() != 0) {
                            resetAllUnreadCounters(false);
                        }
                        arrayList14 = arrayList10;
                        updateWidgets(arrayList14);
                        if (arrayList12 != null) {
                            int i29 = 0;
                            while (i29 < arrayList12.size()) {
                                ArrayList<TopicKey> arrayList40 = arrayList12;
                                TopicKey topicKey3 = arrayList40.get(i29);
                                this.database.executeFast(String.format(Locale.US, "DELETE FROM topics WHERE did = %d AND topic_id = %d", Long.valueOf(topicKey3.dialogId), Long.valueOf(topicKey3.topicId))).stepThis().dispose();
                                i29++;
                                arrayList12 = arrayList40;
                            }
                            getMessagesController().getTopicsController().onTopicsDeletedServerSide(arrayList12);
                        }
                        if (arrayList13 != null) {
                            getMessagesController().getTopicsController().processUpdate(arrayList13);
                        }
                        sQLiteCursor5 = sQLiteCursor4;
                    }
                    sQLiteDatabase3 = this.database;
                    if (sQLiteDatabase3 != null) {
                        sQLiteDatabase3.commitTransaction();
                    }
                    if (sQLiteCursor5 != null) {
                        sQLiteCursor5.dispose();
                    }
                    if (sQLitePreparedStatement3 != null) {
                        sQLitePreparedStatement3.dispose();
                    }
                    return arrayList14;
                } catch (Exception e41) {
                    exc = e41;
                }
            } catch (Throwable th25) {
                th = th25;
                sQLiteCursor = sQLiteCursor3;
            }
        } catch (Exception e42) {
            exc = e42;
            sQLitePreparedStatement = null;
        } catch (Throwable th26) {
            th = th26;
            sQLiteCursor = null;
        }
    }

    public void lambda$markMessagesAsDeletedInternal$196(ArrayList arrayList) {
        getFileLoader().cancelLoadFiles(arrayList);
    }

    public void lambda$markMessagesAsDeletedInternal$197(LongSparseArray longSparseArray) {
        getMessagesController().getSavedMessagesController().updateDeleted(longSparseArray);
    }

    public void lambda$updateDialogsWithDeletedMessages$198(long j, long j2, ArrayList<Integer> arrayList, ArrayList<Long> arrayList2) {
        SQLiteCursor sQLiteCursor;
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLiteDatabase sQLiteDatabase;
        SQLiteDatabase sQLiteDatabase2;
        SQLitePreparedStatement sQLitePreparedStatement2;
        SQLiteCursor sQLiteCursor2;
        ArrayList<TLRPC$EncryptedChat> arrayList3;
        TLRPC$Dialog tLRPC$Dialog;
        int i;
        ArrayList<TLRPC$EncryptedChat> arrayList4;
        TLRPC$TL_dialog tLRPC$TL_dialog;
        SQLitePreparedStatement executeFast;
        long j3 = j2;
        try {
            ArrayList arrayList5 = new ArrayList();
            try {
                if (!arrayList.isEmpty()) {
                    if (j3 != 0) {
                        arrayList5.add(Long.valueOf(-j3));
                        executeFast = this.database.executeFast("UPDATE dialogs SET (last_mid, last_mid_group) = (SELECT mid, group_id FROM messages_v2 WHERE uid = ? AND date = (SELECT MAX(date) FROM messages_v2 WHERE uid = ?)) WHERE did = ?");
                    } else {
                        if (j == 0) {
                            SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT did FROM dialogs WHERE last_mid IN(%s) AND flags = 0", TextUtils.join(",", arrayList)), new Object[0]);
                            while (queryFinalized.next()) {
                                try {
                                    arrayList5.add(Long.valueOf(queryFinalized.longValue(0)));
                                } catch (Exception e) {
                                    e = e;
                                    sQLiteCursor = queryFinalized;
                                    sQLitePreparedStatement = null;
                                    try {
                                        checkSQLException(e);
                                        sQLiteDatabase2 = this.database;
                                        if (sQLiteDatabase2 != null) {
                                        }
                                        if (sQLiteCursor != null) {
                                        }
                                        if (sQLitePreparedStatement == null) {
                                        }
                                    } catch (Throwable th) {
                                        th = th;
                                        sQLiteDatabase = this.database;
                                        if (sQLiteDatabase != null) {
                                            sQLiteDatabase.commitTransaction();
                                        }
                                        if (sQLiteCursor != null) {
                                            sQLiteCursor.dispose();
                                        }
                                        if (sQLitePreparedStatement != null) {
                                            sQLitePreparedStatement.dispose();
                                        }
                                        throw th;
                                    }
                                } catch (Throwable th2) {
                                    th = th2;
                                    sQLiteCursor = queryFinalized;
                                    sQLitePreparedStatement = null;
                                    sQLiteDatabase = this.database;
                                    if (sQLiteDatabase != null) {
                                    }
                                    if (sQLiteCursor != null) {
                                    }
                                    if (sQLitePreparedStatement != null) {
                                    }
                                    throw th;
                                }
                            }
                            queryFinalized.dispose();
                        } else {
                            arrayList5.add(Long.valueOf(j));
                        }
                        executeFast = this.database.executeFast("UPDATE dialogs SET (last_mid, last_mid_group) = (SELECT mid, group_id FROM messages_v2 WHERE uid = ? AND date = (SELECT MAX(date) FROM messages_v2 WHERE uid = ? AND date != 0)) WHERE did = ?");
                    }
                    try {
                        this.database.beginTransaction();
                        for (int i2 = 0; i2 < arrayList5.size(); i2++) {
                            long longValue = ((Long) arrayList5.get(i2)).longValue();
                            executeFast.requery();
                            executeFast.bindLong(1, longValue);
                            executeFast.bindLong(2, longValue);
                            executeFast.bindLong(3, longValue);
                            executeFast.step();
                        }
                        executeFast.dispose();
                        this.database.commitTransaction();
                    } catch (Exception e2) {
                        e = e2;
                        sQLitePreparedStatement = executeFast;
                        sQLiteCursor = null;
                        checkSQLException(e);
                        sQLiteDatabase2 = this.database;
                        if (sQLiteDatabase2 != null) {
                        }
                        if (sQLiteCursor != null) {
                        }
                        if (sQLitePreparedStatement == null) {
                        }
                    } catch (Throwable th3) {
                        th = th3;
                        sQLitePreparedStatement = executeFast;
                        sQLiteCursor = null;
                        sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                        }
                        if (sQLiteCursor != null) {
                        }
                        if (sQLitePreparedStatement != null) {
                        }
                        throw th;
                    }
                } else {
                    arrayList5.add(Long.valueOf(-j3));
                }
                if (arrayList2 != null) {
                    for (int i3 = 0; i3 < arrayList2.size(); i3++) {
                        Long l = arrayList2.get(i3);
                        if (!arrayList5.contains(l)) {
                            arrayList5.add(l);
                        }
                    }
                }
                String join = TextUtils.join(",", arrayList5);
                TLRPC$TL_messages_dialogs tLRPC$TL_messages_dialogs = new TLRPC$TL_messages_dialogs();
                ArrayList<TLRPC$EncryptedChat> arrayList6 = new ArrayList<>();
                ArrayList<Long> arrayList7 = new ArrayList<>();
                ArrayList arrayList8 = new ArrayList();
                ArrayList arrayList9 = new ArrayList();
                LongSparseArray longSparseArray = new LongSparseArray();
                SQLiteCursor queryFinalized2 = this.database.queryFinalized(String.format(Locale.US, "SELECT d.did, d.last_mid, d.unread_count, d.date, m.data, m.read_state, m.mid, m.send_state, m.date, d.pts, d.inbox_max, d.outbox_max, d.pinned, d.unread_count_i, d.flags, d.folder_id, d.data, d.unread_reactions, d.last_mid_group, d.ttl_period FROM dialogs as d LEFT JOIN messages_v2 as m ON d.last_mid = m.mid AND d.did = m.uid AND d.last_mid_group IS NULL WHERE d.did IN(%s)", join), new Object[0]);
                while (queryFinalized2.next()) {
                    try {
                        try {
                            long longValue2 = queryFinalized2.longValue(0);
                            if (DialogObject.isFolderDialogId(longValue2)) {
                                TLRPC$TL_dialogFolder tLRPC$TL_dialogFolder = new TLRPC$TL_dialogFolder();
                                if (queryFinalized2.isNull(16)) {
                                    arrayList4 = arrayList6;
                                    tLRPC$TL_dialog = tLRPC$TL_dialogFolder;
                                } else {
                                    NativeByteBuffer byteBufferValue = queryFinalized2.byteBufferValue(16);
                                    if (byteBufferValue != null) {
                                        arrayList4 = arrayList6;
                                        tLRPC$TL_dialogFolder.folder = TLRPC$TL_folder.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                                        byteBufferValue.reuse();
                                        tLRPC$TL_dialog = tLRPC$TL_dialogFolder;
                                    } else {
                                        arrayList4 = arrayList6;
                                        TLRPC$TL_folder tLRPC$TL_folder = new TLRPC$TL_folder();
                                        tLRPC$TL_dialogFolder.folder = tLRPC$TL_folder;
                                        tLRPC$TL_folder.id = queryFinalized2.intValue(15);
                                        tLRPC$TL_dialog = tLRPC$TL_dialogFolder;
                                    }
                                }
                            } else {
                                arrayList4 = arrayList6;
                                tLRPC$TL_dialog = new TLRPC$TL_dialog();
                            }
                            tLRPC$TL_dialog.id = longValue2;
                            tLRPC$TL_dialog.top_message = queryFinalized2.intValue(1);
                            tLRPC$TL_dialog.read_inbox_max_id = queryFinalized2.intValue(10);
                            tLRPC$TL_dialog.read_outbox_max_id = queryFinalized2.intValue(11);
                            tLRPC$TL_dialog.unread_count = queryFinalized2.intValue(2);
                            tLRPC$TL_dialog.unread_mentions_count = queryFinalized2.intValue(13);
                            tLRPC$TL_dialog.last_message_date = queryFinalized2.intValue(3);
                            tLRPC$TL_dialog.pts = queryFinalized2.intValue(9);
                            tLRPC$TL_dialog.flags = j3 == 0 ? 0 : 1;
                            int intValue = queryFinalized2.intValue(12);
                            tLRPC$TL_dialog.pinnedNum = intValue;
                            tLRPC$TL_dialog.pinned = intValue != 0;
                            int intValue2 = queryFinalized2.intValue(14);
                            tLRPC$TL_dialog.unread_mark = (intValue2 & 1) != 0;
                            tLRPC$TL_dialog.view_forum_as_messages = (intValue2 & 64) != 0;
                            tLRPC$TL_dialog.folder_id = queryFinalized2.intValue(15);
                            tLRPC$TL_dialog.unread_reactions_count = queryFinalized2.intValue(17);
                            long longValue3 = queryFinalized2.longValue(18);
                            if (longValue3 != 0) {
                                longSparseArray.put(longValue2, Long.valueOf(longValue3));
                            }
                            tLRPC$TL_dialog.ttl_period = queryFinalized2.intValue(19);
                            tLRPC$TL_messages_dialogs.dialogs.add(tLRPC$TL_dialog);
                            NativeByteBuffer byteBufferValue2 = queryFinalized2.byteBufferValue(4);
                            if (byteBufferValue2 != null) {
                                TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue2, byteBufferValue2.readInt32(false), false);
                                TLdeserialize.readAttachPath(byteBufferValue2, getUserConfig().clientUserId);
                                byteBufferValue2.reuse();
                                MessageObject.setUnreadFlags(TLdeserialize, queryFinalized2.intValue(5));
                                TLdeserialize.id = queryFinalized2.intValue(6);
                                TLdeserialize.send_state = queryFinalized2.intValue(7);
                                int intValue3 = queryFinalized2.intValue(8);
                                if (intValue3 != 0) {
                                    tLRPC$TL_dialog.last_message_date = intValue3;
                                }
                                TLdeserialize.dialog_id = tLRPC$TL_dialog.id;
                                tLRPC$TL_messages_dialogs.messages.add(TLdeserialize);
                                addUsersAndChatsFromMessage(TLdeserialize, arrayList7, arrayList8, null);
                            }
                            if (!DialogObject.isEncryptedDialog(longValue2) && tLRPC$TL_dialog.read_inbox_max_id > tLRPC$TL_dialog.top_message) {
                                tLRPC$TL_dialog.read_inbox_max_id = 0;
                            }
                            if (DialogObject.isEncryptedDialog(longValue2)) {
                                int encryptedChatId = DialogObject.getEncryptedChatId(longValue2);
                                if (!arrayList9.contains(Integer.valueOf(encryptedChatId))) {
                                    arrayList9.add(Integer.valueOf(encryptedChatId));
                                }
                            } else if (DialogObject.isUserDialog(longValue2)) {
                                if (!arrayList7.contains(Long.valueOf(longValue2))) {
                                    arrayList7.add(Long.valueOf(longValue2));
                                }
                            } else {
                                long j4 = -longValue2;
                                if (!arrayList8.contains(Long.valueOf(j4))) {
                                    arrayList8.add(Long.valueOf(j4));
                                }
                            }
                            j3 = j2;
                            arrayList6 = arrayList4;
                        } catch (Exception e3) {
                            e = e3;
                            sQLiteCursor = queryFinalized2;
                            sQLitePreparedStatement = null;
                            checkSQLException(e);
                            sQLiteDatabase2 = this.database;
                            if (sQLiteDatabase2 != null) {
                                sQLiteDatabase2.commitTransaction();
                            }
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                            }
                            if (sQLitePreparedStatement == null) {
                                sQLitePreparedStatement.dispose();
                                return;
                            }
                            return;
                        } catch (Throwable th4) {
                            th = th4;
                            sQLiteCursor = queryFinalized2;
                            sQLitePreparedStatement = null;
                            sQLiteDatabase = this.database;
                            if (sQLiteDatabase != null) {
                            }
                            if (sQLiteCursor != null) {
                            }
                            if (sQLitePreparedStatement != null) {
                            }
                            throw th;
                        }
                    } catch (Exception e4) {
                        e = e4;
                        sQLitePreparedStatement2 = null;
                        sQLiteCursor = queryFinalized2;
                    } catch (Throwable th5) {
                        th = th5;
                        sQLitePreparedStatement2 = null;
                        sQLiteCursor = queryFinalized2;
                    }
                }
                ArrayList<TLRPC$EncryptedChat> arrayList10 = arrayList6;
                queryFinalized2.dispose();
                if (longSparseArray.isEmpty()) {
                    sQLitePreparedStatement2 = null;
                    sQLiteCursor2 = null;
                } else {
                    StringBuilder sb = new StringBuilder();
                    int i4 = 0;
                    while (i4 < longSparseArray.size()) {
                        sb.append("uid = ");
                        sb.append(longSparseArray.keyAt(i4));
                        sb.append(" AND group_id = ");
                        sb.append(longSparseArray.valueAt(i4));
                        i4++;
                        if (i4 < longSparseArray.size()) {
                            sb.append(" OR ");
                        }
                    }
                    int i5 = 0;
                    sQLiteCursor2 = this.database.queryFinalized(String.format(Locale.US, "SELECT uid, data, read_state, mid, send_state, date, group_id FROM messages_v2 WHERE %s", sb), new Object[0]);
                    while (sQLiteCursor2.next()) {
                        try {
                            long longValue4 = sQLiteCursor2.longValue(i5);
                            int i6 = 0;
                            while (true) {
                                if (i6 >= tLRPC$TL_messages_dialogs.dialogs.size()) {
                                    tLRPC$Dialog = null;
                                    break;
                                }
                                try {
                                    tLRPC$Dialog = tLRPC$TL_messages_dialogs.dialogs.get(i6);
                                    if (tLRPC$Dialog != null && tLRPC$Dialog.id == longValue4) {
                                        break;
                                    } else {
                                        i6++;
                                    }
                                } catch (Exception e5) {
                                    e = e5;
                                    sQLiteCursor = sQLiteCursor2;
                                    sQLitePreparedStatement = null;
                                    checkSQLException(e);
                                    sQLiteDatabase2 = this.database;
                                    if (sQLiteDatabase2 != null) {
                                    }
                                    if (sQLiteCursor != null) {
                                    }
                                    if (sQLitePreparedStatement == null) {
                                    }
                                } catch (Throwable th6) {
                                    th = th6;
                                    sQLiteCursor = sQLiteCursor2;
                                    sQLitePreparedStatement = null;
                                    sQLiteDatabase = this.database;
                                    if (sQLiteDatabase != null) {
                                    }
                                    if (sQLiteCursor != null) {
                                    }
                                    if (sQLitePreparedStatement != null) {
                                    }
                                    throw th;
                                }
                            }
                            if (tLRPC$Dialog == null) {
                                i5 = 0;
                            } else {
                                NativeByteBuffer byteBufferValue3 = sQLiteCursor2.byteBufferValue(1);
                                if (byteBufferValue3 != null) {
                                    i = 0;
                                    TLRPC$Message TLdeserialize2 = TLRPC$Message.TLdeserialize(byteBufferValue3, byteBufferValue3.readInt32(false), false);
                                    TLdeserialize2.readAttachPath(byteBufferValue3, getUserConfig().clientUserId);
                                    byteBufferValue3.reuse();
                                    MessageObject.setUnreadFlags(TLdeserialize2, sQLiteCursor2.intValue(2));
                                    TLdeserialize2.id = sQLiteCursor2.intValue(3);
                                    TLdeserialize2.send_state = sQLiteCursor2.intValue(4);
                                    int intValue4 = sQLiteCursor2.intValue(5);
                                    if (intValue4 != 0) {
                                        tLRPC$Dialog.last_message_date = intValue4;
                                    }
                                    TLdeserialize2.dialog_id = tLRPC$Dialog.id;
                                    tLRPC$TL_messages_dialogs.messages.add(TLdeserialize2);
                                    sQLitePreparedStatement2 = null;
                                    try {
                                        addUsersAndChatsFromMessage(TLdeserialize2, arrayList7, arrayList8, null);
                                    } catch (Exception e6) {
                                        e = e6;
                                        sQLiteCursor = sQLiteCursor2;
                                        sQLitePreparedStatement = sQLitePreparedStatement2;
                                        checkSQLException(e);
                                        sQLiteDatabase2 = this.database;
                                        if (sQLiteDatabase2 != null) {
                                        }
                                        if (sQLiteCursor != null) {
                                        }
                                        if (sQLitePreparedStatement == null) {
                                        }
                                    } catch (Throwable th7) {
                                        th = th7;
                                        sQLiteCursor = sQLiteCursor2;
                                        sQLitePreparedStatement = sQLitePreparedStatement2;
                                        sQLiteDatabase = this.database;
                                        if (sQLiteDatabase != null) {
                                        }
                                        if (sQLiteCursor != null) {
                                        }
                                        if (sQLitePreparedStatement != null) {
                                        }
                                        throw th;
                                    }
                                } else {
                                    i = 0;
                                }
                                i5 = i;
                            }
                        } catch (Exception e7) {
                            e = e7;
                            sQLitePreparedStatement2 = null;
                            sQLiteCursor = sQLiteCursor2;
                            sQLitePreparedStatement = sQLitePreparedStatement2;
                            checkSQLException(e);
                            sQLiteDatabase2 = this.database;
                            if (sQLiteDatabase2 != null) {
                            }
                            if (sQLiteCursor != null) {
                            }
                            if (sQLitePreparedStatement == null) {
                            }
                        } catch (Throwable th8) {
                            th = th8;
                            sQLitePreparedStatement2 = null;
                            sQLiteCursor = sQLiteCursor2;
                            sQLitePreparedStatement = sQLitePreparedStatement2;
                            sQLiteDatabase = this.database;
                            if (sQLiteDatabase != null) {
                            }
                            if (sQLiteCursor != null) {
                            }
                            if (sQLitePreparedStatement != null) {
                            }
                            throw th;
                        }
                    }
                    sQLitePreparedStatement2 = null;
                    sQLiteCursor2.dispose();
                }
                if (arrayList9.isEmpty()) {
                    arrayList3 = arrayList10;
                } else {
                    arrayList3 = arrayList10;
                    getEncryptedChatsInternal(TextUtils.join(",", arrayList9), arrayList3, arrayList7);
                }
                if (!arrayList8.isEmpty()) {
                    getChatsInternal(TextUtils.join(",", arrayList8), tLRPC$TL_messages_dialogs.chats);
                }
                if (!arrayList7.isEmpty()) {
                    getUsersInternal(TextUtils.join(",", arrayList7), tLRPC$TL_messages_dialogs.users);
                }
                getMessagesController().getTopicsController().updateTopicsWithDeletedMessages(j, arrayList);
                if (!tLRPC$TL_messages_dialogs.dialogs.isEmpty() || !arrayList3.isEmpty()) {
                    getMessagesController().processDialogsUpdate(tLRPC$TL_messages_dialogs, arrayList3, true);
                }
                SQLiteDatabase sQLiteDatabase3 = this.database;
                if (sQLiteDatabase3 != null) {
                    sQLiteDatabase3.commitTransaction();
                }
                if (sQLiteCursor2 != null) {
                    sQLiteCursor2.dispose();
                }
            } catch (Exception e8) {
                e = e8;
                sQLiteCursor = null;
            } catch (Throwable th9) {
                th = th9;
                sQLiteCursor = null;
            }
        } catch (Exception e9) {
            e = e9;
            sQLiteCursor = null;
            sQLitePreparedStatement = null;
        } catch (Throwable th10) {
            th = th10;
            sQLiteCursor = null;
            sQLitePreparedStatement = null;
        }
    }

    public void updateDialogsWithDeletedMessages(final long j, final long j2, final ArrayList<Integer> arrayList, final ArrayList<Long> arrayList2, boolean z) {
        if (z) {
            this.storageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$updateDialogsWithDeletedMessages$198(j, j2, arrayList, arrayList2);
                }
            });
        } else {
            lambda$updateDialogsWithDeletedMessages$198(j, j2, arrayList, arrayList2);
        }
    }

    public ArrayList<Long> markMessagesAsDeleted(final long j, final ArrayList<Integer> arrayList, boolean z, final boolean z2, final boolean z3) {
        if (arrayList.isEmpty()) {
            return null;
        }
        if (z) {
            this.storageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$markMessagesAsDeleted$199(j, arrayList, z2, z3);
                }
            });
            return null;
        }
        return lambda$markMessagesAsDeleted$199(j, arrayList, z2, z3);
    }

    public ArrayList<Long> lambda$markMessagesAsDeleted$201(long j, int i, boolean z) {
        final MessagesStorage messagesStorage;
        SQLiteCursor sQLiteCursor;
        SQLiteCursor sQLiteCursor2;
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLitePreparedStatement sQLitePreparedStatement2;
        SQLiteCursor sQLiteCursor3;
        SQLitePreparedStatement sQLitePreparedStatement3;
        LongSparseArray longSparseArray;
        ArrayList<File> arrayList;
        final ArrayList arrayList2;
        ArrayList<Pair<Long, Integer>> arrayList3;
        ArrayList<Long> arrayList4;
        long j2;
        SQLiteCursor sQLiteCursor4;
        boolean z2;
        int i2;
        int i3;
        int intValue;
        int i4;
        int i5;
        boolean z3;
        NativeByteBuffer byteBufferValue;
        boolean z4;
        try {
            try {
                ArrayList<Long> arrayList5 = new ArrayList<>();
                longSparseArray = new LongSparseArray();
                arrayList = new ArrayList<>();
                arrayList2 = new ArrayList();
                arrayList3 = new ArrayList<>();
                long clientUserId = getUserConfig().getClientUserId();
                arrayList4 = arrayList5;
                j2 = -j;
                boolean z5 = true;
                int i6 = 0;
                SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT uid, data, read_state, out, mention FROM messages_v2 WHERE uid = %d AND mid <= %d", Long.valueOf(j2), Integer.valueOf(i)), new Object[0]);
                while (queryFinalized.next()) {
                    try {
                        try {
                            try {
                                long longValue = queryFinalized.longValue(i6);
                                if (longValue != clientUserId) {
                                    try {
                                        try {
                                            int intValue2 = queryFinalized.intValue(2);
                                            if (queryFinalized.intValue(3) == 0) {
                                                Integer[] numArr = (Integer[]) longSparseArray.get(longValue);
                                                if (numArr == null) {
                                                    numArr = new Integer[2];
                                                    numArr[0] = 0;
                                                    try {
                                                        numArr[1] = 0;
                                                        longSparseArray.put(longValue, numArr);
                                                    } catch (Exception e) {
                                                        e = e;
                                                        z2 = true;
                                                        sQLiteCursor4 = queryFinalized;
                                                        messagesStorage = this;
                                                        z3 = z2;
                                                        try {
                                                            messagesStorage.checkSQLException(e);
                                                            i2 = z3;
                                                            sQLiteCursor4.dispose();
                                                            messagesStorage.deleteFromDownloadQueue(arrayList3, i2);
                                                            AndroidUtilities.runOnUIThread(new Runnable() {
                                                                @Override
                                                                public final void run() {
                                                                    MessagesStorage.this.lambda$markMessagesAsDeletedInternal$200(arrayList2);
                                                                }
                                                            });
                                                            getFileLoader().deleteFiles(arrayList, 0);
                                                            i3 = 0;
                                                            while (i3 < longSparseArray.size()) {
                                                            }
                                                            ArrayList<Long> arrayList6 = arrayList4;
                                                            SQLiteDatabase sQLiteDatabase = messagesStorage.database;
                                                            Locale locale = Locale.US;
                                                            Object[] objArr = new Object[2];
                                                            objArr[0] = Long.valueOf(j);
                                                            objArr[i2] = Integer.valueOf(i);
                                                            sQLiteDatabase.executeFast(String.format(locale, "UPDATE chat_settings_v2 SET pinned = 0 WHERE uid = %d AND pinned <= %d", objArr)).stepThis().dispose();
                                                            SQLiteDatabase sQLiteDatabase2 = messagesStorage.database;
                                                            Object[] objArr2 = new Object[2];
                                                            objArr2[0] = Long.valueOf(j);
                                                            objArr2[i2] = Integer.valueOf(i);
                                                            sQLiteDatabase2.executeFast(String.format(locale, "DELETE FROM chat_pinned_v2 WHERE uid = %d AND mid <= %d", objArr2)).stepThis().dispose();
                                                            sQLiteCursor3 = messagesStorage.database.queryFinalized("SELECT changes()", new Object[0]);
                                                            try {
                                                                if (!sQLiteCursor3.next()) {
                                                                }
                                                                sQLiteCursor3.dispose();
                                                                if (intValue > 0) {
                                                                }
                                                                SQLiteDatabase sQLiteDatabase3 = messagesStorage.database;
                                                                Object[] objArr3 = new Object[2];
                                                                objArr3[0] = Long.valueOf(j2);
                                                                objArr3[i2] = Integer.valueOf(i);
                                                                sQLiteDatabase3.executeFast(String.format(locale, "DELETE FROM messages_v2 WHERE uid = %d AND mid <= %d", objArr3)).stepThis().dispose();
                                                                SQLiteDatabase sQLiteDatabase4 = messagesStorage.database;
                                                                Object[] objArr4 = new Object[2];
                                                                objArr4[0] = Long.valueOf(j2);
                                                                objArr4[i2] = Integer.valueOf(i);
                                                                sQLiteDatabase4.executeFast(String.format(locale, "DELETE FROM messages_topics WHERE uid = %d AND mid <= %d", objArr4)).stepThis().dispose();
                                                                SQLiteDatabase sQLiteDatabase5 = messagesStorage.database;
                                                                Object[] objArr5 = new Object[2];
                                                                objArr5[0] = Long.valueOf(j2);
                                                                objArr5[i2] = Integer.valueOf(i);
                                                                sQLiteDatabase5.executeFast(String.format(locale, "DELETE FROM media_v4 WHERE uid = %d AND mid <= %d", objArr5)).stepThis().dispose();
                                                                SQLiteDatabase sQLiteDatabase6 = messagesStorage.database;
                                                                Object[] objArr6 = new Object[i2];
                                                                objArr6[0] = Long.valueOf(j2);
                                                                sQLiteDatabase6.executeFast(String.format(locale, "UPDATE media_counts_v2 SET old = 1 WHERE uid = %d", objArr6)).stepThis().dispose();
                                                                SQLiteDatabase sQLiteDatabase7 = messagesStorage.database;
                                                                Object[] objArr7 = new Object[i2];
                                                                objArr7[0] = Long.valueOf(j2);
                                                                sQLiteDatabase7.executeFast(String.format(locale, "UPDATE media_counts_topics SET old = 1 WHERE uid = %d", objArr7)).stepThis().dispose();
                                                                messagesStorage.updateWidgets(arrayList6);
                                                                return arrayList6;
                                                            } catch (Exception e2) {
                                                                e = e2;
                                                                sQLitePreparedStatement3 = null;
                                                                messagesStorage.checkSQLException(e);
                                                                if (sQLiteCursor3 != null) {
                                                                }
                                                                if (sQLitePreparedStatement3 != null) {
                                                                }
                                                            } catch (Throwable th) {
                                                                th = th;
                                                                sQLiteCursor2 = sQLiteCursor3;
                                                                sQLitePreparedStatement2 = null;
                                                                if (sQLiteCursor2 != null) {
                                                                }
                                                                if (sQLitePreparedStatement2 != null) {
                                                                }
                                                                throw th;
                                                            }
                                                        } catch (Exception e3) {
                                                            e = e3;
                                                            sQLiteCursor3 = sQLiteCursor4;
                                                            sQLitePreparedStatement3 = null;
                                                            messagesStorage.checkSQLException(e);
                                                            if (sQLiteCursor3 != null) {
                                                            }
                                                            if (sQLitePreparedStatement3 != null) {
                                                            }
                                                        } catch (Throwable th2) {
                                                            th = th2;
                                                            sQLiteCursor2 = sQLiteCursor4;
                                                            sQLitePreparedStatement2 = null;
                                                            if (sQLiteCursor2 != null) {
                                                            }
                                                            if (sQLitePreparedStatement2 != null) {
                                                            }
                                                            throw th;
                                                        }
                                                    }
                                                }
                                                if (intValue2 < 2) {
                                                    Integer num = numArr[1];
                                                    numArr[1] = Integer.valueOf(numArr[1].intValue() + 1);
                                                }
                                                if (intValue2 == 0 || intValue2 == 2) {
                                                    Integer num2 = numArr[0];
                                                    numArr[0] = Integer.valueOf(numArr[0].intValue() + 1);
                                                }
                                                if (!DialogObject.isEncryptedDialog(longValue) || z) {
                                                    byteBufferValue = queryFinalized.byteBufferValue(1);
                                                    if (byteBufferValue == null) {
                                                        TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                                                        TLdeserialize.readAttachPath(byteBufferValue, getUserConfig().clientUserId);
                                                        byteBufferValue.reuse();
                                                        sQLiteCursor4 = queryFinalized;
                                                        z2 = true;
                                                        z4 = true;
                                                        try {
                                                            addFilesToDelete(TLdeserialize, arrayList, arrayList3, arrayList2, false);
                                                        } catch (Exception e4) {
                                                            e = e4;
                                                            messagesStorage = this;
                                                            z3 = z2;
                                                            messagesStorage.checkSQLException(e);
                                                            i2 = z3;
                                                            sQLiteCursor4.dispose();
                                                            messagesStorage.deleteFromDownloadQueue(arrayList3, i2);
                                                            AndroidUtilities.runOnUIThread(new Runnable() {
                                                                @Override
                                                                public final void run() {
                                                                    MessagesStorage.this.lambda$markMessagesAsDeletedInternal$200(arrayList2);
                                                                }
                                                            });
                                                            getFileLoader().deleteFiles(arrayList, 0);
                                                            i3 = 0;
                                                            while (i3 < longSparseArray.size()) {
                                                            }
                                                            ArrayList<Long> arrayList62 = arrayList4;
                                                            SQLiteDatabase sQLiteDatabase8 = messagesStorage.database;
                                                            Locale locale2 = Locale.US;
                                                            Object[] objArr8 = new Object[2];
                                                            objArr8[0] = Long.valueOf(j);
                                                            objArr8[i2] = Integer.valueOf(i);
                                                            sQLiteDatabase8.executeFast(String.format(locale2, "UPDATE chat_settings_v2 SET pinned = 0 WHERE uid = %d AND pinned <= %d", objArr8)).stepThis().dispose();
                                                            SQLiteDatabase sQLiteDatabase22 = messagesStorage.database;
                                                            Object[] objArr22 = new Object[2];
                                                            objArr22[0] = Long.valueOf(j);
                                                            objArr22[i2] = Integer.valueOf(i);
                                                            sQLiteDatabase22.executeFast(String.format(locale2, "DELETE FROM chat_pinned_v2 WHERE uid = %d AND mid <= %d", objArr22)).stepThis().dispose();
                                                            sQLiteCursor3 = messagesStorage.database.queryFinalized("SELECT changes()", new Object[0]);
                                                            if (!sQLiteCursor3.next()) {
                                                            }
                                                            sQLiteCursor3.dispose();
                                                            if (intValue > 0) {
                                                            }
                                                            SQLiteDatabase sQLiteDatabase32 = messagesStorage.database;
                                                            Object[] objArr32 = new Object[2];
                                                            objArr32[0] = Long.valueOf(j2);
                                                            objArr32[i2] = Integer.valueOf(i);
                                                            sQLiteDatabase32.executeFast(String.format(locale2, "DELETE FROM messages_v2 WHERE uid = %d AND mid <= %d", objArr32)).stepThis().dispose();
                                                            SQLiteDatabase sQLiteDatabase42 = messagesStorage.database;
                                                            Object[] objArr42 = new Object[2];
                                                            objArr42[0] = Long.valueOf(j2);
                                                            objArr42[i2] = Integer.valueOf(i);
                                                            sQLiteDatabase42.executeFast(String.format(locale2, "DELETE FROM messages_topics WHERE uid = %d AND mid <= %d", objArr42)).stepThis().dispose();
                                                            SQLiteDatabase sQLiteDatabase52 = messagesStorage.database;
                                                            Object[] objArr52 = new Object[2];
                                                            objArr52[0] = Long.valueOf(j2);
                                                            objArr52[i2] = Integer.valueOf(i);
                                                            sQLiteDatabase52.executeFast(String.format(locale2, "DELETE FROM media_v4 WHERE uid = %d AND mid <= %d", objArr52)).stepThis().dispose();
                                                            SQLiteDatabase sQLiteDatabase62 = messagesStorage.database;
                                                            Object[] objArr62 = new Object[i2];
                                                            objArr62[0] = Long.valueOf(j2);
                                                            sQLiteDatabase62.executeFast(String.format(locale2, "UPDATE media_counts_v2 SET old = 1 WHERE uid = %d", objArr62)).stepThis().dispose();
                                                            SQLiteDatabase sQLiteDatabase72 = messagesStorage.database;
                                                            Object[] objArr72 = new Object[i2];
                                                            objArr72[0] = Long.valueOf(j2);
                                                            sQLiteDatabase72.executeFast(String.format(locale2, "UPDATE media_counts_topics SET old = 1 WHERE uid = %d", objArr72)).stepThis().dispose();
                                                            messagesStorage.updateWidgets(arrayList62);
                                                            return arrayList62;
                                                        } catch (Throwable th3) {
                                                            th = th3;
                                                            sQLitePreparedStatement2 = null;
                                                            sQLiteCursor2 = sQLiteCursor4;
                                                            if (sQLiteCursor2 != null) {
                                                            }
                                                            if (sQLitePreparedStatement2 != null) {
                                                            }
                                                            throw th;
                                                        }
                                                    } else {
                                                        sQLiteCursor4 = queryFinalized;
                                                        z4 = true;
                                                    }
                                                    i6 = 0;
                                                    z5 = z4;
                                                    queryFinalized = sQLiteCursor4;
                                                } else {
                                                    i6 = 0;
                                                    z5 = true;
                                                }
                                            }
                                        } catch (Exception e5) {
                                            e = e5;
                                            z3 = true;
                                            messagesStorage = this;
                                            sQLiteCursor4 = queryFinalized;
                                            messagesStorage.checkSQLException(e);
                                            i2 = z3;
                                            sQLiteCursor4.dispose();
                                            messagesStorage.deleteFromDownloadQueue(arrayList3, i2);
                                            AndroidUtilities.runOnUIThread(new Runnable() {
                                                @Override
                                                public final void run() {
                                                    MessagesStorage.this.lambda$markMessagesAsDeletedInternal$200(arrayList2);
                                                }
                                            });
                                            getFileLoader().deleteFiles(arrayList, 0);
                                            i3 = 0;
                                            while (i3 < longSparseArray.size()) {
                                            }
                                            ArrayList<Long> arrayList622 = arrayList4;
                                            SQLiteDatabase sQLiteDatabase82 = messagesStorage.database;
                                            Locale locale22 = Locale.US;
                                            Object[] objArr82 = new Object[2];
                                            objArr82[0] = Long.valueOf(j);
                                            objArr82[i2] = Integer.valueOf(i);
                                            sQLiteDatabase82.executeFast(String.format(locale22, "UPDATE chat_settings_v2 SET pinned = 0 WHERE uid = %d AND pinned <= %d", objArr82)).stepThis().dispose();
                                            SQLiteDatabase sQLiteDatabase222 = messagesStorage.database;
                                            Object[] objArr222 = new Object[2];
                                            objArr222[0] = Long.valueOf(j);
                                            objArr222[i2] = Integer.valueOf(i);
                                            sQLiteDatabase222.executeFast(String.format(locale22, "DELETE FROM chat_pinned_v2 WHERE uid = %d AND mid <= %d", objArr222)).stepThis().dispose();
                                            sQLiteCursor3 = messagesStorage.database.queryFinalized("SELECT changes()", new Object[0]);
                                            if (!sQLiteCursor3.next()) {
                                            }
                                            sQLiteCursor3.dispose();
                                            if (intValue > 0) {
                                            }
                                            SQLiteDatabase sQLiteDatabase322 = messagesStorage.database;
                                            Object[] objArr322 = new Object[2];
                                            objArr322[0] = Long.valueOf(j2);
                                            objArr322[i2] = Integer.valueOf(i);
                                            sQLiteDatabase322.executeFast(String.format(locale22, "DELETE FROM messages_v2 WHERE uid = %d AND mid <= %d", objArr322)).stepThis().dispose();
                                            SQLiteDatabase sQLiteDatabase422 = messagesStorage.database;
                                            Object[] objArr422 = new Object[2];
                                            objArr422[0] = Long.valueOf(j2);
                                            objArr422[i2] = Integer.valueOf(i);
                                            sQLiteDatabase422.executeFast(String.format(locale22, "DELETE FROM messages_topics WHERE uid = %d AND mid <= %d", objArr422)).stepThis().dispose();
                                            SQLiteDatabase sQLiteDatabase522 = messagesStorage.database;
                                            Object[] objArr522 = new Object[2];
                                            objArr522[0] = Long.valueOf(j2);
                                            objArr522[i2] = Integer.valueOf(i);
                                            sQLiteDatabase522.executeFast(String.format(locale22, "DELETE FROM media_v4 WHERE uid = %d AND mid <= %d", objArr522)).stepThis().dispose();
                                            SQLiteDatabase sQLiteDatabase622 = messagesStorage.database;
                                            Object[] objArr622 = new Object[i2];
                                            objArr622[0] = Long.valueOf(j2);
                                            sQLiteDatabase622.executeFast(String.format(locale22, "UPDATE media_counts_v2 SET old = 1 WHERE uid = %d", objArr622)).stepThis().dispose();
                                            SQLiteDatabase sQLiteDatabase722 = messagesStorage.database;
                                            Object[] objArr722 = new Object[i2];
                                            objArr722[0] = Long.valueOf(j2);
                                            sQLiteDatabase722.executeFast(String.format(locale22, "UPDATE media_counts_topics SET old = 1 WHERE uid = %d", objArr722)).stepThis().dispose();
                                            messagesStorage.updateWidgets(arrayList622);
                                            return arrayList622;
                                        }
                                    } catch (Throwable th4) {
                                        th = th4;
                                        sQLitePreparedStatement2 = null;
                                        sQLiteCursor2 = queryFinalized;
                                        if (sQLiteCursor2 != null) {
                                            sQLiteCursor2.dispose();
                                        }
                                        if (sQLitePreparedStatement2 != null) {
                                            sQLitePreparedStatement2.dispose();
                                        }
                                        throw th;
                                    }
                                }
                                if (DialogObject.isEncryptedDialog(longValue)) {
                                }
                                byteBufferValue = queryFinalized.byteBufferValue(1);
                                if (byteBufferValue == null) {
                                }
                                i6 = 0;
                                z5 = z4;
                                queryFinalized = sQLiteCursor4;
                            } catch (Exception e6) {
                                e = e6;
                                sQLiteCursor4 = queryFinalized;
                                z2 = true;
                            }
                        } catch (Throwable th5) {
                            th = th5;
                            sQLiteCursor4 = queryFinalized;
                        }
                    } catch (Exception e7) {
                        e = e7;
                        sQLiteCursor4 = queryFinalized;
                        z2 = z5;
                    }
                }
                sQLiteCursor4 = queryFinalized;
                i2 = z5;
                messagesStorage = this;
                sQLiteCursor4.dispose();
            } catch (Throwable th6) {
                th = th6;
                sQLiteCursor2 = sQLiteCursor;
                sQLitePreparedStatement2 = sQLitePreparedStatement;
            }
            try {
                messagesStorage.deleteFromDownloadQueue(arrayList3, i2);
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$markMessagesAsDeletedInternal$200(arrayList2);
                    }
                });
                getFileLoader().deleteFiles(arrayList, 0);
                i3 = 0;
                while (i3 < longSparseArray.size()) {
                    long keyAt = longSparseArray.keyAt(i3);
                    Integer[] numArr2 = (Integer[]) longSparseArray.valueAt(i3);
                    SQLiteCursor queryFinalized2 = messagesStorage.database.queryFinalized("SELECT unread_count, unread_count_i FROM dialogs WHERE did = " + keyAt, new Object[0]);
                    try {
                        if (queryFinalized2.next()) {
                            i4 = queryFinalized2.intValue(0);
                            i5 = queryFinalized2.intValue(i2);
                        } else {
                            i4 = 0;
                            i5 = 0;
                        }
                        queryFinalized2.dispose();
                        ArrayList<Long> arrayList7 = arrayList4;
                        arrayList7.add(Long.valueOf(keyAt));
                        sQLitePreparedStatement3 = messagesStorage.database.executeFast("UPDATE dialogs SET unread_count = ?, unread_count_i = ? WHERE did = ?");
                        try {
                            sQLitePreparedStatement3.requery();
                            sQLitePreparedStatement3.bindInteger(i2, Math.max(0, i4 - numArr2[0].intValue()));
                            sQLitePreparedStatement3.bindInteger(2, Math.max(0, i5 - numArr2[i2].intValue()));
                            sQLitePreparedStatement3.bindLong(3, keyAt);
                            sQLitePreparedStatement3.step();
                            sQLitePreparedStatement3.dispose();
                            i3++;
                            arrayList4 = arrayList7;
                        } catch (Exception e8) {
                            e = e8;
                            sQLiteCursor3 = null;
                            messagesStorage.checkSQLException(e);
                            if (sQLiteCursor3 != null) {
                            }
                            if (sQLitePreparedStatement3 != null) {
                            }
                        } catch (Throwable th7) {
                            th = th7;
                            sQLitePreparedStatement2 = sQLitePreparedStatement3;
                            sQLiteCursor2 = null;
                            if (sQLiteCursor2 != null) {
                            }
                            if (sQLitePreparedStatement2 != null) {
                            }
                            throw th;
                        }
                    } catch (Exception e9) {
                        e = e9;
                        sQLiteCursor3 = queryFinalized2;
                        sQLitePreparedStatement3 = null;
                        messagesStorage.checkSQLException(e);
                        if (sQLiteCursor3 != null) {
                        }
                        if (sQLitePreparedStatement3 != null) {
                        }
                    } catch (Throwable th8) {
                        th = th8;
                        sQLiteCursor2 = queryFinalized2;
                        sQLitePreparedStatement2 = null;
                        if (sQLiteCursor2 != null) {
                        }
                        if (sQLitePreparedStatement2 != null) {
                        }
                        throw th;
                    }
                }
                ArrayList<Long> arrayList6222 = arrayList4;
                SQLiteDatabase sQLiteDatabase822 = messagesStorage.database;
                Locale locale222 = Locale.US;
                Object[] objArr822 = new Object[2];
                objArr822[0] = Long.valueOf(j);
                objArr822[i2] = Integer.valueOf(i);
                sQLiteDatabase822.executeFast(String.format(locale222, "UPDATE chat_settings_v2 SET pinned = 0 WHERE uid = %d AND pinned <= %d", objArr822)).stepThis().dispose();
                SQLiteDatabase sQLiteDatabase2222 = messagesStorage.database;
                Object[] objArr2222 = new Object[2];
                objArr2222[0] = Long.valueOf(j);
                objArr2222[i2] = Integer.valueOf(i);
                sQLiteDatabase2222.executeFast(String.format(locale222, "DELETE FROM chat_pinned_v2 WHERE uid = %d AND mid <= %d", objArr2222)).stepThis().dispose();
                sQLiteCursor3 = messagesStorage.database.queryFinalized("SELECT changes()", new Object[0]);
                intValue = !sQLiteCursor3.next() ? sQLiteCursor3.intValue(0) : 0;
                sQLiteCursor3.dispose();
                if (intValue > 0) {
                    SQLiteDatabase sQLiteDatabase9 = messagesStorage.database;
                    Object[] objArr9 = new Object[i2];
                    objArr9[0] = Long.valueOf(j2);
                    sQLiteCursor3 = sQLiteDatabase9.queryFinalized(String.format(locale222, "SELECT count FROM chat_pinned_count WHERE uid = %d", objArr9), new Object[0]);
                    if (sQLiteCursor3.next()) {
                        int intValue3 = sQLiteCursor3.intValue(0);
                        sQLitePreparedStatement3 = messagesStorage.database.executeFast("UPDATE chat_pinned_count SET count = ? WHERE uid = ?");
                        try {
                            sQLitePreparedStatement3.requery();
                            sQLitePreparedStatement3.bindInteger(i2, Math.max(0, intValue3 - intValue));
                            sQLitePreparedStatement3.bindLong(2, j2);
                            sQLitePreparedStatement3.step();
                            sQLitePreparedStatement3.dispose();
                        } catch (Exception e10) {
                            e = e10;
                            messagesStorage.checkSQLException(e);
                            if (sQLiteCursor3 != null) {
                                sQLiteCursor3.dispose();
                            }
                            if (sQLitePreparedStatement3 != null) {
                                return null;
                            }
                            sQLitePreparedStatement3.dispose();
                            return null;
                        }
                    }
                    sQLiteCursor3.dispose();
                }
                SQLiteDatabase sQLiteDatabase3222 = messagesStorage.database;
                Object[] objArr3222 = new Object[2];
                objArr3222[0] = Long.valueOf(j2);
                objArr3222[i2] = Integer.valueOf(i);
                sQLiteDatabase3222.executeFast(String.format(locale222, "DELETE FROM messages_v2 WHERE uid = %d AND mid <= %d", objArr3222)).stepThis().dispose();
                SQLiteDatabase sQLiteDatabase4222 = messagesStorage.database;
                Object[] objArr4222 = new Object[2];
                objArr4222[0] = Long.valueOf(j2);
                objArr4222[i2] = Integer.valueOf(i);
                sQLiteDatabase4222.executeFast(String.format(locale222, "DELETE FROM messages_topics WHERE uid = %d AND mid <= %d", objArr4222)).stepThis().dispose();
                SQLiteDatabase sQLiteDatabase5222 = messagesStorage.database;
                Object[] objArr5222 = new Object[2];
                objArr5222[0] = Long.valueOf(j2);
                objArr5222[i2] = Integer.valueOf(i);
                sQLiteDatabase5222.executeFast(String.format(locale222, "DELETE FROM media_v4 WHERE uid = %d AND mid <= %d", objArr5222)).stepThis().dispose();
                SQLiteDatabase sQLiteDatabase6222 = messagesStorage.database;
                Object[] objArr6222 = new Object[i2];
                objArr6222[0] = Long.valueOf(j2);
                sQLiteDatabase6222.executeFast(String.format(locale222, "UPDATE media_counts_v2 SET old = 1 WHERE uid = %d", objArr6222)).stepThis().dispose();
                SQLiteDatabase sQLiteDatabase7222 = messagesStorage.database;
                Object[] objArr7222 = new Object[i2];
                objArr7222[0] = Long.valueOf(j2);
                sQLiteDatabase7222.executeFast(String.format(locale222, "UPDATE media_counts_topics SET old = 1 WHERE uid = %d", objArr7222)).stepThis().dispose();
                messagesStorage.updateWidgets(arrayList6222);
                return arrayList6222;
            } catch (Exception e11) {
                e = e11;
                sQLiteCursor3 = null;
                sQLitePreparedStatement3 = null;
                messagesStorage.checkSQLException(e);
                if (sQLiteCursor3 != null) {
                }
                if (sQLitePreparedStatement3 != null) {
                }
            } catch (Throwable th9) {
                th = th9;
                sQLiteCursor2 = null;
                sQLitePreparedStatement2 = null;
                if (sQLiteCursor2 != null) {
                }
                if (sQLitePreparedStatement2 != null) {
                }
                throw th;
            }
        } catch (Exception e12) {
            e = e12;
            messagesStorage = this;
        } catch (Throwable th10) {
            th = th10;
        }
    }

    public void lambda$markMessagesAsDeletedInternal$200(ArrayList arrayList) {
        getFileLoader().cancelLoadFiles(arrayList);
    }

    public ArrayList<Long> markMessagesAsDeleted(final long j, final int i, boolean z, final boolean z2) {
        if (z) {
            this.storageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$markMessagesAsDeleted$201(j, i, z2);
                }
            });
            return null;
        }
        return lambda$markMessagesAsDeleted$201(j, i, z2);
    }

    private void fixUnsupportedMedia(TLRPC$Message tLRPC$Message) {
        if (tLRPC$Message == null) {
            return;
        }
        TLRPC$MessageMedia tLRPC$MessageMedia = tLRPC$Message.media;
        if (tLRPC$MessageMedia instanceof TLRPC$TL_messageMediaUnsupported_old) {
            if (tLRPC$MessageMedia.bytes.length == 0) {
                tLRPC$MessageMedia.bytes = Utilities.intToBytes(172);
            }
        } else if (tLRPC$MessageMedia instanceof TLRPC$TL_messageMediaUnsupported) {
            TLRPC$TL_messageMediaUnsupported_old tLRPC$TL_messageMediaUnsupported_old = new TLRPC$TL_messageMediaUnsupported_old();
            tLRPC$Message.media = tLRPC$TL_messageMediaUnsupported_old;
            tLRPC$TL_messageMediaUnsupported_old.bytes = Utilities.intToBytes(172);
            tLRPC$Message.flags |= 512;
        }
    }

    private void doneHolesInTable(String str, long j, int i, long j2) throws Exception {
        SQLitePreparedStatement executeFast;
        int i2 = 2;
        if (j2 != 0) {
            if (i == 0) {
                this.database.executeFast(String.format(Locale.US, "DELETE FROM " + str + " WHERE uid = %d AND topic_id = %d", Long.valueOf(j), Long.valueOf(j2))).stepThis().dispose();
            } else {
                this.database.executeFast(String.format(Locale.US, "DELETE FROM " + str + " WHERE uid = %d AND topic_id = %d AND start = 0", Long.valueOf(j), Long.valueOf(j2))).stepThis().dispose();
            }
        } else if (i == 0) {
            this.database.executeFast(String.format(Locale.US, "DELETE FROM " + str + " WHERE uid = %d", Long.valueOf(j))).stepThis().dispose();
        } else {
            this.database.executeFast(String.format(Locale.US, "DELETE FROM " + str + " WHERE uid = %d AND start = 0", Long.valueOf(j))).stepThis().dispose();
        }
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                if (j2 != 0) {
                    executeFast = this.database.executeFast("REPLACE INTO " + str + " VALUES(?, ?, ?, ?)");
                } else {
                    executeFast = this.database.executeFast("REPLACE INTO " + str + " VALUES(?, ?, ?)");
                }
                sQLitePreparedStatement = executeFast;
                sQLitePreparedStatement.requery();
                sQLitePreparedStatement.bindLong(1, j);
                if (j2 != 0) {
                    sQLitePreparedStatement.bindLong(2, j2);
                    i2 = 3;
                }
                sQLitePreparedStatement.bindInteger(i2, 1);
                sQLitePreparedStatement.bindInteger(i2 + 1, 1);
                sQLitePreparedStatement.step();
                sQLitePreparedStatement.dispose();
            } catch (Exception e) {
                throw e;
            }
        } catch (Throwable th) {
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void doneHolesInMedia(long j, int i, int i2, long j2) throws Exception {
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLitePreparedStatement executeFast;
        SQLitePreparedStatement stepThis;
        SQLitePreparedStatement executeFast2;
        int i3;
        int i4 = 3;
        if (i2 == -1) {
            if (j2 != 0) {
                if (i == 0) {
                    stepThis = this.database.executeFast(String.format(Locale.US, "DELETE FROM media_holes_topics WHERE uid = %d AND topic_id = %d", Long.valueOf(j), Long.valueOf(j2))).stepThis();
                    stepThis.dispose();
                } else {
                    stepThis = this.database.executeFast(String.format(Locale.US, "DELETE FROM media_holes_topics WHERE uid = %d AND topic_id = %d AND start = 0", Long.valueOf(j), Long.valueOf(j2))).stepThis();
                    stepThis.dispose();
                }
            } else if (i == 0) {
                stepThis = this.database.executeFast(String.format(Locale.US, "DELETE FROM media_holes_v2 WHERE uid = %d", Long.valueOf(j))).stepThis();
                stepThis.dispose();
            } else {
                stepThis = this.database.executeFast(String.format(Locale.US, "DELETE FROM media_holes_v2 WHERE uid = %d AND start = 0", Long.valueOf(j))).stepThis();
                stepThis.dispose();
            }
            try {
                try {
                    if (j2 != 0) {
                        executeFast2 = this.database.executeFast("REPLACE INTO media_holes_topics VALUES(?, ?, ?, ?, ?)");
                    } else {
                        executeFast2 = this.database.executeFast("REPLACE INTO media_holes_v2 VALUES(?, ?, ?, ?)");
                    }
                    SQLitePreparedStatement sQLitePreparedStatement2 = executeFast2;
                    for (int i5 = 0; i5 < 8; i5++) {
                        try {
                            sQLitePreparedStatement2.requery();
                            sQLitePreparedStatement2.bindLong(1, j);
                            if (j2 != 0) {
                                sQLitePreparedStatement2.bindLong(2, j2);
                                i3 = 3;
                            } else {
                                i3 = 2;
                            }
                            int i6 = i3 + 1;
                            sQLitePreparedStatement2.bindInteger(i3, i5);
                            sQLitePreparedStatement2.bindInteger(i6, 1);
                            sQLitePreparedStatement2.bindInteger(i6 + 1, 1);
                            sQLitePreparedStatement2.step();
                        } catch (Exception e) {
                            throw e;
                        }
                    }
                    if (sQLitePreparedStatement2 != null) {
                        sQLitePreparedStatement2.dispose();
                        return;
                    }
                    return;
                } catch (Throwable th) {
                    th = th;
                    if (stepThis != null) {
                        stepThis.dispose();
                    }
                    throw th;
                }
            } catch (Exception e2) {
                throw e2;
            } catch (Throwable th2) {
                th = th2;
                stepThis = null;
                if (stepThis != null) {
                }
                throw th;
            }
        }
        if (j2 != 0) {
            if (i == 0) {
                this.database.executeFast(String.format(Locale.US, "DELETE FROM media_holes_topics WHERE uid = %d AND topic_id = %d AND type = %d", Long.valueOf(j), Long.valueOf(j2), Integer.valueOf(i2))).stepThis().dispose();
            } else {
                this.database.executeFast(String.format(Locale.US, "DELETE FROM media_holes_topics WHERE uid = %d AND topic_id = %d AND type = %d AND start = 0", Long.valueOf(j), Long.valueOf(j2), Integer.valueOf(i2))).stepThis().dispose();
            }
        } else if (i == 0) {
            this.database.executeFast(String.format(Locale.US, "DELETE FROM media_holes_v2 WHERE uid = %d AND type = %d", Long.valueOf(j), Integer.valueOf(i2))).stepThis().dispose();
        } else {
            this.database.executeFast(String.format(Locale.US, "DELETE FROM media_holes_v2 WHERE uid = %d AND type = %d AND start = 0", Long.valueOf(j), Integer.valueOf(i2))).stepThis().dispose();
        }
        try {
            if (j2 != 0) {
                executeFast = this.database.executeFast("REPLACE INTO media_holes_topics VALUES(?, ?, ?, ?, ?)");
            } else {
                executeFast = this.database.executeFast("REPLACE INTO media_holes_v2 VALUES(?, ?, ?, ?)");
            }
            try {
                executeFast.requery();
                executeFast.bindLong(1, j);
                if (j2 != 0) {
                    executeFast.bindLong(2, j2);
                } else {
                    i4 = 2;
                }
                int i7 = i4 + 1;
                executeFast.bindInteger(i4, i2);
                executeFast.bindInteger(i7, 1);
                executeFast.bindInteger(i7 + 1, 1);
                executeFast.step();
                executeFast.dispose();
                executeFast.dispose();
            } catch (Exception e3) {
                e = e3;
                sQLitePreparedStatement = executeFast;
                try {
                    throw e;
                } catch (Throwable th3) {
                    th = th3;
                    if (sQLitePreparedStatement != null) {
                        sQLitePreparedStatement.dispose();
                    }
                    throw th;
                }
            } catch (Throwable th4) {
                th = th4;
                sQLitePreparedStatement = executeFast;
                if (sQLitePreparedStatement != null) {
                }
                throw th;
            }
        } catch (Exception e4) {
            e = e4;
            sQLitePreparedStatement = null;
        } catch (Throwable th5) {
            th = th5;
            sQLitePreparedStatement = null;
        }
    }

    public static class Hole {
        public int end;
        public int start;
        public int type;

        public Hole(int i, int i2) {
            this.start = i;
            this.end = i2;
        }

        public Hole(int i, int i2, int i3) {
            this.type = i;
            this.start = i2;
            this.end = i3;
        }
    }

    public void closeHolesInMedia(long j, int i, int i2, int i3, long j2) {
        Exception exc;
        SQLitePreparedStatement sQLitePreparedStatement;
        Throwable th;
        SQLiteCursor sQLiteCursor;
        SQLiteCursor queryFinalized;
        ArrayList arrayList;
        SQLitePreparedStatement executeFast;
        int i4;
        boolean z;
        int i5 = 2;
        int i6 = 0;
        try {
            try {
                if (j2 != 0) {
                    if (i3 < 0) {
                        queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT type, start, end FROM media_holes_topics WHERE uid = %d AND topic_id = %d AND type >= 0 AND ((end >= %d AND end <= %d) OR (start >= %d AND start <= %d) OR (start >= %d AND end <= %d) OR (start <= %d AND end >= %d))", Long.valueOf(j), Long.valueOf(j2), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2)), new Object[0]);
                    } else {
                        queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT type, start, end FROM media_holes_topics WHERE uid = %d AND topic_id = %d AND type = %d AND ((end >= %d AND end <= %d) OR (start >= %d AND start <= %d) OR (start >= %d AND end <= %d) OR (start <= %d AND end >= %d))", Long.valueOf(j), Long.valueOf(j2), Integer.valueOf(i3), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2)), new Object[0]);
                    }
                } else if (i3 < 0) {
                    queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT type, start, end FROM media_holes_v2 WHERE uid = %d AND type >= 0 AND ((end >= %d AND end <= %d) OR (start >= %d AND start <= %d) OR (start >= %d AND end <= %d) OR (start <= %d AND end >= %d))", Long.valueOf(j), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2)), new Object[0]);
                } else {
                    queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT type, start, end FROM media_holes_v2 WHERE uid = %d AND type = %d AND ((end >= %d AND end <= %d) OR (start >= %d AND start <= %d) OR (start >= %d AND end <= %d) OR (start <= %d AND end >= %d))", Long.valueOf(j), Integer.valueOf(i3), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2)), new Object[0]);
                }
                ArrayList arrayList2 = null;
                while (queryFinalized.next()) {
                    try {
                        if (arrayList2 == null) {
                            arrayList2 = new ArrayList();
                        }
                        int intValue = queryFinalized.intValue(i6);
                        int intValue2 = queryFinalized.intValue(1);
                        int intValue3 = queryFinalized.intValue(i5);
                        if (intValue2 != intValue3 || intValue2 != 1) {
                            arrayList2.add(new Hole(intValue, intValue2, intValue3));
                        }
                        i5 = 2;
                        i6 = 0;
                    } catch (Exception e) {
                        exc = e;
                        sQLiteCursor = queryFinalized;
                        sQLitePreparedStatement = null;
                        try {
                            checkSQLException(exc);
                            if (sQLitePreparedStatement != null) {
                                sQLitePreparedStatement.dispose();
                            }
                            if (sQLiteCursor == null) {
                                sQLiteCursor.dispose();
                                return;
                            }
                            return;
                        } catch (Throwable th2) {
                            th = th2;
                            if (sQLitePreparedStatement != null) {
                                sQLitePreparedStatement.dispose();
                            }
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                                throw th;
                            }
                            throw th;
                        }
                    } catch (Throwable th3) {
                        th = th3;
                        sQLiteCursor = queryFinalized;
                        sQLitePreparedStatement = null;
                        if (sQLitePreparedStatement != null) {
                        }
                        if (sQLiteCursor != null) {
                        }
                    }
                }
                queryFinalized.dispose();
                if (arrayList2 != null) {
                    int i7 = 0;
                    while (i7 < arrayList2.size()) {
                        Hole hole = (Hole) arrayList2.get(i7);
                        int i8 = hole.end;
                        if (i2 < i8 - 1 || i > hole.start + 1) {
                            arrayList = arrayList2;
                            if (i2 < i8 - 1) {
                                int i9 = hole.start;
                                if (i > i9 + 1) {
                                    if (j2 != 0) {
                                        this.database.executeFast(String.format(Locale.US, "DELETE FROM media_holes_topics WHERE uid = %d AND topic_id = %d AND type = %d AND start = %d AND end = %d", Long.valueOf(j), Long.valueOf(j2), Integer.valueOf(hole.type), Integer.valueOf(hole.start), Integer.valueOf(hole.end))).stepThis().dispose();
                                        executeFast = this.database.executeFast("REPLACE INTO media_holes_topics VALUES(?, ?, ?, ?, ?)");
                                    } else {
                                        this.database.executeFast(String.format(Locale.US, "DELETE FROM media_holes_v2 WHERE uid = %d AND type = %d AND start = %d AND end = %d", Long.valueOf(j), Integer.valueOf(hole.type), Integer.valueOf(hole.start), Integer.valueOf(hole.end))).stepThis().dispose();
                                        executeFast = this.database.executeFast("REPLACE INTO media_holes_v2 VALUES(?, ?, ?, ?)");
                                    }
                                    try {
                                        executeFast.requery();
                                        executeFast.bindLong(1, j);
                                        if (j2 != 0) {
                                            executeFast.bindLong(2, j2);
                                            i4 = 3;
                                        } else {
                                            i4 = 2;
                                        }
                                        int i10 = i4 + 1;
                                        executeFast.bindInteger(i4, hole.type);
                                        executeFast.bindInteger(i10, hole.start);
                                        executeFast.bindInteger(i10 + 1, i);
                                        executeFast.step();
                                        executeFast.requery();
                                        z = true;
                                        executeFast.bindLong(1, j);
                                        executeFast.bindInteger(2, hole.type);
                                        executeFast.bindInteger(3, i2);
                                        executeFast.bindInteger(4, hole.end);
                                        executeFast.step();
                                        executeFast.dispose();
                                        i7++;
                                        arrayList2 = arrayList;
                                    } catch (Exception e2) {
                                        exc = e2;
                                        sQLitePreparedStatement = executeFast;
                                        sQLiteCursor = null;
                                        checkSQLException(exc);
                                        if (sQLitePreparedStatement != null) {
                                        }
                                        if (sQLiteCursor == null) {
                                        }
                                    } catch (Throwable th4) {
                                        th = th4;
                                        sQLitePreparedStatement = executeFast;
                                        sQLiteCursor = null;
                                        if (sQLitePreparedStatement != null) {
                                        }
                                        if (sQLiteCursor != null) {
                                        }
                                    }
                                } else if (i9 != i2) {
                                    if (j2 != 0) {
                                        try {
                                            this.database.executeFast(String.format(Locale.US, "UPDATE media_holes_topics SET start = %d WHERE uid = %d AND topic_id = %d AND type = %d AND start = %d AND end = %d", Integer.valueOf(i2), Long.valueOf(j), Long.valueOf(j2), Integer.valueOf(hole.type), Integer.valueOf(hole.start), Integer.valueOf(hole.end))).stepThis().dispose();
                                        } catch (Exception e3) {
                                            checkSQLException(e3, false);
                                        }
                                    } else {
                                        this.database.executeFast(String.format(Locale.US, "UPDATE media_holes_v2 SET start = %d WHERE uid = %d AND type = %d AND start = %d AND end = %d", Integer.valueOf(i2), Long.valueOf(j), Integer.valueOf(hole.type), Integer.valueOf(hole.start), Integer.valueOf(hole.end))).stepThis().dispose();
                                    }
                                }
                            } else if (i8 != i) {
                                if (j2 != 0) {
                                    try {
                                        this.database.executeFast(String.format(Locale.US, "UPDATE media_holes_topics SET end = %d WHERE uid = %d AND topic_id = %d AND type = %d AND start = %d AND end = %d", Integer.valueOf(i), Long.valueOf(j), Long.valueOf(j2), Integer.valueOf(hole.type), Integer.valueOf(hole.start), Integer.valueOf(hole.end))).stepThis().dispose();
                                    } catch (Exception e4) {
                                        checkSQLException(e4, false);
                                    }
                                } else {
                                    this.database.executeFast(String.format(Locale.US, "UPDATE media_holes_v2 SET end = %d WHERE uid = %d AND type = %d AND start = %d AND end = %d", Integer.valueOf(i), Long.valueOf(j), Integer.valueOf(hole.type), Integer.valueOf(hole.start), Integer.valueOf(hole.end))).stepThis().dispose();
                                }
                            }
                        } else if (j2 != 0) {
                            arrayList = arrayList2;
                            this.database.executeFast(String.format(Locale.US, "DELETE FROM media_holes_topics WHERE uid = %d AND topic_id = %d AND type = %d AND start = %d AND end = %d", Long.valueOf(j), Long.valueOf(j2), Integer.valueOf(hole.type), Integer.valueOf(hole.start), Integer.valueOf(hole.end))).stepThis().dispose();
                        } else {
                            arrayList = arrayList2;
                            this.database.executeFast(String.format(Locale.US, "DELETE FROM media_holes_v2 WHERE uid = %d AND type = %d AND start = %d AND end = %d", Long.valueOf(j), Integer.valueOf(hole.type), Integer.valueOf(hole.start), Integer.valueOf(hole.end))).stepThis().dispose();
                        }
                        z = true;
                        i7++;
                        arrayList2 = arrayList;
                    }
                }
            } catch (Throwable th5) {
                th = th5;
                sQLitePreparedStatement = null;
            }
        } catch (Exception e5) {
            exc = e5;
            sQLitePreparedStatement = null;
        }
    }

    private void closeHolesInTable(String str, long j, int i, int i2, long j2) {
        Throwable th;
        SQLiteCursor sQLiteCursor;
        Exception exc;
        SQLiteCursor sQLiteCursor2;
        SQLitePreparedStatement executeFast;
        long j3;
        long j4;
        int i3;
        int i4;
        Exception exc2;
        boolean z;
        SQLiteDatabase sQLiteDatabase;
        String str2;
        Object[] objArr;
        Exception exc3;
        boolean z2;
        Locale locale;
        SQLiteDatabase sQLiteDatabase2;
        String str3;
        Object[] objArr2;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        int i5 = 0;
        try {
            try {
                try {
                    if (j2 != 0) {
                        sQLiteCursor2 = this.database.queryFinalized(String.format(Locale.US, "SELECT start, end FROM " + str + " WHERE uid = %d AND topic_id = %d AND ((end >= %d AND end <= %d) OR (start >= %d AND start <= %d) OR (start >= %d AND end <= %d) OR (start <= %d AND end >= %d))", Long.valueOf(j), Long.valueOf(j2), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2)), new Object[0]);
                    } else {
                        sQLiteCursor2 = this.database.queryFinalized(String.format(Locale.US, "SELECT start, end FROM " + str + " WHERE uid = %d AND ((end >= %d AND end <= %d) OR (start >= %d AND start <= %d) OR (start >= %d AND end <= %d) OR (start <= %d AND end >= %d))", Long.valueOf(j), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i), Integer.valueOf(i2)), new Object[0]);
                    }
                    ArrayList arrayList = null;
                    while (sQLiteCursor2.next()) {
                        try {
                            if (arrayList == null) {
                                arrayList = new ArrayList();
                            }
                            int intValue = sQLiteCursor2.intValue(i5);
                            int intValue2 = sQLiteCursor2.intValue(1);
                            if (intValue != intValue2 || intValue != 1) {
                                arrayList.add(new Hole(intValue, intValue2));
                            }
                            i5 = 0;
                        } catch (Exception e) {
                            exc = e;
                        }
                    }
                    sQLiteCursor2.dispose();
                    if (arrayList != null) {
                        int i6 = 0;
                        while (i6 < arrayList.size()) {
                            Hole hole = (Hole) arrayList.get(i6);
                            int i7 = hole.end;
                            ArrayList arrayList2 = arrayList;
                            int i8 = i6;
                            if (i2 < i7 - 1 || i > hole.start + 1) {
                                if (i2 < i7 - 1) {
                                    int i9 = hole.start;
                                    if (i > i9 + 1) {
                                        if (j2 != 0) {
                                            this.database.executeFast(String.format(Locale.US, "DELETE FROM " + str + " WHERE uid = %d AND topic_id = %d AND start = %d AND end = %d", Long.valueOf(j), Long.valueOf(j2), Integer.valueOf(hole.start), Integer.valueOf(hole.end))).stepThis().dispose();
                                            executeFast = this.database.executeFast("REPLACE INTO " + str + " VALUES(?, ?, ?, ?)");
                                        } else {
                                            this.database.executeFast(String.format(Locale.US, "DELETE FROM " + str + " WHERE uid = %d AND start = %d AND end = %d", Long.valueOf(j), Integer.valueOf(hole.start), Integer.valueOf(hole.end))).stepThis().dispose();
                                            executeFast = this.database.executeFast("REPLACE INTO " + str + " VALUES(?, ?, ?)");
                                        }
                                        try {
                                            executeFast.requery();
                                            j3 = j;
                                            executeFast.bindLong(1, j3);
                                            if (j2 != 0) {
                                                j4 = j2;
                                                executeFast.bindLong(2, j4);
                                                i3 = 3;
                                            } else {
                                                j4 = j2;
                                                i3 = 2;
                                            }
                                            executeFast.bindInteger(i3, hole.start);
                                            executeFast.bindInteger(i3 + 1, i);
                                            executeFast.step();
                                            executeFast.requery();
                                            executeFast.bindLong(1, j3);
                                            if (j2 != 0) {
                                                executeFast.bindLong(2, j4);
                                                i4 = 3;
                                            } else {
                                                i4 = 2;
                                            }
                                            executeFast.bindInteger(i4, i2);
                                            executeFast.bindInteger(i4 + 1, hole.end);
                                            executeFast.step();
                                            executeFast.dispose();
                                            i6 = i8 + 1;
                                            arrayList = arrayList2;
                                        } catch (Exception e2) {
                                            exc = e2;
                                            sQLiteCursor2 = null;
                                            sQLitePreparedStatement = executeFast;
                                            checkSQLException(exc);
                                            if (sQLitePreparedStatement != null) {
                                                sQLitePreparedStatement.dispose();
                                            }
                                            if (sQLiteCursor2 != null) {
                                                sQLiteCursor2.dispose();
                                                return;
                                            }
                                            return;
                                        } catch (Throwable th2) {
                                            th = th2;
                                            sQLiteCursor = null;
                                            sQLitePreparedStatement = executeFast;
                                            if (sQLitePreparedStatement != null) {
                                                sQLitePreparedStatement.dispose();
                                            }
                                            if (sQLiteCursor != null) {
                                                sQLiteCursor.dispose();
                                                throw th;
                                            }
                                            throw th;
                                        }
                                    } else if (i9 != i2) {
                                        if (j2 != 0) {
                                            try {
                                                SQLiteDatabase sQLiteDatabase3 = this.database;
                                                Locale locale2 = Locale.US;
                                                String str4 = "DELETE FROM " + str + " WHERE uid = %d AND topic_id = %d AND start = %d AND end = %d";
                                                Object[] objArr3 = new Object[4];
                                                try {
                                                    objArr3[0] = Long.valueOf(j);
                                                    objArr3[1] = Long.valueOf(j2);
                                                    objArr3[2] = Integer.valueOf(hole.start);
                                                    objArr3[3] = Integer.valueOf(hole.end);
                                                    sQLiteDatabase3.executeFast(String.format(locale2, str4, objArr3)).stepThis().dispose();
                                                    sQLiteDatabase = this.database;
                                                    str2 = "REPLACE INTO " + str + " VALUES(%d, %d, %d, %d)";
                                                    objArr = new Object[4];
                                                } catch (Exception e3) {
                                                    exc2 = e3;
                                                    z = false;
                                                }
                                                try {
                                                    objArr[0] = Long.valueOf(j);
                                                    objArr[1] = Long.valueOf(j2);
                                                    objArr[2] = Integer.valueOf(i2);
                                                    objArr[3] = Integer.valueOf(hole.end);
                                                    sQLiteDatabase.executeFast(String.format(locale2, str2, objArr)).stepThis().dispose();
                                                } catch (Exception e4) {
                                                    exc2 = e4;
                                                    z = false;
                                                    checkSQLException(exc2, z);
                                                    j3 = j;
                                                    j4 = j2;
                                                    i6 = i8 + 1;
                                                    arrayList = arrayList2;
                                                }
                                            } catch (Exception e5) {
                                                exc2 = e5;
                                                z = false;
                                            }
                                        } else {
                                            SQLiteDatabase sQLiteDatabase4 = this.database;
                                            Locale locale3 = Locale.US;
                                            String str5 = "DELETE FROM " + str + " WHERE uid = %d AND start = %d AND end = %d";
                                            Object[] objArr4 = new Object[3];
                                            objArr4[0] = Long.valueOf(j);
                                            objArr4[1] = Integer.valueOf(hole.start);
                                            objArr4[2] = Integer.valueOf(hole.end);
                                            sQLiteDatabase4.executeFast(String.format(locale3, str5, objArr4)).stepThis().dispose();
                                            SQLiteDatabase sQLiteDatabase5 = this.database;
                                            String str6 = "REPLACE INTO " + str + " VALUES(%d, %d, %d)";
                                            Object[] objArr5 = new Object[3];
                                            objArr5[0] = Long.valueOf(j);
                                            objArr5[1] = Integer.valueOf(i2);
                                            objArr5[2] = Integer.valueOf(hole.end);
                                            sQLiteDatabase5.executeFast(String.format(locale3, str6, objArr5)).stepThis().dispose();
                                        }
                                    }
                                } else if (i7 != i) {
                                    if (j2 != 0) {
                                        try {
                                            SQLiteDatabase sQLiteDatabase6 = this.database;
                                            locale = Locale.US;
                                            String str7 = "DELETE FROM " + str + " WHERE uid = %d AND topic_id = %d AND start = %d AND end = %d";
                                            Object[] objArr6 = new Object[4];
                                            try {
                                                objArr6[0] = Long.valueOf(j);
                                                objArr6[1] = Long.valueOf(j2);
                                                objArr6[2] = Integer.valueOf(hole.start);
                                                objArr6[3] = Integer.valueOf(hole.end);
                                                sQLiteDatabase6.executeFast(String.format(locale, str7, objArr6)).stepThis().dispose();
                                                sQLiteDatabase2 = this.database;
                                                str3 = "REPLACE INTO " + str + " VALUES(%d, %d, %d, %d)";
                                                objArr2 = new Object[4];
                                            } catch (Exception e6) {
                                                exc3 = e6;
                                                z2 = false;
                                            }
                                        } catch (Exception e7) {
                                            exc3 = e7;
                                            z2 = false;
                                        }
                                        try {
                                            objArr2[0] = Long.valueOf(j);
                                            objArr2[1] = Long.valueOf(j2);
                                            objArr2[2] = Integer.valueOf(hole.start);
                                            objArr2[3] = Integer.valueOf(i);
                                            sQLiteDatabase2.executeFast(String.format(locale, str3, objArr2)).stepThis().dispose();
                                        } catch (Exception e8) {
                                            exc3 = e8;
                                            z2 = false;
                                            checkSQLException(exc3, z2);
                                            j3 = j;
                                            j4 = j2;
                                            i6 = i8 + 1;
                                            arrayList = arrayList2;
                                        }
                                    } else {
                                        SQLiteDatabase sQLiteDatabase7 = this.database;
                                        Locale locale4 = Locale.US;
                                        String str8 = "DELETE FROM " + str + " WHERE uid = %d AND start = %d AND end = %d";
                                        Object[] objArr7 = new Object[3];
                                        objArr7[0] = Long.valueOf(j);
                                        objArr7[1] = Integer.valueOf(hole.start);
                                        objArr7[2] = Integer.valueOf(hole.end);
                                        sQLiteDatabase7.executeFast(String.format(locale4, str8, objArr7)).stepThis().dispose();
                                        SQLiteDatabase sQLiteDatabase8 = this.database;
                                        String str9 = "REPLACE INTO " + str + " VALUES(%d, %d, %d)";
                                        Object[] objArr8 = new Object[3];
                                        objArr8[0] = Long.valueOf(j);
                                        objArr8[1] = Integer.valueOf(hole.start);
                                        objArr8[2] = Integer.valueOf(i);
                                        sQLiteDatabase8.executeFast(String.format(locale4, str9, objArr8)).stepThis().dispose();
                                    }
                                }
                            } else if (j2 != 0) {
                                this.database.executeFast(String.format(Locale.US, "DELETE FROM " + str + " WHERE uid = %d AND topic_id = %d AND start = %d AND end = %d", Long.valueOf(j), Long.valueOf(j2), Integer.valueOf(hole.start), Integer.valueOf(hole.end))).stepThis().dispose();
                            } else {
                                this.database.executeFast(String.format(Locale.US, "DELETE FROM " + str + " WHERE uid = %d AND start = %d AND end = %d", Long.valueOf(j), Integer.valueOf(hole.start), Integer.valueOf(hole.end))).stepThis().dispose();
                            }
                            j3 = j;
                            j4 = j2;
                            i6 = i8 + 1;
                            arrayList = arrayList2;
                        }
                    }
                } catch (Throwable th3) {
                    th = th3;
                }
            } catch (Exception e9) {
                exc = e9;
                sQLiteCursor2 = null;
            }
        } catch (Throwable th4) {
            th = th4;
            sQLiteCursor = null;
        }
    }

    public void replaceMessageIfExists(final TLRPC$Message tLRPC$Message, final ArrayList<TLRPC$User> arrayList, final ArrayList<TLRPC$Chat> arrayList2, final boolean z) {
        if (tLRPC$Message == null) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$replaceMessageIfExists$204(tLRPC$Message, z, arrayList, arrayList2);
            }
        });
    }

    public void lambda$replaceMessageIfExists$204(TLRPC$Message tLRPC$Message, boolean z, ArrayList arrayList, ArrayList arrayList2) {
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLitePreparedStatement sQLitePreparedStatement2;
        SQLiteDatabase sQLiteDatabase;
        SQLiteDatabase sQLiteDatabase2;
        SQLiteCursor sQLiteCursor;
        int i;
        NativeByteBuffer nativeByteBuffer;
        int i2;
        final ArrayList arrayList3;
        SQLiteDatabase sQLiteDatabase3;
        int i3;
        int i4;
        int i5;
        NativeByteBuffer nativeByteBuffer2;
        int i6;
        int i7;
        int i8;
        NativeByteBuffer nativeByteBuffer3;
        int i9;
        NativeByteBuffer nativeByteBuffer4;
        TLRPC$MessageReplyHeader tLRPC$MessageReplyHeader;
        int i10;
        long j;
        int i11;
        TLRPC$MessageReplyHeader tLRPC$MessageReplyHeader2;
        int i12;
        int i13;
        int i14 = 2;
        int i15 = 0;
        int i16 = 1;
        try {
            try {
                sQLiteCursor = this.database.queryFinalized(String.format(Locale.US, "SELECT uid, read_state, custom_params FROM messages_v2 WHERE mid = %d AND uid = %d LIMIT 1", Integer.valueOf(tLRPC$Message.id), Long.valueOf(MessageObject.getDialogId(tLRPC$Message))), new Object[0]);
                try {
                    try {
                    } catch (Exception e) {
                        e = e;
                        i = 0;
                    }
                } catch (Throwable th) {
                    th = th;
                    if (sQLiteCursor != null) {
                        sQLiteCursor.dispose();
                    }
                    throw th;
                }
            } catch (Exception e2) {
                e = e2;
                i = 0;
                sQLiteCursor = null;
            } catch (Throwable th2) {
                th = th2;
                sQLiteCursor = null;
            }
            if (!sQLiteCursor.next()) {
                sQLiteCursor.dispose();
                sQLiteCursor.dispose();
                SQLiteDatabase sQLiteDatabase4 = this.database;
                if (sQLiteDatabase4 != null) {
                    sQLiteDatabase4.commitTransaction();
                    return;
                }
                return;
            }
            i = sQLiteCursor.intValue(1);
            try {
                nativeByteBuffer = sQLiteCursor.byteBufferValue(2);
                sQLiteCursor.dispose();
            } catch (Exception e3) {
                e = e3;
                checkSQLException(e);
                if (sQLiteCursor != null) {
                    sQLiteCursor.dispose();
                }
                nativeByteBuffer = null;
                this.database.beginTransaction();
                long j2 = 0;
                if (tLRPC$Message.dialog_id == 0) {
                }
                fixUnsupportedMedia(tLRPC$Message);
                MessageObject.normalizeFlags(tLRPC$Message);
                NativeByteBuffer nativeByteBuffer5 = new NativeByteBuffer(tLRPC$Message.getObjectSize());
                tLRPC$Message.serializeToStream(nativeByteBuffer5);
                long clientUserId = getUserConfig().getClientUserId();
                i2 = 0;
                arrayList3 = null;
                while (i2 < i14) {
                }
                NativeByteBuffer nativeByteBuffer6 = nativeByteBuffer5;
                int i17 = i15;
                if (MediaDataController.canAddMessageToMedia(tLRPC$Message)) {
                }
                if (nativeByteBuffer != null) {
                }
                nativeByteBuffer6.reuse();
                this.database.commitTransaction();
                if (z) {
                }
                if (arrayList3 != null) {
                }
                sQLiteDatabase3 = this.database;
                if (sQLiteDatabase3 == null) {
                }
            }
            this.database.beginTransaction();
            long j22 = 0;
            if (tLRPC$Message.dialog_id == 0) {
                MessageObject.getDialogId(tLRPC$Message);
            }
            fixUnsupportedMedia(tLRPC$Message);
            MessageObject.normalizeFlags(tLRPC$Message);
            NativeByteBuffer nativeByteBuffer52 = new NativeByteBuffer(tLRPC$Message.getObjectSize());
            tLRPC$Message.serializeToStream(nativeByteBuffer52);
            long clientUserId2 = getUserConfig().getClientUserId();
            i2 = 0;
            arrayList3 = null;
            while (i2 < i14) {
                int i18 = i2 == i16 ? i16 : i15;
                long topicId = MessageObject.getTopicId(this.currentAccount, tLRPC$Message, isForum(tLRPC$Message.dialog_id));
                NativeByteBuffer nativeByteBuffer7 = nativeByteBuffer52;
                long j3 = tLRPC$Message.dialog_id;
                if (i18 == 0) {
                    sQLitePreparedStatement = this.database.executeFast("REPLACE INTO messages_v2 VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?)");
                } else if (topicId == j22) {
                    j = clientUserId2;
                    nativeByteBuffer2 = nativeByteBuffer7;
                    i12 = 0;
                    i2++;
                    i15 = i12;
                    clientUserId2 = j;
                    i14 = 2;
                    j22 = 0;
                    nativeByteBuffer52 = nativeByteBuffer2;
                    i16 = 1;
                } else {
                    sQLitePreparedStatement = this.database.executeFast("REPLACE INTO messages_topics VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?)");
                    if (j3 == clientUserId2) {
                        try {
                            if (MessageObject.getSavedDialogId(clientUserId2, tLRPC$Message) != j22) {
                                if (arrayList3 == null) {
                                    arrayList3 = new ArrayList();
                                }
                                arrayList3.add(tLRPC$Message);
                            }
                        } catch (Exception e4) {
                            e = e4;
                            sQLitePreparedStatement2 = null;
                            try {
                                checkSQLException(e);
                                sQLiteDatabase2 = this.database;
                                if (sQLiteDatabase2 != null) {
                                }
                                if (sQLitePreparedStatement != null) {
                                }
                                if (sQLitePreparedStatement2 == null) {
                                }
                            } catch (Throwable th3) {
                                th = th3;
                                sQLiteDatabase = this.database;
                                if (sQLiteDatabase != null) {
                                    sQLiteDatabase.commitTransaction();
                                }
                                if (sQLitePreparedStatement != null) {
                                    sQLitePreparedStatement.dispose();
                                }
                                if (sQLitePreparedStatement2 != null) {
                                    sQLitePreparedStatement2.dispose();
                                }
                                throw th;
                            }
                        } catch (Throwable th4) {
                            th = th4;
                            sQLitePreparedStatement2 = null;
                            sQLiteDatabase = this.database;
                            if (sQLiteDatabase != null) {
                            }
                            if (sQLitePreparedStatement != null) {
                            }
                            if (sQLitePreparedStatement2 != null) {
                            }
                            throw th;
                        }
                    }
                }
                sQLitePreparedStatement.requery();
                sQLitePreparedStatement.bindInteger(1, tLRPC$Message.id);
                sQLitePreparedStatement.bindLong(2, j3);
                if (i18 != 0) {
                    sQLitePreparedStatement.bindLong(3, topicId);
                    i5 = 4;
                } else {
                    i5 = 3;
                }
                int i19 = i5 + 1;
                sQLitePreparedStatement.bindInteger(i5, i);
                int i20 = i19 + 1;
                sQLitePreparedStatement.bindInteger(i19, tLRPC$Message.send_state);
                int i21 = i20 + 1;
                sQLitePreparedStatement.bindInteger(i20, tLRPC$Message.date);
                int i22 = i21 + 1;
                nativeByteBuffer2 = nativeByteBuffer7;
                sQLitePreparedStatement.bindByteBuffer(i21, nativeByteBuffer2);
                int i23 = i22 + 1;
                if (!MessageObject.isOut(tLRPC$Message) && !tLRPC$Message.from_scheduled) {
                    i6 = 0;
                    sQLitePreparedStatement.bindInteger(i22, i6);
                    int i24 = i23 + 1;
                    sQLitePreparedStatement.bindInteger(i23, tLRPC$Message.ttl);
                    if ((tLRPC$Message.flags & 1024) == 0) {
                        i7 = i24 + 1;
                        sQLitePreparedStatement.bindInteger(i24, tLRPC$Message.views);
                    } else {
                        i7 = i24 + 1;
                        sQLitePreparedStatement.bindInteger(i24, getMessageMediaType(tLRPC$Message));
                    }
                    if (tLRPC$Message.replyStory == null) {
                        nativeByteBuffer3 = new NativeByteBuffer(tLRPC$Message.replyStory.getObjectSize());
                        tLRPC$Message.replyStory.serializeToStream(nativeByteBuffer3);
                        i8 = i7 + 1;
                        sQLitePreparedStatement.bindByteBuffer(i7, nativeByteBuffer3);
                    } else {
                        i8 = i7 + 1;
                        sQLitePreparedStatement.bindNull(i7);
                        nativeByteBuffer3 = null;
                    }
                    int i25 = tLRPC$Message.stickerVerified;
                    int i26 = i8 + 1;
                    sQLitePreparedStatement.bindInteger(i8, i25 != 0 ? 1 : i25 == 2 ? 2 : 0);
                    int i27 = i26 + 1;
                    sQLitePreparedStatement.bindInteger(i26, !tLRPC$Message.mentioned ? 1 : 0);
                    int i28 = i27 + 1;
                    sQLitePreparedStatement.bindInteger(i27, tLRPC$Message.forwards);
                    if (tLRPC$Message.replies == null) {
                        nativeByteBuffer4 = new NativeByteBuffer(tLRPC$Message.replies.getObjectSize());
                        tLRPC$Message.replies.serializeToStream(nativeByteBuffer4);
                        i9 = i28 + 1;
                        sQLitePreparedStatement.bindByteBuffer(i28, nativeByteBuffer4);
                    } else {
                        i9 = i28 + 1;
                        sQLitePreparedStatement.bindNull(i28);
                        nativeByteBuffer4 = null;
                    }
                    tLRPC$MessageReplyHeader = tLRPC$Message.reply_to;
                    if (tLRPC$MessageReplyHeader == null) {
                        i10 = i9 + 1;
                        int i29 = tLRPC$MessageReplyHeader.reply_to_top_id;
                        if (i29 == 0) {
                            i29 = tLRPC$MessageReplyHeader.reply_to_msg_id;
                        }
                        sQLitePreparedStatement.bindInteger(i9, i29);
                    } else {
                        i10 = i9 + 1;
                        sQLitePreparedStatement.bindInteger(i9, 0);
                    }
                    int i30 = i10 + 1;
                    j = clientUserId2;
                    sQLitePreparedStatement.bindLong(i10, MessageObject.getChannelId(tLRPC$Message));
                    if (nativeByteBuffer == null) {
                        i11 = i30 + 1;
                        sQLitePreparedStatement.bindByteBuffer(i30, nativeByteBuffer);
                    } else {
                        i11 = i30 + 1;
                        sQLitePreparedStatement.bindNull(i30);
                    }
                    if (i18 == 0) {
                        if ((tLRPC$Message.flags & 131072) != 0) {
                            i13 = i11 + 1;
                            sQLitePreparedStatement.bindLong(i11, tLRPC$Message.grouped_id);
                        } else {
                            i13 = i11 + 1;
                            sQLitePreparedStatement.bindNull(i11);
                        }
                        i11 = i13;
                    }
                    tLRPC$MessageReplyHeader2 = tLRPC$Message.reply_to;
                    if (tLRPC$MessageReplyHeader2 == null) {
                        sQLitePreparedStatement.bindInteger(i11, tLRPC$MessageReplyHeader2.story_id);
                        i12 = 0;
                    } else {
                        i12 = 0;
                        sQLitePreparedStatement.bindInteger(i11, 0);
                    }
                    sQLitePreparedStatement.step();
                    sQLitePreparedStatement.dispose();
                    if (nativeByteBuffer4 != null) {
                        nativeByteBuffer4.reuse();
                    }
                    if (nativeByteBuffer3 == null) {
                        nativeByteBuffer3.reuse();
                    }
                    i2++;
                    i15 = i12;
                    clientUserId2 = j;
                    i14 = 2;
                    j22 = 0;
                    nativeByteBuffer52 = nativeByteBuffer2;
                    i16 = 1;
                }
                i6 = 1;
                sQLitePreparedStatement.bindInteger(i22, i6);
                int i242 = i23 + 1;
                sQLitePreparedStatement.bindInteger(i23, tLRPC$Message.ttl);
                if ((tLRPC$Message.flags & 1024) == 0) {
                }
                if (tLRPC$Message.replyStory == null) {
                }
                int i252 = tLRPC$Message.stickerVerified;
                int i262 = i8 + 1;
                sQLitePreparedStatement.bindInteger(i8, i252 != 0 ? 1 : i252 == 2 ? 2 : 0);
                int i272 = i262 + 1;
                sQLitePreparedStatement.bindInteger(i262, !tLRPC$Message.mentioned ? 1 : 0);
                int i282 = i272 + 1;
                sQLitePreparedStatement.bindInteger(i272, tLRPC$Message.forwards);
                if (tLRPC$Message.replies == null) {
                }
                tLRPC$MessageReplyHeader = tLRPC$Message.reply_to;
                if (tLRPC$MessageReplyHeader == null) {
                }
                int i302 = i10 + 1;
                j = clientUserId2;
                sQLitePreparedStatement.bindLong(i10, MessageObject.getChannelId(tLRPC$Message));
                if (nativeByteBuffer == null) {
                }
                if (i18 == 0) {
                }
                tLRPC$MessageReplyHeader2 = tLRPC$Message.reply_to;
                if (tLRPC$MessageReplyHeader2 == null) {
                }
                sQLitePreparedStatement.step();
                sQLitePreparedStatement.dispose();
                if (nativeByteBuffer4 != null) {
                }
                if (nativeByteBuffer3 == null) {
                }
                i2++;
                i15 = i12;
                clientUserId2 = j;
                i14 = 2;
                j22 = 0;
                nativeByteBuffer52 = nativeByteBuffer2;
                i16 = 1;
            }
            NativeByteBuffer nativeByteBuffer62 = nativeByteBuffer52;
            int i172 = i15;
            if (MediaDataController.canAddMessageToMedia(tLRPC$Message)) {
                int i31 = i172;
                int i32 = 2;
                while (i31 < i32) {
                    int i33 = i31 == 1 ? 1 : i172;
                    long topicId2 = MessageObject.getTopicId(this.currentAccount, tLRPC$Message, isForum(tLRPC$Message.dialog_id));
                    if (i33 == 0 || topicId2 != 0) {
                        SQLitePreparedStatement executeFast = i31 == 0 ? this.database.executeFast("REPLACE INTO media_v4 VALUES(?, ?, ?, ?, ?)") : this.database.executeFast("REPLACE INTO media_topics VALUES(?, ?, ?, ?, ?, ?)");
                        try {
                            executeFast.requery();
                            executeFast.bindInteger(1, tLRPC$Message.id);
                            i3 = 2;
                            executeFast.bindLong(2, tLRPC$Message.dialog_id);
                            if (i31 != 0) {
                                executeFast.bindLong(3, topicId2);
                                i4 = 4;
                            } else {
                                i4 = 3;
                            }
                            int i34 = i4 + 1;
                            executeFast.bindInteger(i4, tLRPC$Message.date);
                            executeFast.bindInteger(i34, MediaDataController.getMediaType(tLRPC$Message));
                            executeFast.bindByteBuffer(i34 + 1, nativeByteBuffer62);
                            executeFast.step();
                            executeFast.dispose();
                        } catch (Exception e5) {
                            e = e5;
                            sQLitePreparedStatement2 = executeFast;
                            sQLitePreparedStatement = null;
                            checkSQLException(e);
                            sQLiteDatabase2 = this.database;
                            if (sQLiteDatabase2 != null) {
                                sQLiteDatabase2.commitTransaction();
                            }
                            if (sQLitePreparedStatement != null) {
                                sQLitePreparedStatement.dispose();
                            }
                            if (sQLitePreparedStatement2 == null) {
                                sQLitePreparedStatement2.dispose();
                                return;
                            }
                            return;
                        } catch (Throwable th5) {
                            th = th5;
                            sQLitePreparedStatement2 = executeFast;
                            sQLitePreparedStatement = null;
                            sQLiteDatabase = this.database;
                            if (sQLiteDatabase != null) {
                            }
                            if (sQLitePreparedStatement != null) {
                            }
                            if (sQLitePreparedStatement2 != null) {
                            }
                            throw th;
                        }
                    } else {
                        i3 = 2;
                    }
                    i31++;
                    i32 = i3;
                }
            }
            if (nativeByteBuffer != null) {
                nativeByteBuffer.reuse();
            }
            nativeByteBuffer62.reuse();
            this.database.commitTransaction();
            if (z) {
                HashMap hashMap = new HashMap();
                HashMap hashMap2 = new HashMap();
                for (int i35 = i172; i35 < arrayList.size(); i35++) {
                    TLRPC$User tLRPC$User = (TLRPC$User) arrayList.get(i35);
                    hashMap.put(Long.valueOf(tLRPC$User.id), tLRPC$User);
                }
                for (int i36 = i172; i36 < arrayList2.size(); i36++) {
                    TLRPC$Chat tLRPC$Chat = (TLRPC$Chat) arrayList2.get(i36);
                    hashMap2.put(Long.valueOf(tLRPC$Chat.id), tLRPC$Chat);
                }
                final MessageObject messageObject = new MessageObject(this.currentAccount, tLRPC$Message, (AbstractMap<Long, TLRPC$User>) hashMap, (AbstractMap<Long, TLRPC$Chat>) hashMap2, true, true);
                final ArrayList arrayList4 = new ArrayList();
                arrayList4.add(messageObject);
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$replaceMessageIfExists$202(messageObject, arrayList4);
                    }
                });
            }
            if (arrayList3 != null) {
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$replaceMessageIfExists$203(arrayList3);
                    }
                });
            }
            sQLiteDatabase3 = this.database;
            if (sQLiteDatabase3 == null) {
                sQLiteDatabase3.commitTransaction();
            }
        } catch (Exception e6) {
            e = e6;
            sQLitePreparedStatement = null;
        } catch (Throwable th6) {
            th = th6;
            sQLitePreparedStatement = null;
        }
    }

    public void lambda$replaceMessageIfExists$202(MessageObject messageObject, ArrayList arrayList) {
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.replaceMessagesObjects, Long.valueOf(messageObject.getDialogId()), arrayList);
    }

    public void lambda$replaceMessageIfExists$203(ArrayList arrayList) {
        if (getMessagesController().getSavedMessagesController().updateSavedDialogs(arrayList)) {
            getMessagesController().getSavedMessagesController().update();
        }
    }

    public void putMessages(final TLRPC$messages_Messages tLRPC$messages_Messages, final long j, final int i, final int i2, final boolean z, final int i3, final long j2) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$putMessages$207(i3, j, tLRPC$messages_Messages, j2, i, i2, z);
            }
        });
    }

    public void lambda$putMessages$207(int r47, long r48, org.telegram.tgnet.TLRPC$messages_Messages r50, long r51, int r53, int r54, boolean r55) {
        throw new UnsupportedOperationException("Method not decompiled: org.telegram.messenger.MessagesStorage.lambda$putMessages$207(int, long, org.telegram.tgnet.TLRPC$messages_Messages, long, int, int, boolean):void");
    }

    public void lambda$putMessages$205(ArrayList arrayList) {
        getFileLoader().cancelLoadFiles(arrayList);
    }

    public void lambda$putMessages$206(ArrayList arrayList) {
        if (getMessagesController().getSavedMessagesController().updateSavedDialogs(arrayList)) {
            getMessagesController().getSavedMessagesController().update();
        }
    }

    public static void addUsersAndChatsFromMessage(TLRPC$Message tLRPC$Message, ArrayList<Long> arrayList, ArrayList<Long> arrayList2, ArrayList<Long> arrayList3) {
        String str;
        TLRPC$MessageFwdHeader tLRPC$MessageFwdHeader;
        TLRPC$Peer tLRPC$Peer;
        TLRPC$Peer tLRPC$Peer2;
        TLRPC$WebPage tLRPC$WebPage;
        TL_stories$StoryFwdHeader tL_stories$StoryFwdHeader;
        TL_stories$StoryItem tL_stories$StoryItem;
        long fromChatId = MessageObject.getFromChatId(tLRPC$Message);
        if (DialogObject.isUserDialog(fromChatId)) {
            if (!arrayList.contains(Long.valueOf(fromChatId))) {
                arrayList.add(Long.valueOf(fromChatId));
            }
        } else if (DialogObject.isChatDialog(fromChatId)) {
            long j = -fromChatId;
            if (!arrayList2.contains(Long.valueOf(j))) {
                arrayList2.add(Long.valueOf(j));
            }
        }
        long j2 = tLRPC$Message.via_bot_id;
        if (j2 != 0 && !arrayList.contains(Long.valueOf(j2))) {
            arrayList.add(Long.valueOf(tLRPC$Message.via_bot_id));
        }
        TLRPC$MessageAction tLRPC$MessageAction = tLRPC$Message.action;
        if (tLRPC$MessageAction != null) {
            long j3 = tLRPC$MessageAction.user_id;
            if (j3 != 0 && !arrayList.contains(Long.valueOf(j3))) {
                arrayList.add(Long.valueOf(tLRPC$Message.action.user_id));
            }
            long j4 = tLRPC$Message.action.channel_id;
            if (j4 != 0 && !arrayList2.contains(Long.valueOf(j4))) {
                arrayList2.add(Long.valueOf(tLRPC$Message.action.channel_id));
            }
            long j5 = tLRPC$Message.action.chat_id;
            if (j5 != 0 && !arrayList2.contains(Long.valueOf(j5))) {
                arrayList2.add(Long.valueOf(tLRPC$Message.action.chat_id));
            }
            TLRPC$MessageAction tLRPC$MessageAction2 = tLRPC$Message.action;
            if (tLRPC$MessageAction2 instanceof TLRPC$TL_messageActionGiftCode) {
                addLoadPeerInfo(((TLRPC$TL_messageActionGiftCode) tLRPC$MessageAction2).boost_peer, arrayList, arrayList2);
            }
            TLRPC$MessageAction tLRPC$MessageAction3 = tLRPC$Message.action;
            if (tLRPC$MessageAction3 instanceof TLRPC$TL_messageActionGeoProximityReached) {
                TLRPC$TL_messageActionGeoProximityReached tLRPC$TL_messageActionGeoProximityReached = (TLRPC$TL_messageActionGeoProximityReached) tLRPC$MessageAction3;
                addLoadPeerInfo(tLRPC$TL_messageActionGeoProximityReached.from_id, arrayList, arrayList2);
                addLoadPeerInfo(tLRPC$TL_messageActionGeoProximityReached.to_id, arrayList, arrayList2);
            }
            if (!tLRPC$Message.action.users.isEmpty()) {
                for (int i = 0; i < tLRPC$Message.action.users.size(); i++) {
                    Long l = tLRPC$Message.action.users.get(i);
                    if (!arrayList.contains(l)) {
                        arrayList.add(l);
                    }
                }
            }
        }
        if (!tLRPC$Message.entities.isEmpty()) {
            for (int i2 = 0; i2 < tLRPC$Message.entities.size(); i2++) {
                TLRPC$MessageEntity tLRPC$MessageEntity = tLRPC$Message.entities.get(i2);
                if (tLRPC$MessageEntity instanceof TLRPC$TL_messageEntityMentionName) {
                    arrayList.add(Long.valueOf(((TLRPC$TL_messageEntityMentionName) tLRPC$MessageEntity).user_id));
                } else if (tLRPC$MessageEntity instanceof TLRPC$TL_inputMessageEntityMentionName) {
                    arrayList.add(Long.valueOf(((TLRPC$TL_inputMessageEntityMentionName) tLRPC$MessageEntity).user_id.user_id));
                } else if (arrayList3 != null && (tLRPC$MessageEntity instanceof TLRPC$TL_messageEntityCustomEmoji)) {
                    arrayList3.add(Long.valueOf(((TLRPC$TL_messageEntityCustomEmoji) tLRPC$MessageEntity).document_id));
                }
            }
        }
        TLRPC$MessageMedia tLRPC$MessageMedia = tLRPC$Message.media;
        if (tLRPC$MessageMedia != null) {
            long j6 = tLRPC$MessageMedia.user_id;
            if (j6 != 0 && !arrayList.contains(Long.valueOf(j6))) {
                arrayList.add(Long.valueOf(tLRPC$Message.media.user_id));
            }
            TLRPC$MessageMedia tLRPC$MessageMedia2 = tLRPC$Message.media;
            if (tLRPC$MessageMedia2 instanceof TLRPC$TL_messageMediaGiveaway) {
                Iterator<Long> it = ((TLRPC$TL_messageMediaGiveaway) tLRPC$MessageMedia2).channels.iterator();
                while (it.hasNext()) {
                    Long next = it.next();
                    if (!arrayList2.contains(next)) {
                        arrayList2.add(next);
                    }
                }
            }
            TLRPC$MessageMedia tLRPC$MessageMedia3 = tLRPC$Message.media;
            if (tLRPC$MessageMedia3 instanceof TLRPC$TL_messageMediaGiveawayResults) {
                Iterator<Long> it2 = ((TLRPC$TL_messageMediaGiveawayResults) tLRPC$MessageMedia3).winners.iterator();
                while (it2.hasNext()) {
                    Long next2 = it2.next();
                    if (!arrayList.contains(next2)) {
                        arrayList.add(next2);
                    }
                }
            }
            TLRPC$MessageMedia tLRPC$MessageMedia4 = tLRPC$Message.media;
            if (tLRPC$MessageMedia4 instanceof TLRPC$TL_messageMediaPoll) {
                TLRPC$TL_messageMediaPoll tLRPC$TL_messageMediaPoll = (TLRPC$TL_messageMediaPoll) tLRPC$MessageMedia4;
                if (!tLRPC$TL_messageMediaPoll.results.recent_voters.isEmpty()) {
                    for (int i3 = 0; i3 < tLRPC$TL_messageMediaPoll.results.recent_voters.size(); i3++) {
                        addLoadPeerInfo(tLRPC$TL_messageMediaPoll.results.recent_voters.get(i3), arrayList, arrayList2);
                    }
                }
            }
            TLRPC$MessageMedia tLRPC$MessageMedia5 = tLRPC$Message.media;
            if ((tLRPC$MessageMedia5 instanceof TLRPC$TL_messageMediaStory) && (tL_stories$StoryItem = tLRPC$MessageMedia5.storyItem) != null) {
                TL_stories$StoryFwdHeader tL_stories$StoryFwdHeader2 = tL_stories$StoryItem.fwd_from;
                if (tL_stories$StoryFwdHeader2 != null) {
                    addLoadPeerInfo(tL_stories$StoryFwdHeader2.from, arrayList, arrayList2);
                }
                TL_stories$StoryItem tL_stories$StoryItem2 = tLRPC$Message.media.storyItem;
                if (tL_stories$StoryItem2 != null && tL_stories$StoryItem2.media_areas != null) {
                    for (int i4 = 0; i4 < tLRPC$Message.media.storyItem.media_areas.size(); i4++) {
                        if (tLRPC$Message.media.storyItem.media_areas.get(i4) instanceof TL_stories$TL_mediaAreaChannelPost) {
                            long j7 = ((TL_stories$TL_mediaAreaChannelPost) tLRPC$Message.media.storyItem.media_areas.get(i4)).channel_id;
                            if (!arrayList2.contains(Long.valueOf(j7))) {
                                arrayList2.add(Long.valueOf(j7));
                            }
                        }
                    }
                }
            }
            TLRPC$MessageMedia tLRPC$MessageMedia6 = tLRPC$Message.media;
            if ((tLRPC$MessageMedia6 instanceof TLRPC$TL_messageMediaWebPage) && (tLRPC$WebPage = tLRPC$MessageMedia6.webpage) != null && tLRPC$WebPage.attributes != null) {
                for (int i5 = 0; i5 < tLRPC$Message.media.webpage.attributes.size(); i5++) {
                    if (tLRPC$Message.media.webpage.attributes.get(i5) instanceof TLRPC$TL_webPageAttributeStory) {
                        TLRPC$TL_webPageAttributeStory tLRPC$TL_webPageAttributeStory = (TLRPC$TL_webPageAttributeStory) tLRPC$Message.media.webpage.attributes.get(i5);
                        TL_stories$StoryItem tL_stories$StoryItem3 = tLRPC$TL_webPageAttributeStory.storyItem;
                        if (tL_stories$StoryItem3 != null && (tL_stories$StoryFwdHeader = tL_stories$StoryItem3.fwd_from) != null) {
                            addLoadPeerInfo(tL_stories$StoryFwdHeader.from, arrayList, arrayList2);
                        }
                        TL_stories$StoryItem tL_stories$StoryItem4 = tLRPC$TL_webPageAttributeStory.storyItem;
                        if (tL_stories$StoryItem4 != null && tL_stories$StoryItem4.media_areas != null) {
                            for (int i6 = 0; i6 < tLRPC$TL_webPageAttributeStory.storyItem.media_areas.size(); i6++) {
                                if (tLRPC$TL_webPageAttributeStory.storyItem.media_areas.get(i6) instanceof TL_stories$TL_mediaAreaChannelPost) {
                                    long j8 = ((TL_stories$TL_mediaAreaChannelPost) tLRPC$TL_webPageAttributeStory.storyItem.media_areas.get(i6)).channel_id;
                                    if (!arrayList2.contains(Long.valueOf(j8))) {
                                        arrayList2.add(Long.valueOf(j8));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            TLRPC$Peer tLRPC$Peer3 = tLRPC$Message.media.peer;
            if (tLRPC$Peer3 != null) {
                addLoadPeerInfo(tLRPC$Peer3, arrayList, arrayList2);
            }
        }
        TLRPC$MessageReplies tLRPC$MessageReplies = tLRPC$Message.replies;
        if (tLRPC$MessageReplies != null) {
            int size = tLRPC$MessageReplies.recent_repliers.size();
            for (int i7 = 0; i7 < size; i7++) {
                addLoadPeerInfo(tLRPC$Message.replies.recent_repliers.get(i7), arrayList, arrayList2);
            }
        }
        TLRPC$MessageReplyHeader tLRPC$MessageReplyHeader = tLRPC$Message.reply_to;
        if (tLRPC$MessageReplyHeader != null && (tLRPC$Peer2 = tLRPC$MessageReplyHeader.reply_to_peer_id) != null) {
            addLoadPeerInfo(tLRPC$Peer2, arrayList, arrayList2);
        }
        TLRPC$MessageFwdHeader tLRPC$MessageFwdHeader2 = tLRPC$Message.fwd_from;
        if (tLRPC$MessageFwdHeader2 != null) {
            addLoadPeerInfo(tLRPC$MessageFwdHeader2.from_id, arrayList, arrayList2);
            addLoadPeerInfo(tLRPC$Message.fwd_from.saved_from_peer, arrayList, arrayList2);
        }
        TLRPC$MessageReplyHeader tLRPC$MessageReplyHeader2 = tLRPC$Message.reply_to;
        if (tLRPC$MessageReplyHeader2 != null && (tLRPC$MessageFwdHeader = tLRPC$MessageReplyHeader2.reply_from) != null && (tLRPC$Peer = tLRPC$MessageFwdHeader.from_id) != null) {
            addLoadPeerInfo(tLRPC$Peer, arrayList, arrayList2);
        }
        HashMap<String, String> hashMap = tLRPC$Message.params;
        if (hashMap == null || (str = hashMap.get("fwd_peer")) == null) {
            return;
        }
        long longValue = Utilities.parseLong(str).longValue();
        if (longValue < 0) {
            long j9 = -longValue;
            if (arrayList2.contains(Long.valueOf(j9))) {
                return;
            }
            arrayList2.add(Long.valueOf(j9));
        }
    }

    public static void addLoadPeerInfo(TLRPC$Peer tLRPC$Peer, ArrayList<Long> arrayList, ArrayList<Long> arrayList2) {
        if (tLRPC$Peer instanceof TLRPC$TL_peerUser) {
            if (arrayList.contains(Long.valueOf(tLRPC$Peer.user_id))) {
                return;
            }
            arrayList.add(Long.valueOf(tLRPC$Peer.user_id));
        } else if (tLRPC$Peer instanceof TLRPC$TL_peerChannel) {
            if (arrayList2.contains(Long.valueOf(tLRPC$Peer.channel_id))) {
                return;
            }
            arrayList2.add(Long.valueOf(tLRPC$Peer.channel_id));
        } else {
            if (!(tLRPC$Peer instanceof TLRPC$TL_peerChat) || arrayList2.contains(Long.valueOf(tLRPC$Peer.chat_id))) {
                return;
            }
            arrayList2.add(Long.valueOf(tLRPC$Peer.chat_id));
        }
    }

    public void getDialogs(final int i, final int i2, final int i3, boolean z) {
        LongSparseArray<LongSparseArray<TLRPC$DraftMessage>> drafts;
        int size;
        long[] jArr = null;
        if (z && (size = (drafts = getMediaDataController().getDrafts()).size()) > 0) {
            jArr = new long[size];
            for (int i4 = 0; i4 < size; i4++) {
                if (drafts.valueAt(i4).get(0L) != null) {
                    jArr[i4] = drafts.keyAt(i4);
                }
            }
        }
        final long[] jArr2 = jArr;
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getDialogs$209(i, i2, i3, jArr2);
            }
        });
    }

    public void lambda$getDialogs$209(int i, int i2, int i3, long[] jArr) {
        Throwable th;
        SQLiteCursor sQLiteCursor;
        MessagesStorage messagesStorage;
        ArrayList<TLRPC$EncryptedChat> arrayList;
        TLRPC$TL_messages_dialogs tLRPC$TL_messages_dialogs;
        Exception exc;
        SQLiteCursor sQLiteCursor2;
        ArrayList<Long> arrayList2;
        HashSet hashSet;
        ArrayList<Long> arrayList3;
        ArrayList arrayList4;
        ArrayList arrayList5;
        ArrayList arrayList6;
        LongSparseArray<SparseArray<ArrayList<TLRPC$Message>>> longSparseArray;
        LongSparseArray<ArrayList<Integer>> longSparseArray2;
        ArrayList arrayList7;
        int i4;
        SQLiteCursor sQLiteCursor3;
        ArrayList<TLRPC$EncryptedChat> arrayList8;
        final MessagesStorage messagesStorage2;
        HashSet hashSet2;
        int i5;
        final LongSparseArray longSparseArray3;
        ArrayList<TLRPC$UserFull> arrayList9;
        int i6;
        int i7;
        ArrayList arrayList10;
        int i8;
        ArrayList<Long> arrayList11;
        ArrayList arrayList12;
        HashSet hashSet3;
        ArrayList arrayList13;
        LongSparseArray<SparseArray<ArrayList<TLRPC$Message>>> longSparseArray4;
        LongSparseArray<ArrayList<Integer>> longSparseArray5;
        MessagesStorage messagesStorage3;
        ArrayList arrayList14;
        ArrayList arrayList15;
        ArrayList arrayList16;
        HashSet hashSet4;
        MessagesStorage messagesStorage4;
        ArrayList arrayList17;
        ArrayList arrayList18;
        TLRPC$Dialog tLRPC$Dialog;
        HashSet hashSet5;
        NativeByteBuffer byteBufferValue;
        ?? tLRPC$TL_dialog;
        ArrayList arrayList19;
        int i9;
        int i10;
        long longValue;
        long longValue2;
        NativeByteBuffer byteBufferValue2;
        int i11;
        ArrayList<Long> arrayList20;
        LongSparseArray<SparseArray<ArrayList<TLRPC$Message>>> longSparseArray6;
        LongSparseArray<ArrayList<Integer>> longSparseArray7;
        ArrayList arrayList21;
        ArrayList arrayList22;
        HashSet hashSet6;
        Exception exc2;
        MessagesStorage messagesStorage5 = this;
        long[] jArr2 = jArr;
        TLRPC$TL_messages_dialogs tLRPC$TL_messages_dialogs2 = new TLRPC$TL_messages_dialogs();
        ArrayList<TLRPC$EncryptedChat> arrayList23 = new ArrayList<>();
        try {
            arrayList2 = new ArrayList<>();
            hashSet = new HashSet();
            arrayList2.add(Long.valueOf(getUserConfig().getClientUserId()));
            arrayList3 = new ArrayList<>();
            arrayList4 = new ArrayList();
            arrayList5 = new ArrayList();
            arrayList6 = new ArrayList();
            longSparseArray = new LongSparseArray<>();
            longSparseArray2 = new LongSparseArray<>();
            arrayList7 = new ArrayList(2);
            arrayList7.add(Integer.valueOf(i));
            i4 = 0;
            sQLiteCursor3 = null;
        } catch (Exception e) {
            messagesStorage = messagesStorage5;
            arrayList = arrayList23;
            tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
            exc = e;
            sQLiteCursor2 = null;
        } catch (Throwable th2) {
            th = th2;
            sQLiteCursor = null;
        }
        try {
            while (true) {
                try {
                    arrayList8 = arrayList23;
                    if (i4 >= arrayList7.size()) {
                        break;
                    }
                    try {
                        int intValue = ((Integer) arrayList7.get(i4)).intValue();
                        if (i4 == 0) {
                            i7 = i2;
                            i6 = i3;
                        } else {
                            i6 = 100;
                            i7 = 0;
                        }
                        ArrayList arrayList24 = new ArrayList();
                        HashSet hashSet7 = hashSet;
                        ArrayList arrayList25 = arrayList4;
                        try {
                            LongSparseArray<SparseArray<ArrayList<TLRPC$Message>>> longSparseArray8 = longSparseArray;
                            LongSparseArray<ArrayList<Integer>> longSparseArray9 = longSparseArray2;
                            SQLiteCursor queryFinalized = messagesStorage5.database.queryFinalized(String.format(Locale.US, "SELECT d.did, d.last_mid, d.unread_count, d.date, m.data, m.read_state, m.mid, m.send_state, s.flags, m.date, d.pts, d.inbox_max, d.outbox_max, m.replydata, d.pinned, d.unread_count_i, d.flags, d.folder_id, d.data, d.unread_reactions, d.last_mid_group, d.ttl_period FROM dialogs as d LEFT JOIN messages_v2 as m ON d.last_mid = m.mid AND d.did = m.uid AND d.last_mid_group IS NULL LEFT JOIN dialog_settings as s ON d.did = s.did WHERE d.folder_id = %d ORDER BY d.pinned DESC, d.date DESC LIMIT %d,%d", Integer.valueOf(intValue), Integer.valueOf(i7), Integer.valueOf(i6)), new Object[0]);
                            while (queryFinalized.next()) {
                                try {
                                    try {
                                        ArrayList<Long> arrayList26 = arrayList3;
                                        long longValue3 = queryFinalized.longValue(0);
                                        if (DialogObject.isFolderDialogId(longValue3)) {
                                            tLRPC$TL_dialog = new TLRPC$TL_dialogFolder();
                                            if (!queryFinalized.isNull(18)) {
                                                NativeByteBuffer byteBufferValue3 = queryFinalized.byteBufferValue(18);
                                                if (byteBufferValue3 != null) {
                                                    tLRPC$TL_dialog.folder = TLRPC$TL_folder.TLdeserialize(byteBufferValue3, byteBufferValue3.readInt32(false), false);
                                                    byteBufferValue3.reuse();
                                                } else {
                                                    TLRPC$TL_folder tLRPC$TL_folder = new TLRPC$TL_folder();
                                                    tLRPC$TL_dialog.folder = tLRPC$TL_folder;
                                                    tLRPC$TL_folder.id = DialogObject.getFolderId(longValue3);
                                                }
                                            }
                                            if (i4 == 0) {
                                                arrayList7.add(Integer.valueOf(tLRPC$TL_dialog.folder.id));
                                            }
                                        } else {
                                            tLRPC$TL_dialog = new TLRPC$TL_dialog();
                                        }
                                        tLRPC$TL_dialog.id = longValue3;
                                        tLRPC$TL_dialog.top_message = queryFinalized.intValue(1);
                                        tLRPC$TL_dialog.unread_count = queryFinalized.intValue(2);
                                        tLRPC$TL_dialog.last_message_date = queryFinalized.intValue(3);
                                        int intValue2 = queryFinalized.intValue(10);
                                        tLRPC$TL_dialog.pts = intValue2;
                                        if (intValue2 != 0) {
                                            arrayList19 = arrayList7;
                                            i9 = i4;
                                            if (!DialogObject.isUserDialog(tLRPC$TL_dialog.id)) {
                                                i10 = 1;
                                                tLRPC$TL_dialog.flags = i10;
                                                tLRPC$TL_dialog.read_inbox_max_id = queryFinalized.intValue(11);
                                                tLRPC$TL_dialog.read_outbox_max_id = queryFinalized.intValue(12);
                                                int intValue3 = queryFinalized.intValue(14);
                                                tLRPC$TL_dialog.pinnedNum = intValue3;
                                                tLRPC$TL_dialog.pinned = intValue3 == 0;
                                                tLRPC$TL_dialog.unread_mentions_count = queryFinalized.intValue(15);
                                                int intValue4 = queryFinalized.intValue(16);
                                                tLRPC$TL_dialog.unread_mark = (intValue4 & 1) == 0;
                                                tLRPC$TL_dialog.view_forum_as_messages = (intValue4 & 64) == 0;
                                                longValue = queryFinalized.longValue(8);
                                                ArrayList arrayList27 = arrayList19;
                                                TLRPC$TL_peerNotifySettings tLRPC$TL_peerNotifySettings = new TLRPC$TL_peerNotifySettings();
                                                tLRPC$TL_dialog.notify_settings = tLRPC$TL_peerNotifySettings;
                                                if ((((int) longValue) & 1) != 0) {
                                                    int i12 = (int) (longValue >> 32);
                                                    tLRPC$TL_peerNotifySettings.mute_until = i12;
                                                    if (i12 == 0) {
                                                        tLRPC$TL_peerNotifySettings.mute_until = ConnectionsManager.DEFAULT_DATACENTER_ID;
                                                    }
                                                }
                                                tLRPC$TL_dialog.folder_id = queryFinalized.intValue(17);
                                                tLRPC$TL_dialog.unread_reactions_count = queryFinalized.intValue(19);
                                                longValue2 = queryFinalized.longValue(20);
                                                if (longValue2 != 0) {
                                                    arrayList24.add(new Pair(Long.valueOf(longValue3), Long.valueOf(longValue2)));
                                                }
                                                tLRPC$TL_dialog.ttl_period = queryFinalized.intValue(21);
                                                tLRPC$TL_messages_dialogs2.dialogs.add(tLRPC$TL_dialog);
                                                if (jArr2 != null) {
                                                    arrayList5.add(Long.valueOf(longValue3));
                                                }
                                                byteBufferValue2 = queryFinalized.byteBufferValue(4);
                                                if (byteBufferValue2 == null) {
                                                    TLRPC$Message TLdeserialize = TLRPC$Message.TLdeserialize(byteBufferValue2, byteBufferValue2.readInt32(false), false);
                                                    if (TLdeserialize != null) {
                                                        int i13 = i9;
                                                        TLdeserialize.readAttachPath(byteBufferValue2, getUserConfig().clientUserId);
                                                        byteBufferValue2.reuse();
                                                        MessageObject.setUnreadFlags(TLdeserialize, queryFinalized.intValue(5));
                                                        TLdeserialize.id = queryFinalized.intValue(6);
                                                        int intValue5 = queryFinalized.intValue(9);
                                                        if (intValue5 != 0) {
                                                            tLRPC$TL_dialog.last_message_date = intValue5;
                                                        }
                                                        TLdeserialize.send_state = queryFinalized.intValue(7);
                                                        TLdeserialize.dialog_id = tLRPC$TL_dialog.id;
                                                        tLRPC$TL_messages_dialogs2.messages.add(TLdeserialize);
                                                        arrayList20 = arrayList26;
                                                        addUsersAndChatsFromMessage(TLdeserialize, arrayList2, arrayList20, arrayList6);
                                                        try {
                                                            TLRPC$MessageReplyHeader tLRPC$MessageReplyHeader = TLdeserialize.reply_to;
                                                            if (tLRPC$MessageReplyHeader != null && tLRPC$MessageReplyHeader.reply_to_msg_id != 0) {
                                                                TLRPC$MessageAction tLRPC$MessageAction = TLdeserialize.action;
                                                                if (!(tLRPC$MessageAction instanceof TLRPC$TL_messageActionPinMessage)) {
                                                                    try {
                                                                        if (!(tLRPC$MessageAction instanceof TLRPC$TL_messageActionPaymentSent)) {
                                                                        }
                                                                    } catch (Exception e2) {
                                                                        exc2 = e2;
                                                                        i11 = i13;
                                                                        arrayList21 = arrayList24;
                                                                        longSparseArray6 = longSparseArray8;
                                                                        longSparseArray7 = longSparseArray9;
                                                                        messagesStorage3 = this;
                                                                        messagesStorage3.checkSQLException(exc2);
                                                                        if (!DialogObject.isEncryptedDialog(longValue3)) {
                                                                        }
                                                                        if (DialogObject.isEncryptedDialog(longValue3)) {
                                                                        }
                                                                        longSparseArray8 = longSparseArray6;
                                                                        arrayList25 = arrayList22;
                                                                        arrayList3 = arrayList20;
                                                                        longSparseArray9 = longSparseArray7;
                                                                        hashSet7 = hashSet6;
                                                                        arrayList24 = arrayList21;
                                                                        arrayList7 = arrayList27;
                                                                        i4 = i11;
                                                                        jArr2 = jArr;
                                                                    }
                                                                }
                                                                try {
                                                                    if (!queryFinalized.isNull(13)) {
                                                                        try {
                                                                            NativeByteBuffer byteBufferValue4 = queryFinalized.byteBufferValue(13);
                                                                            if (byteBufferValue4 != null) {
                                                                                TLRPC$Message TLdeserialize2 = TLRPC$Message.TLdeserialize(byteBufferValue4, byteBufferValue4.readInt32(false), false);
                                                                                TLdeserialize.replyMessage = TLdeserialize2;
                                                                                i11 = i13;
                                                                                arrayList21 = arrayList24;
                                                                                try {
                                                                                    TLdeserialize2.readAttachPath(byteBufferValue4, getUserConfig().clientUserId);
                                                                                    byteBufferValue4.reuse();
                                                                                    TLRPC$Message tLRPC$Message = TLdeserialize.replyMessage;
                                                                                    if (tLRPC$Message != null) {
                                                                                        addUsersAndChatsFromMessage(tLRPC$Message, arrayList2, arrayList20, arrayList6);
                                                                                    }
                                                                                    if (TLdeserialize.replyMessage == null) {
                                                                                        longSparseArray6 = longSparseArray8;
                                                                                        longSparseArray7 = longSparseArray9;
                                                                                        try {
                                                                                            addReplyMessages(TLdeserialize, longSparseArray6, longSparseArray7);
                                                                                        } catch (Exception e3) {
                                                                                            e = e3;
                                                                                            messagesStorage3 = this;
                                                                                            exc2 = e;
                                                                                            messagesStorage3.checkSQLException(exc2);
                                                                                            if (!DialogObject.isEncryptedDialog(longValue3)) {
                                                                                            }
                                                                                            if (DialogObject.isEncryptedDialog(longValue3)) {
                                                                                            }
                                                                                            longSparseArray8 = longSparseArray6;
                                                                                            arrayList25 = arrayList22;
                                                                                            arrayList3 = arrayList20;
                                                                                            longSparseArray9 = longSparseArray7;
                                                                                            hashSet7 = hashSet6;
                                                                                            arrayList24 = arrayList21;
                                                                                            arrayList7 = arrayList27;
                                                                                            i4 = i11;
                                                                                            jArr2 = jArr;
                                                                                        }
                                                                                    }
                                                                                    longSparseArray6 = longSparseArray8;
                                                                                    longSparseArray7 = longSparseArray9;
                                                                                } catch (Exception e4) {
                                                                                    e = e4;
                                                                                    messagesStorage3 = this;
                                                                                    exc2 = e;
                                                                                    longSparseArray6 = longSparseArray8;
                                                                                    longSparseArray7 = longSparseArray9;
                                                                                    messagesStorage3.checkSQLException(exc2);
                                                                                    if (!DialogObject.isEncryptedDialog(longValue3)) {
                                                                                    }
                                                                                    if (DialogObject.isEncryptedDialog(longValue3)) {
                                                                                    }
                                                                                    longSparseArray8 = longSparseArray6;
                                                                                    arrayList25 = arrayList22;
                                                                                    arrayList3 = arrayList20;
                                                                                    longSparseArray9 = longSparseArray7;
                                                                                    hashSet7 = hashSet6;
                                                                                    arrayList24 = arrayList21;
                                                                                    arrayList7 = arrayList27;
                                                                                    i4 = i11;
                                                                                    jArr2 = jArr;
                                                                                }
                                                                            }
                                                                        } catch (Exception e5) {
                                                                            e = e5;
                                                                            i11 = i13;
                                                                            arrayList21 = arrayList24;
                                                                        }
                                                                    }
                                                                    if (TLdeserialize.replyMessage == null) {
                                                                    }
                                                                    longSparseArray6 = longSparseArray8;
                                                                    longSparseArray7 = longSparseArray9;
                                                                } catch (Exception e6) {
                                                                    e = e6;
                                                                    longSparseArray6 = longSparseArray8;
                                                                    longSparseArray7 = longSparseArray9;
                                                                    messagesStorage3 = this;
                                                                    exc2 = e;
                                                                    messagesStorage3.checkSQLException(exc2);
                                                                    if (!DialogObject.isEncryptedDialog(longValue3)) {
                                                                    }
                                                                    if (DialogObject.isEncryptedDialog(longValue3)) {
                                                                    }
                                                                    longSparseArray8 = longSparseArray6;
                                                                    arrayList25 = arrayList22;
                                                                    arrayList3 = arrayList20;
                                                                    longSparseArray9 = longSparseArray7;
                                                                    hashSet7 = hashSet6;
                                                                    arrayList24 = arrayList21;
                                                                    arrayList7 = arrayList27;
                                                                    i4 = i11;
                                                                    jArr2 = jArr;
                                                                }
                                                                i11 = i13;
                                                                arrayList21 = arrayList24;
                                                            }
                                                            i11 = i13;
                                                            arrayList21 = arrayList24;
                                                            longSparseArray6 = longSparseArray8;
                                                            longSparseArray7 = longSparseArray9;
                                                        } catch (Exception e7) {
                                                            e = e7;
                                                            i11 = i13;
                                                            arrayList21 = arrayList24;
                                                        }
                                                    } else {
                                                        i11 = i9;
                                                        arrayList20 = arrayList26;
                                                        longSparseArray6 = longSparseArray8;
                                                        longSparseArray7 = longSparseArray9;
                                                        arrayList21 = arrayList24;
                                                        messagesStorage3 = this;
                                                        byteBufferValue2.reuse();
                                                    }
                                                    if (!DialogObject.isEncryptedDialog(longValue3)) {
                                                        try {
                                                            if (tLRPC$TL_dialog.read_inbox_max_id > tLRPC$TL_dialog.top_message) {
                                                                tLRPC$TL_dialog.read_inbox_max_id = 0;
                                                            }
                                                        } catch (Exception e8) {
                                                            exc = e8;
                                                            sQLiteCursor2 = queryFinalized;
                                                            messagesStorage = messagesStorage3;
                                                            tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
                                                            arrayList = arrayList8;
                                                            tLRPC$TL_messages_dialogs.dialogs.clear();
                                                            tLRPC$TL_messages_dialogs.users.clear();
                                                            tLRPC$TL_messages_dialogs.chats.clear();
                                                            arrayList.clear();
                                                            messagesStorage.checkSQLException(exc);
                                                            getMessagesController().processLoadedDialogs(tLRPC$TL_messages_dialogs, arrayList, null, i, 0, 100, 1, true, false, true);
                                                            if (sQLiteCursor2 == null) {
                                                            }
                                                            sQLiteCursor2.dispose();
                                                            return;
                                                        } catch (Throwable th3) {
                                                            th = th3;
                                                            sQLiteCursor = queryFinalized;
                                                            if (sQLiteCursor == null) {
                                                            }
                                                        }
                                                    }
                                                    if (DialogObject.isEncryptedDialog(longValue3)) {
                                                        int encryptedChatId = DialogObject.getEncryptedChatId(longValue3);
                                                        arrayList22 = arrayList25;
                                                        if (!arrayList22.contains(Integer.valueOf(encryptedChatId))) {
                                                            arrayList22.add(Integer.valueOf(encryptedChatId));
                                                        }
                                                        hashSet6 = hashSet7;
                                                    } else {
                                                        arrayList22 = arrayList25;
                                                        if (DialogObject.isUserDialog(longValue3)) {
                                                            if (!arrayList2.contains(Long.valueOf(longValue3))) {
                                                                arrayList2.add(Long.valueOf(longValue3));
                                                            }
                                                            hashSet6 = hashSet7;
                                                            hashSet6.add(Long.valueOf(longValue3));
                                                        } else {
                                                            hashSet6 = hashSet7;
                                                            if (DialogObject.isChatDialog(longValue3)) {
                                                                long j = -longValue3;
                                                                if (!arrayList20.contains(Long.valueOf(j))) {
                                                                    arrayList20.add(Long.valueOf(j));
                                                                }
                                                            }
                                                        }
                                                    }
                                                    longSparseArray8 = longSparseArray6;
                                                    arrayList25 = arrayList22;
                                                    arrayList3 = arrayList20;
                                                    longSparseArray9 = longSparseArray7;
                                                    hashSet7 = hashSet6;
                                                    arrayList24 = arrayList21;
                                                    arrayList7 = arrayList27;
                                                    i4 = i11;
                                                    jArr2 = jArr;
                                                } else {
                                                    i11 = i9;
                                                    arrayList20 = arrayList26;
                                                    longSparseArray6 = longSparseArray8;
                                                    longSparseArray7 = longSparseArray9;
                                                    arrayList21 = arrayList24;
                                                }
                                                messagesStorage3 = this;
                                                if (!DialogObject.isEncryptedDialog(longValue3)) {
                                                }
                                                if (DialogObject.isEncryptedDialog(longValue3)) {
                                                }
                                                longSparseArray8 = longSparseArray6;
                                                arrayList25 = arrayList22;
                                                arrayList3 = arrayList20;
                                                longSparseArray9 = longSparseArray7;
                                                hashSet7 = hashSet6;
                                                arrayList24 = arrayList21;
                                                arrayList7 = arrayList27;
                                                i4 = i11;
                                                jArr2 = jArr;
                                            }
                                        } else {
                                            arrayList19 = arrayList7;
                                            i9 = i4;
                                        }
                                        i10 = 0;
                                        tLRPC$TL_dialog.flags = i10;
                                        tLRPC$TL_dialog.read_inbox_max_id = queryFinalized.intValue(11);
                                        tLRPC$TL_dialog.read_outbox_max_id = queryFinalized.intValue(12);
                                        int intValue32 = queryFinalized.intValue(14);
                                        tLRPC$TL_dialog.pinnedNum = intValue32;
                                        tLRPC$TL_dialog.pinned = intValue32 == 0;
                                        tLRPC$TL_dialog.unread_mentions_count = queryFinalized.intValue(15);
                                        int intValue42 = queryFinalized.intValue(16);
                                        tLRPC$TL_dialog.unread_mark = (intValue42 & 1) == 0;
                                        tLRPC$TL_dialog.view_forum_as_messages = (intValue42 & 64) == 0;
                                        longValue = queryFinalized.longValue(8);
                                        ArrayList arrayList272 = arrayList19;
                                        TLRPC$TL_peerNotifySettings tLRPC$TL_peerNotifySettings2 = new TLRPC$TL_peerNotifySettings();
                                        tLRPC$TL_dialog.notify_settings = tLRPC$TL_peerNotifySettings2;
                                        if ((((int) longValue) & 1) != 0) {
                                        }
                                        tLRPC$TL_dialog.folder_id = queryFinalized.intValue(17);
                                        tLRPC$TL_dialog.unread_reactions_count = queryFinalized.intValue(19);
                                        longValue2 = queryFinalized.longValue(20);
                                        if (longValue2 != 0) {
                                        }
                                        tLRPC$TL_dialog.ttl_period = queryFinalized.intValue(21);
                                        tLRPC$TL_messages_dialogs2.dialogs.add(tLRPC$TL_dialog);
                                        if (jArr2 != null) {
                                        }
                                        byteBufferValue2 = queryFinalized.byteBufferValue(4);
                                        if (byteBufferValue2 == null) {
                                        }
                                        messagesStorage3 = this;
                                        if (!DialogObject.isEncryptedDialog(longValue3)) {
                                        }
                                        if (DialogObject.isEncryptedDialog(longValue3)) {
                                        }
                                        longSparseArray8 = longSparseArray6;
                                        arrayList25 = arrayList22;
                                        arrayList3 = arrayList20;
                                        longSparseArray9 = longSparseArray7;
                                        hashSet7 = hashSet6;
                                        arrayList24 = arrayList21;
                                        arrayList7 = arrayList272;
                                        i4 = i11;
                                        jArr2 = jArr;
                                    } catch (Exception e9) {
                                        exc = e9;
                                        sQLiteCursor2 = queryFinalized;
                                        tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
                                        arrayList = arrayList8;
                                        messagesStorage = this;
                                    }
                                } catch (Throwable th4) {
                                    th = th4;
                                    sQLiteCursor = queryFinalized;
                                }
                            }
                            arrayList10 = arrayList7;
                            i8 = i4;
                            arrayList11 = arrayList3;
                            arrayList12 = arrayList24;
                            hashSet3 = hashSet7;
                            arrayList13 = arrayList25;
                            longSparseArray4 = longSparseArray8;
                            longSparseArray5 = longSparseArray9;
                            messagesStorage3 = this;
                            queryFinalized.dispose();
                        } catch (Exception e10) {
                            messagesStorage = this;
                            exc = e10;
                            tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
                            sQLiteCursor2 = sQLiteCursor3;
                            arrayList = arrayList8;
                            tLRPC$TL_messages_dialogs.dialogs.clear();
                            tLRPC$TL_messages_dialogs.users.clear();
                            tLRPC$TL_messages_dialogs.chats.clear();
                            arrayList.clear();
                            messagesStorage.checkSQLException(exc);
                            getMessagesController().processLoadedDialogs(tLRPC$TL_messages_dialogs, arrayList, null, i, 0, 100, 1, true, false, true);
                            if (sQLiteCursor2 == null) {
                            }
                            sQLiteCursor2.dispose();
                            return;
                        } catch (Throwable th5) {
                            th = th5;
                            th = th;
                            sQLiteCursor = sQLiteCursor3;
                            if (sQLiteCursor == null) {
                            }
                        }
                    } catch (Exception e11) {
                        exc = e11;
                        messagesStorage = messagesStorage5;
                    } catch (Throwable th6) {
                        th = th6;
                        sQLiteCursor = sQLiteCursor3;
                        if (sQLiteCursor == null) {
                        }
                    }
                    try {
                        if (arrayList12.isEmpty()) {
                            arrayList14 = arrayList6;
                            arrayList15 = arrayList5;
                            arrayList16 = arrayList13;
                            hashSet4 = hashSet3;
                            messagesStorage4 = messagesStorage3;
                            sQLiteCursor3 = null;
                        } else {
                            StringBuilder sb = new StringBuilder();
                            int i14 = 0;
                            while (i14 < arrayList12.size()) {
                                ArrayList arrayList28 = arrayList12;
                                try {
                                    Pair pair = (Pair) arrayList28.get(i14);
                                    sb.append("uid = ");
                                    sb.append(pair.first);
                                    sb.append(" AND group_id = ");
                                    sb.append(pair.second);
                                    i14++;
                                    if (i14 < arrayList28.size()) {
                                        sb.append(" OR ");
                                    }
                                    arrayList12 = arrayList28;
                                } catch (Exception e12) {
                                    exc = e12;
                                    messagesStorage = messagesStorage3;
                                    tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
                                    arrayList = arrayList8;
                                    sQLiteCursor2 = null;
                                } catch (Throwable th7) {
                                    th = th7;
                                    sQLiteCursor = null;
                                    if (sQLiteCursor == null) {
                                    }
                                }
                            }
                            arrayList14 = arrayList6;
                            int i15 = 0;
                            SQLiteCursor queryFinalized2 = messagesStorage3.database.queryFinalized(String.format(Locale.US, "SELECT uid, data, read_state, mid, send_state, date, replydata, group_id FROM messages_v2 WHERE %s ORDER BY date DESC", sb), new Object[0]);
                            while (queryFinalized2.next()) {
                                try {
                                    long longValue4 = queryFinalized2.longValue(i15);
                                    NativeByteBuffer byteBufferValue5 = queryFinalized2.byteBufferValue(1);
                                    int i16 = 0;
                                    while (true) {
                                        if (i16 >= tLRPC$TL_messages_dialogs2.dialogs.size()) {
                                            arrayList17 = arrayList5;
                                            arrayList18 = arrayList13;
                                            tLRPC$Dialog = null;
                                            break;
                                        }
                                        tLRPC$Dialog = tLRPC$TL_messages_dialogs2.dialogs.get(i16);
                                        if (tLRPC$Dialog != null) {
                                            arrayList17 = arrayList5;
                                            arrayList18 = arrayList13;
                                            if (tLRPC$Dialog.id == longValue4) {
                                                break;
                                            }
                                        } else {
                                            arrayList17 = arrayList5;
                                            arrayList18 = arrayList13;
                                        }
                                        i16++;
                                        arrayList5 = arrayList17;
                                        arrayList13 = arrayList18;
                                    }
                                    if (tLRPC$Dialog == null || byteBufferValue5 == null) {
                                        hashSet5 = hashSet3;
                                        messagesStorage2 = messagesStorage3;
                                    } else {
                                        TLRPC$Message TLdeserialize3 = TLRPC$Message.TLdeserialize(byteBufferValue5, byteBufferValue5.readInt32(false), false);
                                        if (TLdeserialize3 != null) {
                                            hashSet5 = hashSet3;
                                            try {
                                                try {
                                                    TLdeserialize3.readAttachPath(byteBufferValue5, getUserConfig().clientUserId);
                                                    byteBufferValue5.reuse();
                                                    MessageObject.setUnreadFlags(TLdeserialize3, queryFinalized2.intValue(2));
                                                    TLdeserialize3.id = queryFinalized2.intValue(3);
                                                    int intValue6 = queryFinalized2.intValue(5);
                                                    if (intValue6 != 0) {
                                                        tLRPC$Dialog.last_message_date = intValue6;
                                                    }
                                                    TLdeserialize3.send_state = queryFinalized2.intValue(4);
                                                    TLdeserialize3.dialog_id = longValue4;
                                                    tLRPC$TL_messages_dialogs2.messages.add(TLdeserialize3);
                                                    addUsersAndChatsFromMessage(TLdeserialize3, arrayList2, arrayList11, null);
                                                    try {
                                                        TLRPC$MessageReplyHeader tLRPC$MessageReplyHeader2 = TLdeserialize3.reply_to;
                                                        if (tLRPC$MessageReplyHeader2 != null && tLRPC$MessageReplyHeader2.reply_to_msg_id != 0) {
                                                            TLRPC$MessageAction tLRPC$MessageAction2 = TLdeserialize3.action;
                                                            if ((tLRPC$MessageAction2 instanceof TLRPC$TL_messageActionPinMessage) || (tLRPC$MessageAction2 instanceof TLRPC$TL_messageActionPaymentSent) || (tLRPC$MessageAction2 instanceof TLRPC$TL_messageActionGameScore)) {
                                                                if (!queryFinalized2.isNull(7) && (byteBufferValue = queryFinalized2.byteBufferValue(7)) != null) {
                                                                    TLRPC$Message TLdeserialize4 = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                                                                    TLdeserialize3.replyMessage = TLdeserialize4;
                                                                    TLdeserialize4.readAttachPath(byteBufferValue, getUserConfig().clientUserId);
                                                                    byteBufferValue.reuse();
                                                                    TLRPC$Message tLRPC$Message2 = TLdeserialize3.replyMessage;
                                                                    if (tLRPC$Message2 != null) {
                                                                        try {
                                                                            addUsersAndChatsFromMessage(tLRPC$Message2, arrayList2, arrayList11, null);
                                                                            if (TLdeserialize3.replyMessage == null) {
                                                                                addReplyMessages(TLdeserialize3, longSparseArray4, longSparseArray5);
                                                                            }
                                                                            messagesStorage2 = this;
                                                                        } catch (Exception e13) {
                                                                            e = e13;
                                                                            messagesStorage2 = this;
                                                                            try {
                                                                                messagesStorage2.checkSQLException(e);
                                                                                messagesStorage3 = messagesStorage2;
                                                                                arrayList5 = arrayList17;
                                                                                i15 = 0;
                                                                                hashSet3 = hashSet5;
                                                                                arrayList13 = arrayList18;
                                                                            } catch (Exception e14) {
                                                                                e = e14;
                                                                                sQLiteCursor2 = queryFinalized2;
                                                                                messagesStorage = messagesStorage2;
                                                                                tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
                                                                                arrayList = arrayList8;
                                                                                exc = e;
                                                                                tLRPC$TL_messages_dialogs.dialogs.clear();
                                                                                tLRPC$TL_messages_dialogs.users.clear();
                                                                                tLRPC$TL_messages_dialogs.chats.clear();
                                                                                arrayList.clear();
                                                                                messagesStorage.checkSQLException(exc);
                                                                                getMessagesController().processLoadedDialogs(tLRPC$TL_messages_dialogs, arrayList, null, i, 0, 100, 1, true, false, true);
                                                                                if (sQLiteCursor2 == null) {
                                                                                }
                                                                                sQLiteCursor2.dispose();
                                                                                return;
                                                                            } catch (Throwable th8) {
                                                                                th = th8;
                                                                                sQLiteCursor = queryFinalized2;
                                                                                th = th;
                                                                                if (sQLiteCursor == null) {
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                                if (TLdeserialize3.replyMessage == null) {
                                                                }
                                                                messagesStorage2 = this;
                                                            }
                                                        }
                                                        messagesStorage2 = this;
                                                    } catch (Exception e15) {
                                                        e = e15;
                                                    }
                                                } catch (Exception e16) {
                                                    e = e16;
                                                    messagesStorage = this;
                                                    sQLiteCursor2 = queryFinalized2;
                                                    tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
                                                    arrayList = arrayList8;
                                                    exc = e;
                                                    tLRPC$TL_messages_dialogs.dialogs.clear();
                                                    tLRPC$TL_messages_dialogs.users.clear();
                                                    tLRPC$TL_messages_dialogs.chats.clear();
                                                    arrayList.clear();
                                                    messagesStorage.checkSQLException(exc);
                                                    getMessagesController().processLoadedDialogs(tLRPC$TL_messages_dialogs, arrayList, null, i, 0, 100, 1, true, false, true);
                                                    if (sQLiteCursor2 == null) {
                                                    }
                                                    sQLiteCursor2.dispose();
                                                    return;
                                                }
                                            } catch (Throwable th9) {
                                                th = th9;
                                                sQLiteCursor = queryFinalized2;
                                            }
                                        } else {
                                            hashSet5 = hashSet3;
                                            messagesStorage2 = messagesStorage3;
                                            byteBufferValue5.reuse();
                                        }
                                    }
                                    messagesStorage3 = messagesStorage2;
                                    arrayList5 = arrayList17;
                                    i15 = 0;
                                    hashSet3 = hashSet5;
                                    arrayList13 = arrayList18;
                                } catch (Exception e17) {
                                    e = e17;
                                    sQLiteCursor2 = queryFinalized2;
                                    messagesStorage = messagesStorage3;
                                } catch (Throwable th10) {
                                    th = th10;
                                    sQLiteCursor = queryFinalized2;
                                }
                            }
                            arrayList15 = arrayList5;
                            arrayList16 = arrayList13;
                            hashSet4 = hashSet3;
                            messagesStorage4 = messagesStorage3;
                            queryFinalized2.dispose();
                            sQLiteCursor3 = queryFinalized2;
                        }
                        i4 = i8 + 1;
                        longSparseArray = longSparseArray4;
                        hashSet = hashSet4;
                        arrayList3 = arrayList11;
                        longSparseArray2 = longSparseArray5;
                        messagesStorage5 = messagesStorage4;
                        arrayList23 = arrayList8;
                        arrayList6 = arrayList14;
                        arrayList5 = arrayList15;
                        arrayList4 = arrayList16;
                        arrayList7 = arrayList10;
                        jArr2 = jArr;
                    } catch (Exception e18) {
                        exc = e18;
                        sQLiteCursor2 = null;
                        messagesStorage = messagesStorage3;
                        tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
                        arrayList = arrayList8;
                        tLRPC$TL_messages_dialogs.dialogs.clear();
                        tLRPC$TL_messages_dialogs.users.clear();
                        tLRPC$TL_messages_dialogs.chats.clear();
                        arrayList.clear();
                        messagesStorage.checkSQLException(exc);
                        getMessagesController().processLoadedDialogs(tLRPC$TL_messages_dialogs, arrayList, null, i, 0, 100, 1, true, false, true);
                        if (sQLiteCursor2 == null) {
                        }
                        sQLiteCursor2.dispose();
                        return;
                    } catch (Throwable th11) {
                        th = th11;
                        sQLiteCursor = null;
                        if (sQLiteCursor == null) {
                        }
                    }
                    tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
                    arrayList = arrayList8;
                } catch (Exception e19) {
                    e = e19;
                    messagesStorage = messagesStorage5;
                    arrayList = arrayList23;
                    tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
                } catch (Throwable th12) {
                    th = th12;
                }
                tLRPC$TL_messages_dialogs.dialogs.clear();
                tLRPC$TL_messages_dialogs.users.clear();
                tLRPC$TL_messages_dialogs.chats.clear();
                arrayList.clear();
                messagesStorage.checkSQLException(exc);
                getMessagesController().processLoadedDialogs(tLRPC$TL_messages_dialogs, arrayList, null, i, 0, 100, 1, true, false, true);
                if (sQLiteCursor2 == null) {
                    return;
                }
                sQLiteCursor2.dispose();
                return;
            }
            if (!hashSet2.isEmpty()) {
                HashSet<Long> hashSet8 = new HashSet<>();
                Iterator it = hashSet2.iterator();
                while (it.hasNext()) {
                    Long l = (Long) it.next();
                    for (int i17 = i5; i17 < tLRPC$TL_messages_dialogs2.users.size(); i17++) {
                        if (tLRPC$TL_messages_dialogs2.users.get(i17).id == l.longValue() && tLRPC$TL_messages_dialogs2.users.get(i17).premium) {
                            hashSet8.add(l);
                        }
                    }
                }
                if (!hashSet8.isEmpty()) {
                    arrayList9 = messagesStorage2.loadUserInfos(hashSet8);
                    messagesStorage = this;
                    tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
                    getMessagesController().processLoadedDialogs(tLRPC$TL_messages_dialogs2, arrayList, arrayList9, i, i2, i3, 1, false, false, true);
                    if (sQLiteCursor2 == null) {
                        return;
                    }
                    sQLiteCursor2.dispose();
                    return;
                }
            }
            getMessagesController().processLoadedDialogs(tLRPC$TL_messages_dialogs2, arrayList, arrayList9, i, i2, i3, 1, false, false, true);
            if (sQLiteCursor2 == null) {
            }
            sQLiteCursor2.dispose();
            return;
        } catch (Throwable th13) {
            Throwable th14 = th13;
            th = th14;
            sQLiteCursor = sQLiteCursor2;
            if (sQLiteCursor == null) {
            }
        }
        ArrayList arrayList29 = arrayList5;
        ArrayList<Long> arrayList30 = arrayList3;
        ArrayList arrayList31 = arrayList4;
        messagesStorage2 = messagesStorage5;
        hashSet2 = hashSet;
        loadReplyMessages(longSparseArray, longSparseArray2, arrayList2, arrayList30, false);
        if (jArr != null) {
            try {
                ArrayList arrayList32 = new ArrayList();
                for (int i18 = 0; i18 < jArr.length; i18++) {
                    long j2 = jArr[i18];
                    if (!DialogObject.isEncryptedDialog(j2)) {
                        if (j2 > 0) {
                            if (!arrayList2.contains(Long.valueOf(j2))) {
                                arrayList2.add(Long.valueOf(j2));
                            }
                        } else {
                            long j3 = -j2;
                            if (!arrayList30.contains(Long.valueOf(j3))) {
                                arrayList30.add(Long.valueOf(j3));
                            }
                        }
                        if (!arrayList29.contains(Long.valueOf(jArr[i18]))) {
                            arrayList32.add(Long.valueOf(jArr[i18]));
                        }
                    }
                }
                if (arrayList32.isEmpty()) {
                    i5 = 0;
                    longSparseArray3 = null;
                } else {
                    longSparseArray3 = new LongSparseArray(arrayList32.size());
                    i5 = 0;
                    SQLiteCursor queryFinalized3 = messagesStorage2.database.queryFinalized(String.format(Locale.US, "SELECT did, folder_id FROM dialogs WHERE did IN(%s)", TextUtils.join(",", arrayList32)), new Object[0]);
                    while (queryFinalized3.next()) {
                        try {
                            longSparseArray3.put(queryFinalized3.longValue(0), Integer.valueOf(queryFinalized3.intValue(1)));
                        } catch (Exception e20) {
                            exc = e20;
                            sQLiteCursor2 = queryFinalized3;
                            messagesStorage = messagesStorage2;
                            tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
                            arrayList = arrayList8;
                            tLRPC$TL_messages_dialogs.dialogs.clear();
                            tLRPC$TL_messages_dialogs.users.clear();
                            tLRPC$TL_messages_dialogs.chats.clear();
                            arrayList.clear();
                            messagesStorage.checkSQLException(exc);
                            getMessagesController().processLoadedDialogs(tLRPC$TL_messages_dialogs, arrayList, null, i, 0, 100, 1, true, false, true);
                            if (sQLiteCursor2 == null) {
                            }
                            sQLiteCursor2.dispose();
                            return;
                        } catch (Throwable th15) {
                            th = th15;
                            sQLiteCursor = queryFinalized3;
                            if (sQLiteCursor == null) {
                            }
                        }
                    }
                    queryFinalized3.dispose();
                    sQLiteCursor3 = null;
                }
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.this.lambda$getDialogs$208(longSparseArray3);
                    }
                });
            } catch (Exception e21) {
                exc = e21;
                messagesStorage = messagesStorage2;
                tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
                sQLiteCursor2 = sQLiteCursor3;
                arrayList = arrayList8;
                tLRPC$TL_messages_dialogs.dialogs.clear();
                tLRPC$TL_messages_dialogs.users.clear();
                tLRPC$TL_messages_dialogs.chats.clear();
                arrayList.clear();
                messagesStorage.checkSQLException(exc);
                getMessagesController().processLoadedDialogs(tLRPC$TL_messages_dialogs, arrayList, null, i, 0, 100, 1, true, false, true);
                if (sQLiteCursor2 == null) {
                }
                sQLiteCursor2.dispose();
                return;
            } catch (Throwable th16) {
                th = th16;
                sQLiteCursor = sQLiteCursor3;
                if (sQLiteCursor == null) {
                }
            }
        } else {
            i5 = 0;
        }
        sQLiteCursor2 = sQLiteCursor3;
        if (arrayList31.isEmpty()) {
            arrayList = arrayList8;
        } else {
            try {
                try {
                    arrayList = arrayList8;
                    try {
                        messagesStorage2.getEncryptedChatsInternal(TextUtils.join(",", arrayList31), arrayList, arrayList2);
                    } catch (Exception e22) {
                        exc = e22;
                        messagesStorage = messagesStorage2;
                        tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
                    }
                } catch (Throwable th17) {
                    th = th17;
                    sQLiteCursor = sQLiteCursor2;
                    if (sQLiteCursor == null) {
                    }
                }
            } catch (Exception e23) {
                exc = e23;
                messagesStorage = messagesStorage2;
                tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
                arrayList = arrayList8;
                tLRPC$TL_messages_dialogs.dialogs.clear();
                tLRPC$TL_messages_dialogs.users.clear();
                tLRPC$TL_messages_dialogs.chats.clear();
                arrayList.clear();
                messagesStorage.checkSQLException(exc);
                getMessagesController().processLoadedDialogs(tLRPC$TL_messages_dialogs, arrayList, null, i, 0, 100, 1, true, false, true);
                if (sQLiteCursor2 == null) {
                }
                sQLiteCursor2.dispose();
                return;
            }
        }
        if (!arrayList30.isEmpty()) {
            messagesStorage2.getChatsInternal(TextUtils.join(",", arrayList30), tLRPC$TL_messages_dialogs2.chats);
        }
        if (!arrayList2.isEmpty()) {
            messagesStorage2.getUsersInternal(TextUtils.join(",", arrayList2), tLRPC$TL_messages_dialogs2.users);
        }
        arrayList9 = null;
        messagesStorage = this;
        tLRPC$TL_messages_dialogs = tLRPC$TL_messages_dialogs2;
    }

    public void lambda$getDialogs$208(LongSparseArray longSparseArray) {
        MediaDataController mediaDataController = getMediaDataController();
        mediaDataController.clearDraftsFolderIds();
        if (longSparseArray != null) {
            int size = longSparseArray.size();
            for (int i = 0; i < size; i++) {
                mediaDataController.setDraftFolderId(longSparseArray.keyAt(i), ((Integer) longSparseArray.valueAt(i)).intValue());
            }
        }
    }

    public static void createFirstHoles(long j, SQLitePreparedStatement sQLitePreparedStatement, SQLitePreparedStatement sQLitePreparedStatement2, int i, long j2) throws Exception {
        int i2;
        int i3;
        FileLog.d("createFirstHoles " + j + " " + i + " " + j2);
        sQLitePreparedStatement.requery();
        sQLitePreparedStatement.bindLong(1, j);
        if (j2 != 0) {
            sQLitePreparedStatement.bindLong(2, j2);
            i2 = 3;
        } else {
            i2 = 2;
        }
        int i4 = i2 + 1;
        sQLitePreparedStatement.bindInteger(i2, i == 1 ? 1 : 0);
        sQLitePreparedStatement.bindInteger(i4, i);
        sQLitePreparedStatement.step();
        for (int i5 = 0; i5 < 8; i5++) {
            sQLitePreparedStatement2.requery();
            sQLitePreparedStatement2.bindLong(1, j);
            if (j2 != 0) {
                sQLitePreparedStatement2.bindLong(2, j2);
                i3 = 3;
            } else {
                i3 = 2;
            }
            int i6 = i3 + 1;
            sQLitePreparedStatement2.bindInteger(i3, i5);
            int i7 = i6 + 1;
            sQLitePreparedStatement2.bindInteger(i6, i == 1 ? 1 : 0);
            sQLitePreparedStatement2.bindInteger(i7, i);
            sQLitePreparedStatement2.step();
        }
    }

    public void updateDialogData(final TLRPC$Dialog tLRPC$Dialog) {
        if (tLRPC$Dialog == null) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateDialogData$210(tLRPC$Dialog);
            }
        });
    }

    public void lambda$updateDialogData$210(TLRPC$Dialog tLRPC$Dialog) {
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLiteCursor sQLiteCursor = null;
        try {
            SQLiteCursor queryFinalized = this.database.queryFinalized("SELECT data FROM dialogs WHERE did = " + tLRPC$Dialog.id, new Object[0]);
            try {
                if (!queryFinalized.next()) {
                    queryFinalized.dispose();
                    return;
                }
                sQLitePreparedStatement = this.database.executeFast("UPDATE dialogs SET data = ? WHERE did = ?");
                try {
                    NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$Dialog.getObjectSize());
                    tLRPC$Dialog.serializeToStream(nativeByteBuffer);
                    sQLitePreparedStatement.bindByteBuffer(1, nativeByteBuffer);
                    sQLitePreparedStatement.bindLong(2, tLRPC$Dialog.id);
                    sQLitePreparedStatement.step();
                    sQLitePreparedStatement.dispose();
                    nativeByteBuffer.reuse();
                    queryFinalized.dispose();
                } catch (Exception e) {
                    e = e;
                    sQLiteCursor = queryFinalized;
                    try {
                        checkSQLException(e);
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                    } catch (Throwable th) {
                        th = th;
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                        throw th;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    sQLiteCursor = queryFinalized;
                    if (sQLiteCursor != null) {
                    }
                    if (sQLitePreparedStatement != null) {
                    }
                    throw th;
                }
            } catch (Exception e2) {
                e = e2;
                sQLitePreparedStatement = null;
            } catch (Throwable th3) {
                th = th3;
                sQLitePreparedStatement = null;
            }
        } catch (Exception e3) {
            e = e3;
            sQLitePreparedStatement = null;
        } catch (Throwable th4) {
            th = th4;
            sQLitePreparedStatement = null;
        }
    }

    private void putDialogsInternal(TLRPC$messages_Dialogs tLRPC$messages_Dialogs, int i) {
        SQLitePreparedStatement sQLitePreparedStatement;
        Throwable th;
        SQLitePreparedStatement sQLitePreparedStatement2;
        SQLitePreparedStatement sQLitePreparedStatement3;
        SQLitePreparedStatement sQLitePreparedStatement4;
        SQLitePreparedStatement sQLitePreparedStatement5;
        Throwable th2;
        SQLitePreparedStatement sQLitePreparedStatement6;
        SQLitePreparedStatement sQLitePreparedStatement7;
        SQLitePreparedStatement sQLitePreparedStatement8;
        SQLitePreparedStatement sQLitePreparedStatement9;
        SQLitePreparedStatement sQLitePreparedStatement10;
        SQLitePreparedStatement sQLitePreparedStatement11;
        SQLitePreparedStatement sQLitePreparedStatement12;
        SQLitePreparedStatement sQLitePreparedStatement13;
        SQLitePreparedStatement sQLitePreparedStatement14;
        SQLitePreparedStatement sQLitePreparedStatement15;
        SQLitePreparedStatement sQLitePreparedStatement16;
        SQLiteCursor sQLiteCursor;
        SQLiteDatabase sQLiteDatabase;
        SQLiteCursor sQLiteCursor2;
        SQLiteDatabase sQLiteDatabase2;
        SQLitePreparedStatement sQLitePreparedStatement17;
        Throwable th3;
        Throwable e;
        SQLitePreparedStatement executeFast;
        int i2;
        SQLitePreparedStatement sQLitePreparedStatement18;
        SQLitePreparedStatement sQLitePreparedStatement19;
        SQLitePreparedStatement sQLitePreparedStatement20;
        SQLitePreparedStatement sQLitePreparedStatement21;
        SQLitePreparedStatement sQLitePreparedStatement22;
        SQLitePreparedStatement sQLitePreparedStatement23;
        SQLiteCursor queryFinalized;
        boolean next;
        int i3;
        SQLitePreparedStatement sQLitePreparedStatement24;
        SQLitePreparedStatement sQLitePreparedStatement25;
        boolean z;
        int i4;
        int i5;
        NativeByteBuffer nativeByteBuffer;
        SQLitePreparedStatement sQLitePreparedStatement26;
        int i6;
        long j;
        int i7;
        TLRPC$Dialog tLRPC$Dialog;
        SQLitePreparedStatement sQLitePreparedStatement27;
        int i8;
        int i9;
        int i10;
        NativeByteBuffer nativeByteBuffer2;
        int i11;
        int i12 = i;
        try {
            this.database.beginTransaction();
            LongSparseArray longSparseArray = new LongSparseArray(tLRPC$messages_Dialogs.messages.size());
            for (int i13 = 0; i13 < tLRPC$messages_Dialogs.messages.size(); i13++) {
                try {
                    TLRPC$Message tLRPC$Message = tLRPC$messages_Dialogs.messages.get(i13);
                    long dialogId = MessageObject.getDialogId(tLRPC$Message);
                    if (!longSparseArray.containsKey(dialogId) || (longSparseArray.get(dialogId) != null && ((TLRPC$Message) longSparseArray.get(dialogId)).date < tLRPC$Message.date)) {
                        longSparseArray.put(dialogId, tLRPC$Message);
                    }
                } catch (Exception e2) {
                    th2 = e2;
                    sQLitePreparedStatement2 = null;
                    sQLitePreparedStatement3 = null;
                    sQLiteCursor2 = 0;
                    sQLitePreparedStatement4 = null;
                    sQLitePreparedStatement9 = null;
                    sQLitePreparedStatement8 = null;
                    sQLitePreparedStatement7 = null;
                    sQLitePreparedStatement6 = null;
                    sQLitePreparedStatement11 = null;
                    try {
                        checkSQLException(th2);
                        sQLiteDatabase2 = this.database;
                        if (sQLiteDatabase2 != null) {
                        }
                        if (sQLiteCursor2 != 0) {
                        }
                        if (sQLitePreparedStatement4 != null) {
                        }
                        if (sQLitePreparedStatement9 != null) {
                        }
                        if (sQLitePreparedStatement6 != null) {
                        }
                        if (sQLitePreparedStatement11 != null) {
                        }
                        if (sQLitePreparedStatement8 != null) {
                        }
                        if (sQLitePreparedStatement8 != null) {
                        }
                        if (sQLitePreparedStatement7 != null) {
                        }
                        if (sQLitePreparedStatement3 != null) {
                        }
                        if (sQLitePreparedStatement2 == null) {
                        }
                        sQLitePreparedStatement2.dispose();
                    } catch (Throwable th4) {
                        th = th4;
                        sQLiteCursor = sQLiteCursor2;
                        sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                            sQLiteDatabase.commitTransaction();
                        }
                        if (sQLiteCursor != 0) {
                            sQLiteCursor.dispose();
                        }
                        if (sQLitePreparedStatement4 != null) {
                            sQLitePreparedStatement4.dispose();
                        }
                        if (sQLitePreparedStatement9 != null) {
                            sQLitePreparedStatement9.dispose();
                        }
                        if (sQLitePreparedStatement6 != null) {
                            sQLitePreparedStatement6.dispose();
                        }
                        if (sQLitePreparedStatement11 != null) {
                            sQLitePreparedStatement11.dispose();
                        }
                        if (sQLitePreparedStatement8 != null) {
                            sQLitePreparedStatement8.dispose();
                        }
                        if (sQLitePreparedStatement8 != null) {
                            sQLitePreparedStatement8.dispose();
                        }
                        if (sQLitePreparedStatement7 != null) {
                            sQLitePreparedStatement7.dispose();
                        }
                        if (sQLitePreparedStatement3 != null) {
                            sQLitePreparedStatement3.dispose();
                        }
                        if (sQLitePreparedStatement2 != null) {
                            sQLitePreparedStatement2.dispose();
                            throw th;
                        }
                        throw th;
                    }
                } catch (Throwable th5) {
                    th = th5;
                    sQLitePreparedStatement2 = null;
                    sQLitePreparedStatement3 = null;
                    sQLiteCursor = 0;
                    sQLitePreparedStatement4 = null;
                    sQLitePreparedStatement9 = null;
                    sQLitePreparedStatement8 = null;
                    sQLitePreparedStatement7 = null;
                    sQLitePreparedStatement6 = null;
                    sQLitePreparedStatement11 = null;
                    sQLiteDatabase = this.database;
                    if (sQLiteDatabase != null) {
                    }
                    if (sQLiteCursor != 0) {
                    }
                    if (sQLitePreparedStatement4 != null) {
                    }
                    if (sQLitePreparedStatement9 != null) {
                    }
                    if (sQLitePreparedStatement6 != null) {
                    }
                    if (sQLitePreparedStatement11 != null) {
                    }
                    if (sQLitePreparedStatement8 != null) {
                    }
                    if (sQLitePreparedStatement8 != null) {
                    }
                    if (sQLitePreparedStatement7 != null) {
                    }
                    if (sQLitePreparedStatement3 != null) {
                    }
                    if (sQLitePreparedStatement2 != null) {
                    }
                }
            }
            if (tLRPC$messages_Dialogs.dialogs.isEmpty()) {
                sQLitePreparedStatement17 = null;
                sQLitePreparedStatement2 = null;
                sQLitePreparedStatement3 = null;
            } else {
                sQLitePreparedStatement4 = this.database.executeFast("REPLACE INTO messages_v2 VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, 0, NULL, ?, ?)");
                try {
                    executeFast = this.database.executeFast("REPLACE INTO dialogs VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                } catch (Exception e3) {
                    sQLitePreparedStatement16 = null;
                    th2 = e3;
                    sQLitePreparedStatement2 = null;
                    sQLitePreparedStatement3 = null;
                    sQLitePreparedStatement9 = null;
                    sQLitePreparedStatement8 = sQLitePreparedStatement9;
                    sQLitePreparedStatement15 = sQLitePreparedStatement16;
                    sQLitePreparedStatement7 = sQLitePreparedStatement8;
                    sQLitePreparedStatement6 = sQLitePreparedStatement7;
                    sQLitePreparedStatement14 = sQLitePreparedStatement15;
                    sQLitePreparedStatement11 = sQLitePreparedStatement6;
                    sQLiteCursor2 = sQLitePreparedStatement14;
                    checkSQLException(th2);
                    sQLiteDatabase2 = this.database;
                    if (sQLiteDatabase2 != null) {
                    }
                    if (sQLiteCursor2 != 0) {
                    }
                    if (sQLitePreparedStatement4 != null) {
                    }
                    if (sQLitePreparedStatement9 != null) {
                    }
                    if (sQLitePreparedStatement6 != null) {
                    }
                    if (sQLitePreparedStatement11 != null) {
                    }
                    if (sQLitePreparedStatement8 != null) {
                    }
                    if (sQLitePreparedStatement8 != null) {
                    }
                    if (sQLitePreparedStatement7 != null) {
                    }
                    if (sQLitePreparedStatement3 != null) {
                    }
                    if (sQLitePreparedStatement2 == null) {
                    }
                    sQLitePreparedStatement2.dispose();
                } catch (Throwable th6) {
                    sQLitePreparedStatement13 = null;
                    th = th6;
                    sQLitePreparedStatement2 = null;
                    sQLitePreparedStatement3 = null;
                    sQLitePreparedStatement9 = null;
                    sQLitePreparedStatement8 = sQLitePreparedStatement9;
                    sQLitePreparedStatement12 = sQLitePreparedStatement13;
                    sQLitePreparedStatement7 = sQLitePreparedStatement8;
                    sQLitePreparedStatement6 = sQLitePreparedStatement7;
                    sQLitePreparedStatement10 = sQLitePreparedStatement12;
                    sQLitePreparedStatement11 = sQLitePreparedStatement6;
                    sQLiteCursor = sQLitePreparedStatement10;
                    sQLiteDatabase = this.database;
                    if (sQLiteDatabase != null) {
                    }
                    if (sQLiteCursor != 0) {
                    }
                    if (sQLitePreparedStatement4 != null) {
                    }
                    if (sQLitePreparedStatement9 != null) {
                    }
                    if (sQLitePreparedStatement6 != null) {
                    }
                    if (sQLitePreparedStatement11 != null) {
                    }
                    if (sQLitePreparedStatement8 != null) {
                    }
                    if (sQLitePreparedStatement8 != null) {
                    }
                    if (sQLitePreparedStatement7 != null) {
                    }
                    if (sQLitePreparedStatement3 != null) {
                    }
                    if (sQLitePreparedStatement2 != null) {
                    }
                }
                try {
                    SQLitePreparedStatement executeFast2 = this.database.executeFast("REPLACE INTO media_v4 VALUES(?, ?, ?, ?, ?)");
                    try {
                        SQLitePreparedStatement executeFast3 = this.database.executeFast("REPLACE INTO dialog_settings VALUES(?, ?)");
                        try {
                            sQLitePreparedStatement8 = this.database.executeFast("REPLACE INTO messages_holes VALUES(?, ?, ?)");
                            try {
                                sQLitePreparedStatement7 = this.database.executeFast("REPLACE INTO media_holes_v2 VALUES(?, ?, ?, ?)");
                                i2 = ConnectionsManager.DEFAULT_DATACENTER_ID;
                                sQLitePreparedStatement2 = null;
                                sQLitePreparedStatement3 = null;
                                int i14 = 0;
                                SQLitePreparedStatement sQLitePreparedStatement28 = executeFast;
                                while (true) {
                                    try {
                                        sQLitePreparedStatement9 = sQLitePreparedStatement28;
                                        sQLitePreparedStatement21 = executeFast2;
                                        if (i14 >= tLRPC$messages_Dialogs.dialogs.size()) {
                                            break;
                                        }
                                        try {
                                            TLRPC$Dialog tLRPC$Dialog2 = tLRPC$messages_Dialogs.dialogs.get(i14);
                                            DialogObject.initDialog(tLRPC$Dialog2);
                                            LongSparseArray longSparseArray2 = longSparseArray;
                                            this.unknownDialogsIds.remove(tLRPC$Dialog2.id);
                                            if (i12 == 1) {
                                                try {
                                                    queryFinalized = this.database.queryFinalized("SELECT did FROM dialogs WHERE did = " + tLRPC$Dialog2.id, new Object[0]);
                                                } catch (Exception e4) {
                                                    th2 = e4;
                                                    sQLitePreparedStatement11 = executeFast3;
                                                    sQLitePreparedStatement6 = sQLitePreparedStatement21;
                                                    sQLiteCursor2 = 0;
                                                    checkSQLException(th2);
                                                    sQLiteDatabase2 = this.database;
                                                    if (sQLiteDatabase2 != null) {
                                                    }
                                                    if (sQLiteCursor2 != 0) {
                                                    }
                                                    if (sQLitePreparedStatement4 != null) {
                                                    }
                                                    if (sQLitePreparedStatement9 != null) {
                                                    }
                                                    if (sQLitePreparedStatement6 != null) {
                                                    }
                                                    if (sQLitePreparedStatement11 != null) {
                                                    }
                                                    if (sQLitePreparedStatement8 != null) {
                                                    }
                                                    if (sQLitePreparedStatement8 != null) {
                                                    }
                                                    if (sQLitePreparedStatement7 != null) {
                                                    }
                                                    if (sQLitePreparedStatement3 != null) {
                                                    }
                                                    if (sQLitePreparedStatement2 == null) {
                                                    }
                                                    sQLitePreparedStatement2.dispose();
                                                } catch (Throwable th7) {
                                                    th = th7;
                                                    sQLitePreparedStatement11 = executeFast3;
                                                    sQLitePreparedStatement6 = sQLitePreparedStatement21;
                                                    sQLiteCursor = 0;
                                                    sQLiteDatabase = this.database;
                                                    if (sQLiteDatabase != null) {
                                                    }
                                                    if (sQLiteCursor != 0) {
                                                    }
                                                    if (sQLitePreparedStatement4 != null) {
                                                    }
                                                    if (sQLitePreparedStatement9 != null) {
                                                    }
                                                    if (sQLitePreparedStatement6 != null) {
                                                    }
                                                    if (sQLitePreparedStatement11 != null) {
                                                    }
                                                    if (sQLitePreparedStatement8 != null) {
                                                    }
                                                    if (sQLitePreparedStatement8 != null) {
                                                    }
                                                    if (sQLitePreparedStatement7 != null) {
                                                    }
                                                    if (sQLitePreparedStatement3 != null) {
                                                    }
                                                    if (sQLitePreparedStatement2 != null) {
                                                    }
                                                }
                                                try {
                                                    next = queryFinalized.next();
                                                    queryFinalized.dispose();
                                                } catch (Exception e5) {
                                                    th2 = e5;
                                                    sQLitePreparedStatement11 = executeFast3;
                                                    sQLiteCursor2 = queryFinalized;
                                                    sQLitePreparedStatement6 = sQLitePreparedStatement21;
                                                    checkSQLException(th2);
                                                    sQLiteDatabase2 = this.database;
                                                    if (sQLiteDatabase2 != null) {
                                                    }
                                                    if (sQLiteCursor2 != 0) {
                                                    }
                                                    if (sQLitePreparedStatement4 != null) {
                                                    }
                                                    if (sQLitePreparedStatement9 != null) {
                                                    }
                                                    if (sQLitePreparedStatement6 != null) {
                                                    }
                                                    if (sQLitePreparedStatement11 != null) {
                                                    }
                                                    if (sQLitePreparedStatement8 != null) {
                                                    }
                                                    if (sQLitePreparedStatement8 != null) {
                                                    }
                                                    if (sQLitePreparedStatement7 != null) {
                                                    }
                                                    if (sQLitePreparedStatement3 != null) {
                                                    }
                                                    if (sQLitePreparedStatement2 == null) {
                                                    }
                                                    sQLitePreparedStatement2.dispose();
                                                } catch (Throwable th8) {
                                                    th = th8;
                                                    sQLitePreparedStatement11 = executeFast3;
                                                    sQLiteCursor = queryFinalized;
                                                    sQLitePreparedStatement6 = sQLitePreparedStatement21;
                                                    sQLiteDatabase = this.database;
                                                    if (sQLiteDatabase != null) {
                                                    }
                                                    if (sQLiteCursor != 0) {
                                                    }
                                                    if (sQLitePreparedStatement4 != null) {
                                                    }
                                                    if (sQLitePreparedStatement9 != null) {
                                                    }
                                                    if (sQLitePreparedStatement6 != null) {
                                                    }
                                                    if (sQLitePreparedStatement11 != null) {
                                                    }
                                                    if (sQLitePreparedStatement8 != null) {
                                                    }
                                                    if (sQLitePreparedStatement8 != null) {
                                                    }
                                                    if (sQLitePreparedStatement7 != null) {
                                                    }
                                                    if (sQLitePreparedStatement3 != null) {
                                                    }
                                                    if (sQLitePreparedStatement2 != null) {
                                                    }
                                                }
                                            } else {
                                                if (i12 == 2) {
                                                    SQLiteCursor queryFinalized2 = this.database.queryFinalized("SELECT pinned FROM dialogs WHERE did = " + tLRPC$Dialog2.id, new Object[0]);
                                                    if (queryFinalized2.next()) {
                                                        if (tLRPC$Dialog2.pinned) {
                                                            tLRPC$Dialog2.pinnedNum = queryFinalized2.intValue(0);
                                                        }
                                                        next = true;
                                                    } else {
                                                        next = false;
                                                    }
                                                    queryFinalized2.dispose();
                                                } else {
                                                    if (i12 == 3) {
                                                        SQLiteCursor queryFinalized3 = this.database.queryFinalized("SELECT last_mid FROM dialogs WHERE did = " + tLRPC$Dialog2.id, new Object[0]);
                                                        int intValue = queryFinalized3.next() ? queryFinalized3.intValue(0) : 0;
                                                        queryFinalized3.dispose();
                                                    }
                                                    next = false;
                                                }
                                                TLRPC$Message tLRPC$Message2 = (TLRPC$Message) longSparseArray2.get(tLRPC$Dialog2.id);
                                                longSparseArray2 = longSparseArray2;
                                                if (tLRPC$Message2 != null) {
                                                    try {
                                                        int max = Math.max(tLRPC$Message2.date, 0);
                                                        if (isValidKeyboardToSave(tLRPC$Message2)) {
                                                            z = next;
                                                            try {
                                                                long j2 = tLRPC$Dialog2.id;
                                                                i3 = i14;
                                                                sQLitePreparedStatement25 = executeFast3;
                                                                try {
                                                                    getMediaDataController().putBotKeyboard(TopicKey.of(j2, MessageObject.getTopicId(this.currentAccount, tLRPC$Message2, isForum(j2))), tLRPC$Message2);
                                                                } catch (Exception e6) {
                                                                    e = e6;
                                                                    th2 = e;
                                                                    sQLitePreparedStatement6 = sQLitePreparedStatement21;
                                                                    sQLitePreparedStatement11 = sQLitePreparedStatement25;
                                                                    sQLiteCursor2 = 0;
                                                                    checkSQLException(th2);
                                                                    sQLiteDatabase2 = this.database;
                                                                    if (sQLiteDatabase2 != null) {
                                                                    }
                                                                    if (sQLiteCursor2 != 0) {
                                                                    }
                                                                    if (sQLitePreparedStatement4 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement9 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement6 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement11 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement8 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement8 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement7 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement3 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement2 == null) {
                                                                    }
                                                                    sQLitePreparedStatement2.dispose();
                                                                } catch (Throwable th9) {
                                                                    th = th9;
                                                                    th = th;
                                                                    sQLitePreparedStatement6 = sQLitePreparedStatement21;
                                                                    sQLitePreparedStatement11 = sQLitePreparedStatement25;
                                                                    sQLiteCursor = 0;
                                                                    sQLiteDatabase = this.database;
                                                                    if (sQLiteDatabase != null) {
                                                                    }
                                                                    if (sQLiteCursor != 0) {
                                                                    }
                                                                    if (sQLitePreparedStatement4 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement9 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement6 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement11 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement8 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement8 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement7 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement3 != null) {
                                                                    }
                                                                    if (sQLitePreparedStatement2 != null) {
                                                                    }
                                                                }
                                                            } catch (Exception e7) {
                                                                e = e7;
                                                                sQLitePreparedStatement25 = executeFast3;
                                                            } catch (Throwable th10) {
                                                                th = th10;
                                                                sQLitePreparedStatement25 = executeFast3;
                                                            }
                                                        } else {
                                                            i3 = i14;
                                                            sQLitePreparedStatement25 = executeFast3;
                                                            z = next;
                                                        }
                                                        try {
                                                            fixUnsupportedMedia(tLRPC$Message2);
                                                            MessageObject.normalizeFlags(tLRPC$Message2);
                                                            NativeByteBuffer nativeByteBuffer3 = new NativeByteBuffer(tLRPC$Message2.getObjectSize());
                                                            tLRPC$Message2.serializeToStream(nativeByteBuffer3);
                                                            sQLitePreparedStatement4.requery();
                                                            sQLitePreparedStatement4.bindInteger(1, tLRPC$Message2.id);
                                                            sQLitePreparedStatement4.bindLong(2, tLRPC$Dialog2.id);
                                                            sQLitePreparedStatement4.bindInteger(3, MessageObject.getUnreadFlags(tLRPC$Message2));
                                                            sQLitePreparedStatement4.bindInteger(4, tLRPC$Message2.send_state);
                                                            sQLitePreparedStatement4.bindInteger(5, tLRPC$Message2.date);
                                                            sQLitePreparedStatement4.bindByteBuffer(6, nativeByteBuffer3);
                                                            sQLitePreparedStatement4.bindInteger(7, (MessageObject.isOut(tLRPC$Message2) || tLRPC$Message2.from_scheduled) ? 1 : 0);
                                                            sQLitePreparedStatement4.bindInteger(8, 0);
                                                            if ((tLRPC$Message2.flags & 1024) != 0) {
                                                                i5 = tLRPC$Message2.views;
                                                                i4 = 9;
                                                            } else {
                                                                i4 = 9;
                                                                i5 = 0;
                                                            }
                                                            sQLitePreparedStatement4.bindInteger(i4, i5);
                                                            int i15 = tLRPC$Message2.stickerVerified;
                                                            sQLitePreparedStatement4.bindInteger(10, i15 == 0 ? 1 : i15 == 2 ? 2 : 0);
                                                            sQLitePreparedStatement4.bindInteger(11, tLRPC$Message2.mentioned ? 1 : 0);
                                                            sQLitePreparedStatement4.bindInteger(12, tLRPC$Message2.forwards);
                                                            if (tLRPC$Message2.replies != null) {
                                                                nativeByteBuffer = new NativeByteBuffer(tLRPC$Message2.replies.getObjectSize());
                                                                tLRPC$Message2.replies.serializeToStream(nativeByteBuffer);
                                                                sQLitePreparedStatement4.bindByteBuffer(13, nativeByteBuffer);
                                                            } else {
                                                                sQLitePreparedStatement4.bindNull(13);
                                                                nativeByteBuffer = null;
                                                            }
                                                            TLRPC$MessageReplyHeader tLRPC$MessageReplyHeader = tLRPC$Message2.reply_to;
                                                            if (tLRPC$MessageReplyHeader != null) {
                                                                int i16 = tLRPC$MessageReplyHeader.reply_to_top_id;
                                                                if (i16 == 0) {
                                                                    i16 = tLRPC$MessageReplyHeader.reply_to_msg_id;
                                                                }
                                                                sQLitePreparedStatement4.bindInteger(14, i16);
                                                            } else {
                                                                sQLitePreparedStatement4.bindInteger(14, 0);
                                                            }
                                                            sQLitePreparedStatement4.bindLong(15, MessageObject.getChannelId(tLRPC$Message2));
                                                            if ((tLRPC$Message2.flags & 131072) != 0) {
                                                                sQLitePreparedStatement4.bindLong(16, tLRPC$Message2.grouped_id);
                                                            } else {
                                                                sQLitePreparedStatement4.bindNull(16);
                                                            }
                                                            TLRPC$MessageReplyHeader tLRPC$MessageReplyHeader2 = tLRPC$Message2.reply_to;
                                                            if (tLRPC$MessageReplyHeader2 != null) {
                                                                sQLitePreparedStatement4.bindInteger(17, tLRPC$MessageReplyHeader2.story_id);
                                                            } else {
                                                                sQLitePreparedStatement4.bindInteger(17, 0);
                                                            }
                                                            sQLitePreparedStatement4.step();
                                                            if (MediaDataController.canAddMessageToMedia(tLRPC$Message2)) {
                                                                try {
                                                                    sQLitePreparedStatement21.requery();
                                                                    sQLitePreparedStatement26 = sQLitePreparedStatement21;
                                                                    try {
                                                                        sQLitePreparedStatement26.bindInteger(1, tLRPC$Message2.id);
                                                                        sQLitePreparedStatement26.bindLong(2, tLRPC$Dialog2.id);
                                                                        sQLitePreparedStatement26.bindInteger(3, tLRPC$Message2.date);
                                                                        sQLitePreparedStatement26.bindInteger(4, MediaDataController.getMediaType(tLRPC$Message2));
                                                                        sQLitePreparedStatement26.bindByteBuffer(5, nativeByteBuffer3);
                                                                        sQLitePreparedStatement26.step();
                                                                    } catch (Exception e8) {
                                                                        e = e8;
                                                                        th2 = e;
                                                                        sQLitePreparedStatement6 = sQLitePreparedStatement26;
                                                                        sQLitePreparedStatement11 = sQLitePreparedStatement25;
                                                                        sQLiteCursor2 = 0;
                                                                        checkSQLException(th2);
                                                                        sQLiteDatabase2 = this.database;
                                                                        if (sQLiteDatabase2 != null) {
                                                                        }
                                                                        if (sQLiteCursor2 != 0) {
                                                                        }
                                                                        if (sQLitePreparedStatement4 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement9 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement6 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement11 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement8 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement8 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement7 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement3 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement2 == null) {
                                                                        }
                                                                        sQLitePreparedStatement2.dispose();
                                                                    } catch (Throwable th11) {
                                                                        th = th11;
                                                                        th = th;
                                                                        sQLitePreparedStatement6 = sQLitePreparedStatement26;
                                                                        sQLitePreparedStatement11 = sQLitePreparedStatement25;
                                                                        sQLiteCursor = 0;
                                                                        sQLiteDatabase = this.database;
                                                                        if (sQLiteDatabase != null) {
                                                                        }
                                                                        if (sQLiteCursor != 0) {
                                                                        }
                                                                        if (sQLitePreparedStatement4 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement9 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement6 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement11 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement8 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement8 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement7 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement3 != null) {
                                                                        }
                                                                        if (sQLitePreparedStatement2 != null) {
                                                                        }
                                                                    }
                                                                } catch (Exception e9) {
                                                                    e = e9;
                                                                    sQLitePreparedStatement26 = sQLitePreparedStatement21;
                                                                } catch (Throwable th12) {
                                                                    th = th12;
                                                                    sQLitePreparedStatement26 = sQLitePreparedStatement21;
                                                                }
                                                            } else {
                                                                sQLitePreparedStatement26 = sQLitePreparedStatement21;
                                                            }
                                                            if (nativeByteBuffer != null) {
                                                                nativeByteBuffer.reuse();
                                                            }
                                                            try {
                                                                nativeByteBuffer3.reuse();
                                                                if (tLRPC$Message2.ttl_period != 0 && tLRPC$Message2.id > 0) {
                                                                    if (sQLitePreparedStatement2 == null) {
                                                                        sQLitePreparedStatement2 = this.database.executeFast("REPLACE INTO enc_tasks_v4 VALUES(?, ?, ?, ?)");
                                                                    }
                                                                    sQLitePreparedStatement2.requery();
                                                                    sQLitePreparedStatement2.bindInteger(1, tLRPC$Message2.id);
                                                                    sQLitePreparedStatement2.bindLong(2, tLRPC$Message2.dialog_id);
                                                                    sQLitePreparedStatement2.bindInteger(3, tLRPC$Message2.date + tLRPC$Message2.ttl_period);
                                                                    sQLitePreparedStatement2.bindInteger(4, 0);
                                                                    sQLitePreparedStatement2.step();
                                                                    i2 = Math.min(i2, tLRPC$Message2.date + tLRPC$Message2.ttl_period);
                                                                }
                                                                int i17 = i2;
                                                                SQLitePreparedStatement sQLitePreparedStatement29 = sQLitePreparedStatement2;
                                                                try {
                                                                    if (tLRPC$Message2.media instanceof TLRPC$TL_messageMediaPoll) {
                                                                        if (sQLitePreparedStatement3 == null) {
                                                                            try {
                                                                                sQLitePreparedStatement3 = this.database.executeFast("REPLACE INTO polls_v2 VALUES(?, ?, ?)");
                                                                            } catch (Exception e10) {
                                                                                th2 = e10;
                                                                                sQLitePreparedStatement6 = sQLitePreparedStatement26;
                                                                                sQLitePreparedStatement11 = sQLitePreparedStatement25;
                                                                                sQLitePreparedStatement2 = sQLitePreparedStatement29;
                                                                                sQLiteCursor2 = 0;
                                                                                checkSQLException(th2);
                                                                                sQLiteDatabase2 = this.database;
                                                                                if (sQLiteDatabase2 != null) {
                                                                                }
                                                                                if (sQLiteCursor2 != 0) {
                                                                                }
                                                                                if (sQLitePreparedStatement4 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement9 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement6 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement11 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement8 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement8 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement7 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement3 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement2 == null) {
                                                                                }
                                                                                sQLitePreparedStatement2.dispose();
                                                                            } catch (Throwable th13) {
                                                                                th = th13;
                                                                                sQLitePreparedStatement6 = sQLitePreparedStatement26;
                                                                                sQLitePreparedStatement11 = sQLitePreparedStatement25;
                                                                                sQLitePreparedStatement2 = sQLitePreparedStatement29;
                                                                                sQLiteCursor = 0;
                                                                                sQLiteDatabase = this.database;
                                                                                if (sQLiteDatabase != null) {
                                                                                }
                                                                                if (sQLiteCursor != 0) {
                                                                                }
                                                                                if (sQLitePreparedStatement4 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement9 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement6 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement11 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement8 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement8 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement7 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement3 != null) {
                                                                                }
                                                                                if (sQLitePreparedStatement2 != null) {
                                                                                }
                                                                            }
                                                                        }
                                                                        TLRPC$TL_messageMediaPoll tLRPC$TL_messageMediaPoll = (TLRPC$TL_messageMediaPoll) tLRPC$Message2.media;
                                                                        sQLitePreparedStatement3.requery();
                                                                        i6 = 1;
                                                                        sQLitePreparedStatement3.bindInteger(1, tLRPC$Message2.id);
                                                                        sQLitePreparedStatement3.bindLong(2, tLRPC$Message2.dialog_id);
                                                                        sQLitePreparedStatement3.bindLong(3, tLRPC$TL_messageMediaPoll.poll.id);
                                                                        sQLitePreparedStatement3.step();
                                                                    } else {
                                                                        i6 = 1;
                                                                    }
                                                                    SQLitePreparedStatement sQLitePreparedStatement30 = sQLitePreparedStatement3;
                                                                    if (z) {
                                                                        try {
                                                                            j = tLRPC$Dialog2.id;
                                                                            i7 = tLRPC$Message2.id;
                                                                            tLRPC$Dialog = tLRPC$Dialog2;
                                                                            sQLitePreparedStatement11 = sQLitePreparedStatement25;
                                                                            sQLitePreparedStatement6 = sQLitePreparedStatement26;
                                                                            sQLitePreparedStatement27 = sQLitePreparedStatement9;
                                                                            i8 = 6;
                                                                            i9 = i6;
                                                                        } catch (Exception e11) {
                                                                            sQLitePreparedStatement6 = sQLitePreparedStatement26;
                                                                            sQLitePreparedStatement11 = sQLitePreparedStatement25;
                                                                            th2 = e11;
                                                                        } catch (Throwable th14) {
                                                                            sQLitePreparedStatement6 = sQLitePreparedStatement26;
                                                                            sQLitePreparedStatement11 = sQLitePreparedStatement25;
                                                                            th = th14;
                                                                        }
                                                                        try {
                                                                            closeHolesInTable("messages_holes", j, i7, i7, 0L);
                                                                            long j3 = tLRPC$Dialog.id;
                                                                            int i18 = tLRPC$Message2.id;
                                                                            closeHolesInMedia(j3, i18, i18, -1, 0L);
                                                                        } catch (Exception e12) {
                                                                            th2 = e12;
                                                                            sQLitePreparedStatement9 = sQLitePreparedStatement27;
                                                                            sQLitePreparedStatement2 = sQLitePreparedStatement29;
                                                                            sQLitePreparedStatement3 = sQLitePreparedStatement30;
                                                                            sQLiteCursor2 = 0;
                                                                            checkSQLException(th2);
                                                                            sQLiteDatabase2 = this.database;
                                                                            if (sQLiteDatabase2 != null) {
                                                                            }
                                                                            if (sQLiteCursor2 != 0) {
                                                                            }
                                                                            if (sQLitePreparedStatement4 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement9 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement6 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement11 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement8 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement8 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement7 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement3 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement2 == null) {
                                                                            }
                                                                            sQLitePreparedStatement2.dispose();
                                                                        } catch (Throwable th15) {
                                                                            th = th15;
                                                                            sQLitePreparedStatement9 = sQLitePreparedStatement27;
                                                                            sQLitePreparedStatement2 = sQLitePreparedStatement29;
                                                                            sQLitePreparedStatement3 = sQLitePreparedStatement30;
                                                                            sQLiteCursor = 0;
                                                                            sQLiteDatabase = this.database;
                                                                            if (sQLiteDatabase != null) {
                                                                            }
                                                                            if (sQLiteCursor != 0) {
                                                                            }
                                                                            if (sQLitePreparedStatement4 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement9 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement6 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement11 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement8 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement8 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement7 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement3 != null) {
                                                                            }
                                                                            if (sQLitePreparedStatement2 != null) {
                                                                            }
                                                                        }
                                                                    } else {
                                                                        tLRPC$Dialog = tLRPC$Dialog2;
                                                                        sQLitePreparedStatement6 = sQLitePreparedStatement26;
                                                                        sQLitePreparedStatement27 = sQLitePreparedStatement9;
                                                                        sQLitePreparedStatement11 = sQLitePreparedStatement25;
                                                                        i8 = 6;
                                                                        i9 = i6;
                                                                        createFirstHoles(tLRPC$Dialog.id, sQLitePreparedStatement8, sQLitePreparedStatement7, tLRPC$Message2.id, 0L);
                                                                    }
                                                                    i10 = max;
                                                                    i2 = i17;
                                                                    sQLitePreparedStatement2 = sQLitePreparedStatement29;
                                                                    sQLitePreparedStatement3 = sQLitePreparedStatement30;
                                                                } catch (Exception e13) {
                                                                    sQLitePreparedStatement6 = sQLitePreparedStatement26;
                                                                    sQLitePreparedStatement11 = sQLitePreparedStatement25;
                                                                    th2 = e13;
                                                                } catch (Throwable th16) {
                                                                    sQLitePreparedStatement6 = sQLitePreparedStatement26;
                                                                    sQLitePreparedStatement11 = sQLitePreparedStatement25;
                                                                    th = th16;
                                                                }
                                                            } catch (Exception e14) {
                                                                e = e14;
                                                                sQLitePreparedStatement6 = sQLitePreparedStatement26;
                                                                sQLitePreparedStatement11 = sQLitePreparedStatement25;
                                                                th2 = e;
                                                                sQLiteCursor2 = 0;
                                                                checkSQLException(th2);
                                                                sQLiteDatabase2 = this.database;
                                                                if (sQLiteDatabase2 != null) {
                                                                }
                                                                if (sQLiteCursor2 != 0) {
                                                                }
                                                                if (sQLitePreparedStatement4 != null) {
                                                                }
                                                                if (sQLitePreparedStatement9 != null) {
                                                                }
                                                                if (sQLitePreparedStatement6 != null) {
                                                                }
                                                                if (sQLitePreparedStatement11 != null) {
                                                                }
                                                                if (sQLitePreparedStatement8 != null) {
                                                                }
                                                                if (sQLitePreparedStatement8 != null) {
                                                                }
                                                                if (sQLitePreparedStatement7 != null) {
                                                                }
                                                                if (sQLitePreparedStatement3 != null) {
                                                                }
                                                                if (sQLitePreparedStatement2 == null) {
                                                                }
                                                                sQLitePreparedStatement2.dispose();
                                                            } catch (Throwable th17) {
                                                                th = th17;
                                                                sQLitePreparedStatement6 = sQLitePreparedStatement26;
                                                                sQLitePreparedStatement11 = sQLitePreparedStatement25;
                                                                th = th;
                                                                sQLiteCursor = 0;
                                                                sQLiteDatabase = this.database;
                                                                if (sQLiteDatabase != null) {
                                                                }
                                                                if (sQLiteCursor != 0) {
                                                                }
                                                                if (sQLitePreparedStatement4 != null) {
                                                                }
                                                                if (sQLitePreparedStatement9 != null) {
                                                                }
                                                                if (sQLitePreparedStatement6 != null) {
                                                                }
                                                                if (sQLitePreparedStatement11 != null) {
                                                                }
                                                                if (sQLitePreparedStatement8 != null) {
                                                                }
                                                                if (sQLitePreparedStatement8 != null) {
                                                                }
                                                                if (sQLitePreparedStatement7 != null) {
                                                                }
                                                                if (sQLitePreparedStatement3 != null) {
                                                                }
                                                                if (sQLitePreparedStatement2 != null) {
                                                                }
                                                            }
                                                        } catch (Exception e15) {
                                                            e = e15;
                                                            sQLitePreparedStatement6 = sQLitePreparedStatement21;
                                                        } catch (Throwable th18) {
                                                            th = th18;
                                                            sQLitePreparedStatement6 = sQLitePreparedStatement21;
                                                        }
                                                    } catch (Exception e16) {
                                                        e = e16;
                                                        sQLitePreparedStatement11 = executeFast3;
                                                        sQLitePreparedStatement6 = sQLitePreparedStatement21;
                                                    } catch (Throwable th19) {
                                                        th = th19;
                                                        sQLitePreparedStatement11 = executeFast3;
                                                        sQLitePreparedStatement6 = sQLitePreparedStatement21;
                                                    }
                                                } else {
                                                    tLRPC$Dialog = tLRPC$Dialog2;
                                                    i3 = i14;
                                                    sQLitePreparedStatement11 = executeFast3;
                                                    sQLitePreparedStatement27 = sQLitePreparedStatement9;
                                                    sQLitePreparedStatement6 = sQLitePreparedStatement21;
                                                    i8 = 6;
                                                    i9 = 1;
                                                    i10 = 0;
                                                }
                                                try {
                                                    sQLitePreparedStatement27.requery();
                                                    sQLitePreparedStatement24 = sQLitePreparedStatement27;
                                                    try {
                                                        sQLitePreparedStatement24.bindLong(i9, tLRPC$Dialog.id);
                                                        sQLitePreparedStatement24.bindInteger(2, i10);
                                                        sQLitePreparedStatement24.bindInteger(3, tLRPC$Dialog.unread_count);
                                                        sQLitePreparedStatement24.bindInteger(4, tLRPC$Dialog.top_message);
                                                        sQLitePreparedStatement24.bindInteger(5, tLRPC$Dialog.read_inbox_max_id);
                                                        sQLitePreparedStatement24.bindInteger(i8, tLRPC$Dialog.read_outbox_max_id);
                                                        sQLitePreparedStatement24.bindLong(7, 0L);
                                                        sQLitePreparedStatement24.bindInteger(8, tLRPC$Dialog.unread_mentions_count);
                                                        sQLitePreparedStatement24.bindInteger(9, tLRPC$Dialog.pts);
                                                        sQLitePreparedStatement24.bindInteger(10, 0);
                                                        sQLitePreparedStatement24.bindInteger(11, tLRPC$Dialog.pinnedNum);
                                                        boolean z2 = tLRPC$Dialog.unread_mark;
                                                        int i19 = z2;
                                                        if (tLRPC$Dialog.view_forum_as_messages) {
                                                            i19 = (z2 ? 1 : 0) | 64;
                                                        }
                                                        sQLitePreparedStatement24.bindInteger(12, i19);
                                                        sQLitePreparedStatement24.bindInteger(13, tLRPC$Dialog.folder_id);
                                                        if (tLRPC$Dialog instanceof TLRPC$TL_dialogFolder) {
                                                            try {
                                                                TLRPC$TL_dialogFolder tLRPC$TL_dialogFolder = (TLRPC$TL_dialogFolder) tLRPC$Dialog;
                                                                nativeByteBuffer2 = new NativeByteBuffer(tLRPC$TL_dialogFolder.folder.getObjectSize());
                                                                tLRPC$TL_dialogFolder.folder.serializeToStream(nativeByteBuffer2);
                                                                sQLitePreparedStatement24.bindByteBuffer(14, nativeByteBuffer2);
                                                            } catch (Exception e17) {
                                                                e = e17;
                                                                th2 = e;
                                                                sQLitePreparedStatement9 = sQLitePreparedStatement24;
                                                                sQLiteCursor2 = 0;
                                                                checkSQLException(th2);
                                                                sQLiteDatabase2 = this.database;
                                                                if (sQLiteDatabase2 != null) {
                                                                }
                                                                if (sQLiteCursor2 != 0) {
                                                                }
                                                                if (sQLitePreparedStatement4 != null) {
                                                                }
                                                                if (sQLitePreparedStatement9 != null) {
                                                                }
                                                                if (sQLitePreparedStatement6 != null) {
                                                                }
                                                                if (sQLitePreparedStatement11 != null) {
                                                                }
                                                                if (sQLitePreparedStatement8 != null) {
                                                                }
                                                                if (sQLitePreparedStatement8 != null) {
                                                                }
                                                                if (sQLitePreparedStatement7 != null) {
                                                                }
                                                                if (sQLitePreparedStatement3 != null) {
                                                                }
                                                                if (sQLitePreparedStatement2 == null) {
                                                                }
                                                                sQLitePreparedStatement2.dispose();
                                                            } catch (Throwable th20) {
                                                                th = th20;
                                                                th = th;
                                                                sQLitePreparedStatement9 = sQLitePreparedStatement24;
                                                                sQLiteCursor = 0;
                                                                sQLiteDatabase = this.database;
                                                                if (sQLiteDatabase != null) {
                                                                }
                                                                if (sQLiteCursor != 0) {
                                                                }
                                                                if (sQLitePreparedStatement4 != null) {
                                                                }
                                                                if (sQLitePreparedStatement9 != null) {
                                                                }
                                                                if (sQLitePreparedStatement6 != null) {
                                                                }
                                                                if (sQLitePreparedStatement11 != null) {
                                                                }
                                                                if (sQLitePreparedStatement8 != null) {
                                                                }
                                                                if (sQLitePreparedStatement8 != null) {
                                                                }
                                                                if (sQLitePreparedStatement7 != null) {
                                                                }
                                                                if (sQLitePreparedStatement3 != null) {
                                                                }
                                                                if (sQLitePreparedStatement2 != null) {
                                                                }
                                                            }
                                                        } else {
                                                            sQLitePreparedStatement24.bindNull(14);
                                                            nativeByteBuffer2 = null;
                                                        }
                                                        sQLitePreparedStatement24.bindInteger(15, tLRPC$Dialog.unread_reactions_count);
                                                        if (tLRPC$Message2 != null && (tLRPC$Message2.flags & 131072) != 0) {
                                                            sQLitePreparedStatement24.bindLong(16, tLRPC$Message2.grouped_id);
                                                        } else {
                                                            sQLitePreparedStatement24.bindNull(16);
                                                        }
                                                        sQLitePreparedStatement24.bindInteger(17, tLRPC$Dialog.ttl_period);
                                                        sQLitePreparedStatement24.step();
                                                        if (nativeByteBuffer2 != null) {
                                                            nativeByteBuffer2.reuse();
                                                        }
                                                        if (tLRPC$Dialog.notify_settings != null) {
                                                            sQLitePreparedStatement11.requery();
                                                            sQLitePreparedStatement23 = sQLitePreparedStatement11;
                                                            try {
                                                                sQLitePreparedStatement23.bindLong(i9, tLRPC$Dialog.id);
                                                                if (tLRPC$Dialog.notify_settings.mute_until != 0) {
                                                                    i11 = 2;
                                                                } else {
                                                                    i11 = 2;
                                                                    i9 = 0;
                                                                }
                                                                sQLitePreparedStatement23.bindInteger(i11, i9);
                                                                sQLitePreparedStatement23.step();
                                                            } catch (Exception e18) {
                                                                th2 = e18;
                                                                sQLitePreparedStatement9 = sQLitePreparedStatement24;
                                                                sQLitePreparedStatement11 = sQLitePreparedStatement23;
                                                                sQLiteCursor2 = 0;
                                                                checkSQLException(th2);
                                                                sQLiteDatabase2 = this.database;
                                                                if (sQLiteDatabase2 != null) {
                                                                }
                                                                if (sQLiteCursor2 != 0) {
                                                                }
                                                                if (sQLitePreparedStatement4 != null) {
                                                                }
                                                                if (sQLitePreparedStatement9 != null) {
                                                                }
                                                                if (sQLitePreparedStatement6 != null) {
                                                                }
                                                                if (sQLitePreparedStatement11 != null) {
                                                                }
                                                                if (sQLitePreparedStatement8 != null) {
                                                                }
                                                                if (sQLitePreparedStatement8 != null) {
                                                                }
                                                                if (sQLitePreparedStatement7 != null) {
                                                                }
                                                                if (sQLitePreparedStatement3 != null) {
                                                                }
                                                                if (sQLitePreparedStatement2 == null) {
                                                                }
                                                                sQLitePreparedStatement2.dispose();
                                                            } catch (Throwable th21) {
                                                                th = th21;
                                                                sQLitePreparedStatement9 = sQLitePreparedStatement24;
                                                                sQLitePreparedStatement11 = sQLitePreparedStatement23;
                                                                sQLiteCursor = 0;
                                                                sQLiteDatabase = this.database;
                                                                if (sQLiteDatabase != null) {
                                                                }
                                                                if (sQLiteCursor != 0) {
                                                                }
                                                                if (sQLitePreparedStatement4 != null) {
                                                                }
                                                                if (sQLitePreparedStatement9 != null) {
                                                                }
                                                                if (sQLitePreparedStatement6 != null) {
                                                                }
                                                                if (sQLitePreparedStatement11 != null) {
                                                                }
                                                                if (sQLitePreparedStatement8 != null) {
                                                                }
                                                                if (sQLitePreparedStatement8 != null) {
                                                                }
                                                                if (sQLitePreparedStatement7 != null) {
                                                                }
                                                                if (sQLitePreparedStatement3 != null) {
                                                                }
                                                                if (sQLitePreparedStatement2 != null) {
                                                                }
                                                            }
                                                        } else {
                                                            sQLitePreparedStatement23 = sQLitePreparedStatement11;
                                                        }
                                                    } catch (Exception e19) {
                                                        e = e19;
                                                        th2 = e;
                                                        sQLitePreparedStatement9 = sQLitePreparedStatement24;
                                                        sQLiteCursor2 = 0;
                                                        checkSQLException(th2);
                                                        sQLiteDatabase2 = this.database;
                                                        if (sQLiteDatabase2 != null) {
                                                        }
                                                        if (sQLiteCursor2 != 0) {
                                                        }
                                                        if (sQLitePreparedStatement4 != null) {
                                                        }
                                                        if (sQLitePreparedStatement9 != null) {
                                                        }
                                                        if (sQLitePreparedStatement6 != null) {
                                                        }
                                                        if (sQLitePreparedStatement11 != null) {
                                                        }
                                                        if (sQLitePreparedStatement8 != null) {
                                                        }
                                                        if (sQLitePreparedStatement8 != null) {
                                                        }
                                                        if (sQLitePreparedStatement7 != null) {
                                                        }
                                                        if (sQLitePreparedStatement3 != null) {
                                                        }
                                                        if (sQLitePreparedStatement2 == null) {
                                                        }
                                                        sQLitePreparedStatement2.dispose();
                                                    } catch (Throwable th22) {
                                                        th = th22;
                                                        th = th;
                                                        sQLitePreparedStatement9 = sQLitePreparedStatement24;
                                                        sQLiteCursor = 0;
                                                        sQLiteDatabase = this.database;
                                                        if (sQLiteDatabase != null) {
                                                        }
                                                        if (sQLiteCursor != 0) {
                                                        }
                                                        if (sQLitePreparedStatement4 != null) {
                                                        }
                                                        if (sQLitePreparedStatement9 != null) {
                                                        }
                                                        if (sQLitePreparedStatement6 != null) {
                                                        }
                                                        if (sQLitePreparedStatement11 != null) {
                                                        }
                                                        if (sQLitePreparedStatement8 != null) {
                                                        }
                                                        if (sQLitePreparedStatement8 != null) {
                                                        }
                                                        if (sQLitePreparedStatement7 != null) {
                                                        }
                                                        if (sQLitePreparedStatement3 != null) {
                                                        }
                                                        if (sQLitePreparedStatement2 != null) {
                                                        }
                                                    }
                                                } catch (Exception e20) {
                                                    e = e20;
                                                    sQLitePreparedStatement24 = sQLitePreparedStatement27;
                                                } catch (Throwable th23) {
                                                    th = th23;
                                                    sQLitePreparedStatement24 = sQLitePreparedStatement27;
                                                }
                                            }
                                            i14 = i3 + 1;
                                            executeFast3 = sQLitePreparedStatement23;
                                            longSparseArray = longSparseArray2;
                                            executeFast2 = sQLitePreparedStatement6;
                                            i12 = i;
                                            sQLitePreparedStatement28 = sQLitePreparedStatement24;
                                        } catch (Exception e21) {
                                            sQLitePreparedStatement23 = executeFast3;
                                            sQLitePreparedStatement6 = sQLitePreparedStatement21;
                                            th2 = e21;
                                        } catch (Throwable th24) {
                                            sQLitePreparedStatement23 = executeFast3;
                                            sQLitePreparedStatement6 = sQLitePreparedStatement21;
                                            th = th24;
                                        }
                                    } catch (Exception e22) {
                                        e = e22;
                                        sQLitePreparedStatement18 = executeFast3;
                                        sQLitePreparedStatement6 = executeFast2;
                                        sQLitePreparedStatement20 = sQLitePreparedStatement28;
                                    } catch (Throwable th25) {
                                        th = th25;
                                        sQLitePreparedStatement18 = executeFast3;
                                        sQLitePreparedStatement6 = executeFast2;
                                        sQLitePreparedStatement19 = sQLitePreparedStatement28;
                                    }
                                }
                                sQLitePreparedStatement18 = executeFast3;
                                sQLitePreparedStatement22 = sQLitePreparedStatement9;
                                sQLitePreparedStatement6 = sQLitePreparedStatement21;
                            } catch (Exception e23) {
                                sQLitePreparedStatement6 = executeFast2;
                                sQLiteCursor2 = 0;
                                th2 = e23;
                                sQLitePreparedStatement9 = executeFast;
                                sQLitePreparedStatement11 = executeFast3;
                                sQLitePreparedStatement2 = null;
                                sQLitePreparedStatement3 = null;
                                sQLitePreparedStatement7 = null;
                            } catch (Throwable th26) {
                                sQLitePreparedStatement6 = executeFast2;
                                sQLiteCursor = 0;
                                th = th26;
                                sQLitePreparedStatement9 = executeFast;
                                sQLitePreparedStatement11 = executeFast3;
                                sQLitePreparedStatement2 = null;
                                sQLitePreparedStatement3 = null;
                                sQLitePreparedStatement7 = null;
                            }
                        } catch (Exception e24) {
                            sQLitePreparedStatement6 = executeFast2;
                            sQLiteCursor2 = 0;
                            th2 = e24;
                            sQLitePreparedStatement9 = executeFast;
                            sQLitePreparedStatement11 = executeFast3;
                            sQLitePreparedStatement2 = null;
                            sQLitePreparedStatement3 = null;
                            sQLitePreparedStatement8 = null;
                            sQLitePreparedStatement7 = null;
                        } catch (Throwable th27) {
                            sQLitePreparedStatement6 = executeFast2;
                            sQLiteCursor = 0;
                            th = th27;
                            sQLitePreparedStatement9 = executeFast;
                            sQLitePreparedStatement11 = executeFast3;
                            sQLitePreparedStatement2 = null;
                            sQLitePreparedStatement3 = null;
                            sQLitePreparedStatement8 = null;
                            sQLitePreparedStatement7 = null;
                        }
                    } catch (Exception e25) {
                        sQLitePreparedStatement6 = executeFast2;
                        sQLiteCursor2 = 0;
                        th2 = e25;
                        sQLitePreparedStatement9 = executeFast;
                        sQLitePreparedStatement2 = null;
                        sQLitePreparedStatement3 = null;
                        sQLitePreparedStatement8 = null;
                        sQLitePreparedStatement7 = null;
                        sQLitePreparedStatement11 = null;
                    } catch (Throwable th28) {
                        sQLitePreparedStatement6 = executeFast2;
                        sQLiteCursor = 0;
                        th = th28;
                        sQLitePreparedStatement9 = executeFast;
                        sQLitePreparedStatement2 = null;
                        sQLitePreparedStatement3 = null;
                        sQLitePreparedStatement8 = null;
                        sQLitePreparedStatement7 = null;
                        sQLitePreparedStatement11 = null;
                    }
                    try {
                        sQLitePreparedStatement4.dispose();
                        try {
                            sQLitePreparedStatement22.dispose();
                            try {
                                sQLitePreparedStatement6.dispose();
                                try {
                                    sQLitePreparedStatement18.dispose();
                                    try {
                                        sQLitePreparedStatement8.dispose();
                                        try {
                                            sQLitePreparedStatement7.dispose();
                                            if (sQLitePreparedStatement2 != null) {
                                                try {
                                                    sQLitePreparedStatement2.dispose();
                                                    sQLitePreparedStatement17 = null;
                                                    sQLitePreparedStatement17 = null;
                                                    sQLitePreparedStatement17 = null;
                                                    try {
                                                        getMessagesController().didAddedNewTask(i2, 0L, null);
                                                    } catch (Exception e26) {
                                                        e = e26;
                                                        th2 = e;
                                                        sQLitePreparedStatement4 = sQLitePreparedStatement17;
                                                        sQLitePreparedStatement5 = sQLitePreparedStatement17;
                                                        sQLitePreparedStatement9 = sQLitePreparedStatement4;
                                                        sQLitePreparedStatement16 = sQLitePreparedStatement5;
                                                        sQLitePreparedStatement8 = sQLitePreparedStatement9;
                                                        sQLitePreparedStatement15 = sQLitePreparedStatement16;
                                                        sQLitePreparedStatement7 = sQLitePreparedStatement8;
                                                        sQLitePreparedStatement6 = sQLitePreparedStatement7;
                                                        sQLitePreparedStatement14 = sQLitePreparedStatement15;
                                                        sQLitePreparedStatement11 = sQLitePreparedStatement6;
                                                        sQLiteCursor2 = sQLitePreparedStatement14;
                                                        checkSQLException(th2);
                                                        sQLiteDatabase2 = this.database;
                                                        if (sQLiteDatabase2 != null) {
                                                            sQLiteDatabase2.commitTransaction();
                                                        }
                                                        if (sQLiteCursor2 != 0) {
                                                            sQLiteCursor2.dispose();
                                                        }
                                                        if (sQLitePreparedStatement4 != null) {
                                                            sQLitePreparedStatement4.dispose();
                                                        }
                                                        if (sQLitePreparedStatement9 != null) {
                                                            sQLitePreparedStatement9.dispose();
                                                        }
                                                        if (sQLitePreparedStatement6 != null) {
                                                            sQLitePreparedStatement6.dispose();
                                                        }
                                                        if (sQLitePreparedStatement11 != null) {
                                                            sQLitePreparedStatement11.dispose();
                                                        }
                                                        if (sQLitePreparedStatement8 != null) {
                                                            sQLitePreparedStatement8.dispose();
                                                        }
                                                        if (sQLitePreparedStatement8 != null) {
                                                            sQLitePreparedStatement8.dispose();
                                                        }
                                                        if (sQLitePreparedStatement7 != null) {
                                                            sQLitePreparedStatement7.dispose();
                                                        }
                                                        if (sQLitePreparedStatement3 != null) {
                                                            sQLitePreparedStatement3.dispose();
                                                        }
                                                        if (sQLitePreparedStatement2 == null) {
                                                            return;
                                                        }
                                                        sQLitePreparedStatement2.dispose();
                                                    } catch (Throwable th29) {
                                                        th3 = th29;
                                                        th = th3;
                                                        sQLitePreparedStatement4 = sQLitePreparedStatement17;
                                                        sQLitePreparedStatement = sQLitePreparedStatement17;
                                                        sQLitePreparedStatement9 = sQLitePreparedStatement4;
                                                        sQLitePreparedStatement13 = sQLitePreparedStatement;
                                                        sQLitePreparedStatement8 = sQLitePreparedStatement9;
                                                        sQLitePreparedStatement12 = sQLitePreparedStatement13;
                                                        sQLitePreparedStatement7 = sQLitePreparedStatement8;
                                                        sQLitePreparedStatement6 = sQLitePreparedStatement7;
                                                        sQLitePreparedStatement10 = sQLitePreparedStatement12;
                                                        sQLitePreparedStatement11 = sQLitePreparedStatement6;
                                                        sQLiteCursor = sQLitePreparedStatement10;
                                                        sQLiteDatabase = this.database;
                                                        if (sQLiteDatabase != null) {
                                                        }
                                                        if (sQLiteCursor != 0) {
                                                        }
                                                        if (sQLitePreparedStatement4 != null) {
                                                        }
                                                        if (sQLitePreparedStatement9 != null) {
                                                        }
                                                        if (sQLitePreparedStatement6 != null) {
                                                        }
                                                        if (sQLitePreparedStatement11 != null) {
                                                        }
                                                        if (sQLitePreparedStatement8 != null) {
                                                        }
                                                        if (sQLitePreparedStatement8 != null) {
                                                        }
                                                        if (sQLitePreparedStatement7 != null) {
                                                        }
                                                        if (sQLitePreparedStatement3 != null) {
                                                        }
                                                        if (sQLitePreparedStatement2 != null) {
                                                        }
                                                    }
                                                } catch (Exception e27) {
                                                    e = e27;
                                                    sQLitePreparedStatement17 = null;
                                                    th2 = e;
                                                    sQLitePreparedStatement4 = sQLitePreparedStatement17;
                                                    sQLitePreparedStatement5 = sQLitePreparedStatement17;
                                                    sQLitePreparedStatement9 = sQLitePreparedStatement4;
                                                    sQLitePreparedStatement16 = sQLitePreparedStatement5;
                                                    sQLitePreparedStatement8 = sQLitePreparedStatement9;
                                                    sQLitePreparedStatement15 = sQLitePreparedStatement16;
                                                    sQLitePreparedStatement7 = sQLitePreparedStatement8;
                                                    sQLitePreparedStatement6 = sQLitePreparedStatement7;
                                                    sQLitePreparedStatement14 = sQLitePreparedStatement15;
                                                    sQLitePreparedStatement11 = sQLitePreparedStatement6;
                                                    sQLiteCursor2 = sQLitePreparedStatement14;
                                                    checkSQLException(th2);
                                                    sQLiteDatabase2 = this.database;
                                                    if (sQLiteDatabase2 != null) {
                                                    }
                                                    if (sQLiteCursor2 != 0) {
                                                    }
                                                    if (sQLitePreparedStatement4 != null) {
                                                    }
                                                    if (sQLitePreparedStatement9 != null) {
                                                    }
                                                    if (sQLitePreparedStatement6 != null) {
                                                    }
                                                    if (sQLitePreparedStatement11 != null) {
                                                    }
                                                    if (sQLitePreparedStatement8 != null) {
                                                    }
                                                    if (sQLitePreparedStatement8 != null) {
                                                    }
                                                    if (sQLitePreparedStatement7 != null) {
                                                    }
                                                    if (sQLitePreparedStatement3 != null) {
                                                    }
                                                    if (sQLitePreparedStatement2 == null) {
                                                    }
                                                    sQLitePreparedStatement2.dispose();
                                                } catch (Throwable th30) {
                                                    th3 = th30;
                                                    sQLitePreparedStatement17 = null;
                                                    th = th3;
                                                    sQLitePreparedStatement4 = sQLitePreparedStatement17;
                                                    sQLitePreparedStatement = sQLitePreparedStatement17;
                                                    sQLitePreparedStatement9 = sQLitePreparedStatement4;
                                                    sQLitePreparedStatement13 = sQLitePreparedStatement;
                                                    sQLitePreparedStatement8 = sQLitePreparedStatement9;
                                                    sQLitePreparedStatement12 = sQLitePreparedStatement13;
                                                    sQLitePreparedStatement7 = sQLitePreparedStatement8;
                                                    sQLitePreparedStatement6 = sQLitePreparedStatement7;
                                                    sQLitePreparedStatement10 = sQLitePreparedStatement12;
                                                    sQLitePreparedStatement11 = sQLitePreparedStatement6;
                                                    sQLiteCursor = sQLitePreparedStatement10;
                                                    sQLiteDatabase = this.database;
                                                    if (sQLiteDatabase != null) {
                                                    }
                                                    if (sQLiteCursor != 0) {
                                                    }
                                                    if (sQLitePreparedStatement4 != null) {
                                                    }
                                                    if (sQLitePreparedStatement9 != null) {
                                                    }
                                                    if (sQLitePreparedStatement6 != null) {
                                                    }
                                                    if (sQLitePreparedStatement11 != null) {
                                                    }
                                                    if (sQLitePreparedStatement8 != null) {
                                                    }
                                                    if (sQLitePreparedStatement8 != null) {
                                                    }
                                                    if (sQLitePreparedStatement7 != null) {
                                                    }
                                                    if (sQLitePreparedStatement3 != null) {
                                                    }
                                                    if (sQLitePreparedStatement2 != null) {
                                                    }
                                                }
                                            } else {
                                                sQLitePreparedStatement17 = null;
                                            }
                                            if (sQLitePreparedStatement3 != null) {
                                                sQLitePreparedStatement3.dispose();
                                            }
                                        } catch (Exception e28) {
                                            sQLitePreparedStatement14 = null;
                                            th2 = e28;
                                            sQLitePreparedStatement4 = null;
                                            sQLitePreparedStatement9 = null;
                                            sQLitePreparedStatement8 = null;
                                            sQLitePreparedStatement6 = null;
                                            sQLitePreparedStatement11 = sQLitePreparedStatement6;
                                            sQLiteCursor2 = sQLitePreparedStatement14;
                                            checkSQLException(th2);
                                            sQLiteDatabase2 = this.database;
                                            if (sQLiteDatabase2 != null) {
                                            }
                                            if (sQLiteCursor2 != 0) {
                                            }
                                            if (sQLitePreparedStatement4 != null) {
                                            }
                                            if (sQLitePreparedStatement9 != null) {
                                            }
                                            if (sQLitePreparedStatement6 != null) {
                                            }
                                            if (sQLitePreparedStatement11 != null) {
                                            }
                                            if (sQLitePreparedStatement8 != null) {
                                            }
                                            if (sQLitePreparedStatement8 != null) {
                                            }
                                            if (sQLitePreparedStatement7 != null) {
                                            }
                                            if (sQLitePreparedStatement3 != null) {
                                            }
                                            if (sQLitePreparedStatement2 == null) {
                                            }
                                            sQLitePreparedStatement2.dispose();
                                        } catch (Throwable th31) {
                                            sQLitePreparedStatement10 = null;
                                            th = th31;
                                            sQLitePreparedStatement4 = null;
                                            sQLitePreparedStatement9 = null;
                                            sQLitePreparedStatement8 = null;
                                            sQLitePreparedStatement6 = null;
                                            sQLitePreparedStatement11 = sQLitePreparedStatement6;
                                            sQLiteCursor = sQLitePreparedStatement10;
                                            sQLiteDatabase = this.database;
                                            if (sQLiteDatabase != null) {
                                            }
                                            if (sQLiteCursor != 0) {
                                            }
                                            if (sQLitePreparedStatement4 != null) {
                                            }
                                            if (sQLitePreparedStatement9 != null) {
                                            }
                                            if (sQLitePreparedStatement6 != null) {
                                            }
                                            if (sQLitePreparedStatement11 != null) {
                                            }
                                            if (sQLitePreparedStatement8 != null) {
                                            }
                                            if (sQLitePreparedStatement8 != null) {
                                            }
                                            if (sQLitePreparedStatement7 != null) {
                                            }
                                            if (sQLitePreparedStatement3 != null) {
                                            }
                                            if (sQLitePreparedStatement2 != null) {
                                            }
                                        }
                                    } catch (Exception e29) {
                                        sQLitePreparedStatement14 = null;
                                        th2 = e29;
                                        sQLitePreparedStatement4 = null;
                                        sQLitePreparedStatement9 = null;
                                        sQLitePreparedStatement6 = null;
                                    } catch (Throwable th32) {
                                        sQLitePreparedStatement10 = null;
                                        th = th32;
                                        sQLitePreparedStatement4 = null;
                                        sQLitePreparedStatement9 = null;
                                        sQLitePreparedStatement6 = null;
                                    }
                                } catch (Exception e30) {
                                    sQLiteCursor2 = 0;
                                    th2 = e30;
                                    sQLitePreparedStatement11 = sQLitePreparedStatement18;
                                    sQLitePreparedStatement4 = null;
                                    sQLitePreparedStatement9 = null;
                                    sQLitePreparedStatement6 = null;
                                    checkSQLException(th2);
                                    sQLiteDatabase2 = this.database;
                                    if (sQLiteDatabase2 != null) {
                                    }
                                    if (sQLiteCursor2 != 0) {
                                    }
                                    if (sQLitePreparedStatement4 != null) {
                                    }
                                    if (sQLitePreparedStatement9 != null) {
                                    }
                                    if (sQLitePreparedStatement6 != null) {
                                    }
                                    if (sQLitePreparedStatement11 != null) {
                                    }
                                    if (sQLitePreparedStatement8 != null) {
                                    }
                                    if (sQLitePreparedStatement8 != null) {
                                    }
                                    if (sQLitePreparedStatement7 != null) {
                                    }
                                    if (sQLitePreparedStatement3 != null) {
                                    }
                                    if (sQLitePreparedStatement2 == null) {
                                    }
                                    sQLitePreparedStatement2.dispose();
                                } catch (Throwable th33) {
                                    sQLiteCursor = 0;
                                    th = th33;
                                    sQLitePreparedStatement11 = sQLitePreparedStatement18;
                                    sQLitePreparedStatement4 = null;
                                    sQLitePreparedStatement9 = null;
                                    sQLitePreparedStatement6 = null;
                                    sQLiteDatabase = this.database;
                                    if (sQLiteDatabase != null) {
                                    }
                                    if (sQLiteCursor != 0) {
                                    }
                                    if (sQLitePreparedStatement4 != null) {
                                    }
                                    if (sQLitePreparedStatement9 != null) {
                                    }
                                    if (sQLitePreparedStatement6 != null) {
                                    }
                                    if (sQLitePreparedStatement11 != null) {
                                    }
                                    if (sQLitePreparedStatement8 != null) {
                                    }
                                    if (sQLitePreparedStatement8 != null) {
                                    }
                                    if (sQLitePreparedStatement7 != null) {
                                    }
                                    if (sQLitePreparedStatement3 != null) {
                                    }
                                    if (sQLitePreparedStatement2 != null) {
                                    }
                                }
                            } catch (Exception e31) {
                                sQLiteCursor2 = 0;
                                th2 = e31;
                                sQLitePreparedStatement11 = sQLitePreparedStatement18;
                                sQLitePreparedStatement4 = null;
                                sQLitePreparedStatement9 = null;
                            } catch (Throwable th34) {
                                sQLiteCursor = 0;
                                th = th34;
                                sQLitePreparedStatement11 = sQLitePreparedStatement18;
                                sQLitePreparedStatement4 = null;
                                sQLitePreparedStatement9 = null;
                            }
                        } catch (Exception e32) {
                            sQLiteCursor2 = 0;
                            th2 = e32;
                            sQLitePreparedStatement9 = sQLitePreparedStatement22;
                            sQLitePreparedStatement11 = sQLitePreparedStatement18;
                            sQLitePreparedStatement4 = null;
                        } catch (Throwable th35) {
                            sQLiteCursor = 0;
                            th = th35;
                            sQLitePreparedStatement9 = sQLitePreparedStatement22;
                            sQLitePreparedStatement11 = sQLitePreparedStatement18;
                            sQLitePreparedStatement4 = null;
                        }
                    } catch (Exception e33) {
                        e = e33;
                        sQLitePreparedStatement20 = sQLitePreparedStatement22;
                        sQLiteCursor2 = 0;
                        th2 = e;
                        sQLitePreparedStatement9 = sQLitePreparedStatement20;
                        sQLitePreparedStatement11 = sQLitePreparedStatement18;
                        checkSQLException(th2);
                        sQLiteDatabase2 = this.database;
                        if (sQLiteDatabase2 != null) {
                        }
                        if (sQLiteCursor2 != 0) {
                        }
                        if (sQLitePreparedStatement4 != null) {
                        }
                        if (sQLitePreparedStatement9 != null) {
                        }
                        if (sQLitePreparedStatement6 != null) {
                        }
                        if (sQLitePreparedStatement11 != null) {
                        }
                        if (sQLitePreparedStatement8 != null) {
                        }
                        if (sQLitePreparedStatement8 != null) {
                        }
                        if (sQLitePreparedStatement7 != null) {
                        }
                        if (sQLitePreparedStatement3 != null) {
                        }
                        if (sQLitePreparedStatement2 == null) {
                        }
                        sQLitePreparedStatement2.dispose();
                    } catch (Throwable th36) {
                        th = th36;
                        sQLitePreparedStatement19 = sQLitePreparedStatement22;
                        sQLiteCursor = 0;
                        th = th;
                        sQLitePreparedStatement9 = sQLitePreparedStatement19;
                        sQLitePreparedStatement11 = sQLitePreparedStatement18;
                        sQLiteDatabase = this.database;
                        if (sQLiteDatabase != null) {
                        }
                        if (sQLiteCursor != 0) {
                        }
                        if (sQLitePreparedStatement4 != null) {
                        }
                        if (sQLitePreparedStatement9 != null) {
                        }
                        if (sQLitePreparedStatement6 != null) {
                        }
                        if (sQLitePreparedStatement11 != null) {
                        }
                        if (sQLitePreparedStatement8 != null) {
                        }
                        if (sQLitePreparedStatement8 != null) {
                        }
                        if (sQLitePreparedStatement7 != null) {
                        }
                        if (sQLitePreparedStatement3 != null) {
                        }
                        if (sQLitePreparedStatement2 != null) {
                        }
                    }
                } catch (Exception e34) {
                    sQLitePreparedStatement15 = null;
                    th2 = e34;
                    sQLitePreparedStatement9 = executeFast;
                    sQLitePreparedStatement2 = null;
                    sQLitePreparedStatement3 = null;
                    sQLitePreparedStatement8 = null;
                    sQLitePreparedStatement7 = sQLitePreparedStatement8;
                    sQLitePreparedStatement6 = sQLitePreparedStatement7;
                    sQLitePreparedStatement14 = sQLitePreparedStatement15;
                    sQLitePreparedStatement11 = sQLitePreparedStatement6;
                    sQLiteCursor2 = sQLitePreparedStatement14;
                    checkSQLException(th2);
                    sQLiteDatabase2 = this.database;
                    if (sQLiteDatabase2 != null) {
                    }
                    if (sQLiteCursor2 != 0) {
                    }
                    if (sQLitePreparedStatement4 != null) {
                    }
                    if (sQLitePreparedStatement9 != null) {
                    }
                    if (sQLitePreparedStatement6 != null) {
                    }
                    if (sQLitePreparedStatement11 != null) {
                    }
                    if (sQLitePreparedStatement8 != null) {
                    }
                    if (sQLitePreparedStatement8 != null) {
                    }
                    if (sQLitePreparedStatement7 != null) {
                    }
                    if (sQLitePreparedStatement3 != null) {
                    }
                    if (sQLitePreparedStatement2 == null) {
                    }
                    sQLitePreparedStatement2.dispose();
                } catch (Throwable th37) {
                    sQLitePreparedStatement12 = null;
                    th = th37;
                    sQLitePreparedStatement9 = executeFast;
                    sQLitePreparedStatement2 = null;
                    sQLitePreparedStatement3 = null;
                    sQLitePreparedStatement8 = null;
                    sQLitePreparedStatement7 = sQLitePreparedStatement8;
                    sQLitePreparedStatement6 = sQLitePreparedStatement7;
                    sQLitePreparedStatement10 = sQLitePreparedStatement12;
                    sQLitePreparedStatement11 = sQLitePreparedStatement6;
                    sQLiteCursor = sQLitePreparedStatement10;
                    sQLiteDatabase = this.database;
                    if (sQLiteDatabase != null) {
                    }
                    if (sQLiteCursor != 0) {
                    }
                    if (sQLitePreparedStatement4 != null) {
                    }
                    if (sQLitePreparedStatement9 != null) {
                    }
                    if (sQLitePreparedStatement6 != null) {
                    }
                    if (sQLitePreparedStatement11 != null) {
                    }
                    if (sQLitePreparedStatement8 != null) {
                    }
                    if (sQLitePreparedStatement8 != null) {
                    }
                    if (sQLitePreparedStatement7 != null) {
                    }
                    if (sQLitePreparedStatement3 != null) {
                    }
                    if (sQLitePreparedStatement2 != null) {
                    }
                }
            }
            putUsersInternal(tLRPC$messages_Dialogs.users);
            putChatsInternal(tLRPC$messages_Dialogs.chats);
            this.database.commitTransaction();
            resetAllUnreadCounters(false);
            SQLiteDatabase sQLiteDatabase3 = this.database;
            if (sQLiteDatabase3 != null) {
                sQLiteDatabase3.commitTransaction();
            }
            if (sQLitePreparedStatement3 != null) {
                sQLitePreparedStatement3.dispose();
            }
            if (sQLitePreparedStatement2 == null) {
                return;
            }
        } catch (Exception e35) {
            sQLitePreparedStatement5 = null;
            th2 = e35;
            sQLitePreparedStatement2 = null;
            sQLitePreparedStatement3 = null;
            sQLitePreparedStatement4 = null;
        } catch (Throwable th38) {
            sQLitePreparedStatement = null;
            th = th38;
            sQLitePreparedStatement2 = null;
            sQLitePreparedStatement3 = null;
            sQLitePreparedStatement4 = null;
        }
        sQLitePreparedStatement2.dispose();
    }

    public void getDialogFolderId(final long j, final IntCallback intCallback) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getDialogFolderId$212(j, intCallback);
            }
        });
    }

    public void lambda$getDialogFolderId$212(long j, final IntCallback intCallback) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                if (this.unknownDialogsIds.get(j) == null) {
                    sQLiteCursor = this.database.queryFinalized("SELECT folder_id FROM dialogs WHERE did = ?", Long.valueOf(j));
                    r2 = sQLiteCursor.next() ? sQLiteCursor.intValue(0) : -1;
                    sQLiteCursor.dispose();
                }
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        MessagesStorage.IntCallback.this.run(r2);
                    }
                });
                if (sQLiteCursor == null) {
                    return;
                }
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLiteCursor == null) {
                    return;
                }
            }
            sQLiteCursor.dispose();
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public void setDialogsFolderId(final ArrayList<TLRPC$TL_folderPeer> arrayList, final ArrayList<TLRPC$TL_inputFolderPeer> arrayList2, final long j, final int i) {
        if (arrayList == null && arrayList2 == null && j == 0) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$setDialogsFolderId$213(arrayList, arrayList2, i, j);
            }
        });
    }

    public void lambda$setDialogsFolderId$213(ArrayList arrayList, ArrayList arrayList2, int i, long j) {
        SQLitePreparedStatement executeFast;
        boolean z;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                this.database.beginTransaction();
                executeFast = this.database.executeFast("UPDATE dialogs SET folder_id = ?, pinned = ? WHERE did = ?");
            } catch (Exception e) {
                e = e;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            if (arrayList != null) {
                int size = arrayList.size();
                z = false;
                for (int i2 = 0; i2 < size; i2++) {
                    TLRPC$TL_folderPeer tLRPC$TL_folderPeer = (TLRPC$TL_folderPeer) arrayList.get(i2);
                    long peerDialogId = DialogObject.getPeerDialogId(tLRPC$TL_folderPeer.peer);
                    executeFast.requery();
                    executeFast.bindInteger(1, tLRPC$TL_folderPeer.folder_id);
                    if (tLRPC$TL_folderPeer.folder_id == 1) {
                        z = true;
                    }
                    executeFast.bindInteger(2, 0);
                    executeFast.bindLong(3, peerDialogId);
                    executeFast.step();
                    this.unknownDialogsIds.remove(peerDialogId);
                }
            } else if (arrayList2 != null) {
                int size2 = arrayList2.size();
                z = false;
                for (int i3 = 0; i3 < size2; i3++) {
                    TLRPC$TL_inputFolderPeer tLRPC$TL_inputFolderPeer = (TLRPC$TL_inputFolderPeer) arrayList2.get(i3);
                    long peerDialogId2 = DialogObject.getPeerDialogId(tLRPC$TL_inputFolderPeer.peer);
                    executeFast.requery();
                    executeFast.bindInteger(1, tLRPC$TL_inputFolderPeer.folder_id);
                    if (tLRPC$TL_inputFolderPeer.folder_id == 1) {
                        z = true;
                    }
                    executeFast.bindInteger(2, 0);
                    executeFast.bindLong(3, peerDialogId2);
                    executeFast.step();
                    this.unknownDialogsIds.remove(peerDialogId2);
                }
            } else {
                executeFast.requery();
                executeFast.bindInteger(1, i);
                boolean z2 = i == 1;
                executeFast.bindInteger(2, 0);
                executeFast.bindLong(3, j);
                executeFast.step();
                z = z2;
            }
            executeFast.dispose();
            this.database.commitTransaction();
            if (!z) {
                lambda$checkIfFolderEmpty$215(1);
            }
            resetAllUnreadCounters(false);
            SQLiteDatabase sQLiteDatabase = this.database;
            if (sQLiteDatabase != null) {
                sQLiteDatabase.commitTransaction();
            }
        } catch (Exception e2) {
            e = e2;
            sQLitePreparedStatement = executeFast;
            checkSQLException(e);
            SQLiteDatabase sQLiteDatabase2 = this.database;
            if (sQLiteDatabase2 != null) {
                sQLiteDatabase2.commitTransaction();
            }
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
        } catch (Throwable th2) {
            th = th2;
            sQLitePreparedStatement = executeFast;
            SQLiteDatabase sQLiteDatabase3 = this.database;
            if (sQLiteDatabase3 != null) {
                sQLiteDatabase3.commitTransaction();
            }
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void lambda$checkIfFolderEmpty$215(final int i) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                boolean z = true;
                sQLiteCursor = this.database.queryFinalized("SELECT did FROM dialogs WHERE folder_id = ?", Integer.valueOf(i));
                while (sQLiteCursor.next()) {
                    long longValue = sQLiteCursor.longValue(0);
                    if (!DialogObject.isUserDialog(longValue) && !DialogObject.isEncryptedDialog(longValue)) {
                        TLRPC$Chat chat = getChat(-longValue);
                        if (ChatObject.isNotInChat(chat) || chat.migrated_to != null) {
                        }
                    }
                    z = false;
                }
                sQLiteCursor.dispose();
                if (z) {
                    AndroidUtilities.runOnUIThread(new Runnable() {
                        @Override
                        public final void run() {
                            MessagesStorage.this.lambda$checkIfFolderEmptyInternal$214(i);
                        }
                    });
                    this.database.executeFast("DELETE FROM dialogs WHERE did = " + DialogObject.makeFolderDialogId(i)).stepThis().dispose();
                }
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLiteCursor == null) {
                    return;
                }
            }
            sQLiteCursor.dispose();
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public void lambda$checkIfFolderEmptyInternal$214(int i) {
        getMessagesController().onFolderEmpty(i);
    }

    public void checkIfFolderEmpty(final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$checkIfFolderEmpty$215(i);
            }
        });
    }

    public void unpinAllDialogsExceptNew(final ArrayList<Long> arrayList, final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$unpinAllDialogsExceptNew$216(arrayList, i);
            }
        });
    }

    public void lambda$unpinAllDialogsExceptNew$216(ArrayList arrayList, int i) {
        SQLitePreparedStatement sQLitePreparedStatement;
        SQLiteCursor sQLiteCursor = null;
        try {
            ArrayList arrayList2 = new ArrayList();
            SQLiteCursor queryFinalized = this.database.queryFinalized(String.format(Locale.US, "SELECT did, folder_id FROM dialogs WHERE pinned > 0 AND did NOT IN (%s)", TextUtils.join(",", arrayList)), new Object[0]);
            while (queryFinalized.next()) {
                try {
                    long longValue = queryFinalized.longValue(0);
                    if (queryFinalized.intValue(1) == i && !DialogObject.isEncryptedDialog(longValue) && !DialogObject.isFolderDialogId(longValue)) {
                        arrayList2.add(Long.valueOf(queryFinalized.longValue(0)));
                    }
                } catch (Exception e) {
                    e = e;
                    sQLiteCursor = queryFinalized;
                    sQLitePreparedStatement = null;
                } catch (Throwable th) {
                    th = th;
                    sQLiteCursor = queryFinalized;
                    sQLitePreparedStatement = null;
                    if (sQLiteCursor != null) {
                    }
                    if (sQLitePreparedStatement != null) {
                    }
                    throw th;
                }
            }
            queryFinalized.dispose();
            if (arrayList2.isEmpty()) {
                return;
            }
            sQLitePreparedStatement = this.database.executeFast("UPDATE dialogs SET pinned = ? WHERE did = ?");
            for (int i2 = 0; i2 < arrayList2.size(); i2++) {
                try {
                    try {
                        long longValue2 = ((Long) arrayList2.get(i2)).longValue();
                        sQLitePreparedStatement.requery();
                        sQLitePreparedStatement.bindInteger(1, 0);
                        sQLitePreparedStatement.bindLong(2, longValue2);
                        sQLitePreparedStatement.step();
                    } catch (Exception e2) {
                        e = e2;
                        checkSQLException(e);
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                        }
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                            return;
                        }
                        return;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    if (sQLiteCursor != null) {
                        sQLiteCursor.dispose();
                    }
                    if (sQLitePreparedStatement != null) {
                        sQLitePreparedStatement.dispose();
                    }
                    throw th;
                }
            }
            sQLitePreparedStatement.dispose();
        } catch (Exception e3) {
            e = e3;
            sQLitePreparedStatement = null;
        } catch (Throwable th3) {
            th = th3;
            sQLitePreparedStatement = null;
        }
    }

    public void setDialogUnread(final long j, final boolean z) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$setDialogUnread$217(j, z);
            }
        });
    }

    public void lambda$setDialogUnread$217(long j, boolean z) {
        SQLiteCursor sQLiteCursor;
        int i;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        SQLiteCursor sQLiteCursor2 = null;
        SQLitePreparedStatement sQLitePreparedStatement2 = null;
        try {
            try {
                try {
                    try {
                        sQLiteCursor = this.database.queryFinalized("SELECT flags FROM dialogs WHERE did = " + j, new Object[0]);
                        try {
                            i = sQLiteCursor.next() ? sQLiteCursor.intValue(0) : 0;
                            sQLiteCursor.dispose();
                        } catch (Exception e) {
                            e = e;
                            checkSQLException(e);
                            if (sQLiteCursor != null) {
                                sQLiteCursor.dispose();
                            }
                            i = 0;
                            if (!z) {
                            }
                            sQLitePreparedStatement2 = this.database.executeFast("UPDATE dialogs SET flags = ? WHERE did = ?");
                            sQLitePreparedStatement2.bindInteger(1, r8);
                            sQLitePreparedStatement2.bindLong(2, j);
                            sQLitePreparedStatement2.step();
                            sQLitePreparedStatement2.dispose();
                            resetAllUnreadCounters(false);
                            sQLitePreparedStatement2.dispose();
                        }
                    } catch (Throwable th) {
                        th = th;
                        if (0 != 0) {
                            sQLiteCursor2.dispose();
                        }
                        throw th;
                    }
                } catch (Throwable th2) {
                    if (0 != 0) {
                        sQLitePreparedStatement.dispose();
                    }
                    throw th2;
                }
            } catch (Exception e2) {
                e = e2;
                sQLiteCursor = null;
            } catch (Throwable th3) {
                th = th3;
                if (0 != 0) {
                }
                throw th;
            }
            int i2 = !z ? i | 1 : i & (-2);
            sQLitePreparedStatement2 = this.database.executeFast("UPDATE dialogs SET flags = ? WHERE did = ?");
            sQLitePreparedStatement2.bindInteger(1, i2);
            sQLitePreparedStatement2.bindLong(2, j);
            sQLitePreparedStatement2.step();
            sQLitePreparedStatement2.dispose();
            resetAllUnreadCounters(false);
        } catch (Exception e3) {
            checkSQLException(e3);
            if (0 == 0) {
                return;
            }
        }
        sQLitePreparedStatement2.dispose();
    }

    public void setDialogViewThreadAsMessages(final long j, final boolean z) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$setDialogViewThreadAsMessages$218(j, z);
            }
        });
    }

    public void lambda$setDialogViewThreadAsMessages$218(long j, boolean z) {
        SQLiteCursor sQLiteCursor;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        int i = 0;
        try {
            try {
                try {
                    try {
                        sQLiteCursor = this.database.queryFinalized("SELECT flags FROM dialogs WHERE did = " + j, new Object[0]);
                        try {
                            if (sQLiteCursor.next()) {
                                i = sQLiteCursor.intValue(0);
                            }
                        } catch (Exception e) {
                            e = e;
                            checkSQLException(e);
                        }
                    } catch (Throwable th) {
                        if (0 != 0) {
                            sQLitePreparedStatement.dispose();
                        }
                        throw th;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    if (sQLiteCursor != null) {
                        sQLiteCursor.dispose();
                    }
                    throw th;
                }
            } catch (Exception e2) {
                e = e2;
                sQLiteCursor = null;
            } catch (Throwable th3) {
                th = th3;
                sQLiteCursor = null;
                if (sQLiteCursor != null) {
                }
                throw th;
            }
            sQLiteCursor.dispose();
            int i2 = z ? i | 64 : i & (-65);
            sQLitePreparedStatement = this.database.executeFast("UPDATE dialogs SET flags = ? WHERE did = ?");
            sQLitePreparedStatement.bindInteger(1, i2);
            sQLitePreparedStatement.bindLong(2, j);
            sQLitePreparedStatement.step();
            sQLitePreparedStatement.dispose();
        } catch (Exception e3) {
            checkSQLException(e3);
            if (0 == 0) {
                return;
            }
        }
        sQLitePreparedStatement.dispose();
    }

    public void resetAllUnreadCounters(boolean z) {
        int size = this.dialogFilters.size();
        for (int i = 0; i < size; i++) {
            MessagesController.DialogFilter dialogFilter = this.dialogFilters.get(i);
            if (z) {
                if ((dialogFilter.flags & MessagesController.DIALOG_FILTER_FLAG_EXCLUDE_MUTED) != 0) {
                    dialogFilter.pendingUnreadCount = -1;
                }
            } else {
                dialogFilter.pendingUnreadCount = -1;
            }
        }
        calcUnreadCounters(false);
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$resetAllUnreadCounters$219();
            }
        });
    }

    public void lambda$resetAllUnreadCounters$219() {
        ArrayList<MessagesController.DialogFilter> arrayList = getMessagesController().dialogFilters;
        int size = arrayList.size();
        for (int i = 0; i < size; i++) {
            arrayList.get(i).unreadCount = arrayList.get(i).pendingUnreadCount;
        }
        this.mainUnreadCount = this.pendingMainUnreadCount;
        this.archiveUnreadCount = this.pendingArchiveUnreadCount;
        getNotificationCenter().lambda$postNotificationNameOnUIThread$1(NotificationCenter.updateInterfaces, Integer.valueOf(MessagesController.UPDATE_MASK_READ_DIALOG_MESSAGE));
    }

    public void setDialogPinned(final long j, final int i) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$setDialogPinned$220(i, j);
            }
        });
    }

    public void lambda$setDialogPinned$220(int i, long j) {
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                sQLitePreparedStatement = this.database.executeFast("UPDATE dialogs SET pinned = ? WHERE did = ?");
                sQLitePreparedStatement.bindInteger(1, i);
                sQLitePreparedStatement.bindLong(2, j);
                sQLitePreparedStatement.step();
                sQLitePreparedStatement.dispose();
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLitePreparedStatement != null) {
                    sQLitePreparedStatement.dispose();
                }
            }
        } catch (Throwable th) {
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void setDialogsPinned(final ArrayList<Long> arrayList, final ArrayList<Integer> arrayList2) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$setDialogsPinned$221(arrayList, arrayList2);
            }
        });
    }

    public void lambda$setDialogsPinned$221(ArrayList arrayList, ArrayList arrayList2) {
        SQLitePreparedStatement sQLitePreparedStatement = null;
        try {
            try {
                sQLitePreparedStatement = this.database.executeFast("UPDATE dialogs SET pinned = ? WHERE did = ?");
                int size = arrayList.size();
                for (int i = 0; i < size; i++) {
                    sQLitePreparedStatement.requery();
                    sQLitePreparedStatement.bindInteger(1, ((Integer) arrayList2.get(i)).intValue());
                    sQLitePreparedStatement.bindLong(2, ((Long) arrayList.get(i)).longValue());
                    sQLitePreparedStatement.step();
                }
                sQLitePreparedStatement.dispose();
            } catch (Exception e) {
                checkSQLException(e);
                if (sQLitePreparedStatement != null) {
                    sQLitePreparedStatement.dispose();
                }
            }
        } catch (Throwable th) {
            if (sQLitePreparedStatement != null) {
                sQLitePreparedStatement.dispose();
            }
            throw th;
        }
    }

    public void putDialogs(final TLRPC$messages_Dialogs tLRPC$messages_Dialogs, final int i) {
        if (tLRPC$messages_Dialogs.dialogs.isEmpty()) {
            return;
        }
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$putDialogs$222(tLRPC$messages_Dialogs, i);
            }
        });
    }

    public void lambda$putDialogs$222(TLRPC$messages_Dialogs tLRPC$messages_Dialogs, int i) {
        putDialogsInternal(tLRPC$messages_Dialogs, i);
        try {
            loadUnreadMessages();
        } catch (Exception e) {
            checkSQLException(e);
        }
    }

    public void getDialogMaxMessageId(final long j, final IntCallback intCallback) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getDialogMaxMessageId$224(j, intCallback);
            }
        });
    }

    public void lambda$getDialogMaxMessageId$224(long j, final IntCallback intCallback) {
        final int[] iArr = new int[1];
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                sQLiteCursor = this.database.queryFinalized("SELECT MAX(mid) FROM messages_v2 WHERE uid = " + j, new Object[0]);
                if (sQLiteCursor.next()) {
                    iArr[0] = sQLiteCursor.intValue(0);
                }
            } catch (Exception e) {
                checkSQLException(e);
            }
            sQLiteCursor.dispose();
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.lambda$getDialogMaxMessageId$223(MessagesStorage.IntCallback.this, iArr);
                }
            });
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public static void lambda$getDialogMaxMessageId$223(IntCallback intCallback, int[] iArr) {
        intCallback.run(iArr[0]);
    }

    public int getDialogReadMax(final boolean z, final long j) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        final Integer[] numArr = {0};
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getDialogReadMax$225(z, j, numArr, countDownLatch);
            }
        });
        try {
            countDownLatch.await();
        } catch (Exception e) {
            checkSQLException(e);
        }
        return numArr[0].intValue();
    }

    public void lambda$getDialogReadMax$225(boolean z, long j, Integer[] numArr, CountDownLatch countDownLatch) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                if (z) {
                    sQLiteCursor = this.database.queryFinalized("SELECT outbox_max FROM dialogs WHERE did = " + j, new Object[0]);
                    if (sQLiteCursor.next()) {
                        numArr[0] = Integer.valueOf(sQLiteCursor.intValue(0));
                    }
                } else {
                    sQLiteCursor = this.database.queryFinalized("SELECT last_mid, inbox_max FROM dialogs WHERE did = " + j, new Object[0]);
                    if (sQLiteCursor.next()) {
                        int intValue = sQLiteCursor.intValue(0);
                        int intValue2 = sQLiteCursor.intValue(1);
                        if (intValue2 > intValue) {
                            numArr[0] = 0;
                        } else {
                            numArr[0] = Integer.valueOf(intValue2);
                        }
                    }
                }
            } catch (Exception e) {
                checkSQLException(e);
            }
            sQLiteCursor.dispose();
            countDownLatch.countDown();
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public int getChannelPtsSync(final long j) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        final Integer[] numArr = {0};
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getChannelPtsSync$226(j, numArr, countDownLatch);
            }
        });
        try {
            countDownLatch.await();
        } catch (Exception e) {
            checkSQLException(e);
        }
        return numArr[0].intValue();
    }

    public void lambda$getChannelPtsSync$226(long j, Integer[] numArr, CountDownLatch countDownLatch) {
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                sQLiteCursor = this.database.queryFinalized("SELECT pts FROM dialogs WHERE did = " + (-j), new Object[0]);
                if (sQLiteCursor.next()) {
                    numArr[0] = Integer.valueOf(sQLiteCursor.intValue(0));
                }
            } catch (Exception e) {
                checkSQLException(e);
            }
            sQLiteCursor.dispose();
            try {
                countDownLatch.countDown();
            } catch (Exception e2) {
                checkSQLException(e2);
            }
        } catch (Throwable th) {
            if (sQLiteCursor != null) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public TLRPC$User getUserSync(final long j) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        final TLRPC$User[] tLRPC$UserArr = new TLRPC$User[1];
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getUserSync$227(tLRPC$UserArr, j, countDownLatch);
            }
        });
        try {
            countDownLatch.await();
        } catch (Exception e) {
            checkSQLException(e);
        }
        return tLRPC$UserArr[0];
    }

    public void lambda$getUserSync$227(TLRPC$User[] tLRPC$UserArr, long j, CountDownLatch countDownLatch) {
        tLRPC$UserArr[0] = getUser(j);
        countDownLatch.countDown();
    }

    public TLRPC$Chat getChatSync(final long j) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        final TLRPC$Chat[] tLRPC$ChatArr = new TLRPC$Chat[1];
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$getChatSync$228(tLRPC$ChatArr, j, countDownLatch);
            }
        });
        try {
            countDownLatch.await();
        } catch (Exception e) {
            checkSQLException(e);
        }
        return tLRPC$ChatArr[0];
    }

    public void lambda$getChatSync$228(TLRPC$Chat[] tLRPC$ChatArr, long j, CountDownLatch countDownLatch) {
        tLRPC$ChatArr[0] = getChat(j);
        countDownLatch.countDown();
    }

    public TLRPC$User getUser(long j) {
        try {
            ArrayList<TLRPC$User> arrayList = new ArrayList<>();
            getUsersInternal("" + j, arrayList);
            if (arrayList.isEmpty()) {
                return null;
            }
            return arrayList.get(0);
        } catch (Exception e) {
            checkSQLException(e);
            return null;
        }
    }

    public ArrayList<TLRPC$User> getUsers(ArrayList<Long> arrayList) {
        ArrayList<TLRPC$User> arrayList2 = new ArrayList<>();
        try {
            getUsersInternal(TextUtils.join(",", arrayList), arrayList2);
        } catch (Exception e) {
            arrayList2.clear();
            checkSQLException(e);
        }
        return arrayList2;
    }

    public ArrayList<TLRPC$Chat> getChats(ArrayList<Long> arrayList) {
        ArrayList<TLRPC$Chat> arrayList2 = new ArrayList<>();
        try {
            getChatsInternal(TextUtils.join(",", arrayList), arrayList2);
        } catch (Exception e) {
            arrayList2.clear();
            checkSQLException(e);
        }
        return arrayList2;
    }

    public TLRPC$Chat getChat(long j) {
        try {
            ArrayList<TLRPC$Chat> arrayList = new ArrayList<>();
            getChatsInternal("" + j, arrayList);
            if (arrayList.isEmpty()) {
                return null;
            }
            return arrayList.get(0);
        } catch (Exception e) {
            checkSQLException(e);
            return null;
        }
    }

    public TLRPC$EncryptedChat getEncryptedChat(long j) {
        try {
            ArrayList<TLRPC$EncryptedChat> arrayList = new ArrayList<>();
            getEncryptedChatsInternal("" + j, arrayList, null);
            if (arrayList.isEmpty()) {
                return null;
            }
            return arrayList.get(0);
        } catch (Exception e) {
            checkSQLException(e);
            return null;
        }
    }

    public void localSearch(int i, String str, ArrayList<Object> arrayList, ArrayList<CharSequence> arrayList2, ArrayList<TLRPC$User> arrayList3, ArrayList<Long> arrayList4, int i2) {
        Throwable th;
        SQLiteCursor sQLiteCursor;
        Exception exc;
        Throwable th2;
        String str2;
        String str3;
        String str4;
        SQLiteCursor queryFinalized;
        String str5;
        MessagesStorage messagesStorage;
        int i3;
        ArrayList arrayList5;
        LongSparseArray longSparseArray;
        String str6;
        int i4;
        int i5;
        LongSparseArray longSparseArray2;
        LongSparseArray longSparseArray3;
        ArrayList arrayList6;
        int i6;
        int i7;
        LongSparseArray longSparseArray4;
        ?? r15;
        Object obj;
        LongSparseArray longSparseArray5;
        int i8;
        TLRPC$EncryptedChat tLRPC$EncryptedChat;
        TLRPC$User tLRPC$User;
        LongSparseArray longSparseArray6;
        String str7;
        String str8;
        ArrayList arrayList7;
        LongSparseArray longSparseArray7;
        int i9;
        ?? r10;
        String str9;
        String str10;
        LongSparseArray longSparseArray8;
        String str11;
        TLRPC$User user;
        MessagesStorage messagesStorage2 = this;
        long clientUserId = UserConfig.getInstance(messagesStorage2.currentAccount).getClientUserId();
        try {
            String lowerCase = str.trim().toLowerCase();
            if (TextUtils.isEmpty(lowerCase)) {
                return;
            }
            String lowerCase2 = LocaleController.getString("SavedMessages", R.string.SavedMessages).toLowerCase();
            String str12 = "saved messages";
            String lowerCase3 = LocaleController.getString("RepliesTitle", R.string.RepliesTitle).toLowerCase();
            String translitString = LocaleController.getInstance().getTranslitString(lowerCase);
            if (!lowerCase.equals(translitString)) {
                try {
                } catch (Exception e) {
                    exc = e;
                    sQLiteCursor = null;
                    try {
                        messagesStorage2.checkSQLException(exc);
                        if (sQLiteCursor == null) {
                        }
                    } catch (Throwable th3) {
                        th2 = th3;
                        th = th2;
                        if (sQLiteCursor != null) {
                            sQLiteCursor.dispose();
                            throw th;
                        }
                        throw th;
                    }
                } catch (Throwable th4) {
                    th = th4;
                    sQLiteCursor = null;
                    if (sQLiteCursor != null) {
                    }
                }
            }
            translitString = null;
            int i10 = 1 + (translitString != null ? 1 : 0);
            String[] strArr = new String[i10];
            strArr[0] = lowerCase;
            if (translitString != null) {
                strArr[1] = translitString;
            }
            ArrayList arrayList8 = new ArrayList();
            ArrayList arrayList9 = new ArrayList();
            String[] strArr2 = strArr;
            ArrayList arrayList10 = new ArrayList();
            int i11 = i10;
            LongSparseArray longSparseArray9 = new LongSparseArray();
            if (i2 >= 0) {
                str2 = "RepliesTitle";
                try {
                    str4 = "replies";
                    str3 = lowerCase3;
                    queryFinalized = getDatabase().queryFinalized("SELECT did, date FROM dialogs WHERE folder_id = ? ORDER BY date DESC LIMIT 600", Integer.valueOf(i2));
                } catch (Exception e2) {
                    sQLiteCursor = null;
                    messagesStorage2 = this;
                    exc = e2;
                    messagesStorage2.checkSQLException(exc);
                    if (sQLiteCursor == null) {
                    }
                } catch (Throwable th5) {
                    th2 = th5;
                    sQLiteCursor = null;
                    th = th2;
                    if (sQLiteCursor != null) {
                    }
                }
            } else {
                str2 = "RepliesTitle";
                str3 = lowerCase3;
                str4 = "replies";
                queryFinalized = getDatabase().queryFinalized("SELECT did, date FROM dialogs ORDER BY date DESC LIMIT 600", new Object[0]);
            }
            while (queryFinalized.next()) {
                try {
                    long longValue = queryFinalized.longValue(0);
                    DialogsSearchAdapter.DialogSearchResult dialogSearchResult = new DialogsSearchAdapter.DialogSearchResult();
                    String str13 = lowerCase;
                    String str14 = str12;
                    dialogSearchResult.date = queryFinalized.intValue(1);
                    longSparseArray9.put(longValue, dialogSearchResult);
                    if (i != 15 || (arrayList4 != null && arrayList4.contains(Long.valueOf(longValue)))) {
                        if (DialogObject.isEncryptedDialog(longValue)) {
                            if (i == 0 || i == 3) {
                                int encryptedChatId = DialogObject.getEncryptedChatId(longValue);
                                if (!arrayList10.contains(Integer.valueOf(encryptedChatId))) {
                                    arrayList10.add(Integer.valueOf(encryptedChatId));
                                }
                            }
                        } else if (DialogObject.isUserDialog(longValue)) {
                            if ((i != 4 || longValue != clientUserId) && i != 6 && i != 5 && i != 2 && !arrayList8.contains(Long.valueOf(longValue))) {
                                arrayList8.add(Long.valueOf(longValue));
                            }
                        } else if (i != 4) {
                            long j = -longValue;
                            if (!arrayList9.contains(Long.valueOf(j))) {
                                arrayList9.add(Long.valueOf(j));
                            }
                        }
                    }
                    str12 = str14;
                    lowerCase = str13;
                } catch (Exception e3) {
                    exc = e3;
                    sQLiteCursor = queryFinalized;
                    messagesStorage2 = this;
                    messagesStorage2.checkSQLException(exc);
                    if (sQLiteCursor == null) {
                    }
                } catch (Throwable th6) {
                    th = th6;
                    sQLiteCursor = queryFinalized;
                    if (sQLiteCursor != null) {
                    }
                }
            }
            String str15 = lowerCase;
            String str16 = str12;
            queryFinalized.dispose();
            if (i != 4) {
                str5 = str15;
            } else {
                str5 = str15;
            }
            try {
                if (!str16.startsWith(str5)) {
                    messagesStorage = this;
                    i3 = 0;
                    if ((i != 4 && str3.startsWith(str5)) || str4.startsWith(str5)) {
                        user = getMessagesController().getUser(708513L);
                        if (user == null) {
                            user = getMessagesController().getUser(Long.valueOf(UserObject.REPLY_BOT));
                        }
                        if (user != null) {
                            DialogsSearchAdapter.DialogSearchResult dialogSearchResult2 = new DialogsSearchAdapter.DialogSearchResult();
                            dialogSearchResult2.date = ConnectionsManager.DEFAULT_DATACENTER_ID;
                            dialogSearchResult2.name = LocaleController.getString(str2, R.string.RepliesTitle);
                            dialogSearchResult2.object = user;
                            longSparseArray9.put(user.id, dialogSearchResult2);
                            i3++;
                        }
                    }
                    String str17 = ";;;";
                    if (arrayList8.isEmpty()) {
                        arrayList5 = arrayList10;
                        longSparseArray = longSparseArray9;
                        str6 = ";;;";
                        i4 = i11;
                    } else {
                        int i12 = i3;
                        SQLiteCursor queryFinalized2 = getDatabase().queryFinalized(String.format(Locale.US, "SELECT data, status, name FROM users WHERE uid IN(%s)", TextUtils.join(",", arrayList8)), new Object[0]);
                        i3 = i12;
                        while (queryFinalized2.next()) {
                            String stringValue = queryFinalized2.stringValue(2);
                            String translitString2 = LocaleController.getInstance().getTranslitString(stringValue);
                            if (stringValue.equals(translitString2)) {
                                translitString2 = null;
                            }
                            int lastIndexOf = stringValue.lastIndexOf(str17);
                            String substring = lastIndexOf != -1 ? stringValue.substring(lastIndexOf + 3) : null;
                            String str18 = str17;
                            int i13 = i11;
                            int i14 = 0;
                            boolean z = false;
                            while (true) {
                                if (i14 >= i13) {
                                    arrayList7 = arrayList10;
                                    longSparseArray7 = longSparseArray9;
                                    i9 = i13;
                                    break;
                                }
                                arrayList7 = arrayList10;
                                String str19 = strArr2[i14];
                                if (stringValue.startsWith(str19)) {
                                    i9 = i13;
                                } else {
                                    i9 = i13;
                                    if (!stringValue.contains(" " + str19)) {
                                        if (translitString2 != null) {
                                            if (!translitString2.startsWith(str19)) {
                                                if (translitString2.contains(" " + str19)) {
                                                }
                                            }
                                        }
                                        r10 = (substring == null || !substring.startsWith(str19)) ? z : 2;
                                        if (r10 == 0) {
                                            str9 = translitString2;
                                            str10 = stringValue;
                                            NativeByteBuffer byteBufferValue = queryFinalized2.byteBufferValue(0);
                                            if (byteBufferValue == null) {
                                                longSparseArray7 = longSparseArray9;
                                                break;
                                            }
                                            str11 = substring;
                                            TLRPC$User TLdeserialize = TLRPC$User.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                                            byteBufferValue.reuse();
                                            if (i != 15 || (arrayList4 != null && arrayList4.contains(Long.valueOf(TLdeserialize.id)))) {
                                                break;
                                            } else {
                                                longSparseArray8 = longSparseArray9;
                                            }
                                        } else {
                                            str9 = translitString2;
                                            str10 = stringValue;
                                            longSparseArray8 = longSparseArray9;
                                            str11 = substring;
                                        }
                                        i14++;
                                        translitString2 = str9;
                                        stringValue = str10;
                                        arrayList10 = arrayList7;
                                        substring = str11;
                                        longSparseArray9 = longSparseArray8;
                                        z = r10;
                                        i13 = i9;
                                    }
                                }
                                r10 = 1;
                                if (r10 == 0) {
                                }
                                i14++;
                                translitString2 = str9;
                                stringValue = str10;
                                arrayList10 = arrayList7;
                                substring = str11;
                                longSparseArray9 = longSparseArray8;
                                z = r10;
                                i13 = i9;
                            }
                            str17 = str18;
                            arrayList10 = arrayList7;
                            i11 = i9;
                            longSparseArray9 = longSparseArray7;
                        }
                        arrayList5 = arrayList10;
                        longSparseArray = longSparseArray9;
                        str6 = str17;
                        i4 = i11;
                        queryFinalized2.dispose();
                    }
                    if (arrayList9.isEmpty()) {
                        i5 = i4;
                        longSparseArray2 = longSparseArray;
                    } else {
                        SQLiteCursor queryFinalized3 = getDatabase().queryFinalized(String.format(Locale.US, "SELECT data, name FROM chats WHERE uid IN(%s)", TextUtils.join(",", arrayList9)), new Object[0]);
                        while (queryFinalized3.next()) {
                            String stringValue2 = queryFinalized3.stringValue(1);
                            String translitString3 = LocaleController.getInstance().getTranslitString(stringValue2);
                            if (stringValue2.equals(translitString3)) {
                                translitString3 = null;
                            }
                            int i15 = i4;
                            int i16 = 0;
                            while (i16 < i15) {
                                String str20 = strArr2[i16];
                                if (!stringValue2.startsWith(str20)) {
                                    if (!stringValue2.contains(" " + str20)) {
                                        if (translitString3 != null) {
                                            if (!translitString3.startsWith(str20)) {
                                            }
                                        }
                                        str7 = translitString3;
                                        str8 = stringValue2;
                                        i16++;
                                        stringValue2 = str8;
                                        translitString3 = str7;
                                    }
                                }
                                NativeByteBuffer byteBufferValue2 = queryFinalized3.byteBufferValue(0);
                                if (byteBufferValue2 == null) {
                                    break;
                                }
                                TLRPC$Chat TLdeserialize2 = TLRPC$Chat.TLdeserialize(byteBufferValue2, byteBufferValue2.readInt32(false), false);
                                byteBufferValue2.reuse();
                                if (i == 15) {
                                    if (arrayList4 != null) {
                                        str7 = translitString3;
                                        str8 = stringValue2;
                                        if (!arrayList4.contains(Long.valueOf(-TLdeserialize2.id))) {
                                            i16++;
                                            stringValue2 = str8;
                                            translitString3 = str7;
                                        }
                                    }
                                    str7 = translitString3;
                                    str8 = stringValue2;
                                    i16++;
                                    stringValue2 = str8;
                                    translitString3 = str7;
                                } else {
                                    str7 = translitString3;
                                    str8 = stringValue2;
                                }
                                if ((i != 6 || !ChatObject.isChannelAndNotMegaGroup(TLdeserialize2)) && (i != 5 || ChatObject.isChannelAndNotMegaGroup(TLdeserialize2))) {
                                    if (TLdeserialize2 != null && !TLdeserialize2.deactivated && (!ChatObject.isChannel(TLdeserialize2) || !ChatObject.isNotInChat(TLdeserialize2))) {
                                        longSparseArray6 = longSparseArray;
                                        DialogsSearchAdapter.DialogSearchResult dialogSearchResult3 = (DialogsSearchAdapter.DialogSearchResult) longSparseArray6.get(-TLdeserialize2.id);
                                        dialogSearchResult3.name = AndroidUtilities.generateSearchName(TLdeserialize2.title, null, str20);
                                        dialogSearchResult3.object = TLdeserialize2;
                                        i3++;
                                        longSparseArray = longSparseArray6;
                                        i4 = i15;
                                    }
                                }
                                i16++;
                                stringValue2 = str8;
                                translitString3 = str7;
                            }
                            longSparseArray6 = longSparseArray;
                            longSparseArray = longSparseArray6;
                            i4 = i15;
                        }
                        i5 = i4;
                        longSparseArray2 = longSparseArray;
                        queryFinalized3.dispose();
                    }
                    if (!arrayList5.isEmpty() || i == 15) {
                        longSparseArray3 = longSparseArray2;
                    } else {
                        SQLiteCursor queryFinalized4 = getDatabase().queryFinalized(String.format(Locale.US, "SELECT q.data, u.name, q.user, q.g, q.authkey, q.ttl, u.data, u.status, q.layer, q.seq_in, q.seq_out, q.use_count, q.exchange_id, q.key_date, q.fprint, q.fauthkey, q.khash, q.in_seq_no, q.admin_id, q.mtproto_seq FROM enc_chats as q INNER JOIN users as u ON q.user = u.uid WHERE q.uid IN(%s)", TextUtils.join(",", arrayList5)), new Object[0]);
                        while (queryFinalized4.next()) {
                            String stringValue3 = queryFinalized4.stringValue(1);
                            String translitString4 = LocaleController.getInstance().getTranslitString(stringValue3);
                            if (stringValue3.equals(translitString4)) {
                                translitString4 = null;
                            }
                            String str21 = str6;
                            int lastIndexOf2 = stringValue3.lastIndexOf(str21);
                            String substring2 = lastIndexOf2 != -1 ? stringValue3.substring(lastIndexOf2 + 2) : null;
                            char c = 0;
                            int i17 = 0;
                            while (true) {
                                if (i17 >= i5) {
                                    break;
                                }
                                String str22 = strArr2[i17];
                                if (!stringValue3.startsWith(str22)) {
                                    if (!stringValue3.contains(" " + str22)) {
                                        if (translitString4 != null) {
                                            if (!translitString4.startsWith(str22)) {
                                                if (translitString4.contains(" " + str22)) {
                                                }
                                            }
                                        }
                                        if (substring2 != null && substring2.startsWith(str22)) {
                                            c = 2;
                                        }
                                        if (c == 0) {
                                            NativeByteBuffer byteBufferValue3 = queryFinalized4.byteBufferValue(0);
                                            if (byteBufferValue3 != null) {
                                                tLRPC$EncryptedChat = TLRPC$EncryptedChat.TLdeserialize(byteBufferValue3, byteBufferValue3.readInt32(false), false);
                                                byteBufferValue3.reuse();
                                                i8 = 6;
                                            } else {
                                                i8 = 6;
                                                tLRPC$EncryptedChat = null;
                                            }
                                            NativeByteBuffer byteBufferValue4 = queryFinalized4.byteBufferValue(i8);
                                            if (byteBufferValue4 != null) {
                                                tLRPC$User = TLRPC$User.TLdeserialize(byteBufferValue4, byteBufferValue4.readInt32(false), false);
                                                byteBufferValue4.reuse();
                                            } else {
                                                tLRPC$User = null;
                                            }
                                            if (tLRPC$EncryptedChat != null && tLRPC$User != null) {
                                                DialogsSearchAdapter.DialogSearchResult dialogSearchResult4 = (DialogsSearchAdapter.DialogSearchResult) longSparseArray2.get(DialogObject.makeEncryptedDialogId(tLRPC$EncryptedChat.id));
                                                str6 = str21;
                                                longSparseArray5 = longSparseArray2;
                                                tLRPC$EncryptedChat.user_id = queryFinalized4.longValue(2);
                                                tLRPC$EncryptedChat.a_or_b = queryFinalized4.byteArrayValue(3);
                                                tLRPC$EncryptedChat.auth_key = queryFinalized4.byteArrayValue(4);
                                                tLRPC$EncryptedChat.ttl = queryFinalized4.intValue(5);
                                                tLRPC$EncryptedChat.layer = queryFinalized4.intValue(8);
                                                tLRPC$EncryptedChat.seq_in = queryFinalized4.intValue(9);
                                                tLRPC$EncryptedChat.seq_out = queryFinalized4.intValue(10);
                                                int intValue = queryFinalized4.intValue(11);
                                                tLRPC$EncryptedChat.key_use_count_in = (short) (intValue >> 16);
                                                tLRPC$EncryptedChat.key_use_count_out = (short) intValue;
                                                tLRPC$EncryptedChat.exchange_id = queryFinalized4.longValue(12);
                                                tLRPC$EncryptedChat.key_create_date = queryFinalized4.intValue(13);
                                                tLRPC$EncryptedChat.future_key_fingerprint = queryFinalized4.longValue(14);
                                                tLRPC$EncryptedChat.future_auth_key = queryFinalized4.byteArrayValue(15);
                                                tLRPC$EncryptedChat.key_hash = queryFinalized4.byteArrayValue(16);
                                                tLRPC$EncryptedChat.in_seq_no = queryFinalized4.intValue(17);
                                                long longValue2 = queryFinalized4.longValue(18);
                                                if (longValue2 != 0) {
                                                    tLRPC$EncryptedChat.admin_id = longValue2;
                                                }
                                                tLRPC$EncryptedChat.mtproto_seq = queryFinalized4.intValue(19);
                                                TLRPC$UserStatus tLRPC$UserStatus = tLRPC$User.status;
                                                if (tLRPC$UserStatus != null) {
                                                    tLRPC$UserStatus.expires = queryFinalized4.intValue(7);
                                                }
                                                if (c == 1) {
                                                    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(ContactsController.formatName(tLRPC$User.first_name, tLRPC$User.last_name));
                                                    dialogSearchResult4.name = spannableStringBuilder;
                                                    spannableStringBuilder.setSpan(new ForegroundColorSpan(Theme.getColor(Theme.key_chats_secretName)), 0, dialogSearchResult4.name.length(), 33);
                                                } else {
                                                    dialogSearchResult4.name = AndroidUtilities.generateSearchName("@" + UserObject.getPublicUsername(tLRPC$User), null, "@" + str22);
                                                }
                                                dialogSearchResult4.object = tLRPC$EncryptedChat;
                                                arrayList3.add(tLRPC$User);
                                                i3++;
                                            }
                                        } else {
                                            i17++;
                                            str21 = str21;
                                        }
                                    }
                                }
                                c = 1;
                                if (c == 0) {
                                }
                            }
                            str6 = str21;
                            longSparseArray5 = longSparseArray2;
                            longSparseArray2 = longSparseArray5;
                        }
                        longSparseArray3 = longSparseArray2;
                        queryFinalized4.dispose();
                    }
                    arrayList6 = new ArrayList(i3);
                    i6 = 0;
                    while (i6 < longSparseArray3.size()) {
                        LongSparseArray longSparseArray10 = longSparseArray3;
                        DialogsSearchAdapter.DialogSearchResult dialogSearchResult5 = (DialogsSearchAdapter.DialogSearchResult) longSparseArray10.valueAt(i6);
                        if (dialogSearchResult5.object != null && dialogSearchResult5.name != null) {
                            arrayList6.add(dialogSearchResult5);
                        }
                        i6++;
                        longSparseArray3 = longSparseArray10;
                    }
                    LongSparseArray longSparseArray11 = longSparseArray3;
                    Collections.sort(arrayList6, new Comparator() {
                        @Override
                        public final int compare(Object obj2, Object obj3) {
                            int lambda$localSearch$229;
                            lambda$localSearch$229 = MessagesStorage.lambda$localSearch$229((DialogsSearchAdapter.DialogSearchResult) obj2, (DialogsSearchAdapter.DialogSearchResult) obj3);
                            return lambda$localSearch$229;
                        }
                    });
                    i7 = 0;
                    while (i7 < arrayList6.size()) {
                        DialogsSearchAdapter.DialogSearchResult dialogSearchResult6 = (DialogsSearchAdapter.DialogSearchResult) arrayList6.get(i7);
                        arrayList.add(dialogSearchResult6.object);
                        String[] strArr3 = strArr2;
                        arrayList2.add(dialogSearchResult6.name);
                        i7++;
                        strArr2 = strArr3;
                    }
                    String[] strArr4 = strArr2;
                    if (i != 2 && i != 15 && i != 6 && i != 5) {
                        queryFinalized = getDatabase().queryFinalized("SELECT u.data, u.status, u.name, u.uid FROM users as u INNER JOIN contacts as c ON u.uid = c.uid", new Object[0]);
                        while (queryFinalized.next()) {
                            if (longSparseArray11.indexOfKey(queryFinalized.longValue(3)) < 0) {
                                String stringValue4 = queryFinalized.stringValue(2);
                                String translitString5 = LocaleController.getInstance().getTranslitString(stringValue4);
                                if (stringValue4.equals(translitString5)) {
                                    translitString5 = null;
                                }
                                String str23 = str6;
                                int lastIndexOf3 = stringValue4.lastIndexOf(str23);
                                String substring3 = lastIndexOf3 != -1 ? stringValue4.substring(lastIndexOf3 + 3) : null;
                                int i18 = 0;
                                boolean z2 = false;
                                while (true) {
                                    if (i18 >= i5) {
                                        longSparseArray4 = longSparseArray11;
                                        break;
                                    }
                                    String str24 = strArr4[i18];
                                    if (!stringValue4.startsWith(str24)) {
                                        if (!stringValue4.contains(" " + str24)) {
                                            if (translitString5 != null) {
                                                if (!translitString5.startsWith(str24)) {
                                                    if (translitString5.contains(" " + str24)) {
                                                    }
                                                }
                                            }
                                            r15 = (substring3 == null || !substring3.startsWith(str24)) ? z2 : 2;
                                            if (r15 == 0) {
                                                longSparseArray4 = longSparseArray11;
                                                NativeByteBuffer byteBufferValue5 = queryFinalized.byteBufferValue(0);
                                                if (byteBufferValue5 != null) {
                                                    TLRPC$User TLdeserialize3 = TLRPC$User.TLdeserialize(byteBufferValue5, byteBufferValue5.readInt32(false), false);
                                                    byteBufferValue5.reuse();
                                                    TLRPC$UserStatus tLRPC$UserStatus2 = TLdeserialize3.status;
                                                    if (tLRPC$UserStatus2 != null) {
                                                        tLRPC$UserStatus2.expires = queryFinalized.intValue(1);
                                                    }
                                                    if (r15 == 1) {
                                                        arrayList2.add(AndroidUtilities.generateSearchName(TLdeserialize3.first_name, TLdeserialize3.last_name, str24));
                                                        obj = null;
                                                    } else {
                                                        obj = null;
                                                        arrayList2.add(AndroidUtilities.generateSearchName("@" + UserObject.getPublicUsername(TLdeserialize3), null, "@" + str24));
                                                    }
                                                    arrayList.add(TLdeserialize3);
                                                }
                                            } else {
                                                i18++;
                                                z2 = r15;
                                                longSparseArray11 = longSparseArray11;
                                            }
                                        }
                                    }
                                    r15 = 1;
                                    if (r15 == 0) {
                                    }
                                }
                                str6 = str23;
                                longSparseArray11 = longSparseArray4;
                            }
                        }
                        queryFinalized.dispose();
                    }
                    return;
                }
                TLRPC$User currentUser = UserConfig.getInstance(messagesStorage.currentAccount).getCurrentUser();
                DialogsSearchAdapter.DialogSearchResult dialogSearchResult7 = new DialogsSearchAdapter.DialogSearchResult();
                dialogSearchResult7.date = ConnectionsManager.DEFAULT_DATACENTER_ID;
                dialogSearchResult7.name = lowerCase2;
                dialogSearchResult7.object = currentUser;
                longSparseArray9.put(currentUser.id, dialogSearchResult7);
                i3 = 1;
                if (i != 4) {
                    user = getMessagesController().getUser(708513L);
                    if (user == null) {
                    }
                    if (user != null) {
                    }
                    String str172 = ";;;";
                    if (arrayList8.isEmpty()) {
                    }
                    if (arrayList9.isEmpty()) {
                    }
                    if (arrayList5.isEmpty()) {
                    }
                    longSparseArray3 = longSparseArray2;
                    arrayList6 = new ArrayList(i3);
                    i6 = 0;
                    while (i6 < longSparseArray3.size()) {
                    }
                    LongSparseArray longSparseArray112 = longSparseArray3;
                    Collections.sort(arrayList6, new Comparator() {
                        @Override
                        public final int compare(Object obj2, Object obj3) {
                            int lambda$localSearch$229;
                            lambda$localSearch$229 = MessagesStorage.lambda$localSearch$229((DialogsSearchAdapter.DialogSearchResult) obj2, (DialogsSearchAdapter.DialogSearchResult) obj3);
                            return lambda$localSearch$229;
                        }
                    });
                    i7 = 0;
                    while (i7 < arrayList6.size()) {
                    }
                    String[] strArr42 = strArr2;
                    if (i != 2) {
                        queryFinalized = getDatabase().queryFinalized("SELECT u.data, u.status, u.name, u.uid FROM users as u INNER JOIN contacts as c ON u.uid = c.uid", new Object[0]);
                        while (queryFinalized.next()) {
                        }
                        queryFinalized.dispose();
                    }
                    return;
                }
                user = getMessagesController().getUser(708513L);
                if (user == null) {
                }
                if (user != null) {
                }
                String str1722 = ";;;";
                if (arrayList8.isEmpty()) {
                }
                if (arrayList9.isEmpty()) {
                }
                if (arrayList5.isEmpty()) {
                }
                longSparseArray3 = longSparseArray2;
                arrayList6 = new ArrayList(i3);
                i6 = 0;
                while (i6 < longSparseArray3.size()) {
                }
                LongSparseArray longSparseArray1122 = longSparseArray3;
                Collections.sort(arrayList6, new Comparator() {
                    @Override
                    public final int compare(Object obj2, Object obj3) {
                        int lambda$localSearch$229;
                        lambda$localSearch$229 = MessagesStorage.lambda$localSearch$229((DialogsSearchAdapter.DialogSearchResult) obj2, (DialogsSearchAdapter.DialogSearchResult) obj3);
                        return lambda$localSearch$229;
                    }
                });
                i7 = 0;
                while (i7 < arrayList6.size()) {
                }
                String[] strArr422 = strArr2;
                if (i != 2) {
                }
                return;
            } catch (Exception e4) {
                exc = e4;
                messagesStorage2 = messagesStorage;
                sQLiteCursor = null;
                messagesStorage2.checkSQLException(exc);
                if (sQLiteCursor == null) {
                    sQLiteCursor.dispose();
                    return;
                }
                return;
            } catch (Throwable th7) {
                th = th7;
                sQLiteCursor = null;
                if (sQLiteCursor != null) {
                }
            }
            messagesStorage = this;
        } catch (Exception e5) {
            messagesStorage2 = this;
            exc = e5;
            sQLiteCursor = null;
        } catch (Throwable th8) {
            th = th8;
            sQLiteCursor = null;
        }
    }

    public static int lambda$localSearch$229(DialogsSearchAdapter.DialogSearchResult dialogSearchResult, DialogsSearchAdapter.DialogSearchResult dialogSearchResult2) {
        int i = dialogSearchResult.date;
        int i2 = dialogSearchResult2.date;
        if (i < i2) {
            return 1;
        }
        return i > i2 ? -1 : 0;
    }

    public ArrayList<Integer> getCachedMessagesInRange(long j, int i, int i2) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        SQLiteCursor sQLiteCursor = null;
        try {
            try {
                sQLiteCursor = this.database.queryFinalized(String.format(Locale.US, "SELECT mid FROM messages_v2 WHERE uid = %d AND date >= %d AND date <= %d", Long.valueOf(j), Integer.valueOf(i), Integer.valueOf(i2)), new Object[0]);
                while (sQLiteCursor.next()) {
                    try {
                        arrayList.add(Integer.valueOf(sQLiteCursor.intValue(0)));
                    } catch (Exception e) {
                        checkSQLException(e);
                    }
                }
                sQLiteCursor.dispose();
            } catch (Exception e2) {
                checkSQLException(e2);
            }
            sQLiteCursor.dispose();
            return arrayList;
        } catch (Throwable th) {
            if (0 != 0) {
                sQLiteCursor.dispose();
            }
            throw th;
        }
    }

    public void updateUnreadReactionsCount(long j, long j2, int i) {
        updateUnreadReactionsCount(j, j2, i, false);
    }

    public void updateUnreadReactionsCount(final long j, final long j2, final int i, final boolean z) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateUnreadReactionsCount$230(j2, z, j, i);
            }
        });
    }

    public void lambda$updateUnreadReactionsCount$230(long j, boolean z, long j2, int i) {
        int intValue;
        SQLiteException e;
        SQLitePreparedStatement executeFast;
        SQLitePreparedStatement executeFast2;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        if (j != 0) {
            try {
                if (z) {
                    try {
                        SQLiteCursor queryFinalized = this.database.queryFinalized(String.format("SELECT unread_reactions FROM topics WHERE did = %d AND topic_id = %d", Long.valueOf(j2), Long.valueOf(j)), new Object[0]);
                        intValue = queryFinalized.next() ? queryFinalized.intValue(0) : 0;
                        queryFinalized.dispose();
                    } catch (SQLiteException e2) {
                        e = e2;
                        e.printStackTrace();
                        if (sQLitePreparedStatement == null) {
                            sQLitePreparedStatement.dispose();
                            return;
                        }
                        return;
                    }
                } else {
                    intValue = 0;
                }
                executeFast = this.database.executeFast("UPDATE topics SET unread_reactions = ? WHERE did = ? AND topic_id = ?");
            } catch (Throwable th) {
                th = th;
            }
            try {
                executeFast.bindInteger(1, Math.max(intValue + i, 0));
                executeFast.bindLong(2, j2);
                executeFast.bindLong(3, j);
                executeFast.step();
                executeFast.dispose();
                if (i == 0) {
                    sQLitePreparedStatement = this.database.executeFast("UPDATE reaction_mentions_topics SET state = 0 WHERE dialog_id = ? AND topic_id = ? ");
                    sQLitePreparedStatement.bindLong(1, j2);
                    sQLitePreparedStatement.bindLong(2, j);
                    sQLitePreparedStatement.step();
                    sQLitePreparedStatement.dispose();
                }
            } catch (SQLiteException e3) {
                e = e3;
                sQLitePreparedStatement = executeFast;
                e.printStackTrace();
                if (sQLitePreparedStatement == null) {
                }
            } catch (Throwable th2) {
                th = th2;
                sQLitePreparedStatement = executeFast;
                if (sQLitePreparedStatement != null) {
                    sQLitePreparedStatement.dispose();
                }
                throw th;
            }
        } else {
            try {
                try {
                    executeFast2 = this.database.executeFast("UPDATE dialogs SET unread_reactions = ? WHERE did = ?");
                } catch (SQLiteException e4) {
                    e = e4;
                }
            } catch (Throwable th3) {
                th = th3;
            }
            try {
                executeFast2.bindInteger(1, Math.max(i, 0));
                executeFast2.bindLong(2, j2);
                executeFast2.step();
                executeFast2.dispose();
                if (i == 0) {
                    sQLitePreparedStatement = this.database.executeFast("UPDATE reaction_mentions SET state = 0 WHERE dialog_id = ?");
                    sQLitePreparedStatement.bindLong(1, j2);
                    sQLitePreparedStatement.step();
                    sQLitePreparedStatement.dispose();
                }
            } catch (SQLiteException e5) {
                sQLitePreparedStatement = executeFast2;
                e = e5;
                e.printStackTrace();
                if (sQLitePreparedStatement != null) {
                    sQLitePreparedStatement.dispose();
                }
            } catch (Throwable th4) {
                sQLitePreparedStatement = executeFast2;
                th = th4;
                if (sQLitePreparedStatement != null) {
                    sQLitePreparedStatement.dispose();
                }
                throw th;
            }
        }
    }

    public void markMessageReactionsAsRead(final long j, final long j2, final int i, boolean z) {
        if (z) {
            getStorageQueue().postRunnable(new Runnable() {
                @Override
                public final void run() {
                    MessagesStorage.this.lambda$markMessageReactionsAsRead$231(j, j2, i);
                }
            });
        } else {
            lambda$markMessageReactionsAsRead$231(j, j2, i);
        }
    }

    public void lambda$markMessageReactionsAsRead$231(long j, long j2, int i) {
        SQLiteCursor queryFinalized;
        TLRPC$Message tLRPC$Message;
        SQLitePreparedStatement executeFast;
        NativeByteBuffer byteBufferValue;
        MessagesStorage messagesStorage = this;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        SQLitePreparedStatement sQLitePreparedStatement2 = null;
        int i2 = 0;
        while (i2 < 2) {
            boolean z = i2 == 1;
            if (!z || j2 != 0) {
                if (!z) {
                    try {
                        sQLitePreparedStatement2 = getMessagesStorage().getDatabase().executeFast("UPDATE reaction_mentions SET state = 0 WHERE message_id = ? AND dialog_id = ?");
                        sQLitePreparedStatement2.bindInteger(1, i);
                        sQLitePreparedStatement2.bindLong(2, j);
                        sQLitePreparedStatement2.step();
                        sQLitePreparedStatement2.dispose();
                    } catch (SQLiteException e) {
                        e = e;
                        SQLitePreparedStatement sQLitePreparedStatement3 = sQLitePreparedStatement2;
                        queryFinalized = null;
                        sQLitePreparedStatement = sQLitePreparedStatement3;
                        try {
                            messagesStorage.checkSQLException(e);
                            if (sQLitePreparedStatement != null) {
                            }
                            if (queryFinalized != null) {
                            }
                        } catch (Throwable th) {
                            th = th;
                            if (sQLitePreparedStatement != null) {
                            }
                            if (queryFinalized != null) {
                            }
                            throw th;
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        SQLitePreparedStatement sQLitePreparedStatement4 = sQLitePreparedStatement2;
                        queryFinalized = null;
                        sQLitePreparedStatement = sQLitePreparedStatement4;
                        if (sQLitePreparedStatement != null) {
                        }
                        if (queryFinalized != null) {
                        }
                        throw th;
                    }
                    try {
                        queryFinalized = messagesStorage.database.queryFinalized(String.format(Locale.US, "SELECT data FROM messages_v2 WHERE uid = %d AND mid = %d", Long.valueOf(j), Integer.valueOf(i)), new Object[0]);
                    } catch (SQLiteException e2) {
                        e = e2;
                        queryFinalized = null;
                        messagesStorage.checkSQLException(e);
                        if (sQLitePreparedStatement != null) {
                        }
                        if (queryFinalized != null) {
                        }
                    } catch (Throwable th3) {
                        th = th3;
                        queryFinalized = null;
                        if (sQLitePreparedStatement != null) {
                        }
                        if (queryFinalized != null) {
                        }
                        throw th;
                    }
                } else {
                    SQLitePreparedStatement executeFast2 = getMessagesStorage().getDatabase().executeFast("UPDATE reaction_mentions_topics SET state = 0 WHERE message_id = ? AND dialog_id = ? AND topic_id = ? ");
                    executeFast2.bindInteger(1, i);
                    executeFast2.bindLong(2, j);
                    executeFast2.bindLong(3, j2);
                    executeFast2.step();
                    executeFast2.dispose();
                    queryFinalized = messagesStorage.database.queryFinalized(String.format(Locale.US, "SELECT data FROM messages_topics WHERE uid = %d AND mid = %d", Long.valueOf(j), Integer.valueOf(i)), new Object[0]);
                }
                try {
                    if (!queryFinalized.next() || (byteBufferValue = queryFinalized.byteBufferValue(0)) == null) {
                        tLRPC$Message = null;
                    } else {
                        tLRPC$Message = TLRPC$Message.TLdeserialize(byteBufferValue, byteBufferValue.readInt32(false), false);
                        tLRPC$Message.readAttachPath(byteBufferValue, getUserConfig().clientUserId);
                        byteBufferValue.reuse();
                        TLRPC$TL_messageReactions tLRPC$TL_messageReactions = tLRPC$Message.reactions;
                        if (tLRPC$TL_messageReactions != null && tLRPC$TL_messageReactions.recent_reactions != null) {
                            for (int i3 = 0; i3 < tLRPC$Message.reactions.recent_reactions.size(); i3++) {
                                tLRPC$Message.reactions.recent_reactions.get(i3).unread = false;
                            }
                        }
                    }
                    queryFinalized.dispose();
                    if (tLRPC$Message != null) {
                        if (!z) {
                            try {
                                executeFast = getMessagesStorage().getDatabase().executeFast(String.format(Locale.US, "UPDATE messages_v2 SET data = ? WHERE uid = %d AND mid = %d", Long.valueOf(j), Integer.valueOf(i)));
                            } catch (SQLiteException e3) {
                                e = e3;
                                messagesStorage = this;
                                queryFinalized = null;
                                messagesStorage.checkSQLException(e);
                                if (sQLitePreparedStatement != null) {
                                }
                                if (queryFinalized != null) {
                                }
                            } catch (Throwable th4) {
                                th = th4;
                                queryFinalized = null;
                                if (sQLitePreparedStatement != null) {
                                }
                                if (queryFinalized != null) {
                                }
                                throw th;
                            }
                        } else {
                            executeFast = getMessagesStorage().getDatabase().executeFast(String.format(Locale.US, "UPDATE messages_topics SET data = ? WHERE uid = %d AND mid = %d", Long.valueOf(j), Integer.valueOf(i)));
                        }
                        sQLitePreparedStatement2 = executeFast;
                        try {
                            try {
                                MessageObject.normalizeFlags(tLRPC$Message);
                                NativeByteBuffer nativeByteBuffer = new NativeByteBuffer(tLRPC$Message.getObjectSize());
                                tLRPC$Message.serializeToStream(nativeByteBuffer);
                                sQLitePreparedStatement2.bindByteBuffer(1, nativeByteBuffer);
                                sQLitePreparedStatement2.step();
                                sQLitePreparedStatement2.dispose();
                                nativeByteBuffer.reuse();
                            } catch (Throwable th5) {
                                th = th5;
                                SQLitePreparedStatement sQLitePreparedStatement42 = sQLitePreparedStatement2;
                                queryFinalized = null;
                                sQLitePreparedStatement = sQLitePreparedStatement42;
                                if (sQLitePreparedStatement != null) {
                                    sQLitePreparedStatement.dispose();
                                }
                                if (queryFinalized != null) {
                                    queryFinalized.dispose();
                                }
                                throw th;
                            }
                        } catch (Exception e4) {
                            try {
                                FileLog.e(e4);
                            } catch (SQLiteException e5) {
                                e = e5;
                                messagesStorage = this;
                                SQLitePreparedStatement sQLitePreparedStatement32 = sQLitePreparedStatement2;
                                queryFinalized = null;
                                sQLitePreparedStatement = sQLitePreparedStatement32;
                                messagesStorage.checkSQLException(e);
                                if (sQLitePreparedStatement != null) {
                                    sQLitePreparedStatement.dispose();
                                }
                                if (queryFinalized != null) {
                                    queryFinalized.dispose();
                                    return;
                                }
                                return;
                            }
                        }
                    } else {
                        sQLitePreparedStatement2 = null;
                    }
                } catch (SQLiteException e6) {
                    e = e6;
                    messagesStorage = this;
                    messagesStorage.checkSQLException(e);
                    if (sQLitePreparedStatement != null) {
                    }
                    if (queryFinalized != null) {
                    }
                } catch (Throwable th6) {
                    th = th6;
                    if (sQLitePreparedStatement != null) {
                    }
                    if (queryFinalized != null) {
                    }
                    throw th;
                }
            }
            i2++;
            messagesStorage = this;
        }
        if (sQLitePreparedStatement2 != null) {
            sQLitePreparedStatement2.dispose();
        }
    }

    public void updateDialogUnreadReactions(final long j, final long j2, final int i, final boolean z) {
        this.storageQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                MessagesStorage.this.lambda$updateDialogUnreadReactions$232(z, j, i, j2);
            }
        });
    }

    public void lambda$updateDialogUnreadReactions$232(boolean z, long j, int i, long j2) {
        SQLiteCursor queryFinalized;
        int max;
        SQLitePreparedStatement sQLitePreparedStatement = null;
        if (z) {
            try {
                queryFinalized = this.database.queryFinalized("SELECT unread_reactions FROM dialogs WHERE did = " + j, new Object[0]);
                try {
                    try {
                        max = queryFinalized.next() ? Math.max(0, queryFinalized.intValue(0)) : 0;
                        queryFinalized.dispose();
                    } catch (SQLiteException e) {
                        e = e;
                        e.printStackTrace();
                        if (sQLitePreparedStatement != null) {
                            sQLitePreparedStatement.dispose();
                        }
                        if (queryFinalized != null) {
                            queryFinalized.dispose();
                            return;
                        }
                        return;
                    }
                } catch (Throwable th) {
                    th = th;
                    if (sQLitePreparedStatement != null) {
                        sQLitePreparedStatement.dispose();
                    }
                    if (queryFinalized != null) {
                        queryFinalized.dispose();
                    }
                    throw th;
                }
            } catch (SQLiteException e2) {
                e = e2;
                queryFinalized = null;
            } catch (Throwable th2) {
                th = th2;
                queryFinalized = null;
                if (sQLitePreparedStatement != null) {
                }
                if (queryFinalized != null) {
                }
                throw th;
            }
        } else {
            max = 0;
        }
        int i2 = max + i;
        SQLitePreparedStatement executeFast = getMessagesStorage().getDatabase().executeFast("UPDATE dialogs SET unread_reactions = ? WHERE did = ?");
        try {
            executeFast.bindInteger(1, i2);
            executeFast.bindLong(2, j);
            executeFast.step();
            executeFast.dispose();
            if (j2 == 0) {
                return;
            }
            if (z) {
                SQLiteCursor queryFinalized2 = this.database.queryFinalized(String.format(Locale.US, "SELECT unread_reactions FROM topics WHERE did = %d AND topic_id = %d", Long.valueOf(j), Long.valueOf(j2)), new Object[0]);
                try {
                    r1 = queryFinalized2.next() ? Math.max(0, queryFinalized2.intValue(0)) : 0;
                    queryFinalized2.dispose();
                } catch (SQLiteException e3) {
                    e = e3;
                    queryFinalized = queryFinalized2;
                    e = e;
                    e.printStackTrace();
                    if (sQLitePreparedStatement != null) {
                    }
                    if (queryFinalized != null) {
                    }
                } catch (Throwable th3) {
                    th = th3;
                    queryFinalized = queryFinalized2;
                    th = th;
                    if (sQLitePreparedStatement != null) {
                    }
                    if (queryFinalized != null) {
                    }
                    throw th;
                }
            }
            int i3 = r1 + i;
            SQLitePreparedStatement executeFast2 = getMessagesStorage().getDatabase().executeFast("UPDATE topics SET unread_reactions = ? WHERE did = ? AND topic_id = ?");
            try {
                executeFast2.bindInteger(1, i3);
                executeFast2.bindLong(2, j);
                executeFast2.bindLong(3, j2);
                executeFast2.step();
                executeFast2.dispose();
            } catch (SQLiteException e4) {
                e = e4;
                queryFinalized = null;
                sQLitePreparedStatement = executeFast2;
                e = e;
                e.printStackTrace();
                if (sQLitePreparedStatement != null) {
                }
                if (queryFinalized != null) {
                }
            } catch (Throwable th4) {
                th = th4;
                queryFinalized = null;
                sQLitePreparedStatement = executeFast2;
                th = th;
                if (sQLitePreparedStatement != null) {
                }
                if (queryFinalized != null) {
                }
                throw th;
            }
        } catch (SQLiteException e5) {
            e = e5;
            queryFinalized = null;
            sQLitePreparedStatement = executeFast;
        } catch (Throwable th5) {
            th = th5;
            queryFinalized = null;
            sQLitePreparedStatement = executeFast;
        }
    }

    private boolean isForum(long j) {
        int i = this.dialogIsForum.get(j, -1);
        if (i == -1) {
            TLRPC$Chat chat = getChat(-j);
            i = (chat == null || !chat.forum) ? 0 : 1;
            this.dialogIsForum.put(j, i);
        }
        return i == 1;
    }

    public static class TopicKey {
        public long dialogId;
        public long topicId;

        public static TopicKey of(long j, long j2) {
            TopicKey topicKey = new TopicKey();
            topicKey.dialogId = j;
            topicKey.topicId = j2;
            return topicKey;
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null || getClass() != obj.getClass()) {
                return false;
            }
            TopicKey topicKey = (TopicKey) obj;
            return this.dialogId == topicKey.dialogId && this.topicId == topicKey.topicId;
        }

        public int hashCode() {
            return Objects.hash(Long.valueOf(this.dialogId), Long.valueOf(this.topicId));
        }

        public String toString() {
            return "TopicKey{dialogId=" + this.dialogId + ", topicId=" + this.topicId + '}';
        }
    }
}