import { useState, useEffect, useCallback, useRef } from 'react'
import aiService, { AIMessage, AIData, AI_OPTIONS, InitOptions } from '@/utils/ai'

export function useAI(globalOptions?: InitOptions) {
  const [ai, setAI] = useState<string>(localStorage.getItem('ai') || 'qwen')
  const [messages, setMessages] = useState<AIMessage[]>([])
  const [message, setMessage] = useState<AIMessage | null>(null)
  const [isLoading, setIsLoading] = useState<boolean>(false)

  // 使用 useRef 存储最新消息，避免闭包问题
  const latestMessageRef = useRef<AIMessage | null>(null)
  // 使用 useRef 存储消息列表，避免异步更新问题
  const messagesRef = useRef<AIMessage[]>([])

  useEffect(() => {
    localStorage.setItem('ai', ai)
  }, [ai])

  // 更新消息的辅助函数
  const updateMessage = useCallback((response: any, isNew: boolean) => {
    if (isNew) {
      // 新消息
      const newMessage: AIMessage = {
        id: response.id,
        role: 'assistant',
        content: response.content,
      }

      latestMessageRef.current = newMessage
      messagesRef.current = [...messagesRef.current, newMessage]

      // 更新状态
      setMessage(newMessage)
      setMessages(messagesRef.current)
    } else {
      // 更新现有消息
      messagesRef.current = messagesRef.current.map((msg) => {
        if (msg.id === response.id) {
          const updatedMessage = {
            ...msg,
            content: msg.content + response.content,
          }
          latestMessageRef.current = updatedMessage
          setMessage(updatedMessage)
          return updatedMessage
        }
        return msg
      })

      setMessages([...messagesRef.current])
    }
  }, [])

  const post = useCallback(
    async (data: AIData, options?: InitOptions) => {
      try {
        setIsLoading(true)

        // 添加用户消息
        const userMessages = data.messages.filter((item) => item.role !== 'system')
        messagesRef.current = [...messagesRef.current, ...userMessages]
        setMessages((prev) => [...prev, ...userMessages])

        const onComplete = options?.onComplete || globalOptions?.onComplete

        await aiService.post(ai, data, {
          onMessage: (response) => {
            const messageIndex = messagesRef.current.findIndex((msg) => msg.id === response.id)
            updateMessage(response, messageIndex === -1)
          },
          onerror: (error) => {
            console.error('AI service error:', error)
            setIsLoading(false)
          },
          onclose: () => {
            setIsLoading(false)
            if (latestMessageRef.current && onComplete) {
              onComplete(latestMessageRef.current)
            }
          },
          ...globalOptions,
          ...options,
        })
      } catch (error) {
        console.error('Post error:', error)
        setIsLoading(false)
      }
    },
    [ai, updateMessage, globalOptions]
  )

  return {
    ai,
    setAI,
    options: AI_OPTIONS,
    post,
    messages,
    message,
    isLoading,
  }
}
