﻿using System.Collections.Generic;

namespace ChatSDK
{
	 /**
	     * The abstract class for the group manager.
	     *
		 */
    public abstract class IGroupManager
    {
        /**
	     * Requests to join a group.
	     *
	     * This is an asynchronous method.
	     *
	     * @param groupId	The group ID.
	     * @param reason    The reason for requesting to joining the group.
	     * @param handle    The callback of application. See {@link CallBack}.
	     */
        public abstract void applyJoinToGroup(string groupId, string reason = "", CallBack handle = null);

        /**
	     * Accepts a group invitation.
	     *
	     * This is an asynchronous method.
	     *
	     * @param groupId 	The group ID.
	     * @param handle    The callback of acceptance. Returns the group instance which the user has accepted the invitation to join. See {@link ValueCallBack}.
	     */
        public abstract void AcceptGroupInvitation(string groupId, ValueCallBack<Group> handle = null);

        /**
	     * Approves a group request.
		 * 
         * Only the group owner or admin can call this method.
	     *
	     * This is an asynchronous method.
	     *
	     * @param groupId  	The group ID.
	     * @param username 	The ID of the user who sends the request to join the group.
	     * @param handle    The callback of approval. See {@link CallBack}.
	     */
        public abstract void AcceptGroupJoinApplication(string groupId, string username, CallBack handle = null);

        /**
         * Adds a group admin.
	     * Only the group owner, not the group admin, can call this method.
	     *
	     * This is an asynchronous method.
         *
	     * @param groupId	The group ID.
	     * @param memberId	The new admin ID.
	     * @param handle    The callback of addition. See {@link CallBack}.
	     */
        public abstract void AddGroupAdmin(string groupId, string memberId, CallBack handle = null);

        /**
	     * Adds users to the group.
		 * 
	     * Only the group owner or admin can call this method.
	     *
	     * This is an asynchronous method.
	     *
	     * @param groupId		The group ID.
	     * @param newmembers	The list of new members to add.
	     * @param handle        The operation callback. See {@link CallBack}.
	     */
        public abstract void AddGroupMembers(string groupId, List<string> members, CallBack handle = null);

        /**
	     * Adds members to the allow list.
		 * 
	     * Only the group owner or admin can call this method.
	     *
	     * This is an asynchronous method.
	     *
	     * @param groupId 	The group ID.
	     * @param members 	The members to be added to the allow list.
	     * @param handle    The operation callback. See {@link CallBack}.
	     */
        public abstract void AddGroupWhiteList(string groupId, List<string> members, CallBack handle = null);

        /**
	     * Blocks group messages.
		 * 
         * The user that blocks group messages is still a group member. They just cannot receive group messages.
	     *
	     * This is an asynchronous method.
	     *
	     * @param groupId	The group ID.
	     * @param handle    The operation callback. See {@link CallBack}.
	     */
        public abstract void BlockGroup(string groupId, CallBack handle = null);

        /**
         * Adds the user to the group block list.
		 * 
	     * After this method call succeeds, the user is first removed from the chat group, and then added to the group block list. Users on the chat group block list cannot send or receive group messages, nor can they re-join the chat group.
	     * 
		 * Only the group owner or admin can call this method.
	     *
	     * This is an asynchronous method.
	     *
	     * @param groupId	The group ID.
	     * @param members 	The users to be added to the block list.
	     * @param handle    The operation callback. See {@link CallBack}.
	     */
        public abstract void BlockGroupMembers(string groupId, List<string> members, CallBack handle = null);

        /**
	     * Changes the group description.
		 * 
	     * Only the group owner or admin can call this method.
	     *
	     * This is an asynchronous method.
	     *
	     * @param groupId 	The group ID.
	     * @param desc 	    The new group description.
         * @param handle    The operation callback. See {@link CallBack}.
	     */
        public abstract void ChangeGroupDescription(string groupId, string desc, CallBack handle = null);

        /**
	     * Changes the group name.
		 * 
	     * Only the group owner or admin can call this method.
	     *
	     * This is an asynchronous method.
	     *
	     * @param groupId 	The ID of group whose name is to be changed.
	     * @param name 	    The new group name.
	     * @param handle    The operation callback. See {@link CallBack}.
	     */
        public abstract void ChangeGroupName(string groupId, string name, CallBack handle = null);

        /**
	     * Transfers the group ownership.
		 * 
	     * Only the group owner can call this method.
	     *
	     * This is an asynchronous method.
         *
	     * @param groupId	The group ID.
	     * @param newOwner	The user ID of the new owner.
	     * @param handle    The operation callback. See {@link CallBack}.
	     */
        public abstract void ChangeGroupOwner(string groupId, string newOwner, CallBack handle = null);

