<template>
  <view v-if="quoteMessage && user" class="message-box" @click='scrollTo'>
    <view v-if="quoteMessage.messageType === MessageType.text" class="message">
      {{ user.name }}:{{ quoteMessage.content }}
    </view>
    <view v-if="quoteMessage.messageType === MessageType.image" class="message-img">
      <text>{{ user.name }}:</text>
      <image alt="图片" :src="quoteMessage.extend?.url" style="height: 60rpx;width: 60rpx" />
    </view>
    <view v-if="quoteMessage.messageType === MessageType.file" class="message">
      <text>{{ user.name }}:</text>{{ getFileName(quoteMessage.extend?.fileName) }}
    </view>
    <view v-if="quoteMessage.messageType === MessageType.video" class="message-img">
      <text>{{ user.name }}:</text>
      <video style="height: 100%">
        <source :src="quoteMessage.extend?.url" type="video/mp4" />
      </video>
    </view>
    <view v-if="quoteMessage.messageType === MessageType.voice" class="message">
      <view>
        <text>{{ user.name }}:</text>
        <text class="cuIcon-sound text-xxl" :class="{ 'icon-v-voice-right': quoteMessage.mine }"></text>
      </view>
    </view>
  </view>
</template>
<script setup lang="ts">
import {ref} from 'vue'
import MessageType from '../utils/MessageType'
import {storeToRefs} from 'pinia'
import {useUserStore} from '@/store/userStore'
import {getFileName} from '@/utils/FileUtils'
import type Message from "@/mode/Message";
import {last} from "@/hooks/useMessageImageLoad";
import type UserSimple from "@/mode/UserSimple";

const { userMap } = storeToRefs(useUserStore())
interface IProps{
  message:Message
}
const props = defineProps<IProps>();
const quoteMessage = ref<Message|undefined>(props.message.extend?.quoteMessage)
const user = ref<UserSimple>()
if(quoteMessage.value){
  user.value = userMap.value.get(quoteMessage.value?.fromId)
}

const scrollTo = () => {
  last.value = 'm-' + props.message.extend?.quoteMessage?.id
}

</script>
<style scoped lang="scss">
.message-box {
  cursor: pointer;
  flex: 1;
  overflow: hidden;
  background-color: #e9e9e9;
  margin-top: 10px;
  padding: 5px 11px;
  color: #999999;
  font-size: 24rpx;
  .message {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: inline-block;
    max-width: 100%;
  }
  .message-img {
    line-height: 30px;
    white-space: nowrap;
    display: inline-flex;
    padding: 0 11px;
    max-width: 620px;
  }
}
</style>
