/**
 * Web唐明明
 * 匆匆数载恍如梦，岁月迢迢华发增。
 * 碌碌无为枉半生，一朝惊醒万事空。
 * moduleName: 聊聊
 */
import store from '@/store'
import {
    request
} from '../index'

// 获取好友列表
const getFriends = () => {
    return request({
        url: 'im/friends',
    }, true)
}

// 获取好友列表
const getFriendsLetter = () => {
    return request({
        url: 'im/friends/letter',
    }, true)
}

// 获取融云token
const getImToken = () => {
    return request({
        url: 'im/token',
    }, true)
}

// 删除好友
const deleteFriend = (targetId) => {
    return request({
        method: 'DELETE',
        url: 'im/friends/' + targetId,
    })
}

// 获取用户信息
const getUserInfo = (targetId) => {
    return request({
        url: 'im/userInfo/' + targetId,
    })
}

/**
 * 查看好友资料，附带好友关系
 */
const getFriendInfo = (userId) => {
    return request({
        url: 'im/friendInfo/' + userId
    })
}
/**
 * 获取好友申请列表
 */
const getPedings = () => {
    return request({
        url: 'im/friends/pending'
    })
}

const resolveFriend = (targetId) => {
    return request({
        method: 'POST',
        url: 'im/friends/accept/' + targetId
    })
}

const rejectFriend = (targetId) => {
    return request({
        method: 'DELETE',
        url: 'im/friends/accept/' + targetId
    })
}

const searchFriend = (value) => {
    return request({
        url: 'im/search',
        data: {
            keyword: value
        }
    })
}

const pedingFriend = (recipient, message) => {
    return request({
        method: 'POST',
        url: 'im/friends/' + recipient,
        data: {
            message
        }
    })
}

// 以下是群组相关业务的接口
const getMyGroups = () => {
    return request({
        url: 'im/groups'
    })
}

/**
 * 获取群信息，包含基础信息和 14 个用户
 */
const getGroupInfo = (groupId) => {
    return request({
        url: 'im/groups/' + groupId
    })
}

/**
 * 群组基础信息
 */
const getGroupBase = (groupId) => {
    return request({
        url: 'im/groups/' + groupId + '/base'
    })
}

const getGroupUsers = (groupId, limit) => {
    limit = limit || 0
    return request({
        url: 'im/groups/' + groupId + '/users?limit=' + limit
    })
}
//  公告列表
const getGroupAnnouncements = (groupId) => {
    return request({
        url: 'im/groups/' + groupId + '/announcements'
    })
}
// 创建公告
const createGroupAnnouncement = (groupId, content) => {
    return request({
        method: 'POST',
        url: 'im/groups/' + groupId + '/announcements',
        data: {
            content: content
        }
    })
}
//  查看该公告详情
const getGroupAnnouncement = (groupId, announcementId) => {
    return request({
        method: 'GET',
        url: 'im/groups/' + groupId + '/announcements/' + announcementId
    })
}
// 删除该公告
const deleteGroupAnnouncement = (groupId, announcementId) => {
    return request({
        method: 'DELETE',
        url: 'im/groups/' + groupId + '/announcements/' + announcementId
    })
}
// 置顶群公告
const topGroupAnnouncement = (groupId, announcementId) => {
    return request({
        method: 'POST',
        url: 'im/groups/' + groupId + '/announcements/' + announcementId + '/top',
    })
}

/**
 * 创建群聊
 */
const createGroup = (data) => {
    return request({
        method: 'POST',
        url: 'im/groups',
        data: data
    })
}

const updateGroup = (groupId, data) => {
    return request({
        method: 'PUT',
        url: 'im/groups/' + groupId,
        data: data
    })
}

/**
 * 搜索群聊
 */
const searchGroup = (name) => {
    return request({
        url: 'im/groups/search?name=' + name
    })
}

/**
 * 加群前，获取的群信息
 */
const joinGroupPre = (groupId) => {
    return request({
        url: 'im/groups/' + groupId + '/join'
    })
}

const joinGroup = (groupId, message) => {
    return request({
        method: 'POST',
        url: 'im/groups/' + groupId + '/join',
        data: {
            message
        }
    })
}

const quitGroup = (groupId) => {
    return request({
        method: 'DELETE',
        url: 'im/groups/' + groupId + '/quit'
    })
}

/**
 * 解散群聊
 */
const dismissGroup = (groupId) => {
    return request({
        method: 'DELETE',
        url: 'im/groups/' + groupId
    })
}

/**
 * 移除群成员
 */
const removeGroupUser = (groupId, userId) => {
    return request({
        method: 'DELETE',
        url: 'im/groups/' + groupId + '/users/' + userId
    })
}

/**
 * 邀请群成员
 */
const inviteGroupUser = (groupId, userIds, allowIds) => {
    return request({
        method: 'POST',
        url: 'im/groups/' + groupId + '/invite',
        data: {
            userIds: userIds,
            allowIds: allowIds 
        }
    })
}

/**
 * 设置群管理
 */
const setGroupAdmin = (groupId, userId) => {
    return request({
        method: 'POST',
        url: 'im/groups/' + groupId + '/admin/' + userId
    })
}

/**
 * 移除群管理
 */
const removeGroupAdmin = (groupId, userId) => {
    return request({
        method: 'DELETE',
        url: 'im/groups/' + groupId + '/admin/' + userId
    })
}

/**
 * 转移群主
 */
const transferGroupOwner = (groupId, userId) => {
    return request({
        method: 'POST',
        url: 'im/groups/' + groupId + '/owner/' + userId
    })
}


//  通过审核验证群成员
const groupMakeSure = (groupId, user) => {
    return request({
        url: `im/groups/${groupId}/makesure/${user}`,
        method: 'POST'
    })
}



export {
    getImToken,
    deleteFriend,
    getFriends,
    getFriendsLetter,
    getUserInfo,
    getFriendInfo,
    getPedings,
    resolveFriend,
    rejectFriend,
    searchFriend,
    pedingFriend,
    getMyGroups,
    createGroup,
    updateGroup,
    getGroupInfo,
    getGroupBase,
    getGroupUsers,
    getGroupAnnouncements,
    getGroupAnnouncement,
    createGroupAnnouncement,
    deleteGroupAnnouncement,
    topGroupAnnouncement,
    searchGroup,
    joinGroupPre,
    joinGroup,
    quitGroup,
    dismissGroup,
    inviteGroupUser,
    removeGroupUser,
    transferGroupOwner,
    setGroupAdmin,
    removeGroupAdmin,
    groupMakeSure, //  通过审核
}
