<!-- Chat.vue -->
<template>

  <div class="chat-container" style="padding-top: 3rem;">
    <!-- <div v-if="!messages.length && !msgLoading" class="message-list">
      <pageMask />
    </div> -->
    <!-- 消息列表 -->
    <div class="message-list">
      <el-scrollbar ref="scrollbar" @scroll="handleScroll" v-loading="msgLoading">
        <!-- 加载更多指示器 -->
        <div v-if="isLoadingMore" class="loading-more-indicator">
          <div class="loading-spinner"></div>
          <span>加载更多消息...</span>
        </div>
        <div class="messages" ref="messagesContainer">
          <div v-for="(message, index) in messages" :key="index"
               :class="['message-item', message.role === 'user'  ? 'self' : 'other']">
            <el-avatar :size="32" :src="message.role === 'user' ? userAvatar : avatar" />
            <div class="message-content">
              <div class="content" v-if="message.role ==='system'"
                   :class="{ 'loading-content': message.content === '' }">
                <StepsDisplay
                  v-if="message.type === 'plan'"
                  :content="message.rawContent"
                  :initialStep="0"
                  @step-change="handleStepChange" @complete="handleComplete"
                />
                <span v-else v-html="message.content"></span>
                <span class="loading-indicator" v-if="sendLoading && index === messages.length - 1">
                  <span class="dot"></span>
                  <span class="dot"></span>
                  <span class="dot"></span>
                </span>
              </div>
              <document v-else-if="message.type === 'document' && message.role === 'user'" :content="message.content"
                        :rawContent="message.rawContent" />
              <div v-else class="content">
                <span v-if="message.type === ''">{{ message.content }}</span>
                <span class="loading-indicator" v-if="sendLoading && index === messages.length - 1">
                  <span class="dot"></span>
                  <span class="dot"></span>
                  <span class="dot"></span>
                </span>
              </div>
              <div class="timestamp ">{{ moment(message.sortKey).format('MM-DD HH:mm') }}
                <!-- <span v-if="message.add" style="cursor: pointer;" @click="handleInput">填充</span> -->
              </div>
            </div>
          </div>
        </div>
      </el-scrollbar>
      <ScrollToBottom :target="scrollbar" ref="scrollToBottomRef" />
    </div>

    <!-- <Tools v-if="selectModal" :disHistory="sendLoading" :upload="type === FunctionList.File_Operation || !!formInfo" @read-click="readClick" @upload-file="(file) => createFileObj(file)" @handle-capture="handleCapture" @his-records="hisRecords"
      @add-new-dialogue="addNewDialogue" @handle-current-change="handleCurrentChange"
      @handel-intelligent-filling-click="handelIntelligentFillingClick" /> -->

    <div>
      <!-- 输入区域 -->
      <div class="input-area">
        <el-icon class="closeShow" :style="{ display: isShowPage ? 'block' : 'none'}" size="16px" color="#909399"
                 @click="closePageInfo">
          <CircleClose />
        </el-icon>
        <!-- <div v-show="isShowPage" style="border-bottom: 1px solid #F0F0F0;">
          <div class="card_list">
            <div v-for="(v, i) in pageInfoList" :key="i"
              :class="`card-content ${pageInfoList.length > 1 ? 'card_width' : ''}`">
              <div class="loading-more-indicator" v-if="v.loading">
                <div class="loading-spinner"></div>
              </div>
              <img v-else :src="v?.favIconUrl" style="width: 24px;display: block" />

              <div class="title-wrapper">
                <span class="els title-scroller">{{ v?.title }}</span>
                <span class="els url-scroller">{{ v.loading ? v?.state : v?.url }}</span>
              </div>
              <el-icon class="closeIcon" size="16px" color="#909399" @click="deletePageInfo(i)">
                <CircleClose />
              </el-icon>
            </div>
           <div v-show="!pageInfoList.length && type === FunctionList.Intelligent_Form_filling">智能填表</div>
          </div>

          <div class="card-btn">
            <el-tooltip  content="总结" placement="top">
              <el-button :disabled="disabledBtn" v-if="type !== FunctionList.Intelligent_Form_filling"  round @click="handleSummary">总结</el-button>
            </el-tooltip>
             <el-tooltip   content="抽取表单信息" placement="top">
              <el-button :disabled="disabledBtn" v-if="type === FunctionList.Intelligent_Form_filling && pageInfoList.length" round @click="handleSummaryFile">抽取</el-button>
            </el-tooltip>
          </div>
        </div> -->

        <el-input ref="textareaRef" v-model="inputMessage" type="textarea" :rows="3" placeholder="输入消息..."
                  @keyup.enter="() => handleAsk()" />
        <div class="chat_area_op">

          <el-button v-if="sendLoading" style="background-color:#ffffff;border:2px solid rgb(134 143 153);" circle
                     @click="handleStopAsk">
            <svg-icon icon-class="stop" color="red" />
          </el-button>
          <el-button
            v-else
            :style="`background-color:${inputMessage.trim() ? '#4d6bfe' : '#d6dee8'};border-color:${inputMessage.trim() ? '#4d6bfe' : '#d6dee8'}`"
            type="primary" circle @click="() => handleAsk()"
            :disabled="disSend">
            <svg-icon icon-class="send" color="#000000" />
          </el-button>
        </div>
      </div>

    </div>

    <!-- <historyComponent :msgUuid="msgUuid" ref="historyComponentRef" @currentData="(e) => handleCurrentData(e)" /> -->
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, nextTick, inject, useTemplateRef, reactive, onBeforeMount } from 'vue'
import { ElScrollbar, ElAvatar, ElInput, ElButton } from 'element-plus'
import moment from 'moment'
import { sendMessage } from 'webext-bridge/side-panel'
import {
  controllerList
} from '@/entrypoints/sidepanel/utils/ai-service.js'
import { storeToRefs } from 'pinia'
import { useMsg } from '@/entrypoints/sidepanel/hook/useMsg.ts'
import document from '@/entrypoints/sidepanel/component/document.vue'
import ScrollToBottom from '@/entrypoints/sidepanel/component/ScrollToBottom.vue'
import StepsDisplay from './component/StepsDisplay.vue'
import userAvatar from '@/assets/images/user.png'
import avatar from '@/public/icon/icon.png'
import { FunctionList } from '@/entrypoints/sidepanel/mock'
import { useAutoResizeTextarea } from '@/entrypoints/sidepanel/hook/useAutoResizeTextarea.ts'
import { useMsgStore } from '@/store/modules/msg.ts'
import { useUserStore } from '@/store/modules/user'
import { debounce } from 'lodash'

