'use client'

import React, { useEffect, useMemo, useState } from 'react'
import { TaskObject } from '@/api/DTO/movieEdit'
import { motion } from 'framer-motion'
import { Heart, Camera, Film, Scissors, type LucideIcon } from 'lucide-react'

interface H5TaskInfoProps {
  /** 标题文案 */
  title: string
  /** 当前分镜序号（从1开始） */
  current: number
  /** 任务对象（用于读取总数等信息） */
  taskObject: TaskObject
  className?: string
  currentLoadingText: string
}

const H5TaskInfo: React.FC<H5TaskInfoProps> = ({
  title,
  current,
  taskObject,
  className,
  currentLoadingText
}) => {
  type StageIndex = 0 | 1 | 2 | 3

  const [currentStage, setCurrentStage] = useState<StageIndex>(0)

  const stageIconMap: Record<StageIndex, { icon: LucideIcon; color: string }> = {
    0: { icon: Heart, color: '#8b5cf6' },
    1: { icon: Camera, color: '#06b6d4' },
    2: { icon: Film, color: '#10b981' },
    3: { icon: Scissors, color: '#f59e0b' }
  }

  const computeStage = (text: string): StageIndex => {
    if (text.includes('initializing...') || text.includes('script') || text.includes('character')) return 0
    if (text.includes('sketch') && !text.includes('shot sketch')) return 1
    if (!text.includes('Post-production') && (text.includes('shot sketch') || text.includes('video'))) return 2
    if (text.includes('Post-production')) return 3
    return 0
  }

  useEffect(() => {
    setCurrentStage(computeStage(currentLoadingText))
  }, [currentLoadingText])

  const stageColor = useMemo(() => stageIconMap[currentStage].color, [currentStage])
  const total = useMemo(() => {
    if (taskObject.currentStage === 'video' || taskObject.currentStage === 'final_video') {
      return taskObject.videos?.total_count || taskObject.videos?.data?.length || 0
    }
    if (taskObject.currentStage === 'scene' || taskObject.currentStage === 'character') {
      const rolesTotal = taskObject.roles?.total_count || taskObject.roles?.data?.length || 0
      const scenesTotal = taskObject.scenes?.total_count || taskObject.scenes?.data?.length || 0
      return rolesTotal + scenesTotal
    }
    return 0
  }, [taskObject])

  const shouldShowCount = taskObject.currentStage !== 'script'

  const displayCurrent = useMemo(() => {
    if (taskObject.currentStage === 'video' || taskObject.currentStage === 'final_video') {
      return Math.max(current, 1)
    }
    if (taskObject.currentStage === 'scene' || taskObject.currentStage === 'character') {
      const bounded = Math.min(Math.max(current, 1), Math.max(total, 1))
      return bounded
    }
    return 0
  }, [taskObject, current, total])

  return (
    <div
      data-alt="h5-header"
      className={`absolute top-0 left-0 right-0 z-[50] pr-1 ${className || ''}`}
    >
      <div
        data-alt="h5-header-bar"
        className="flex items-start justify-between"
      >
        <div data-alt="title-area" className="flex flex-col min-w-0 bg-gradient-to-b from-slate-900/80 via-slate-900/40 to-transparent backdrop-blur-sm rounded-lg py-4">
          <h1
            data-alt="title"
            className="text-white text-lg font-bold"
            title={title}
          >
            {title || '...'}
          </h1>
          {shouldShowCount && (
            <span data-alt="shot-count" className="flex items-center gap-4 text-sm text-slate-300">
              {taskObject.currentStage === 'video' ? 'Shots' : 'Roles & Scenes '} {displayCurrent}/{Math.max(total, 1)}
            </span>
          )}
          
        </div>
      </div>
    </div>
  )
}

export default H5TaskInfo


