<template>
    <view class="msg--text">
        <message-state :message="message" :isGroup="isGroup" />
        <view class="">
            <view class="name" v-if="isGroup && isRemote">{{ contact(message.senderUserId).name }}</view>
            <view @longpress="backMessage" :class="['text', isRemote ? 'left': 'right']">{{ content }}</view>
        </view>
    </view>
</template>

<script>
    import * as RongIMLib from '@/uni_modules/RongCloud-IMWrapper/js_sdk/index'
    import messageState from './messageState'

    export default {
        name: 'showText',
        props: {
            message: {
                type: Object,
                default: () => {
                    return {}
                }
            },
            isGroup: {
                type: Boolean,
                default: false
            }
        },
        components: {
            messageState
        },
        computed: {
            isRemote() {
                return this.message.messageDirection == 2
            },
            content() {
                return this.message.content.content
            },
            contact() {
                return function(targetId) {
                    return this.$store.getters.contactInfo(targetId)
                }
            }
        },
        methods: {
            // 撤回消息测试
            backMessage() {
                if (this.$store.getters.sender.userId != this.message.senderUserId) {
                    uni.showToast({
                        icon: 'none',
                        title: '不能撤回别人的消息'
                    })
                    return
                }
                // 判断时间超过了多久 就不能撤回 TODO
                // this.message.sentTime
                const pushContent = this.$store.getters.sender.name + '撤回了一条消息'
                RongIMLib.recallMessage(this.message.messageId, pushContent,
                    ({
                        code,
                        message
                    }) => {
                        if (code === 0) {
                            uni.showToast({
                                icon: 'none',
                                title: '消息撤回成功'
                            })
                            RongIMLib.getMessage(this.message.messageId, res => {
                                uni.$emit('onRecallMessage', res.message)
                            })
                        } else {
                            uni.showToast({
                                icon: 'none',
                                title: '撤回失败' + code
                            })
                        }
                    }
                )
            }
        }
    }
</script>

<style scoped lang="scss">
    .msg--text {
        display: flex;
        align-items: flex-end;

        .name {
            font-size: 24rpx;
            color: $text-gray-m;
        }

        .text {
            box-sizing: border-box;
            max-width: 502rpx;
            padding: 20rpx;
            line-height: 46rpx;
            font-size: 32rpx;
            color: $text-color;
            word-break: break-all;

            &.left {
                border-radius: 0 20rpx 20rpx 20rpx;
                background: white;
            }

            &.right {
                border-radius: 20rpx 0 20rpx 20rpx;
                background: $main-color;
                color: white;
            }
        }
    }
</style>
