
import React from "react"
import { Button, Tooltip, Divider } from 'antd'
import { history } from '@umijs/max'
import { PlusOutlined, MessageOutlined, DeleteOutlined, HistoryOutlined } from '@ant-design/icons'
import styles from './RenderPageContentForm.less'
import {
  dataAnalysisStore,
  conversationStore,
  writingStore,
  mobileStore,
  assistantStore
} from '@/utils/pageConversationStore'

// 渲染页面展开内容 - 通用函数
export const renderPageContent = (
  pageType, conversations, expanded, setExpanded,) => {
  if (!expanded) return null

  const storeMap = {
    dataanalysis: { store: dataAnalysisStore, path: '/topnavbar00/hrefficiency/dataanalysis' },
    conversation: { store: conversationStore, path: '/topnavbar00/hrefficiency/ChatConversation' },
    writing: { store: writingStore, path: '/topnavbar00/hrefficiency/writing' },
    assistant: { store: assistantStore, path: '/topnavbar00/hrefficiency/assistant' },
    mobile: { store: mobileStore, path: '/topnavbar00/hrefficiency/mobile' }
  }

  // 创建新对话 - 根据页面类型
  const createNewConversation = (pageType) => {
    switch (pageType) {
      case 'dataanalysis':
        dataAnalysisStore.createNewConversation()
        history.replace('/topnavbar00/hrefficiency/dataanalysis')
        break
      case 'conversation':
        conversationStore.createNewConversation()
        history.replace('/topnavbar00/hrefficiency/ChatConversation')
        break
      case 'writing':
        writingStore.createNewConversation()
        history.replace('/topnavbar00/hrefficiency/writing')
        break
      case 'assistant':
        assistantStore.createNewConversation()
        history.replace('/topnavbar00/hrefficiency/assistant')
        break
      case 'mobile':
        mobileStore.createNewConversation()
        history.replace('/topnavbar00/hrefficiency/mobile')
        break
      default:
        break
    }
  }

  // 删除对话 - 根据页面类型
  const deleteConversation = (conversationId, pageType) => {
    switch (pageType) {
      case 'dataanalysis':
        dataAnalysisStore.deleteConversation(conversationId)
        break
      case 'conversation':
        conversationStore.deleteConversation(conversationId)
        break
      case 'writing':
        writingStore.deleteConversation(conversationId)
        break
      case 'assistant':
        assistantStore.deleteConversation(conversationId)
        break
      case 'mobile':
        mobileStore.deleteConversation(conversationId)
        break
      default:
        break
    }
  }


  // 切换对话 - 根据页面类型
  const switchConversation = (conversationId, pageType) => {
    switch (pageType) {
      case 'dataanalysis':
        dataAnalysisStore.switchConversation(conversationId)
        history.replace('/topnavbar00/hrefficiency/dataanalysis')
        break
      case 'conversation':
        conversationStore.switchConversation(conversationId)
        history.replace('/topnavbar00/hrefficiency/ChatConversation')
        break
      case 'writing':
        writingStore.switchConversation(conversationId)
        history.replace('/topnavbar00/hrefficiency/writing')
        break
      case 'assistant':
        assistantStore.switchConversation(conversationId)
        history.replace('/topnavbar00/hrefficiency/assistant')
        break
      case 'mobile':
        mobileStore.switchConversation(conversationId)
        history.replace('/topnavbar00/hrefficiency/mobile')
        break
      default:
        break
    }
  }



  return (
    <div className={styles.pageExpandedContent}>
      <div className={styles.conversationsList}>
        <div className={styles.conversationsTitle}>
          <HistoryOutlined />
          <span>历史会话</span>
          <Divider type="vertical" style={{ borderColor: '#666', margin: '0 0' }} />
          {/* 新对话 */}
          <Tooltip
            title="新会话"
            color="#fff"
            overlayInnerStyle={{ color: '#222', fontWeight: 400, fontSize: 12 }}
          >
            <Button
              type="primary"
              icon={<PlusOutlined />}
              onClick={() => createNewConversation(pageType)}
              block
              className={styles.newChatBtn}
            />
          </Tooltip>
        </div>

        <div className={styles.conversationsContainer}>
          {conversations.map((conversation) => (
            <div
              key={conversation.id}
              className={`${styles.conversationItem} ${conversation.isActive ? styles.active : ''}`}
              onClick={() => switchConversation(conversation.id, pageType)}
            >
              <div className={styles.conversationContent}>
                <div className={styles.conversationTitle}>
                  <MessageOutlined className={styles.conversationIcon} />
                  <span className={styles.titleText} title={conversation.title}>
                    {conversation.title}
                  </span>
                </div>
                <div className={styles.conversationTime}>
                  {conversation.lastUpdate.toLocaleDateString()}
                </div>
              </div>

              <div className={styles.conversationActions}>
                <Tooltip title="删除对话"
                  color="#fff"
                  overlayInnerStyle={{ color: '#222', fontWeight: 400, fontSize: 12 }}>
                  <Button
                    type="text"
                    size="small"
                    icon={<DeleteOutlined />}
                    onClick={(e) => {
                      e.stopPropagation()
                      deleteConversation(conversation.id, pageType)
                    }}
                    danger
                  />
                </Tooltip>
              </div>
            </div>
          ))}
        </div>
      </div>
    </div>
  )
}

export default renderPageContent