/*
 * Copyright (c) NoticeDog 2017.
 * GNU LESSER GENERAL PUBLIC LICENSE
 *     Version 3, 29 June 2007
 *
 * Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
 * Everyone is permitted to copy and distribute verbatim copies
 * of this license document, but changing it is not allowed.
 *
 *
 * This version of the GNU Lesser General Public License incorporates
 * the terms and conditions of version 3 of the GNU General Public
 * License, supplemented by the additional permissions listed below.
 *
 * 0. Additional Definitions.
 *
 * As used herein, "this License" refers to version 3 of the GNU Lesser
 * General Public License, and the "GNU GPL" refers to version 3 of the GNU
 * General Public License.
 *
 * "The Library" refers to a covered work governed by this License,
 * other than an Application or a Combined Work as defined below.
 *
 * An "Application" is any work that makes use of an interface provided
 * by the Library, but which is not otherwise based on the Library.
 * Defining a subclass of a class defined by the Library is deemed a mode
 * of using an interface provided by the Library.
 *
 * A "Combined Work" is a work produced by combining or linking an
 * Application with the Library.  The particular version of the Library
 * with which the Combined Work was made is also called the "Linked
 * Version".
 *
 * The "Minimal Corresponding Source" for a Combined Work means the
 * Corresponding Source for the Combined Work, excluding any source code
 * for portions of the Combined Work that, considered in isolation, are
 * based on the Application, and not on the Linked Version.
 *
 * The "Corresponding Application Code" for a Combined Work means the
 * object code and/or source code for the Application, including any data
 * and utility programs needed for reproducing the Combined Work from the
 * Application, but excluding the System Libraries of the Combined Work.
 *
 * 1. Exception to Section 3 of the GNU GPL.
 *
 * You may convey a covered work under sections 3 and 4 of this License
 * without being bound by section 3 of the GNU GPL.
 *
 * 2. Conveying Modified Versions.
 *
 * If you modify a copy of the Library, and, in your modifications, a
 * facility refers to a function or data to be supplied by an Application
 * that uses the facility (other than as an argument passed when the
 * facility is invoked), then you may convey a copy of the modified
 * version:
 *
 * a) under this License, provided that you make a good faith effort to
 * ensure that, in the event an Application does not supply the
 * function or data, the facility still operates, and performs
 * whatever part of its purpose remains meaningful, or
 *
 * b) under the GNU GPL, with none of the additional permissions of
 * this License applicable to that copy.
 *
 * 3. Object Code Incorporating Material from Library Header Files.
 *
 * The object code form of an Application may incorporate material from
 * a header file that is part of the Library.  You may convey such object
 * code under terms of your choice, provided that, if the incorporated
 * material is not limited to numerical parameters, data structure
 * layouts and accessors, or small macros, inline functions and templates
 * (ten or fewer lines in length), you do both of the following:
 *
 * a) Give prominent notice with each copy of the object code that the
 * Library is used in it and that the Library and its use are
 * covered by this License.
 *
 * b) Accompany the object code with a copy of the GNU GPL and this license
 * document.
 *
 * 4. Combined Works.
 *
 * You may convey a Combined Work under terms of your choice that,
 * taken together, effectively do not restrict modification of the
 * portions of the Library contained in the Combined Work and reverse
 * engineering for debugging such modifications, if you also do each of
 * the following:
 *
 * a) Give prominent notice with each copy of the Combined Work that
 * the Library is used in it and that the Library and its use are
 * covered by this License.
 *
 * b) Accompany the Combined Work with a copy of the GNU GPL and this license
 * document.
 *
 * c) For a Combined Work that displays copyright notices during
 * execution, include the copyright notice for the Library among
 * these notices, as well as a reference directing the user to the
 * copies of the GNU GPL and this license document.
 *
 * d) Do one of the following:
 *
 * 0) Convey the Minimal Corresponding Source under the terms of this
 * License, and the Corresponding Application Code in a form
 * suitable for, and under terms that permit, the user to
 * recombine or relink the Application with a modified version of
 * the Linked Version to produce a modified Combined Work, in the
 * manner specified by section 6 of the GNU GPL for conveying
 * Corresponding Source.
 *
 * 1) Use a suitable shared library mechanism for linking with the
 * Library.  A suitable mechanism is one that (a) uses at run time
 * a copy of the Library already present on the user's computer
 * system, and (b) will operate properly with a modified version
 * of the Library that is interface-compatible with the Linked
 * Version.
 *
 * e) Provide Installation Information, but only if you would otherwise
 * be required to provide such information under section 6 of the
 * GNU GPL, and only to the extent that such information is
 * necessary to install and execute a modified version of the
 * Combined Work produced by recombining or relinking the
 * Application with a modified version of the Linked Version. (If
 * you use option 4d0, the Installation Information must accompany
 * the Minimal Corresponding Source and Corresponding Application
 * Code. If you use option 4d1, you must provide the Installation
 * Information in the manner specified by section 6 of the GNU GPL
 * for conveying Corresponding Source.)
 *
 * 5. Combined Libraries.
 *
 * You may place library facilities that are a work based on the
 * Library side by side in a single library together with other library
 * facilities that are not Applications and are not covered by this
 * License, and convey such a combined library under terms of your
 * choice, if you do both of the following:
 *
 * a) Accompany the combined library with a copy of the same work based
 * on the Library, uncombined with any other library facilities,
 * conveyed under the terms of this License.
 *
 * b) Give prominent notice with the combined library that part of it
 * is a work based on the Library, and explaining where to find the
 * accompanying uncombined form of the same work.
 *
 * 6. Revised Versions of the GNU Lesser General Public License.
 *
 * The Free Software Foundation may publish revised and/or new versions
 * of the GNU Lesser General Public License from time to time. Such new
 * versions will be similar in spirit to the present version, but may
 * differ in detail to address new problems or concerns.
 *
 * Each version is given a distinguishing version number. If the
 * Library as you received it specifies that a certain numbered version
 * of the GNU Lesser General Public License "or any later version"
 * applies to it, you have the option of following the terms and
 * conditions either of that published version or of any later version
 * published by the Free Software Foundation. If the Library as you
 * received it does not specify a version number of the GNU Lesser
 * General Public License, you may choose any version of the GNU Lesser
 * General Public License ever published by the Free Software Foundation.
 *
 * If the Library as you received it specifies that a proxy can decide
 * whether future versions of the GNU Lesser General Public License shall
 * apply, that proxy's public statement of acceptance of any version is
 * permanent authorization for you to choose that version for the
 * Library.
 */

