<template>
    <view class="msg--image">
        <message-state :message="message" :isGroup="isGroup" :isRemote="isRemote" />

        <view class="">
            <view class="name" v-if="isGroup && isRemote">{{ contact(message.senderUserId).name }}</view>
            <view class="image" :class="isRemote ? 'left': 'right'">
                <image class="img" :src="content.thumbnail" @click="previewImage" mode="widthFix"></image>
            </view>
        </view>
    </view>
</template>

<script>
    import * as RongIMLib from '@/uni_modules/RongCloud-IMWrapper/js_sdk/index'
    import messageState from './messageState'
    import imBase from '../../mixins/imBase.js'
    
    export default {
        mixins: [
            imBase
        ],
        name: 'showImage',
        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
            }
        },
        methods: {
            showImage(path) {
                uni.previewImage({
                    urls: [
                        path
                    ]
                })
            },
            previewImage() {
                if (this.content.local && this.content.local.indexOf('///data/user/') < 0) {
                    this.showImage(this.content.local)
                } else {
                    this.showImage(this.content.remote)
                    RongIMLib.downloadMediaMessage(this.message.messageId, {
                        success: (path) => {
                            this.content.local = path
                        },
                        error: (errorCode, messageId) => {
                            uni.showToast({
                                icon: 'none',
                                title: errorCode
                            })
                        }
                    })
                }
            }
        }
    }
</script>

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

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

        .image {
            .img {
                width: 180rpx;
            }

            &.left {
                .img {
                    border-radius: 0 10rpx 10rpx 10rpx;
                }
            }

            &.right {
                .img {
                    border-radius: 10rpx 0 10rpx 10rpx;
                }
            }
        }
    }
</style>
