﻿using System;
using System.Collections.Generic;

namespace ChatSDK
{
    /**
         * The abstract class for the conversation manager.
         * 
         */
    internal abstract class IConversationManager
    {
        private static IConversationManager instance;

        /**
         * Gets the latest message in the conversation.
         * 
         * A call of this method has no impact on the unread message count of the conversation.
         * 
         * The SDK first retrieves the latest message from the memory. If no message is found, the SDK retrieves it from the local database and loads it.
         *
         * @param conversationId    The ID of the conversation to which the last message belongs.
         * @param conversationType  The type of the conversation to which the last message belongs. See {@link ConversationType}.
         *
         * @return  The message instance.
         */
        internal abstract Message LastMessage(string conversationId, ConversationType conversationType);

        /**
         * Gets the latest message received in the conversation.
         * 
         * @param conversationId    The ID of the conversation to which the latest received message belongs.
         * @param conversationType  The type of the conversation to which the latest received message belongs. See {@link ConversationType}.
         * 
         * @return  The message instance.
         */
        internal abstract Message LastReceivedMessage(string conversationId, ConversationType conversationType);

        /**
         * Gets the extension information of the conversation.
         * 
         * The conversation extension information is stored only in the local memory and local database, but not in the server.
         * 
         * @param conversationId    The ID of the conversation to which the extension information belongs.
         * @param conversationType  The type of the conversation to which the extension information belongs. See {@link ConversationType}.
         *
         * @return                  The extension information of the conversation.
         */
        internal abstract Dictionary<string, string> GetExt(string conversationId, ConversationType conversationType);

        /**
         * Sets the extension information of the conversation.
         * 
         * The conversation extension information is stored only in the local memory and local database, but not in the server.
         *
         * @param conversationId    The ID of the conversation to which the extension field belongs.
         * @param conversationType  The type of the conversation to which the extension field belongs. See {@link ConversationType}.
         * @param ext               The extension information of the conversation.
         */
        internal abstract void SetExt(string conversationId, ConversationType conversationType, Dictionary<string, string> ext);

        /**
         * Gets the count of unread messages in the conversation.
         * 
         * @param conversationId    The conversation ID.
         * @param conversationType  The conversation type. See {@link ConversationType}.
         * 
         * @return                  The count of unread messages.
         */
        internal abstract int UnReadCount(string conversationId, ConversationType conversationType);

        /**
         * Gets the total number of messages in the conversation in the local database.
         * 
         * @param conversationId    The conversation ID.
         * @param conversationType  The conversation type. See {@link ConversationType}.
         * 
         * @return                  The total number of messages in the conversation.
         */
        internal abstract int MessagesCount(string conversationId, ConversationType conversationType);

        /**
         * Marks a message as read.
         * 
         * @param conversationId    The ID of the conversation to which the message belongs.
         * @param conversationType  The type of the conversation to which the message belongs. See {@link ConversationType}。
         * @param messageId         The message ID.
         */
        internal abstract void MarkMessageAsRead(string conversationId, ConversationType conversationType, string messageId);

        /**
         * Marks all unread messages in the conversation as read.
         *  
         * @param conversationId    The conversation ID.
         * @param conversationType  The conversation type. See {@link ConversationType}.
         */
        internal abstract void MarkAllMessageAsRead(string conversationId, ConversationType conversationType);

        /**
         * Inserts a message into a conversation in the local database.
         * 
         * The conversation ID of the message should be the same as that of the conversation to make sure that the message is correctly inserted. 
         * 
         * The message will be inserted based on the Unix timestamp included in it. Upon message insertion, the SDK will automatically update attributes of the conversation, including `latestMessage`.
         * 
         * @param conversationId    The ID of the conversation to which the message belongs.
         * @param conversationType  The type of the conversation to which the message belongs. See {@link ConversationType}.
         * @param message           The message instance.
         *
         * @return Whether the message is successfully inserted.
         *           - `true`: Yes.
         *           - `false`: No.
         */
        internal abstract bool InsertMessage(string conversationId, ConversationType conversationType, Message message);

        /**
        * Inserts a message to the end of a conversation in the local database.
        * 
        * The conversation ID of the message should be the same as that of the conversation to make sure that the message is correctly inserted.
        * 
        * After a message is inserted, the SDK will automatically update attributes of the conversation, including `latestMessage`.
        *
        * @param conversationId    The ID of the conversation to which the message belongs.
        * @param conversationType  The type of the conversation to which the message belongs. See {@link ConversationType}.
        * @param message           The message instance.
        *
        * @return Whether the message is successfully inserted.
        *           - `true`: Yes.
        *           - `false`: No.
        */
        internal abstract bool AppendMessage(string conversationId, ConversationType conversationType, Message message);

        /**
         * Updates a message in the local database.
         * 
         * The ID of the message cannot be changed during message update.
         * 
         * After a message is updated, the SDK will automatically update attributes of the conversation, including `latestMessage`.
         *
         * @param conversationId    The ID of the conversation to which the message belongs.
         * @param conversationType  The type of the conversation to which the message belongs. See {@link ConversationType}.
         * @param message           The message to be updated.
         * 
         * @return Whether this message is successfully updated.
         *         - `true`: Yes.
         *         - `false`: No.

         */
        internal abstract bool UpdateMessage(string conversationId, ConversationType conversationType, Message message);

        /**
         * Deletes a message from the local database.
         *
         * @param conversationId    The ID of the conversation to which the message belongs.
         * @param conversationType  The type of the conversation to which the message belongs. See {@link ConversationType}.
         * @param messageId         The message to be deleted.
         * 
         * @return Whether the message is successfully deleted.
         *         - `true`: Yes.
         *         - `false`: No.

         */
        internal abstract bool DeleteMessage(string conversationId, ConversationType conversationType, string messageId);