        /**
	     * Gets whether the current user is on the allow list of the group.
	     *
	     * This is an asynchronous method.
	     *
	     * @param groupId 	The group ID.
	     * @param handle    The operation callback. See {@link CallBack}.
	     */
        public abstract void CheckIfInGroupWhiteList(string groupId, ValueCallBack<bool> handle = null);

        /**
	     * Creates a group instance.
		 * 
         * After the group is created, the data in the memory and database will be updated and multiple devices will receive the notification event and update the group to the memory and database. 
		 * 
		 * You can set {@link IMultiDeviceDelegate} to listen for the event. 
		 * 
		 * If an event occurs, the callback function {@link onGroupMultiDevicesEvent((MultiDevicesOperation, string, List<string>)} will be triggered.
	     * 
	     * This is an asynchronous method.
	     *
	     * @param groupName     The group name. It is optional. Pass `null` if you do not want to set this parameter.
	     * @param options       The options for creating a group. They are optional and cannot be `null`. See {@link GroupOptions}.
	     *                      The options are as follows:
	     *                      - The maximum number of members allowed in the group. The default value is 200.
	     *                      - The group style. See {@link GroupStyle}. 
	     *                      - Whether to ask for permission when inviting a user to join the group. The default value is `false`, indicating that invitees are automaticall added to the group without their permission.
	     *                      - The extention of group details.* 
         * @param desc          The group description. It is optional. Pass `null` if you do not want to set this parameter.
	     * @param inviteMembers The group member array. The group owner ID is optional. This parameter cannot be `null`.
	     * @param inviteReason  The group joining invitation. It is optional. Pass `null` if you do not want to set this parameter.
         * @param handle        The operation callback. See {@link CallBack}.
         */
        public abstract void CreateGroup(string groupName, GroupOptions options, string desc = null, List<string> inviteMembers = null, string inviteReason = null, ValueCallBack<Group> handle = null);

        /**
	     * Declines a group invitation.
	     *
	     * This is an asynchronous method.
	     *
	     * @param groupId 	The group ID.
	     * @param reason	The reason for declining the group invitation.
	     * @param handle    The operation callback. See {@link CallBack}.
	     */
        public abstract void DeclineGroupInvitation(string groupId, string reason = null, CallBack handle = null);

		/**
		 * Declines a group request.
		 * 
		 * Only the group owner or admin can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId  	The group ID.
		 * @param username 	The ID of the user who sends the request to join the group.
		 * @param reason   	The reason for declining the group request.
		 * @param handle    The operation callback. See {@link CallBack}.
		 */
		public abstract void DeclineGroupJoinApplication(string groupId, string username, string reason = null, CallBack handle = null);

		/**
		 * Destroys the group instance.
		 * 
		 * Only the group owner can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId	The group ID.
		 * @param handle    The operation callback. See {@link CallBack}.
		 */
		public abstract void DestroyGroup(string groupId, CallBack handle = null);

		/**
		 * Downloads the shared file of the group.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId   The group ID.
		 * @param fileId    The ID of the shared file.
		 * @param savePath  The path to save the downloaded file.
		 * @param handle    The operation callback. See {@link CallBack}.
		 */
		public abstract void DownloadGroupSharedFile(string groupId, string fileId, string savePath, CallBack handle = null);

		/**
		 * Gets the group announcement from the server.
		 * Group members can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId	The group ID.
		 * @param handle    The operation callback. See {@link ValueCallBack}.
		 */
		public abstract void GetGroupAnnouncementFromServer(string groupId, ValueCallBack<string> handle = null);

		/**
		 * Gets the group block list from the server with pagination.
		 * 
		 * Only the group owner or admin can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId               The group ID.
		 * @param pageNum            	The page number, starting from 1.
		 * @param pageSize              The number of members on the block list that you expect to get on each page.
		 * @param handle				The operation callback. If success, the obtained block list will be returned; otherwise, an error will be returned. See {@link ValueCallBack}.
		 */		
		public abstract void GetGroupBlockListFromServer(string groupId, int pageNum = 1, int pageSize = 200, ValueCallBack<List<string>> handle = null);

		/**
		 * Gets the shared files of the group from the server.
         *
         * For a large but unknown quantity of data, the server will return data with pagination as specified by `pageSize` and `pageNum`.
		 * 
		 * This is an asynchronous method.
		 *
		 * @param groupId 	The group ID.
		 * @param pageNum 	The page number, starting from 1.
		 *                  For the last page, the actual number of returned shared files is less than the value of `pageSize`.     
		 * @param pageSize 	The number of shared files that you expect to get on each page.
		 * @param handle	The operation callback. If success, the SDK returns the list of shared files; otherwise, an error will be returned. See {@link ValueCallBack}.
		 *
		 */
		public abstract void GetGroupFileListFromServer(string groupId, int pageNum = 1, int pageSize = 200, ValueCallBack<List<GroupSharedFile>> handle = null);

