<template>
	<div class="flex_c row">
		<div class="flex_r text-box" :class="{ text_box: isMy }" @tap.stop="onClick">
			<div
				class="text_32 text"
				:class="isMy ? 'text_r' : 'text_l'"
				:hover-class="isMy ? 'hover_classr' : 'hover_classl'"
				:hover-stay-time="60"
				:style="{ whiteSpace: 'pre-wrap' }"
				v-html="renderTextMessage"
			></div>
		</div>
		<div class="text_26 flex_r" :class="{ row_: isMy }" v-if="value.type === 'text_quote'">
			<div v-if="value.payload.quoteSource.type === 'image' || value.payload.quoteSource.type === 'image_transmit'">
				<m-image :value="value.payload.quoteSource"></m-image>
			</div>
			<div class="" v-else-if="value.payload.quoteSource.type === 'audio' || value.payload.quoteSource.type === 'audio_quote'">
				<m-audio :value="value.payload.quoteSource"></m-audio>
			</div>
			<div class="" v-else-if="value.payload.quoteSource.type === 'text' || value.payload.quoteSource.type === 'text_quote'">
				<m-text :value="value.payload.quoteSource"></m-text>
			</div>
			<div class="" v-else>
				<m-other :value="value.payload.quoteSource"></m-other>
			</div>
		</div>
	</div>
</template>

<script>
import mText from './quoteType/m-text.vue';
import mImage from './quoteType/m-image.vue';
import mAudio from './quoteType/m-audio.vue';
import mOther from './quoteType/m-other.vue';

import { EmojiDecoder, emojiMap } from '@/util/EmojiDecoder.js';
const emojiUrl = 'https://imgcache.qq.com/open/qcloud/tim/assets/emoji/';
const decoder = new EmojiDecoder(emojiUrl, emojiMap);
export default {
	components: {
		mText,
		mImage,
		mAudio,
		mOther
	},
	props: {
		isMy: {
			type: [Boolean, Number],
			default: false
		},
		value: {
			type: Object,
			default: {}
		}
	},
	data() {
		return {};
	},
	computed: {
		//渲染文本消息，如果包含表情，替换为图片
		//todo:本不需要该方法，可以在标签里完成，但小程序有兼容性问题，被迫这样实现
		renderTextMessage() {
			const { text = '' } = this.value.payload;
			if (!text) return '<span>' + '[未知内容]' + '</span>';
			return '<span>' + decoder.decode(text) + '</span>';
		}
	},
	methods: {
		onClick() {
			this.$emit('onClick');
		}
	}
};
</script>

<style scoped lang="scss">
.row {
}
.row_ {
	flex-direction: row-reverse;
}
.text_box {
	flex-direction: row-reverse;
}
.text {
	position: relative;
	z-index: 99;
	box-sizing: border-box;
	padding: 16px 26px;
	border-radius: 8px;
	background-color: #fff;
	word-break: break-all;
	vertical-align: center;
	font-size: 14px;
	span{
		font-size: 14px;
	}
}

.text_r {
	position: relative;
	background-color: #95ec6a;
}
.text_l {
	position: relative;
}

.text_r::after {
	position: absolute;
	z-index: -1;
	content: '';
	top: 26px;
	right: -8px;
	width: 18px;
	height: 18px;
	border-radius: 2px;
	transform: rotate(45deg);
	background-color: #95ec6a;
}
.text_l::after {
	position: absolute;
	z-index: -1;
	content: '';
	top: 26px;
	left: -8px;
	width: 18px;
	height: 18px;
	border-radius: 2px;
	transform: rotate(45deg);
	background-color: #fff;
}

.hover_classr {
	background-color: #89d961;
}
.hover_classl {
	background-color: #e2e2e2;
}

.hover_classr::after {
	position: absolute;
	z-index: -1;
	content: '';
	top: 26px;
	right: -8px;
	width: 18px;
	height: 18px;
	border-radius: 2px;
	transform: rotate(45deg);
	background-color: #89d961;
}
.hover_classl::after {
	position: absolute;
	z-index: -1;
	content: '';
	top: 26px;
	left: -8px;
	width: 18px;
	height: 18px;
	border-radius: 2px;
	transform: rotate(45deg);
	background-color: #e2e2e2;
}
</style>
