import store from '@/store/index.js'

import * as RongIMLib from '@/uni_modules/RongCloud-IMWrapper/js_sdk/index'

const getMessageList = (conversationType, targetId, timeStamp, count, isForward, callback) => {
    // 获取消息列表 https://doc.rongcloud.cn/imserver/server/v1/message/objectname#objectName
    const objectNames = [
        'RC:TxtMsg',
        'RC:VcMsg',
        'RC:HQVCMsg',
        'RC:ImgMsg',
        'RC:GIFMsg',
        'RC:ImgTextMsg',
        'RC:FileMsg',
        'RC:LBSMsg',
        'RC:SightMsg',
        'RC:ReferenceMsg',
        'RC:CombineMsg',
        'RC:GrpNtf',
        'RC:InfoNtf',
        'RC:RcNtf'
    ]

    RongIMLib.getHistoryMessagesByTimestamp(
        conversationType,
        targetId,
        objectNames,
        timeStamp,
        count,
        isForward,
        ({
            code,
            messages
        }) => {
            if (code === 0) {
                callback(messages)
            } else {
                uni.showToast({
                    icon: 'error',
                    title: code
                })
            }
        }
    )
}

// 获取好友申请列表
const getPendingList = (callback, total) => {
    total = total || 100
    RongIMLib.getConversationList([RongIMLib.ConversationType.SYSTEM], total, 0, (res) => {
        if (res.code === 0) {
            const pendings = res.conversations.filter((item) => {
                return item.objectName == RongIMLib.ObjectName.ContactNotification &&
                    item.latestMessage.operation === 'Request'
            })

            callback(pendings)
        }
    })
}

// 群组申请列表，邀请列表
const getGroupPendinglist = (targetId, callback) => {
    const conversationType = RongIMLib.ConversationType.SYSTEM
    const objectNames = ['RC:ContactNtf']

    RongIMLib.getHistoryMessagesByTimestamp(conversationType, targetId, objectNames, 0, 1000, true,
        ({
            messages
        }) => {
            callback(messages)
        }
    )
}

/**
 * 发送文本消息
 * @param {number}  conversationType 消息类型
 * @param {string}  targetId 会话id
 * @param {string}  content 消息内容
 * @param {function}  callback 回调函数
 */
const sentText = (conversationType, targetId, content, user, callback) => {
    const msg = {
        conversationType: conversationType,
        targetId: String(targetId),
        content: {
            objectName: 'RC:TxtMsg',
            content: content,
            userInfo: user
        }
    }

    RongIMLib.sendMessage(msg, ({
        code,
        messageId
    }) => {
        if (code === 0) {
            if (conversationType == 3) {
                RongIMLib.sendReadReceiptRequest(messageId, (res) => {
                    console.log('发送回执请求', res);
                })
            }
            callback(messageId)
        } else {
            console.log('发送失败', msg);
            uni.showToast({
                icon: 'none',
                title: '发送失败' + code
            })
        }
    })
}

/**
 * 发送消息
 * @param {number}  conversationType 消息类型
 * @param {string}  targetId 会话id
 * @param {string}  voiceUrl 录音的本地路径
 * @param {integer}  time 录音时长
 * @param {function}  callback 录音时长
 */
const sentVoice = (conversationType, targetId, voiceUrl, time, user, callback) => {
    const msg = {
        conversationType: conversationType,
        targetId: String(targetId),
        content: {
            objectName: 'RC:HQVCMsg',
            local: 'file:///' + plus.io.convertLocalFileSystemURL(voiceUrl),
            duration: time,
            userInfo: user
        }
    }
    RongIMLib.sendMediaMessage(msg, {
        success: (messageId) => {
            callback(messageId);
        },
        progress: (progress, messageId) => {
            console.log(messageId);
        },
        cancel: (messageId) => {
            // 发送取消回调
        },
        error: (errorCode, messageId) => {
            console.log(errorCode, messageId);
        }
    })
}

const sentImage = (conversationType, targetId, imageUrl, user, callback) => {
    const msg = {
        conversationType: conversationType,
        targetId: String(targetId),
        content: {
            objectName: 'RC:ImgMsg',
            local: 'file:///' + plus.io.convertLocalFileSystemURL(imageUrl),
            userInfo: user
        }
    }
    RongIMLib.sendMediaMessage(msg, {
        success: (messageId) => {
            callback(messageId);
        },
        progress: (progress, messageId) => {
            console.log(messageId);
        },
        cancel: (messageId) => {
            // 发送取消回调
        },
        error: (errorCode, messageId) => {
            console.log(errorCode, messageId);
        }
    })
}

const sentGif = (conversationType, targetId, gifUrl, time, user, callback) => {
    const msg = {
        conversationType: conversationType,
        targetId: String(targetId),
        content: {
            objectName: 'RC:GIFMsg',
            local: 'file:///' + plus.io.convertLocalFileSystemURL(gifUrl),
            duration: time,
            userInfo: user
        }
    }
    RongIMLib.sendMediaMessage(msg, {
        success: (messageId) => {
            callback(messageId);
        },
        progress: (progress, messageId) => {
            console.log(messageId);
        },
        cancel: (messageId) => {
            // 发送取消回调
        },
        error: (errorCode, messageId) => {
            console.log(errorCode, messageId);
        }
    })
}

const sendFile = (conversationType, targetId, fileUrl, time, user, callback) => {
    const msg = {
        conversationType: conversationType,
        targetId: String(targetId),
        content: {
            objectName: 'RC:FileMsg',
            local: 'file:///' + plus.io.convertLocalFileSystemURL(fileUrl),
            duration: time,
            userInfo: user
        }
    }
    RongIMLib.sendMediaMessage(msg, {
        success: (messageId) => {
            callback(messageId);
        },
        progress: (progress, messageId) => {
            console.log(messageId);
        },
        cancel: (messageId) => {
            // 发送取消回调
        },
        error: (errorCode, messageId) => {
            console.log(errorCode, messageId);
        }
    })
}

export default {
    getMessageList,
    getPendingList,
    getGroupPendinglist,
    sentText,
    sentVoice,
    sentImage,
    sentGif,
    sendFile
}