		/**
		 * Gets the group member list from the server.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId 	The group ID.
		 * @param pageSize 	The number of members that you expect to get on each page.
		   @param cursor    The position from which to start getting data. At the first method call, if you set `cursor` as `null`, the SDK gets the data in the reverse chronological order of when users joined the group.
		 * @param handle	The operation callback. If success, the SDK returns the obtained group member list and the cursor for the next query; otherwise, an error will be returned. See {@link ValueCallBack}.
		 *
		 */
		public abstract void GetGroupMemberListFromServer(string groupId, int pageSize = 200, string cursor = "", ValueCallBack<CursorResult<string>> handle = null);

		/**
		 * Gets the mute list of the group from the server.
		 * 
		 * Only the group owner or admin can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId	The group ID.
		 * @param pageNum	The page number, starting from 1.
		 * @param pageSize	The number of muted members that you expect to get on each page.
		 * @param handle	The operation callback. If success, the SDK returns the obtained mute list; otherwise, an error will be returned. See {@link ValueCallBack}.
		 *
		 */
		public abstract void GetGroupMuteListFromServer(string groupId, int pageNum = 1, int pageSize = 200, ValueCallBack<List<string>> handle = null);

		/**
		 * Gets group details.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId	The group ID.
		 * @param handle	The operation callback. If success, the SDK returns the group instance; otherwise, an error will be returned. See {@link ValueCallBack}.
		 *
		 */
		public abstract void GetGroupSpecificationFromServer(string groupId, ValueCallBack<Group> handle = null);

		/**
		 * Gets the allow list of the group from the server.
		 * 
		 * Only the group owner or admin can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId 	The group ID.
		 * @param handle	The operation callback. If success, the SDK returns the obtained allow list; otherwise, an error will be returned. See {@link ValueCallBack}.
		 */
		public abstract void GetGroupWhiteListFromServer(string groupId, ValueCallBack<List<string>> handle = null);

		/**
		 * Gets the group instance from the memory by group ID.
		 *
		 * @param groupId	The group ID.
		 * @return 			The group instance. Returns `null` if the group does not exist.
		 */
		public abstract Group GetGroupWithId(string groupId);

		/**
		 * Gets the list of groups that the user has joined.
		 * 
		 * This method gets the groups from the local memory and database.
		 * 
		 * This is an asynchronous method.
		 *
		 * @return 			The group list. 
		 */
		public abstract List<Group> GetJoinedGroups();

		/**
		 * Gets the list of groups with pagination.
		 * 
		 * This method gets a group list from the server, which does not contain member information. 
		 * 
		 * This is an asynchronous method and blocks the current thread.
		 *
		 * @param pageNum 		The page number, starting from 1.
		 * @param pageSize		The number of groups that you expect to get on each page.
		 * @param handle		The operation callback. If success, the SDK returns the obtained group list; otherwise, an error will be returned. See {@link ValueCallBack}. 
		 */
		public abstract void FetchJoinedGroupsFromServer(int pageNum = 1, int pageSize = 200, ValueCallBack<List<Group>> handle = null);

		/**
		 * Gets public groups from the server with pagination.
		 *
		 * This is an asynchronous method.
		 *
		 * @param pageSize  	The number of public groups that you expect to get on each page.
		 * @param cursor    	The position from which to start getting data. During the first call to this method, if `null` is passed to `cursor`, the SDK will get data in the reverse chronological order.
		 * @param handle		The operation callback. If success, the SDK returns the obtained public group list and the cursor used for the next query; otherwise, an error will be returned. See {@link ValueCallBack}.
		 */
		public abstract void FetchPublicGroupsFromServer(int pageSize = 200, string cursor = "", ValueCallBack<CursorResult<GroupInfo>> handle = null);

		/**
		 * Joins a public group.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId	  	The ID of the public group.
		 * @param handle		The operation callback. See {@link CallBack}.
		 */
		public abstract void JoinPublicGroup(string groupId, CallBack handle = null);

		/**
		 * Leaves a group.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId	The group ID.
		 * @param handle    The operation callback. See {@link CallBack}.
		 */
		public abstract void LeaveGroup(string groupId, CallBack handle = null);

		/**
		 * Mutes all members in the group.
		 * 
		 * Only the group owner or admin can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId		The group ID.
		 * @param handle		The operation callback. See {@link CallBack}.
		 */
		public abstract void MuteGroupAllMembers(string groupId, CallBack handle = null);

		/**
		 * Mutes the specified group members.

		 * Only the group owner or admin can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId		The group ID.
		 * @param members 		The list of members to be muted.
		 * @param handle		The operation callback. See {@link CallBack}.
		 */
		public abstract void MuteGroupMembers(string groupId, List<string> members, CallBack handle = null);

