﻿using System.Collections.Generic;

namespace ChatSDK
{
    public abstract class IChatThreadManager
    {
        /**
         * Creates a message thread.
         *
         * Each member of the chat group where the message thread belongs can call this method.
         *
         * Upon the creation of a message thread, the following will occur:
         *
         * - In a single-device login scenario, each member of the group to which the message thread belongs will receive the {@link IChatThreadManagerDelegate#OnChatThreadCreate(ChatThreadEvent)} callback.
         *   You can listen for message thread events by setting {@link EMChatThreadChangeListener}.
         *
         * - In a multi-device login scenario, the devices will receive the {@link IMultiDeviceDelegate#onThreadMultiDevicesEvent(MultiDevicesOperation, String, List)} callback.
         *   In this callback method, the first parameter indicates a message thread event, for example, {@link MultiDevicesOperation#THREAD_CREATE} for a message thread creation event.
         *   You can listen for message thread events by setting {@link IMultiDeviceDelegate}.
         *
         * @param threadName The name of the new message thread. It can contain a maximum of 64 characters.
         * @param msgId     The ID of the parent message.
         * @param groupId              The parent ID, which is the group ID.
         * 
         * 
         * @param handle      The result callback:
         *                    - If success, {@link EMValueCallBack#onSuccess(Object)} is triggered to return the new message thread object;
         *                    - If a failure occurs, {@link EMValueCallBack#onError(int, String)} is triggered to return an error.
         */
        public abstract void CreateThread(string threadName, string msgId, string groupId, ValueCallBack<ChatThread> handle = null);

        /**
         * Joins a message thread.
         *
         * Each member of the group where the message thread belongs can call this method.
         *
         * In a multi-device login scenario, note the following:
         *
         * - The devices will receive the {@link IMultiDeviceDelegate#onThreadMultiDevicesEvent(MultiDevicesOperation, String, List)} callback.
         *
         * - In this callback method, the first parameter indicates a message thread event, for example, {@link MultiDevicesOperation#THREAD_JOIN} for a message thread join event.
         *
         * - You can listen for message thread events by setting {@link IMultiDeviceDelegate}.
         *
         * @param threadId   The message thread ID.
         * @param handle     The result callback:
         *                     - If success, {@link ValueCallBack#onSuccess(Object)} is triggered to return the message thread details {@link ChatThread} which do not include the member count.
         *                     - If a failure occurs, {@link ValueCallBack#onError(int, String)} is triggered to return an error.
         *                       If you join a message thread repeatedly, the SDK will return an error related to USER_ALREADY_EXIST.
         */
        public abstract void JoinThread(string threadId, ValueCallBack<ChatThread> handle = null);

        /**
        * Leaves a message thread.
        *
        * Each member in the message thread can call this method.
        *
        * In a multi-device login scenario, note the following:
        *
        * - The devices will receive the {@link IMultiDeviceDelegate#onThreadMultiDevicesEvent(MultiDevicesOperation, String, List)} callback.
        *
        * - In this callback method, the first parameter indicates a message thread event, for example, {@link MultiDevicesOperation#THREAD_LEAVE} for a message thread leaving event.
        *
        * - You can listen for message thread events by setting {@link IMultiDeviceDelegate}.
        *
        * @param threadId     The ID of the message thread that the current user wants to leave.
        * @param handle       The result callback:
        *                     - If success, {@link CallBack#onSuccess()} is triggered;
        *                     - If a failure occurs, {@link CallBack#onError(int, String)} is triggered to return an error.
        */
        public abstract void LeaveThread(string threadId, CallBack handle = null);

        /**
        * Destroys the message thread.
        *
        * Only the owner or admins of the group where the message thread belongs can call this method.
        *
        * **Note**
        *
        *
        * - In a multi-device login scenario, The devices will receive the {@link IMultiDeviceDelegate#onThreadMultiDevicesEvent(MultiDevicesOperation, String, List)} callback.
        *   In this callback method, the first parameter indicates a message thread event, for example, {@link MultiDevicesOperation#THREAD_DESTROY} for a message thread destruction event.
        *   You can listen for message thread events by setting {@link IMultiDeviceDelegate}.
        *
        * @param threadId  The message thread ID.
        * @param handle    The result callback:
        *                  - If success, {@link CallBack#onSuccess()} is triggered.
        *                   - If a failure occurs, {@link CallBack#onError(int, String)} is triggered to return an error.
        */

        public abstract void DestroyThread(string threadId, CallBack handle = null);

        /**
        * Removes a member from the message thread.
        *
        * Only the owner or admins of the group where the message thread belongs and the message thread creator can call this method.
        *
        * The removed member will receive the {@link IChatThreadManagerDelegate#OnUserKickOutOfChatThread(ChatThreadEvent)} callback.
        *
        * You can listen for message thread events by setting {@link IChatThreadManagerDelegate}.
        *
        * @param threadId  The message thread ID.
        * @param username  The user ID of the member to be removed from the message thread.
        * @param handle    The result callback.
        *                     - If success, {@link CallBack#onSuccess()} is triggered.
        *                     - If a failure occurs, {@link CallBack#onError(int, String)} is triggered to return an error.
        */
        public abstract void RemoveThreadMember(string threadId, string username, CallBack handle = null);