const userStore = useUserStore()
// 在其他状态变量附近添加
const isLoadingMore = ref(false)
import { getPlan2 } from '@/api/advance.js'
// 滚动条引用
const scrollbar = ref(null)
const scrollToBottomRef = ref(null)
const isShowPage = ref(false)
const tareRef = useTemplateRef('textareaRef')
const disSend = computed(() => {
  if (pageInfoList.value.length && type.value === FunctionList.Intelligent_Form_filling) return true
  if (sendLoading.value) return true
  if (inputMessage.value.trim()) return false
  return true
})
// 获取父组件提供的 Hook 实例
const msgStore = useMsgStore()
const { pageInfoList, messages, msgUuid, hasNext, msgLoading } = storeToRefs(useMsgStore())
const formInfo = ref('')
const {
  taklToHtml,
  sendLoading,
  type,
  fetchRes
} = useMsg(scrollbar)
const inputMessage = ref('')
const pageInfo = ref('')
const summaryHtml = ref(false)

function handleStopAsk() {
  inputMessage.value = ''
  pageInfoList.value = []
  isShowPage.value = false
  taklToHtml.value = false
  type.value = FunctionList.File_Operation
  controllerList.value[0]?.abort()
  controllerList.value = []
  sendLoading.value = false
  formInfo.value = null
}

function handleScroll(a) {
  const { wrapRef } = scrollbar.value
  const { scrollHeight, clientHeight } = wrapRef
  scrollToBottomRef.value.showButton = scrollHeight - a.scrollTop - clientHeight >= 350

  // 检测是否滚动到顶部
  if (a.scrollTop <= 10 && hasNext.value) {
    handleScrollToTop()
  }
}

const messagesContainer = ref(null)
let innerText = ''
// 处理滚动到顶部的事件
const handleScrollToTop = debounce(async function() {
  console.log('滚动到顶部，可以加载更多历史消息')
  if (!sendLoading.value && !isLoadingMore.value) {
    isLoadingMore.value = true
    try {
      // 记录当前第一条消息的位置
      const firstMessage = messagesContainer.value?.firstElementChild.querySelector('.timestamp')
      innerText = firstMessage.innerText
      const oldHeight = firstMessage?.offsetTop || 0
      await msgStore.changePage()
      setTimeout(() => {
        if (firstMessage) {
          console.log([...messagesContainer.value.querySelectorAll('.timestamp')].find(_ => _.innerText === innerText).offsetTop, innerText, 778)
          const newHeight = [...messagesContainer.value.querySelectorAll('.timestamp')].find(_ => _.innerText === innerText).offsetTop
          const scrollOffset = newHeight - oldHeight
          scrollbar.value?.setScrollTop(scrollOffset)
        }
      }, 400)
      // 恢复滚动位置

    } finally {
      isLoadingMore.value = false
    }
  }
}, 500, { leading: false, trailing: true })

const handleStepChange = (step) => {
  console.log('当前步骤:', step)
}
const handleComplete = () => {
  console.log('所有步骤已完成')
}

/**
 * @param {string} msg
 * @param {string} raw
 * @param {string} type 发送的类型 document 、text
 * **/
