import { useEffect, useState } from 'react'
import { App, Tooltip, Button, FloatButton } from 'antd'
import { LoadingOutlined } from '@ant-design/icons'
import { getEmailFetchAction } from '@/actions/EmailActions'
import useAuthStore from '@/stores/AuthStore'
import useConversationStore from '@/stores/ConversationStore'
import { MailDownloadIcon } from '@/components/Icons'
import { isEmpty } from '@/utils/commons'

const EmailFetch = ({ ...props }) => {
  const { notification, message } = App.useApp()
  const { userId, emailList } = useAuthStore((state) => state.loginUser)

  const [globalNotify, clearGlobalNotify] = useConversationStore((state) => [state.globalNotify, state.clearGlobalNotify]);
  useEffect(() => {
    if (isEmpty(globalNotify)) {
      return () => {}
    }
    // message.info(globalNotify[0].content, 3)
    notification.open({
      key: globalNotify[0].id,
      message: globalNotify[0].title,
      description: globalNotify[0].content,
      duration: 3,
      placement: 'top',
      type: globalNotify[0].type,
      onClick: () => {
        clearGlobalNotify()
      },
    })
    setTimeout(() => {
      clearGlobalNotify()
    }, 3030)

    return () => {}
  }, [globalNotify])


  const [getEmailLoading, setEmailLoading] = useState(false)
  const [fetchingText, setFetchingText] = useState('立即收件')
  const handleGetEmail = async () => {
    setEmailLoading(true)
    setFetchingText('正在接收邮件...');
    const ret = await getEmailFetchAction({opi_sn: userId})
    setEmailLoading(false)
    setFetchingText('立即收件');
    // message.info('正在接收邮件...') // .info
  }
  return (
    <FloatButton
      key={'email-fetch'}
      icon={getEmailLoading ? <LoadingOutlined className='text-indigo-500' /> : <MailDownloadIcon className='text-indigo-500' />}
      tooltip={fetchingText}
      onClick={handleGetEmail}
    />
  )

  // return (
  //   <>
  //     <Tooltip key={'email-fetch'} title={fetchingText}>
  //       <Button type='text' onClick={handleGetEmail} icon={getEmailLoading ? <LoadingOutlined className='text-indigo-500' /> : <MailDownloadIcon className='text-indigo-500' />} />
  //     </Tooltip>
  //   </>
  // )
}
export default EmailFetch
