"use client"

import type { FamilyMember } from "@/types/family"
import { cn } from "@/lib/utils"
import { useAvatarCache } from "@/hooks/use-avatar-cache"
import { CircleUser, CircleUserRound, Heart, UserPlus, Users, Baby } from "lucide-react"
import { MemberNameWithStatus } from "@/components/member-name-with-status"
import { useRouter } from "next/navigation"
import { useFamily } from "@/context/family-context"
import {
  ContextMenu,
  ContextMenuContent,
  ContextMenuItem,
  ContextMenuSeparator,
  ContextMenuLabel,
  ContextMenuTrigger,
} from "@/components/ui/context-menu"

interface FamilyNodeProps {
  member: FamilyMember
  spouse?: FamilyMember
  spouses?: FamilyMember[]
  isRoot?: boolean
  onSelect?: (member: FamilyMember) => void
  isHighlighted?: boolean
  relationMode?: boolean
  isSelected?: boolean
  selectedMembers?: string[]
  canEdit?: boolean
  onAddRelation?: (type: "child" | "spouse") => void
  // 折叠功能
  hasChildren?: boolean
  isCollapsed?: boolean
  onToggleCollapse?: () => void
}

// 格式化日期显示（只显示年份）
function formatYear(dateString?: string): string {
  if (!dateString) return ''
  const year = dateString.split('-')[0]
  return year
}

// 计算年龄
function calculateAge(birthDate?: string, deathDate?: string): number | null {
  if (!birthDate) return null
  
  const birth = new Date(birthDate)
  const end = deathDate ? new Date(deathDate) : new Date()
  
  let age = end.getFullYear() - birth.getFullYear()
  const monthDiff = end.getMonth() - birth.getMonth()
  
  if (monthDiff < 0 || (monthDiff === 0 && end.getDate() < birth.getDate())) {
    age--
  }
  
  return age
}