async function addMessage(msg, raw, type) {
  const newMessage = reactive({
    id: +new Date(),
    type: type || '',
    rawContent: raw ?? msg,
    senderId: userStore.userInfo.id,
    receiverId: -1, //大模型
    content: msg,
    sortKey: moment().valueOf(),
    role: 'user',
    conversationId: msgUuid.value,
    addToHistory: `${!taklToHtml.value}`,
    redisKey: undefined,
    fileId: undefined
  })
  if (type === 'document') {
    newMessage.content = msg
    newMessage.rawContent = raw
  }
  if (!msg) return
  messages.value.push(newMessage)
  nextTick(() => {
    if (scrollbar.value && scrollbar.value.wrapRef) {
      scrollbar.value.setScrollTop(scrollbar.value.wrapRef.scrollHeight)
    }
  })
  return newMessage
}

let SystemMsg = null

watchEffect(() => {
  if (!isShowPage.value) {
    summaryHtml.value = false
    type.value = FunctionList.File_Operation
    SystemMsg = null
    inputMessage.value = ''
  }
})

function closePageInfo() {
  pageInfoList.value = []
  if (type.value === FunctionList.Intelligent_Form_filling && SystemMsg) {
    SystemMsg.content = '智能填表流程中断'
    SystemMsg.rawContent = '智能填表流程中断'
    putChat({
      ...SystemMsg,
      content: ''
    })
  }
  handleStopAsk()
}

async function handleAsk(value) {
  if (sendLoading.value) return
  const str = value ?? inputMessage.value.trim()
  inputMessage.value = ''
  if (type.value === FunctionList.Intelligent_Form_filling) {
    const msg = await addMessage(str)
    // await putChat(msg)
    const [res, obj] = await fetchRes(str)
    if (res.status === 'ok') {
      console.log(obj)
      formInfo.value = res.data
      SystemMsg = obj
    } else {
      type.value = FunctionList.File_Operation
      isShowPage.value = false
    }
    return
  }
  if (sendLoading.value) return
  let msg = null
  if (pageInfoList.value.length) {
    msg = await addMessage(JSON.stringify([...pageInfoList.value, { type: 'text', value: str }]), str, 'document')
    msg.fileId = pageInfoList.value[0].fileId
    msg.redisKey = pageInfoList.value[0].redisKey
  } else msg = await addMessage(str)
  pageInfoList.value = []
  isShowPage.value = false
  // await putChat(msg)
  createWS(msg)
}

/**
 *
 * @param msg 用户消息对象，调用askQues时需要
 */
async function createWS(msg) {
  sendLoading.value = true
  const obj = reactive({
    type: '',
    rawContent: '',
    senderId: -1,
    receiverId: userStore.userInfo.id, //大模型
    content: '',
    sortKey: moment().valueOf(),
    role: 'system',
    conversationId: msgUuid.value
  })
  try {
    messages.value.push(obj)
    nextTick(() => {
      if (scrollbar.value && scrollbar.value.wrapRef) {
        scrollbar.value.setScrollTop(scrollbar.value.wrapRef.scrollHeight)
      }
    })
    let params = {
      conversationId: Math.floor(Math.random() * 1000) + 1,
      messageId: Math.floor(Math.random() * 1000) + 1,
      messageContent: msg.rawContent
    }
    getPlan2(params).then(res => {
      sendLoading.value = false
      obj.type = 'plan'
      console.log(res, 5558)
      obj.rawContent = res.data.plan
      sendMessage('FROM_PLAN', {
        params,
        payload: { ...res.data.plan }
      }, 'background')
    }).catch(res => {
      obj.rawContent = '接口出错，请重试。'
      obj.content = '接口出错，请重试。'
    })
  } finally {
  }
}

// 组件挂载时滚动到底部
onBeforeMount(async () => {
  await msgStore.initMsg()
  await msgStore.initModal()
})
onMounted(async () => {
  // msgStore.updateAIModel(options[0].options[0])
  useAutoResizeTextarea(tareRef, inputMessage)
  chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
    if (message.type === 'TO_SIDE_PANEL_PAGE_INFO') {
      pageInfo.value = message.data
      // 转发到 content.js
      // chrome.tabs.sendMessage(sender.tab.id, {
      //   type: 'TO_CONTENT_SCRIPT',
      //   data: message.data
      // })
    }
    if (message.type === 'TO_SIDE_PANEL_PAGE_CHANGE') {
      pageInfo.value = message.data
    }
  })
  nextTick(() => {
    if (scrollbar.value && scrollbar.value.wrapRef) {
      scrollbar.value.setScrollTop(scrollbar.value.wrapRef.scrollHeight)
    }
  })
})
</script>

<style lang="scss" scoped>
@use '@/entrypoints/sidepanel/css/chat.scss';
@use '@/entrypoints/sidepanel/css/markdown.scss';

.loading-more-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 0;
  color: #909399;
  font-size: 14px;

  .loading-spinner {
    width: 20px;
    height: 20px;
    margin-right: 8px;
    border: 2px solid #e6e6e6;
    border-top-color: #4d6bfe;
    border-radius: 50%;
    animation: spin 1s linear infinite;
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
</style>
