<script setup lang="ts">
import { computed, readonly, ref } from 'vue'
import { CheckOne, Down } 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 isShowReasoningContent = ref(true)

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
})

function handleShowReasoningContentSwitch() {
  isShowReasoningContent.value = !isShowReasoningContent.value
}
</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">
        <template v-if="isAgentMessage && messageItem.name === 'Deepseek R1'">
          <div class="mb-[7px] select-none text-[14px]">
            <div class="inline-flex cursor-pointer" @click="handleShowReasoningContentSwitch">
              <span class="mr-[6px]">{{ name }}</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 class="my-[14px] border-l-[1px] border-solid border-l-[#ccc] p-[13px]">
              <div>
                <img
                  v-if="!messageItem.reasoningContent && !messageItem.content"
                  src="@/assets/images/home/bubble-loading.gif"
                  alt="bubble-loading"
                />
                <template v-else>
                  <MarkdownRender
                    :raw-text-content="
                      messageItem.reasoningContent
                        ? messageItem.reasoningContent
                        : t('common_module.dialogue_module.empty_message_content')
                    "
                    color="#999"
                  />
                </template>
              </div>
            </div>
          </n-collapse-transition>
        </template>
        <div v-else 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>