        /**
         * Changes the name of the message thread.
         *
         * Only the owner or admins of the group where the message thread belongs and the message thread creator can call this method.
         *
         * Each member of the group to which the message thread belongs will receive the {@link IChatThreadManagerDelegate#OnChatThreadUpdate(ChatThreadEvent)} callback.
         *
         * You can listen for message thread events by setting {@link IChatThreadManagerDelegate}.
         *
         * @param threadId      The message thread ID.
         * @param newName       The new message thread name. It can contain a maximum of 64 characters.
         * @param handle        The result callback:
         *                      - If success, {@link CallBack#onSuccess()} is triggered.
         *                      - If a failure occurs, {@link CallBack#onError(int, String)} is triggered to return an error.
         */
        public abstract void ChangeThreadName(string threadId, string newName, CallBack handle = null);

        /**
         */
        public abstract void FetchThreadMembers(string threadId, string cursor = null, int pageSize = 20, ValueCallBack<CursorResult<string>> handle = null);

        /**
        * Use the pagination to get the list of message threads in the specified group.
        *
        * This method gets data from the server.
        *
        * @param groupId   The parent ID, which is the group ID.       
        * @param joined    The threads are joined or not.
        * @param cursor    The position from which to start getting data. At the first method call, if you set `cursor` to `null` or an empty string, the SDK will get data in the reverse chronological order of when message threads are created.
        * @param pageSize  The number of message threads that you expect to get on each page. The value range is [1,50].
        * @param callBack  The result callback:
        *                  - If success, {@link ValueCallBack#onSuccess(Object)} is triggered to return the result {@link CursorResult}, including the message thread list and the cursor for the next query.
        *                  - If a failure occurs, {@link ValueCallBack#onError(int, String)} is triggered to return an error.
        */

        public abstract void FetchThreadListOfGroup(string groupId, bool joined, string cursor = null, int pageSize = 20, ValueCallBack<CursorResult<ChatThread>> handle = null);

        /**
         * Uses the pagination to get the list of message threads that the current user has joined.
         *
         * This method gets data from the server.
         *         
         * @param cursor    The position from which to start getting data. At the first method call, if you set `cursor` to `null` or an empty string, the SDK will get data in the reverse chronological order of when the user joins the message threads.
         * @param pageSize     The number of message threads that you expect to get on each page. The value range is [1,50].
         * @param callBack  The result callback:
         *                  - If success, {@link ValueCallBack#onSuccess(Object)} is triggered to return the result {@link CursorResult}, including the message thread list and the cursor for the next query.
         *                  - If a failure occurs, {@link ValueCallBack#onError(int, String)} is triggered to return an error.
         */

        public abstract void FetchMineJoinedThreadList(string cursor = null, int pageSize = 20, ValueCallBack<CursorResult<ChatThread>> handle = null);
        
        /**
         * Gets the details of the message thread from the server.
         *
         * @param threadId   The message thread ID.
         * @param handle       The result callback:
         *                       - If success, {@link ValueCallBack#onSuccess(Object)} is triggered to return the message thread details;
         *                       - If a failure occurs, {@link ValueCallBack#onError(int, String)} is triggered to return an error.
         */
        public abstract void GetThreadDetail(string threadId, ValueCallBack<ChatThread> handle = null);

        /**
         */
        public abstract void GetLastMessageAccordingThreads(List<string> threadIds, ValueCallBack<Dictionary<string, Message>> handle = null);

        /**
         * Adds the message thread event listener, which listens for message thread changes, such as the message thread creation and destruction.
         *
         * You can call {@link #RemoveThreadManagerDelegate} to remove an unnecessary message thread event listener.
         *
         * @param threadManagerDelegate The message thread event listener to add.
         */
        public void AddThreadManagerDelegate(IChatThreadManagerDelegate threadManagerDelegate)
        {
            if (!CallbackManager.Instance().threadManagerListener.delegater.Contains(threadManagerDelegate))
            {
                CallbackManager.Instance().threadManagerListener.delegater.Add(threadManagerDelegate);
            }
        }

        /**
         * Removes the message thread event listener.
         *
         * After a message thread event listener is added with {@link #AddThreadManagerDelegate}, you can call this method to remove it when it is not required.
         *
         * @param listener  The message thread event listener to remove.
         */
        public void RemoveThreadManagerDelegate(IChatThreadManagerDelegate threadManagerDelegate)
        {
            if (CallbackManager.IsQuit()) return;
            if (CallbackManager.Instance().threadManagerListener.delegater.Contains(threadManagerDelegate))
            {
                CallbackManager.Instance().threadManagerListener.delegater.Remove(threadManagerDelegate);
            }
        }

        internal void ClearDelegates()
        {
            CallbackManager.Instance().threadManagerListener.delegater.Clear();
        }
    }
}
