import { useEffect } from 'react'
import { Bot, Download, Wand2 } from 'lucide-react'
import { toast } from 'sonner'

import { showProgressToast } from '@renderer/components/ui/progress-toast'

function getToastId(kind: 'comments' | 'reports' | 'report-downloads', batchId: string): string {
  return `${kind}-progress-${batchId}`
}

export function GenerationProgressToasts(): null {
  useEffect(() => {
    return window.api.onCommentGenerationProgress((progress) => {
      const toastId = getToastId('comments', progress.batchId)

      if (progress.status === 'finished') {
        toast.dismiss(toastId)
        return
      }

      const isPaused = progress.status === 'paused'
      const isStopped = progress.status === 'stopped'

      showProgressToast({
        id: toastId,
        icon: Bot,
        title: isStopped
          ? '评语生成已停止'
          : isPaused
            ? '评语生成已暂停'
            : progress.studentName
              ? `正在生成 ${progress.studentName} 的评语`
              : '正在准备生成评语',
        description: `${progress.className || '学生数据'} · ${progress.current}/${progress.total}`,
        current: progress.current,
        total: progress.total,
        status:
          progress.status === 'student-failed'
            ? 'error'
            : isStopped
              ? 'stopped'
              : isPaused
                ? 'paused'
                : 'running',
        statusLabel:
          progress.status === 'student-failed'
            ? '本份失败'
            : isStopped
              ? '已停止'
              : isPaused
                ? '已暂停'
                : '生成中',
        error: progress.error,
        duration: isStopped ? 5000 : undefined,
        actionLabel: isStopped ? undefined : isPaused ? '继续' : '暂停',
        onAction: isStopped
          ? undefined
          : async () => {
              const response = isPaused
                ? await window.api.resumeCommentGeneration(progress.batchId)
                : await window.api.pauseCommentGeneration(progress.batchId)

              if (!response.ok) {
                toast.error(isPaused ? '继续生成失败' : '暂停生成失败', {
                  description: response.message
                })
              }
            },
        secondaryActionLabel: isStopped ? undefined : '停止',
        onSecondaryAction: isStopped
          ? undefined
          : async () => {
              const response = await window.api.stopCommentGeneration(progress.batchId)

              if (!response.ok) {
                toast.error('停止生成失败', {
                  description: response.message
                })
              }
            }
      })
    })
  }, [])

  useEffect(() => {
    return window.api.onReportGenerationProgress((progress) => {
      const toastId = getToastId('reports', progress.batchId)

      if (progress.status === 'finished') {
        toast.dismiss(toastId)
        return
      }

      const isPaused = progress.status === 'paused'
      const isStopped = progress.status === 'stopped'

      showProgressToast({
        id: toastId,
        icon: Wand2,
        title: isStopped
          ? '报告生成已停止'
          : isPaused
            ? '报告生成已暂停'
            : progress.studentName
              ? `正在生成 ${progress.studentName} 的报告`
              : '正在准备生成报告',
        description: `${progress.className} · ${progress.current}/${progress.total}`,
        current: progress.current,
        total: progress.total,
        status:
          progress.status === 'student-failed'
            ? 'error'
            : isStopped
              ? 'stopped'
              : isPaused
                ? 'paused'
                : 'running',
        statusLabel:
          progress.status === 'student-failed'
            ? '本份失败'
            : isStopped
              ? '已停止'
              : isPaused
                ? '已暂停'
                : '生成中',
        error: progress.error,
        duration: isStopped ? 5000 : undefined,
        actionLabel: isStopped ? undefined : isPaused ? '继续' : '暂停',
        onAction: isStopped
          ? undefined
          : async () => {
              const response = isPaused
                ? await window.api.resumeReportGeneration(progress.batchId)
                : await window.api.pauseReportGeneration(progress.batchId)

              if (!response.ok) {
                toast.error(isPaused ? '继续生成失败' : '暂停生成失败', {
                  description: response.message
                })
              }
            },
        secondaryActionLabel: isStopped ? undefined : '停止',
        onSecondaryAction: isStopped
          ? undefined
          : async () => {
              const response = await window.api.stopReportGeneration(progress.batchId)

              if (!response.ok) {
                toast.error('停止生成失败', {
                  description: response.message
                })
              }
            }
      })
    })
  }, [])

  useEffect(() => {
    return window.api.onReportDownloadProgress((progress) => {
      const toastId = getToastId('report-downloads', progress.batchId)
      const isFinished = progress.status === 'finished'
      const isFailed = progress.status === 'failed'
      const isWriting = progress.status === 'writing'
      const activeReportTitle = progress.reportTitle
        ? `正在打包 ${progress.reportTitle}`
        : '正在打包班级报告'

      showProgressToast({
        id: toastId,
        icon: Download,
        title: isFinished
          ? '班级报告导出完成'
          : isFailed
            ? '班级报告导出失败'
            : isWriting
              ? '正在写入压缩包'
              : activeReportTitle,
        description: isFinished
          ? progress.filePath
          : `${progress.className} · ${progress.current}/${progress.total}`,
        current: progress.current,
        total: progress.total,
        status: isFinished ? 'success' : isFailed ? 'error' : 'running',
        statusLabel: isFinished ? '已完成' : isFailed ? '失败' : isWriting ? '写入中' : '打包中',
        error: progress.error,
        duration: isFinished || isFailed ? 5000 : undefined
      })
    })
  }, [])

  return null
}
