<template>
  <!-- BASE CLASS FOR OUTGOING MESSAGE -->
  <div :class="messageClasses">
    <div class="senderAndTime">
      <div class="sender" v-if="room && room.displayType == ROOM_TYPE_CHANNEL">{{ eventSenderDisplayName(event) }}</div>
      <div class="time">
        {{ room.displayType == ROOM_TYPE_CHANNEL ? formatTimeAgo(event?.event.origin_server_ts) : formatTime(event?.event.origin_server_ts) }}
      </div>
      <div class="status">{{ event?.status }}</div>
    </div>

    <div class="op-button" ref="opbutton" v-if="event && !event.isRedacted() && $matrix.userCanSendMessageInCurrentRoom">
      <v-btn id="btn-show-menu" icon @click.stop="showContextMenu(opbutton)">
        <v-icon>more_vert</v-icon>
      </v-btn>
    </div>
    <div class="pin-icon" v-if="isPinned"><v-icon>$vuetify.icons.ic_pin_filled</v-icon></div>

    <!-- SLOT FOR CONTENT -->
    <span ref="messageInOutRef" class="content">
      <slot></slot>
    </span>
    <v-avatar
      class="avatar"
      size="32"
      color="#ededed"
      @click.stop="ownAvatarClicked"
    >
      <AuthedImage v-if="userAvatar" :src="userAvatar" />
      <span v-else class="text-white headline">{{ userAvatarLetter }}</span>
    </v-avatar>
    <QuickReactionsChannel v-if="room.displayType == ROOM_TYPE_CHANNEL" :event="eventForReactions" :timelineSet="timelineSet" v-bind="$attrs"/>
    <QuickReactions v-else :event="eventForReactions" :timelineSet="timelineSet" v-bind="$attrs"/>
    <SeenBy v-if="room.displayType != ROOM_TYPE_CHANNEL" :room="room" :event="event"/>
  </div>
</template>

<script setup lang="ts">
import SeenBy from "../SeenBy.vue";
import { MessageEmits, MessageProps, useMessage } from "./messageMixin";
import util, { ROOM_TYPE_CHANNEL } from "@/plugins/utils";
import QuickReactions from "../QuickReactions.vue";
import QuickReactionsChannel from "../channel/QuickReactionsChannel.vue";
import AuthedImage from "../../AuthedImage.vue";
import { inject, onMounted, ref } from "vue";
import { useI18n } from "vue-i18n";

const opbutton = ref(null);
const messageInOutRef = ref(null);

const { t } = useI18n()
const $matrix: any = inject('globalMatrix');

const props = defineProps<MessageProps>();
const emits = defineEmits<MessageEmits>();

const { room } = props;

const {
    event,
    thread,
    validEvent,
    eventForReactions,
    showSenderAndTime,
    inReplyToSender,
    inReplyToEvent,
    inReplyToText,
    messageText,
    isPinned,
    messageClasses,
    userAvatar,
    userAvatarLetter,
    ownAvatarClicked,
    otherAvatarClicked,
    showContextMenu,
    eventSenderDisplayName,
    eventStateKeyDisplayName,
    messageEventAvatar,
    senderIsAdminOrModerator,
    redactedBySomeoneElse,
    formatTimeAgo,
    formatTime,
    linkify,
    initMsgHammerJs,
  } = useMessage($matrix, t, props, emits);

onMounted(() => {
  if (util.isMobileOrTabletBrowser() && messageInOutRef.value && opbutton.value) {
    initMsgHammerJs(messageInOutRef.value, opbutton.value);
  }
})
</script>
<style lang="scss">
@use "@/assets/css/chat.scss" as *;
</style>