// 单个人员卡片组件
function PersonCard({ 
  member, 
  isRoot, 
  isSpouse, 
  onSelect, 
  isHighlighted, 
  relationMode, 
  isSelected,
  canEdit = true,
  onAddRelation,
  hasChildren = false,
  isCollapsed = false,
  onToggleCollapse
}: { 
  member: FamilyMember
  isRoot?: boolean
  isSpouse?: boolean
  onSelect?: (member: FamilyMember) => void
  isHighlighted?: boolean
  relationMode?: boolean
  isSelected?: boolean
  canEdit?: boolean
  onAddRelation?: (type: "child" | "spouse") => void
  hasChildren?: boolean
  isCollapsed?: boolean
  onToggleCollapse?: () => void
}) {
  const { avatarUrl: avatarBlobUrl } = useAvatarCache(member?.avatarUrl)
  const router = useRouter()
  const { currentTree } = useFamily()
  
  const birthYear = formatYear(member.birthDate)
  const deathYear = formatYear(member.deathDate)
  const age = calculateAge(member.birthDate, member.deathDate)
  
  const lifeSpan = birthYear
    ? deathYear
      ? `${birthYear}-${deathYear}`
      : `${birthYear}-`
    : ''
  
  const ageText = age !== null
    ? member.deathDate
      ? `享年${age}岁`
      : `${age}岁`
    : ''

  // 判断可以添加的关系
  const canAddFather = !member.fatherId
  const canAddMother = !member.motherId
  const canAddSpouse = true // 配偶总是可以添加
  const canAddChild = true // 子女总是可以添加

  // 构建新增成员的 URL
  const buildAddUrl = (type: 'father' | 'mother' | 'spouse' | 'child') => {
    const params = new URLSearchParams()
    if (currentTree?.id) {
      params.set('treeId', currentTree.id)
    }
    
    switch (type) {
      case 'father':
        params.set('childId', member.id)
        params.set('gender', 'MALE')
        params.set('generation', (member.generation - 1).toString())
        // 如果已有母亲，设为配偶
        if (member.motherId) {
          params.set('spouseId', member.motherId)
        }
        break
      case 'mother':
        params.set('childId', member.id)
        params.set('gender', 'FEMALE')
        params.set('generation', (member.generation - 1).toString())
        // 如果已有父亲，设为配偶
        if (member.fatherId) {
          params.set('spouseId', member.fatherId)
        }
        break
      case 'spouse':
        params.set('spouseId', member.id)
        params.set('generation', member.generation.toString())
        // 配偶性别与当前成员相反
        params.set('gender', member.gender === 'MALE' ? 'FEMALE' : 'MALE')
        
        // 如果当前成员有子女，将他们也作为新配偶的子女
        if (member.childrenIds && member.childrenIds.length > 0) {
          // 传递所有子女ID
          member.childrenIds.forEach(id => {
            params.append('childrenIds', id)
          })
        }
        break
      case 'child':
        // 根据当前成员性别设置父/母
        if (member.gender === 'MALE') {
          params.set('fatherId', member.id)
          // 如果父亲有配偶，设置配偶为母亲
          if (member.spouseIds && member.spouseIds.length > 0) {
            params.set('motherId', member.spouseIds[0])
          }
        } else {
          params.set('motherId', member.id)
          // 如果母亲有配偶，设置配偶为父亲
          if (member.spouseIds && member.spouseIds.length > 0) {
            params.set('fatherId', member.spouseIds[0])
          }
        }
        params.set('generation', (member.generation + 1).toString())
        break
    }
    
    return `/members/new?${params.toString()}`
  }

  const handleAddMember = (type: 'father' | 'mother' | 'spouse' | 'child') => {
    const url = buildAddUrl(type)
    console.log('handleAddMember called:', type, url)
    // 使用 setTimeout 延迟跳转，确保菜单关闭后再执行
    setTimeout(() => {
      console.log('Navigating to:', url)
      window.location.href = url
    }, 100)
  }

  // 是否显示右键菜单（有可添加的选项时才显示）
  const hasAddOptions = canEdit && (canAddFather || canAddMother || canAddSpouse || canAddChild)
  
  // 调试日志
  console.log('PersonCard render:', member.fullName, { canEdit, hasAddOptions, relationMode })

  // 庄严正式的中国风配色
  const isDead = !!member.deathDate
  const isMale = member.gender === 'MALE'
  const isFounder = member.isFounder === true
  
  const cardContent = (
    <div
      data-member-id={member.id}
      className={cn(
        "family-node-card relative flex flex-col items-center p-4 transition-all duration-300 cursor-pointer w-[140px]",
        // 现代简约中国风 - 圆角卡片
        "rounded-xl",
        "hover:scale-[1.03] hover:-translate-y-1",
        // 柔和阴影效果
        isHighlighted
          ? "shadow-[0_8px_30px_rgba(234,179,8,0.4)] ring-2 ring-yellow-400"
          : "shadow-[0_4px_20px_rgba(0,0,0,0.08)] hover:shadow-[0_12px_40px_rgba(0,0,0,0.15)]",
        // 根据状态设置背景 - 更柔和的渐变
        isHighlighted
          ? "bg-gradient-to-br from-yellow-50 via-amber-50 to-yellow-100 dark:from-yellow-950/60 dark:to-amber-900/40"
          : isFounder || isRoot
            ? "bg-gradient-to-br from-amber-50 via-orange-50 to-yellow-50 dark:from-amber-950/50 dark:to-orange-900/30" 
            : isSpouse
              ? "bg-gradient-to-br from-rose-50/80 via-pink-50/60 to-rose-50/80 dark:from-rose-950/30 dark:to-pink-900/20"
              : isDead
                ? "bg-gradient-to-br from-stone-100 via-gray-50 to-stone-100 dark:from-stone-900/50 dark:to-gray-800/30"
                : isMale
                  ? "bg-gradient-to-br from-sky-50 via-blue-50/50 to-indigo-50/30 dark:from-sky-950/40 dark:to-blue-900/20"
                  : "bg-gradient-to-br from-rose-50 via-pink-50/50 to-fuchsia-50/30 dark:from-rose-950/40 dark:to-pink-900/20",
        relationMode && "hover:ring-2 hover:ring-primary/50"
      )}
      onClick={() => onSelect?.(member)}
    >
      {/* 顶部装饰条 */}
      <div className={cn(
        "absolute top-0 left-4 right-4 h-1 rounded-b-full",
        isHighlighted ? "bg-gradient-to-r from-yellow-400 via-amber-500 to-yellow-400"
          : isFounder || isRoot ? "bg-gradient-to-r from-amber-400 via-orange-500 to-amber-400" 
          : isSpouse ? "bg-gradient-to-r from-rose-300 via-pink-400 to-rose-300"
          : isDead ? "bg-gradient-to-r from-stone-400 via-gray-500 to-stone-400" 
          : isMale ? "bg-gradient-to-r from-sky-400 via-blue-500 to-sky-400" 
          : "bg-gradient-to-r from-rose-400 via-pink-500 to-rose-400"
      )} />
      
      {/* 选中打钩标记 */}
      {isSelected && (
        <div className="absolute top-2 left-2 z-20">
          <div className="w-5 h-5 rounded-full bg-blue-500 flex items-center justify-center shadow-lg">
            <svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="white" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round">
              <polyline points="20 6 9 17 4 12"/>
            </svg>
          </div>
        </div>
      )}
      
      {/* 世代标签 - 现代胶囊风格 */}
      <div className={cn(
        "absolute -top-2.5 px-3 py-0.5 text-[10px] font-medium tracking-wider rounded-full",
        "shadow-md backdrop-blur-sm",
        isFounder || isRoot 
          ? "bg-gradient-to-r from-amber-500 to-orange-500 text-white" 
          : isDead
            ? "bg-gradient-to-r from-stone-500 to-gray-500 text-white"
            : isMale
              ? "bg-gradient-to-r from-sky-500 to-blue-500 text-white"
              : "bg-gradient-to-r from-rose-500 to-pink-500 text-white"
      )}>
        第{member.generation}世
      </div>

      {/* 头像 - 圆形带光晕效果 */}
      <div className="relative mt-3">
        {/* 光晕背景 */}
        <div className={cn(
          "absolute inset-0 rounded-full blur-md opacity-40",
          isFounder || isRoot ? "bg-amber-400" 
            : isDead ? "bg-stone-400"
            : isMale ? "bg-sky-400" 
            : "bg-rose-400"
        )} />
        <div className={cn(
          "relative h-14 w-14 rounded-full flex items-center justify-center overflow-hidden",
          "border-[3px] shadow-lg bg-white dark:bg-gray-800",
          "ring-2 ring-white dark:ring-gray-700",
          isFounder || isRoot 
            ? "border-amber-400" 
            : isDead
              ? "border-stone-400"
              : isMale
                ? "border-sky-400"
                : "border-rose-400"
        )}>
          {avatarBlobUrl ? (
            <img
              src={avatarBlobUrl}
              alt={member.fullName}
              className="h-full w-full object-cover"
            />
          ) : (
            member.gender === 'FEMALE' ? (
              <CircleUserRound className={cn("h-8 w-8", isDead ? "text-stone-400" : "text-rose-500")} />
            ) : (
              <CircleUser className={cn("h-8 w-8", isDead ? "text-stone-400" : "text-sky-500")} />
            )
          )}
        </div>
        {/* 在线/已故状态指示器 */}
        <div className={cn(
          "absolute -bottom-0.5 -right-0.5 w-4 h-4 rounded-full border-2 border-white dark:border-gray-800",
          isDead ? "bg-stone-400" : "bg-emerald-500"
        )} />
      </div>

      {/* 姓名和信息 */}
      <div className="text-center w-full mt-3 space-y-1">
        <div className="font-semibold leading-tight text-[15px] tracking-wide text-black dark:text-white">
          <MemberNameWithStatus 
            name={`${member.surname}${member.givenName}`}
            isDead={isDead}
          />
        </div>
        {member.courtesyName && (
          <div className="text-[11px] tracking-wide text-black dark:text-white">
            字 {member.courtesyName}
          </div>
        )}
        {lifeSpan && (
          <div className={cn(
            "text-[10px] font-mono tracking-wider px-2 py-0.5 rounded-full inline-block text-black dark:text-white",
            isDead 
              ? "bg-stone-200 dark:bg-stone-700" 
              : "bg-gray-200 dark:bg-gray-700"
          )}>
            {lifeSpan}
          </div>
        )}
        {ageText && (
          <div className="text-[10px] text-black dark:text-white">
            {ageText}
          </div>
        )}
      </div>
      
      {/* 折叠/展开按钮 - 更现代的样式 */}
      {hasChildren && onToggleCollapse && (
        <button
          onClick={(e) => {
            e.stopPropagation()
            onToggleCollapse()
          }}
          className={cn(
            "absolute -bottom-3 left-1/2 -translate-x-1/2 z-20",
            "w-6 h-6 rounded-full flex items-center justify-center",
            "bg-white dark:bg-gray-800 shadow-lg transition-all duration-200",
            "hover:scale-110 active:scale-95",
            "border border-gray-200 dark:border-gray-700",
            isCollapsed 
              ? "text-amber-500 hover:bg-amber-50 dark:hover:bg-amber-900/30" 
              : "text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700/50"
          )}
          title={isCollapsed ? "展开子孙" : "折叠子孙"}
        >
          <svg 
            xmlns="http://www.w3.org/2000/svg" 
            width="14" 
            height="14" 
            viewBox="0 0 24 24" 
            fill="none" 
            stroke="currentColor" 
            strokeWidth="2.5" 
            strokeLinecap="round" 
            strokeLinejoin="round"
            className={cn("transition-transform duration-200", isCollapsed ? "rotate-180" : "")}
          >
            <polyline points="18 15 12 9 6 15"></polyline>
          </svg>
        </button>
      )}
    </div>
  )

  // 如果没有可添加的选项或在关系模式下，直接返回卡片
  if (!hasAddOptions || relationMode) {
    return cardContent
  }

  // 有可添加选项时，包裹右键菜单
  return (
    <ContextMenu>
      <ContextMenuTrigger>
        {cardContent}
      </ContextMenuTrigger>
      <ContextMenuContent className="w-48">
        <ContextMenuLabel className="text-xs text-muted-foreground">
          为 {member.fullName} 添加
        </ContextMenuLabel>
        <ContextMenuSeparator />
        
        {canAddFather && (
          <ContextMenuItem 
            className="gap-2 cursor-pointer"
            onPointerDown={(e) => {
              e.preventDefault()
              handleAddMember('father')
            }}
          >
            <UserPlus className="h-4 w-4 text-blue-500" />
            添加父亲
          </ContextMenuItem>
        )}
        
        {canAddMother && (
          <ContextMenuItem 
            className="gap-2 cursor-pointer"
            onPointerDown={(e) => {
              e.preventDefault()
              handleAddMember('mother')
            }}
          >
            <UserPlus className="h-4 w-4 text-pink-500" />
            添加母亲
          </ContextMenuItem>
        )}
        
        {(canAddFather || canAddMother) && (canAddSpouse || canAddChild) && (
          <ContextMenuSeparator />
        )}
        
        {canAddSpouse && (
          <ContextMenuItem 
            className="gap-2 cursor-pointer"
            onPointerDown={(e) => {
              e.preventDefault()
              if (onAddRelation) {
                onAddRelation('spouse')
              } else {
                handleAddMember('spouse')
              }
            }}
          >
            <Users className="h-4 w-4 text-red-500" />
            添加配偶
          </ContextMenuItem>
        )}
        
        {canAddChild && (
          <ContextMenuItem 
            className="gap-2 cursor-pointer"
            onPointerDown={(e) => {
              e.preventDefault()
              if (onAddRelation) {
                onAddRelation('child')
              } else {
                handleAddMember('child')
              }
            }}
          >
            <Baby className="h-4 w-4 text-green-500" />
            添加子女
          </ContextMenuItem>
        )}
      </ContextMenuContent>
    </ContextMenu>
  )
}

