<script setup lang="ts">
import { computed, readonly, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { CheckOne, Down } from '@icon-park/vue-next'
import type { MessageItemInterface } from '../types'
import MarkdownRender from '@/components/markdown-render/markdown-render.vue'
import MessageBubbleLoading from './message-bubble-loading.vue'

interface Props {
  messageItem: MessageItemInterface
}

const props = defineProps<Props>()

const emit = defineEmits<{
  showKnowledgeResult: []
}>()

const { t } = useI18n()

const isShowReasoningContent = ref(true)

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

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

const assistantAvatarUrl = computed(() => {
  return props.messageItem.avatar || agentAvatarUrl.url
})

function handleShowReasoningContentSwitch() {
  isShowReasoningContent.value = !isShowReasoningContent.value
}
</script>

<template>
  <div class="mb-[20px] last:mb-0">
    <div class="flex">
      <img
        class="h-[36px] w-[36px] flex-shrink-0 rounded-full object-cover"
        :src="isAssistant ? assistantAvatarUrl : props.messageItem.avatar"
        alt="Avatar"
      />

      <div class="ml-[11px] flex flex-col items-start overflow-hidden">
        <template v-if="isAssistant && messageItem.nickName === 'DeepSeek'">
          <div class="mb-[7px] select-none text-[14px]">
            <div class="inline-flex cursor-pointer" @click="handleShowReasoningContentSwitch">
              <span v-if="messageItem.isTextContentLoading" class="mr-[6px]">
                {{ t('common_module.deep_thinking', { modelName: messageItem.nickName }) }}
              </span>
              <span v-else class="mr-[6px]">{{ t('common_module.have_thought_deeply') }}</span>
              <Down
                theme="outline"
                size="21"
                fill="#333"
                :stroke-width="3"
                class="transition-[rotate] duration-100 ease-linear"
                :class="{ '-rotate-180': isShowReasoningContent }"
              />
            </div>
          </div>

          <n-collapse-transition :show="isShowReasoningContent">
            <div
              v-show="messageItem.reasoningContent"
              class="my-[14px] border-l-[1px] border-solid border-l-[#ccc] p-[13px]"
            >
              <MarkdownRender
                :raw-text-content="
                  messageItem.reasoningContent
                    ? messageItem.reasoningContent
                    : t('common_module.dialogue_module.empty_message_content')
                "
                color="#999"
              />
            </div>
          </n-collapse-transition>
        </template>
        <div v-else class="mb-[7px] line-clamp-1 break-all text-[12px] text-[#999]">
          {{ props.messageItem.nickName }}
        </div>

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

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

        <div class="flex min-w-[40px] max-w-full flex-col items-start overflow-hidden">
          <div
            class="min-h-[21px] w-full flex-wrap rounded-[10px] border border-[#9EA3FF] px-[15px] py-[12px] text-justify"
            :class="{
              'bg-[#777EF9]': isAssistant,
              'text-[#fff]': isAssistant,
              '!min-w-[80px]': messageItem.isTextContentLoading,
            }"
          >
            <img
              v-show="!isAssistant && messageItem.imageUrl"
              :src="messageItem.imageUrl"
              class="max-h-[120px]! mb-[12px] rounded-[10px] object-contain"
            />

            <div v-if="messageItem.isTextContentLoading" class="flex h-[21px] w-full items-center justify-center">
              <MessageBubbleLoading :active-color="isAssistant ? '#fff' : '#192338'" />
            </div>

            <div v-else>
              <MarkdownRender
                :raw-text-content="
                  messageItem.content ? messageItem.content : t('common_module.dialogue_module.empty_message_content')
                "
                :color="isAssistant ? '#fff' : '#192338'"
              />

              <div
                v-show="isAssistant && messageItem.isAnswerResponseLoading"
                class="mt-2.5 flex h-[21px] w-[30px] items-center justify-center"
              >
                <MessageBubbleLoading :active-color="isAssistant ? '#fff' : '#192338'" />
              </div>
            </div>
          </div>

          <div class="flex justify-between py-[2px]">
            <div
              v-show="isAssistant && messageItem.knowledgeContentResult.length"
              class="flex-center rounded-theme h-8 cursor-pointer gap-[5px] px-[14px] font-['Microsoft_YaHei_UI'] text-[#0B7DFF] hover:opacity-80"
              @click="emit('showKnowledgeResult')"
            >
              <i class="iconfont icon-knowledge" />
              <span>{{ t('common_module.knowledge') }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
