<script setup lang="ts">
import { computed, readonly } from 'vue'
import { CheckOne } from '@icon-park/vue-next'
import type { MessageItemInterface } from '../types'
import { useUserStore } from '@/store/modules/user'
import MessageBubbleLoading from './message-bubble-loading.vue'
import { useI18n } from 'vue-i18n'

interface Props {
  messageItem: MessageItemInterface
}

const props = defineProps<Props>()

const { t } = useI18n()
const userStore = useUserStore()

const agentDefaultAvatarUrl = readonly({ url: 'https://gsst-poe-sit.gz.bcebos.com/icon/agent-avatar.png' })

const isAgentMessage = computed(() => {
  return props.messageItem.role === 'assistant'
})

const avatarUrl = computed(() => {
  return isAgentMessage.value ? props.messageItem.avatar || agentDefaultAvatarUrl.url : userStore.userInfo.avatarUrl
})

const name = computed(() => {
  return isAgentMessage.value ? props.messageItem.name || 'AI助理' : userStore.userInfo.nickName
})
</script>

<template>
  <div class="mb-[20px] last:mb-0">
    <div class="flex">
      <img class="h-[36px] w-[36px] rounded-[6px] object-cover" :src="avatarUrl" alt="Avatar" />

      <div class="ml-[11px] overflow-hidden">
        <div class="mb-[7px] text-[12px] text-[#999]">
          {{ name }}
        </div>

        <div
          class="box-content min-h-[21px] min-w-[10px] max-w-full rounded-[10px] border border-[#9EA3FF] px-[15px] py-[14px] text-justify"
          :class="{
            'bg-[#777EF9]': isAgentMessage,
            'text-[#fff]': isAgentMessage,
            '!min-w-[80px]': messageItem.isAnswerLoading,
          }"
        >
          <img
            v-show="!isAgentMessage && messageItem.imageUrl"
            :src="messageItem.imageUrl"
            class="max-h-[120px]! mb-[14px] rounded-[10px] object-contain"
          />
          <div
            v-if="messageItem.isAnswerLoading && !messageItem.content"
            class="flex h-[21px] items-center justify-center"
          >
            <MessageBubbleLoading :active-color="isAgentMessage ? '#fff' : '#192338'" />
          </div>
          <template v-else>
            <MarkdownRender
              :raw-text-content="
                messageItem.content ? messageItem.content : t('common_module.dialogue_module.empty_message_content')
              "
              :color="isAgentMessage ? '#fff' : '#192338'"
            />

            <div v-if="messageItem.isAnswerLoading" class="ml-[15px] pt-[12px]">
              <MessageBubbleLoading active-color="#fff" width="5px" />
            </div>
          </template>
        </div>

        <div
          v-show="isAgentMessage && messageItem.pluginName"
          class="mt-[7px] flex items-center gap-[5px] font-['Microsoft_YaHei_UI'] text-[#999]"
        >
          <div
            v-show="!messageItem.content"
            class="bg-px-plugin_loading-gif h-[14px] w-[14px] bg-contain bg-center bg-no-repeat"
          />
          <CheckOne v-show="messageItem.content" theme="outline" size="16" fill="#40bd23" />

          <span class="leading-5">
            {{
              !messageItem.content
                ? t('common_module.plugin_in_progress', { pluginName: messageItem.pluginName })
                : t('common_module.plugin_executed_successfully', { pluginName: messageItem.pluginName })
            }}
          </span>
        </div>
      </div>
    </div>
  </div>
</template>
