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

export default {
    data() {
        return {
            winSize: {},
            showPop: false,
            /* 弹窗按钮列表 */
            popButton: ['复制', '删除', '撤回'],
            /* 弹窗定位样式 */
            popStyle: "",
            pickedItem: {}
        }
    },
    onLoad() {
        uni.getSystemInfo({
            success: (e) => {
                this.winSize = {
                    width: e.windowWidth,
                    height: e.windowHeight
                }
            }
        })
    },
    methods: {
        // 隐藏功能菜单
        hidePop() {
            this.showPop = false
            this.pickedItem = {}
            setTimeout(() => {
                this.showShade = false
            }, 250)
        },
        // 点击会话功能菜单
        pickerMenu(e) {
            const msg = this.pickedItem
            console.log(msg);
            switch (e) {
                case '复制':
                    uni.setClipboardData({
                        data: msg.content.content
                    })
                    uni.showToast({
                        icon: 'none',
                        title: '复制成功'
                    })
                    break;
                case '删除':
                    IMLib.deleteMessagesByIds([msg.messageId], ({
                        code
                    }) => {
                        if (code === 0) {
                            uni.$emit('onRemoveMessage_' + msg.targetId, msg.messageId)
                            uni.showToast({
                                title: '消息删除成功',
                                icon: 'none',
                            })
                        }
                    })
                    break;
                case '撤回':
                    const pushContent = this.$store.getters.sender.name + '撤回了一条消息'
                    IMLib.recallMessage(msg.messageId, pushContent,
                        ({
                            code,
                            message
                        }) => {
                            if (code === 0) {
                                uni.showToast({
                                    icon: 'none',
                                    title: '消息撤回成功'
                                })
                                IMLib.getMessage(msg.messageId, res => {
                                    uni.$emit('onRecallMessage_' + msg.targetId, res.message)
                                })
                            } else {
                                uni.showToast({
                                    icon: 'none',
                                    title: '撤回失败' + code
                                })
                            }
                        }
                    )
                    break;
            }
            this.hidePop()
        },
        messageAction(e) {
            let [touches, style, item] = [e.event.touches[0], "", e.message]

            if (touches.clientY > (this.winSize.height / 2)) {
                style = `bottom:${this.winSize.height-touches.clientY}px;`
            } else {
                style = `top:${touches.clientY}px;`
            }

            if (touches.clientX > (this.winSize.width / 2)) {
                style += `right:${this.winSize.width-touches.clientX}px`
            } else {
                style += `left:${touches.clientX}px`
            }

            if (item.messageDirection == 2) {
                if (item.objectName == 'RC:TxtMsg') {
                    this.popButton = ['复制', '删除']
                } else {
                    this.popButton = ['删除']
                }
            } else {
                if ((new Date()).getTime() - item.sentTime > 120 * 1000) {
                    if (item.objectName == 'RC:TxtMsg') {
                        this.popButton = ['复制', '删除']
                    } else {
                        this.popButton = ['删除']
                    }
                } else {
                    if (item.objectName == 'RC:TxtMsg') {
                        this.popButton = ['复制', '撤回']
                    } else {
                        this.popButton = ['删除']
                    }
                }
            }

            this.popStyle = style
            this.pickedItem = item
            this.$nextTick(() => {
                setTimeout(() => {
                    this.showPop = true;
                }, 10)
            })
        },
    }
}