export function FamilyNode({ 
  member, 
  spouse, 
  spouses, 
  isRoot, 
  onSelect, 
  isHighlighted, 
  relationMode, 
  isSelected, 
  selectedMembers = [], 
  canEdit = true, 
  onAddRelation,
  hasChildren = false,
  isCollapsed = false,
  onToggleCollapse
}: FamilyNodeProps) {
  // 优先使用 spouses 数组，如果没有则使用单个 spouse
  const spouseList = spouses || (spouse ? [spouse] : [])
  
  if (spouseList.length === 0) {
    return (
      <PersonCard 
        member={member} 
        isRoot={isRoot} 
        onSelect={onSelect} 
        isHighlighted={isHighlighted} 
        relationMode={relationMode} 
        isSelected={isSelected} 
        canEdit={canEdit} 
        onAddRelation={onAddRelation}
        hasChildren={hasChildren}
        isCollapsed={isCollapsed}
        onToggleCollapse={onToggleCollapse}
      />
    )
  }
  
  // 有配偶时，并排显示所有配偶
  return (
    <div className="flex items-center gap-2">
      <PersonCard 
        member={member} 
        isRoot={isRoot} 
        onSelect={onSelect} 
        isHighlighted={isHighlighted} 
        relationMode={relationMode} 
        isSelected={isSelected} 
        canEdit={canEdit} 
        onAddRelation={onAddRelation}
        hasChildren={hasChildren}
        isCollapsed={isCollapsed}
        onToggleCollapse={onToggleCollapse}
      />
      {spouseList.map((spouseMember, index) => (
        <div key={spouseMember.id} className="flex items-center gap-2">
          <Heart className="h-4 w-4 text-red-400 flex-shrink-0" />
          <PersonCard 
            member={spouseMember} 
            isSpouse={true} 
            onSelect={onSelect} 
            relationMode={relationMode} 
            isSelected={selectedMembers.includes(spouseMember.id)}
            canEdit={canEdit} 
          />
        </div>
      ))}
    </div>
  )
}