package io.bunnyblue.noticedog.app.inbox;

import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.util.Log;
import android.util.LruCache;

import java.util.ArrayList;
import java.util.List;

import io.bunnyblue.noticedog.app.notifications.Action;

public class InboxPersistence {
    private static final int ACTIONS_CACHE_SIZE = 10;
    private static final int PENDING_INTENT_CACHE_SIZE = 50;
    private static final String TAG = "InboxPersistence";
    LruCache<Integer, List<Action>> actionsCache;
    LruCache<Integer, PendingIntent> pendingLaunchIntentCache;
    private SQLiteDatabase database;
    private InboxSQLiteHelper dbHelper;

    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    public InboxPersistence(Context context) {
        this.dbHelper = new InboxSQLiteHelper(context);
        this.pendingLaunchIntentCache = new LruCache<Integer, PendingIntent>(PENDING_INTENT_CACHE_SIZE);
        this.actionsCache = new LruCache<Integer, List<Action>>(ACTIONS_CACHE_SIZE);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    public int saveMessage(RawMessage message) {
        int v3;
        boolean v2 = false;
        int v10 = -1;
        if (message.getState() == 0) {
            v2 = true;
            // goto label_5;
        }

        try {
            this.database.beginTransaction();
            int v4 = this.getUserId(message.getAppId(), message.getSenderAppSpecificUserId(), message
                    .getSenderDisplayName());
            if (v4 == v10) {
                v4 = this.insertUser(message.getAppId(), message.getSenderAppSpecificUserId(), message
                        .getSenderDisplayName());
            }

            if (v4 == v10) {
                throw new SQLException("Failed to find or create user");
            }

            v3 = this.insertMessage(v4, message);
            if (v3 == v10) {
                throw new SQLException("Failed to create message");
            }

            int v0 = this.getConversationId(v4);
            if (v0 == v10) {
                if (this.insertConversation(message.getAppId(), v4, v3, v2) != v10) {
                    this.database.setTransactionSuccessful();
                    this.updatePendingIntent(v4, message.getPendingLaunchIntent());
                    if (message.getActions() == null) {
                        this.database.endTransaction();
                        return v3;
                    }

                    this.actionsCache.put(Integer.valueOf(v3), message.getActions());
                } else {
                    throw new SQLException("Failed to insert conversation");
                }

            } else {
                this.database.setTransactionSuccessful();
            }

            int v1 = 0;
            if (v2) {
                v1 = 1;
            }

            if (!this.updateConversation(v0, v3, v1)) {
                throw new SQLException("Failed to update conversation");
            }

        } catch (SQLException v5) {
            v5.printStackTrace();

            v3 = -1;
            this.database.endTransaction();
            return v3;
        }
        // this.database.setTransactionSuccessful();
        this.database.endTransaction();
        return v3;
    }

    public void open() throws SQLException {
        this.database = this.dbHelper.getWritableDatabase();
    }

    public void close() {
        this.dbHelper.close();
    }

    public Message getMessage(int messageId) {
        Message message = null;
        Cursor cursor = this.database.query(InboxSQLiteHelper.VIEW_MESSAGES, null, "_message_id = " + messageId, null,
                null, null, null);
        if (cursor.moveToFirst()) {
            message = getMessageFromViewCursor(cursor);
        }
        cursor.close();
        return message;
    }

    public List<Message> getUnreadMessages(int limit) {
        List<Message> messages = new ArrayList<Message>();
        Cursor cursor = this.database.query(InboxSQLiteHelper.VIEW_MESSAGES, null, "_state = 0", null, null, null,
                "_message_id DESC", Integer.toString(limit));
        while (cursor.moveToNext()) {
            Message message = getMessageFromViewCursor(cursor);
            if (message != null) {
                messages.add(message);
            }
        }
        cursor.close();
        return messages;
    }

    public boolean deleteConversation(Conversation conversation) {
        boolean success;
        try {
            this.database.beginTransaction();
            if (this.database.delete(InboxSQLiteHelper.TABLE_MESSAGES, "_sender_user_id="
                    + conversation.getSender().getId(), null) == 0) {
                throw new SQLException("Failed to delete message from DB");
            } else if (this.database.delete(InboxSQLiteHelper.TABLE_CONVERSATIONS, "_sender_user_id="
                    + conversation.getSender().getId(), null) != 1) {
                throw new SQLException("Failed to delete message from DB");
            } else {
                this.database.setTransactionSuccessful();
                success = true;
                return success;
            }
        } catch (SQLException e) {
            Log.d(TAG, "Error trying to delete a message");
            success = false;
        } finally {
            this.database.endTransaction();
        }
        return success;
    }

    public boolean deleteMessage(Message message) {
        boolean success;
        try {
            this.database.beginTransaction();
            if (this.database.delete(InboxSQLiteHelper.TABLE_MESSAGES, "_message_id=" + message.getId(), null) != 1) {
                throw new SQLException("Failed to delete message from DB");
            }
            int conversationId = getConversationId(message.getSenderUserId());
            if (conversationId == -1) {
                throw new SQLException(
                        "Failed while deleting message because we couldn't find a matching conversation id");
            } else if (updateConversationAfterMessageDelete(conversationId, message.getSenderUserId())) {
                this.database.setTransactionSuccessful();
                success = true;
                return success;
            } else {
                throw new SQLException("Failed while deleting message because we couldn't update the conversation");
            }
        } catch (SQLException e) {
            Log.d(TAG, "Error trying to delete a message");
            success = false;
        } finally {
            this.database.endTransaction();
        }

        return success;
    }

    public boolean deleteAllMessages() {
        boolean success;
        try {
            this.database.beginTransaction();
            this.database.delete(InboxSQLiteHelper.TABLE_CONVERSATIONS, null, null);
            this.database.delete(InboxSQLiteHelper.TABLE_MESSAGES, null, null);
            this.database.setTransactionSuccessful();
            success = true;
        } catch (SQLException e) {
            Log.d(TAG, "Error trying to clear inbox");
            success = false;
        } finally {
            this.database.endTransaction();
        }
        return success;
    }

    public boolean markAppAsRead(String appId) {
        boolean success;
        try {
            this.database.beginTransaction();
            ContentValues values = new ContentValues();
            values.put(InboxSQLiteHelper.COLUMN_MESSAGES_STATE, Integer.valueOf(1));
            this.database
                    .update(InboxSQLiteHelper.TABLE_MESSAGES, values, "_state=0 and _app_id='" + appId + "'", null);
            values = new ContentValues();
            values.put(InboxSQLiteHelper.COLUMN_CONVERSATIONS_NUM_UNREAD_MESSAGES, Integer.valueOf(0));
            this.database.update(InboxSQLiteHelper.TABLE_CONVERSATIONS, values, "_app_id='" + appId + "'", null);
            this.database.setTransactionSuccessful();
            success = true;
        } catch (SQLException e) {
            Log.d(TAG, "Error trying to mark all messages as read");
            success = false;
        } finally {
            this.database.endTransaction();
        }
        return success;
    }

    public boolean markMessageAsRead(Message message) {
        boolean updateConversationCount;
        try {
            this.database.beginTransaction();
            int conversationId = getConversationId(message.getSenderUserId());
            if (conversationId == -1) {
                throw new Exception("Could not find conversation for message with id=" + message.getId());
            }
            updateConversationCount = updateConversationCount(conversationId, -1);
            if (updateConversationCount) {
                ContentValues values = new ContentValues();
                values.put(InboxSQLiteHelper.COLUMN_MESSAGES_STATE, Integer.valueOf(1));
                this.database.update(InboxSQLiteHelper.TABLE_MESSAGES, values, "_message_id=" + message.getId(), null);
                this.database.setTransactionSuccessful();
                updateConversationCount = true;
                return updateConversationCount;
            }
            throw new Exception("Failed to decrement unread count for message with id=" + message.getId()
                    + " and conversation with id=" + conversationId);
        } catch (SQLException e) {
            Log.d(TAG, "Error trying to mark all messages as read");
            updateConversationCount = false;
        } catch (Exception e2) {
            Log.d(TAG, "Error trying to mark all messages as read");
            updateConversationCount = false;
        } finally {
            this.database.endTransaction();
        }
        return updateConversationCount;
    }

    public boolean markConversationAsRead(Conversation conversation) {
        boolean success;
        try {
            this.database.beginTransaction();
            ContentValues values = new ContentValues();
            values.put(InboxSQLiteHelper.COLUMN_CONVERSATIONS_NUM_UNREAD_MESSAGES, Integer.valueOf(0));
            this.database.update(InboxSQLiteHelper.TABLE_CONVERSATIONS, values,
                    "_conversation_id=" + conversation.getId(), null);
            values = new ContentValues();
            values.put(InboxSQLiteHelper.COLUMN_MESSAGES_STATE, Integer.valueOf(1));
            this.database.update(InboxSQLiteHelper.TABLE_MESSAGES, values, "_sender_user_id="
                            + conversation.getSender().getId() + " and " + InboxSQLiteHelper.COLUMN_MESSAGES_STATE + "=" + 0,
                    null);
            this.database.setTransactionSuccessful();
            success = true;
        } catch (SQLException e) {
            Log.d(TAG, "Error trying to mark conversation as read");
            success = false;
        } catch (Exception e2) {
            Log.d(TAG, "Error trying to mark conversation as read");
            success = false;
        } finally {
            this.database.endTransaction();
        }
        return success;
    }

    public boolean markAllMessageAsRead() {
        boolean success;
        try {
            this.database.beginTransaction();
            ContentValues values = new ContentValues();
            values.put(InboxSQLiteHelper.COLUMN_MESSAGES_STATE, Integer.valueOf(1));
            this.database.update(InboxSQLiteHelper.TABLE_MESSAGES, values, "_state=0", null);
            values = new ContentValues();
            values.put(InboxSQLiteHelper.COLUMN_CONVERSATIONS_NUM_UNREAD_MESSAGES, Integer.valueOf(0));
            this.database.update(InboxSQLiteHelper.TABLE_CONVERSATIONS, values, null, null);
            this.database.setTransactionSuccessful();
            success = true;
        } catch (SQLException e) {
            Log.d(TAG, "Error trying to mark all messages as read");
            success = false;
        } finally {
            this.database.endTransaction();
        }
        return success;
    }

    public int getLatestMessageId() {
        Cursor cursor = this.database.query(InboxSQLiteHelper.TABLE_MESSAGES,
                new String[]{InboxSQLiteHelper.COLUMN_MESSAGES_ID}, null, null, null, null, "_message_id DESC", "1");
        return cursor.moveToFirst() ? cursor.getInt(cursor.getColumnIndex(InboxSQLiteHelper.COLUMN_MESSAGES_ID)) : -1;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    public void updatePendingIntent(int senderUserId, PendingIntent pendingIntent) {
        if (senderUserId != -1 && pendingIntent != null) {
            this.pendingLaunchIntentCache.put(Integer.valueOf(senderUserId), pendingIntent);
        }
    }

    private int getUserId(String appId, String appSpecificUserId, String displayName) {
        String[] selectionArgs;
        String where;
        int userId = -1;
        String[] columns = new String[]{InboxSQLiteHelper.COLUMN_USERS_ID};
        if (appSpecificUserId == null) {
            selectionArgs = new String[]{appId, displayName};
            where = "_app_id=? and _display_name=?";
        } else {
            selectionArgs = new String[]{appId, displayName, appSpecificUserId};
            where = "_app_id=? and _display_name=? and _appspecific_user_id=?";
        }
        Cursor cursor = this.database.query(InboxSQLiteHelper.TABLE_USERS, columns, where, selectionArgs, null, null,
                null);
        if (cursor.moveToFirst()) {
            userId = cursor.getInt(cursor.getColumnIndex(InboxSQLiteHelper.COLUMN_USERS_ID));
        }
        cursor.close();
        return userId;
    }

    private int insertUser(String appId, String appSpecificUserId, String displayName) {
        ContentValues values = new ContentValues();
        values.put(InboxSQLiteHelper.COLUMN_USERS_APP_ID, appId);
        values.put(InboxSQLiteHelper.COLUMN_USERS_APPSPECIFIC_USER_ID, appSpecificUserId);
        values.put(InboxSQLiteHelper.COLUMN_USERS_DISPLAY_NAME, displayName);
        values.put(InboxSQLiteHelper.COLUMN_USERS_TYPE, Integer.valueOf(0));
        long rowId = this.database.insert(InboxSQLiteHelper.TABLE_USERS, null, values);
        return rowId != -1 ? (int) rowId : -1;
    }

    private int insertMessage(int senderUserId, RawMessage message) {
        ContentValues values = new ContentValues();
        values.put(InboxSQLiteHelper.COLUMN_MESSAGES_SENDER_USER_ID, Integer.valueOf(senderUserId));
        values.put(InboxSQLiteHelper.COLUMN_USERS_APP_ID, message.getAppId());
        values.put(InboxSQLiteHelper.COLUMN_MESSAGES_BODY, message.getBody());
        values.put(InboxSQLiteHelper.COLUMN_MESSAGES_STATE, Integer.valueOf(0));
        values.put(InboxSQLiteHelper.COLUMN_MESSAGES_TIMESTAMP, Long.valueOf(message.getTimestamp()));
        long rowId = this.database.insert(InboxSQLiteHelper.TABLE_MESSAGES, null, values);
        return rowId != -1 ? (int) rowId : -1;
    }

    private int getConversationId(int senderUserId) {
        int conversationId = -1;
        Cursor cursor = this.database.query(InboxSQLiteHelper.TABLE_CONVERSATIONS,
                new String[]{InboxSQLiteHelper.COLUMN_CONVERSATIONS_ID}, "_sender_user_id=" + senderUserId, null,
                null, null, null);
        if (cursor.moveToFirst()) {
            conversationId = cursor.getInt(cursor.getColumnIndex(InboxSQLiteHelper.COLUMN_CONVERSATIONS_ID));
        }
        cursor.close();
        return conversationId;
    }

    private int insertConversation(String appId, int senderUserId, int lastMessageId, boolean isUnread) {
        int numUnread;
        if (isUnread) {
            numUnread = 1;
        } else {
            numUnread = 0;
        }
        ContentValues values = new ContentValues();
        values.put(InboxSQLiteHelper.COLUMN_USERS_APP_ID, appId);
        values.put(InboxSQLiteHelper.COLUMN_MESSAGES_SENDER_USER_ID, Integer.valueOf(senderUserId));
        values.put(InboxSQLiteHelper.COLUMN_CONVERSATIONS_LAST_MESSAGE_ID, Integer.valueOf(lastMessageId));
        values.put(InboxSQLiteHelper.COLUMN_CONVERSATIONS_NUM_UNREAD_MESSAGES, Integer.valueOf(numUnread));
        long rowId = this.database.insert(InboxSQLiteHelper.TABLE_CONVERSATIONS, null, values);
        return rowId != -1 ? (int) rowId : -1;
    }

    private boolean deleteConversation(int conversationId) {
        return this.database.delete(InboxSQLiteHelper.TABLE_CONVERSATIONS,
                new StringBuilder().append("_conversation_id=").append(conversationId).toString(), null) == 1;
    }

    private boolean updateConversationCount(int conversationId, int unreadCountIncrement) {
        Cursor cursor = this.database.rawQuery("UPDATE conversations SET _num_unread_messages=_num_unread_messages+"
                        + unreadCountIncrement + " WHERE " + InboxSQLiteHelper.COLUMN_CONVERSATIONS_ID + "=" + conversationId,
                null);
        cursor.moveToFirst();
        cursor.close();
        return true;
    }

    private boolean updateConversation(int conversationId, int lastMessageId, int unreadCountIncrement) {
        Cursor cursor = this.database.rawQuery("UPDATE conversations SET _last_message_id=" + lastMessageId + ", "
                + InboxSQLiteHelper.COLUMN_CONVERSATIONS_NUM_UNREAD_MESSAGES + "="
                + InboxSQLiteHelper.COLUMN_CONVERSATIONS_NUM_UNREAD_MESSAGES + "+" + unreadCountIncrement + " WHERE "
                + InboxSQLiteHelper.COLUMN_CONVERSATIONS_ID + "=" + conversationId, null);
        cursor.moveToFirst();
        cursor.close();
        return true;
    }

    private boolean updateConversationAfterMessageDelete(int conversationId, int senderUserId) {
        boolean success;
        Cursor cursor = this.database.query(InboxSQLiteHelper.TABLE_MESSAGES,
                new String[]{InboxSQLiteHelper.COLUMN_MESSAGES_ID}, "_sender_user_id=" + senderUserId, null, null,
                null, "_timestamp DESC");
        if (cursor.getCount() == 0) {
            success = deleteConversation(conversationId);
        } else if (cursor.moveToFirst()) {
            success = updateConversation(conversationId,
                    cursor.getInt(cursor.getColumnIndex(InboxSQLiteHelper.COLUMN_MESSAGES_ID)), -1);
        } else {
            success = false;
        }
        cursor.close();
        return success;
    }

    public Cursor getConversationsView() {
        return this.database.query(InboxSQLiteHelper.VIEW_CONVERSATIONS, null, null, null, null, null,
                "_num_unread_messages<=0, _timestamp DESC");
    }

    public Cursor getMessagesView(int senderUserId) {
        return this.database.query(InboxSQLiteHelper.VIEW_MESSAGES, null, "_sender_user_id=" + senderUserId + " and "
                + InboxSQLiteHelper.COLUMN_MESSAGES_STATE + "=" + 0, null, null, null, "_timestamp DESC");
    }

    public Cursor getUnreadMessagesViewSince(int messageId) {
        String where = "_state=0";
        if (messageId != -1) {
            where = where + " and _message_id > " + messageId;
        }
        return this.database.query(InboxSQLiteHelper.VIEW_MESSAGES, null, where, null, null, null, "_timestamp DESC");
    }

    public Conversation getConversationFromViewCursor(Cursor cursor) {
        Conversation conversation = getConversationFromCursor(cursor);
        Message message = getMessageFromCursor(cursor);
        User user = getUserFromCursor(cursor);
        conversation.setLastMessage(message);
        conversation.setSender(user);
        message.setSender(user);
        return conversation;
    }

    public Message getMessageFromViewCursor(Cursor cursor) {
        Message message = getMessageFromCursor(cursor);
        User user = getUserFromCursor(cursor);
        if (!(message == null || user == null)) {
            message.setSender(user);
        }
        return message;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    private Conversation getConversationFromCursor(Cursor cursor) {
        int id = cursor.getInt(cursor.getColumnIndex(InboxSQLiteHelper.COLUMN_CONVERSATIONS_ID));
        String appId = cursor.getString(cursor.getColumnIndex(InboxSQLiteHelper.COLUMN_USERS_APP_ID));
        int senderUserId = cursor.getInt(cursor.getColumnIndex(InboxSQLiteHelper.COLUMN_MESSAGES_SENDER_USER_ID));
        PendingIntent pendingLaunchIntent = this.pendingLaunchIntentCache.get(Integer.valueOf(senderUserId));
        Conversation conversation = new Conversation(id, appId, senderUserId, cursor.getInt(cursor
                .getColumnIndex(InboxSQLiteHelper.COLUMN_CONVERSATIONS_LAST_MESSAGE_ID)), cursor.getInt(cursor
                .getColumnIndex(InboxSQLiteHelper.COLUMN_CONVERSATIONS_NUM_UNREAD_MESSAGES)));
        conversation.setPendingIntent(pendingLaunchIntent);
        return conversation;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    private Message getMessageFromCursor(Cursor cursor) {
        int id = cursor.getInt(cursor.getColumnIndex(InboxSQLiteHelper.COLUMN_MESSAGES_ID));
        String appId = cursor.getString(cursor.getColumnIndex(InboxSQLiteHelper.COLUMN_USERS_APP_ID));
        int senderUserId = cursor.getInt(cursor.getColumnIndex(InboxSQLiteHelper.COLUMN_MESSAGES_SENDER_USER_ID));
        PendingIntent pendingLaunchIntent = this.pendingLaunchIntentCache.get(Integer.valueOf(senderUserId));
        Message message = new Message(id, appId, senderUserId, cursor.getString(cursor
                .getColumnIndex(InboxSQLiteHelper.COLUMN_MESSAGES_BODY)), cursor.getInt(cursor
                .getColumnIndex(InboxSQLiteHelper.COLUMN_MESSAGES_STATE)), cursor.getLong(cursor
                .getColumnIndex(InboxSQLiteHelper.COLUMN_MESSAGES_TIMESTAMP)));
        message.setPendingIntent(pendingLaunchIntent);
        message.setActions(this.actionsCache.get(Integer.valueOf(id)));
        return message;
    }

    private User getUserFromCursor(Cursor cursor) {
        return new User(cursor.getInt(cursor.getColumnIndex(InboxSQLiteHelper.COLUMN_USERS_ID)),
                cursor.getString(cursor.getColumnIndex(InboxSQLiteHelper.COLUMN_USERS_APP_ID)), cursor.getString(cursor
                .getColumnIndex(InboxSQLiteHelper.COLUMN_USERS_APPSPECIFIC_USER_ID)), cursor.getString(cursor
                .getColumnIndex(InboxSQLiteHelper.COLUMN_USERS_DISPLAY_NAME)), cursor.getInt(cursor
                .getColumnIndex(InboxSQLiteHelper.COLUMN_USERS_TYPE)));
    }

    public void reset() {
        this.database.delete(InboxSQLiteHelper.TABLE_CONVERSATIONS, null, null);
        this.database.delete(InboxSQLiteHelper.TABLE_MESSAGES, null, null);
        this.database.delete(InboxSQLiteHelper.TABLE_USERS, null, null);
    }
}