		/**
		 * Removes a group admin.
		 * Only the group owner can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId	The group ID.
		 * @param memberId	The ID of the admin to remove.
		 * @param handle	The operation callback. See {@link CallBack}.
		 */		
		public abstract void RemoveGroupAdmin(string groupId, string memberId, CallBack handle = null);

		/**
		 * Removes the shared file of the group.
		 * 
		 * Group members can only delete the shared files uploaded by themselves. The group owner and admins can delete all the shared files.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId 	The group ID.
		 * @param fileId 	The ID of the shared file.
		 * @param handle	The operation callback. See {@link CallBack}.
		 */		
		public abstract void DeleteGroupSharedFile(string groupId, string fileId, CallBack handle = null);

		/**
		 * Removes members from the group.
		 * 
		 * Only the group owner or admin can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId	The group ID.
		 * @param members   The members to remove.
		 * @param handle	The operation callback. See {@link CallBack}.
		 */
		public abstract void DeleteGroupMembers(string groupId, List<string> members, CallBack handle = null);

		/**
		 * Removes members from the group allow list.
		 * 
		 * Only the group owner or admin can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId 	The group ID.
		 * @param members 	The list of members to be removed from the group allow list.
		 * @param handle	The operation callback. See {@link CallBack}.
		 */		
		public abstract void RemoveGroupWhiteList(string groupId, List<string> members, CallBack handle = null);

		/**
		 * Unblocks group messages.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId	The group ID
		 * @param handle	The operation callback. See {@link CallBack}.
		 */
		public abstract void UnBlockGroup(string groupId, CallBack handle = null);

		/**
		 * Removes users from the group block list.
		 * 
		 * Only the group owner or admin can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId	The group ID.
		 * @param members	The list of users to be removed from the block list.
		 * @param handle	The operation callback. See {@link CallBack}.
		 */
		public abstract void UnBlockGroupMembers(string groupId, List<string> members, CallBack handle = null);

		/**
		 * Unmutes all members in the group.
		 * 
		 * Only the group owner or admin can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId	The group ID.
		 * @param handle	The operation callback. See {@link CallBack}.
		 */
		public abstract void UnMuteGroupAllMembers(string groupId, CallBack handle = null);

		/**
		 * Unmutes the specified group members.
		 * 
		 * Only the group owner or admin can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId	The group ID.
		 * @param members	The list of members to be unmuted.
		 * @param handle	The operation callback. See {@link CallBack}.
		 */
		public abstract void UnMuteGroupMembers(string groupId, List<string> members, CallBack handle = null);

		/**
		 * Updates the group announcement.
		 * 
		 * Only the group owner or admin can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId 		The group ID.
		 * @param announcement 	The group announcement.
		 * @param handle	The operation callback. See {@link CallBack}.
		 */
		public abstract void UpdateGroupAnnouncement(string groupId, string announcement, CallBack handle = null);

		/**
		 * Updates the group extension field.
		 * 
		 * Only the group owner or admin can call this method.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId       The group ID.
		 * @param ext     		The group extension field.
		 * @param handle		The operation callback. See {@link CallBack}.
		 */
		public abstract void UpdateGroupExt(string groupId, string ext, CallBack handle = null);

		/**
		 * Uploads the shared file to the group.
		 *
		 * This is an asynchronous method.
		 *
		 * @param groupId 	The group ID.
		 * @param filePath 	The local path of the shared file.
		 * @param handle	The operation callback. See {@link CallBack}.
		 */
		public abstract void UploadGroupSharedFile(string groupId, string filePath, CallBack handle = null);

		/**
		 * Adds a group manager listener.
		 *
		 * @param groupManagerDelegate 		The group manager listener to add. It is inherited from {@link IGroupManagerDelegate}.
		 * 
		 */
		public void AddGroupManagerDelegate(IGroupManagerDelegate groupManagerDelegate)
        {
            if (!CallbackManager.Instance().groupManagerListener.delegater.Contains(groupManagerDelegate))
            {
                CallbackManager.Instance().groupManagerListener.delegater.Add(groupManagerDelegate);
            }
        }

		/**
		 * Removes a group manager listener.
		 *
		 * @param groupManagerDelegate 		The group manager listener to remove. It is inherited from {@link IGroupManagerDelegate}.
		 * 
		 */
		public void RemoveGroupManagerDelegate(IGroupManagerDelegate groupManagerDelegate)
        {
            if (CallbackManager.IsQuit()) return;
            if (CallbackManager.Instance().groupManagerListener.delegater.Contains(groupManagerDelegate))
            {
                CallbackManager.Instance().groupManagerListener.delegater.Remove(groupManagerDelegate);
            }
        }

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

    }

}