        /**
         * Deletes all the messages of the conversation.
         * 
         * This method deletes all messages of the conversation from both the memory and the local database.
         * 
         * @param conversationId    The ID of the conversation for which all messages will be deleted.
         * @param conversationType  The type of the conversation for which all messages will be deleted. See {@link ConversationType}.
         * 
         * @return Whether messages are successfully deleted.
         *         - `true`: Yes.
         *         - `false`: No.
         */
        internal abstract bool DeleteAllMessages(string conversationId, ConversationType conversationType);

        /**
         * Loads a message.
         * 
         * The SDK first retrieves the message from the memory. If the message is not found, the SDK will retrieve it from the local database and load it.
         *
         * @param conversationId    The ID of the conversation to which the message belongs.
         * @param conversationType  The type of the conversation to which the message belongs. See {@link ConversationType}.
         * @param messageId         The ID of the message to be loaded.
         * @return                  The message instance. If the message is not found, the SDK returns `null`.
         */
        internal abstract Message LoadMessage(string conversationId, ConversationType conversationType, string messageId);

        /**
         * Loads the messages of a specific type.
         * 
         * The SDK first retrieves the messages from the memory. If no message is not found, the SDK will retrieve them from the local database and load them.
         *
         * @param conversationId    The ID of the conversation to which the messages belong.
         * @param conversationType  The type of the conversation to which the messages belong. See {@link ConversationType}.
         * @param bodyType          The type of messages to load. Ensure that you set this parameter.
         * @param sender            The user ID of the message sender. Ensure that you set this parameter.
         * @param timestamp         The starting Unix timestamp for query. The default value is `-1`, indicating the current Unix timestamp.
         * @param count             The maximum number of messages to load. The default value is `20`.
         * @param direction         The message loading direction. By default, the SDK loads messages in the reverse chronological order of the Unix timestamp ({@link SortMessageByServerTime}) in the messages. See {@link MessageSearchDirection}.
         * @param callback          The loading result callback. If success, a list of loaded messages are returned; otherwise, an error is returned. See {@link ValueCallBack}.
         */
        internal abstract void LoadMessagesWithMsgType(string conversationId, ConversationType conversationType, MessageBodyType bodyType, string sender, long timestamp = -1, int count = 20, MessageSearchDirection direction = MessageSearchDirection.UP, ValueCallBack<List<Message>> callback = null);

        /**
         * Loads the messages, starting from a specific message ID.
         * 
         * The SDK first retrieves the messages from the memory. If no message is not found, the SDK will retrieve them from the local database and load them.
         *
         * @param conversationId    The ID of the conversation to which the messages belong.
         * @param conversationType  The type of the conversation to which the messages belong. See {@link ConversationType}.
         * @param startMessageId    The starting message ID for query. If this parameter is set as "" or `null`, the SDK will load from the latest message.
         * @param count             The maximum number of messages to load. The default value is `20`.
         * @param direction         The message loading direction. By default, the SDK loads messages in the reverse chronological order of the Unix timestamp ({@link SortMessageByServerTime}) in the messages. See {@link MessageSearchDirection}.
         * @param callback          The loading result callback. If success, a list of loaded messages are returned; otherwise, an error is returned. See {@link ValueCallBack}.
         */
        internal abstract void LoadMessages(string conversationId, ConversationType conversationType, string startMessageId = "", int count = 20, MessageSearchDirection direction = MessageSearchDirection.UP, ValueCallBack<List<Message>> callback = null);

        /**
         * Loads the messages by keyword.
         * 
         * The SDK first retrieves the messages from the memory. If no message is not found, the SDK will retrieve them from the local database and load them.
         *
         * @param conversationId    The ID of the conversation to which the messages belong.
         * @param conversationType  The type of the conversation to which the messages belong. See {@link ConversationType}.
         * @param keywords          The keyword for query. 
         * @param sender            The user ID of the message sender. 
         * @param timestamp         The starting Unix timestamp for query. The default value is `-1`, indicating the current Unix timestamp.
         * @param count             The maximum number of messages to load. The default value is `20`.
         * @param direction         The message loading direction. By default, the SDK loads messages in the reverse chronological order of the Unix timestamp ({@link SortMessageByServerTime}) in the messages. See {@link MessageSearchDirection}.
         * @param callback          The loading result callback. If success, a list of loaded messages are returned; otherwise, an error is returned. See {@link ValueCallBack}.
         */
        internal abstract void LoadMessagesWithKeyword(string conversationId, ConversationType conversationType, string keywords = "", string sender = "", long timestamp = -1, int count = 20, MessageSearchDirection direction = MessageSearchDirection.UP, ValueCallBack<List<Message>> callback = null);

        /**
         * Loads the messages within a period.
         * 
         * The SDK first retrieves the messages from the memory. If no message is not found, the SDK will retrieve them from the local database and load them.
         * 
         * Pay attention to the memory usage when you load a great number of messages.
         *
         * @param conversationId    The ID of the conversation to which the messages belong.
         * @param conversationType  The type of the conversation to which the messages belong. See {@link ConversationType}.
         * @param startTime         The starting Unix timestamp for query. Ensure that you set this parameter.
         * @param endTime           The ending Unix timestamp for query. Ensure that you set this parameter.
         * @param count             The maximum number of messages to load. The default value is `20`.
         * @param callback          The loading result callback. If success, a list of loaded messages are returned; otherwise, an error is returned. See {@link ValueCallBack}.
         */
        internal abstract void LoadMessagesWithTime(string conversationId, ConversationType conversationType, long startTime, long endTime, int count = 20, ValueCallBack<List<Message>> callback = null);

    }
}
