import { useRef, useEffect } from 'react'
import type { BacktrackPhase, BacktrackVessel, BacktrackConditions } from '@common/types/backtrack'

interface BacktrackModalProps {
  isOpen: boolean
  onClose: () => void
  phase: BacktrackPhase
  conditions: BacktrackConditions
  vessels: BacktrackVessel[]
  onRunAnalysis: () => void
  onStartReplay: () => void
}

export function BacktrackModal({
  isOpen,
  onClose,
  phase,
  conditions,
  vessels,
  onRunAnalysis,
  onStartReplay,
}: BacktrackModalProps) {
  const backdropRef = useRef<HTMLDivElement>(null)

  useEffect(() => {
    const handler = (e: MouseEvent) => {
      if (e.target === backdropRef.current) onClose()
    }
    if (isOpen) document.addEventListener('mousedown', handler)
    return () => document.removeEventListener('mousedown', handler)
  }, [isOpen, onClose])

  if (!isOpen) return null

  return (
    <div
      ref={backdropRef}
      style={{
        inset: 0,
        background: 'rgba(0,0,0,0.55)', backdropFilter: 'blur(4px)',
      }}
      className="fixed z-[9999] flex items-center justify-center"
    >
      <div style={{
        width: '580px', maxHeight: 'calc(100vh - 120px)',
        background: 'var(--bg1)',
        borderRadius: '14px',
        boxShadow: '0 20px 60px rgba(0,0,0,0.5)',
      }} className="border border-border overflow-hidden flex flex-col">
        {/* Header */}
        <div style={{
          padding: '20px 24px',
        }} className="border-b border-border flex items-center gap-[14px]">
          <div style={{
            width: '40px', height: '40px', borderRadius: '10px',
            background: 'linear-gradient(135deg, rgba(168,85,247,0.2), rgba(6,182,212,0.2))',
            border: '1px solid rgba(168,85,247,0.3)',
            fontSize: '18px',
          }} className="flex items-center justify-center">
            🔍
          </div>
          <div className="flex-1">
            <h2 className="text-base font-bold m-0">
              유출유 역추적 분석
            </h2>
            <div className="text-[11px] text-text-3 mt-[2px]">
              AIS 항적 기반 유출 선박 추정
            </div>
          </div>
          <button
            onClick={onClose}
            style={{
              width: '32px', height: '32px', borderRadius: '8px',
              background: 'var(--bg3)',
              fontSize: '14px',
            }}
            className="border border-border text-text-3 cursor-pointer flex items-center justify-center"
          >
            ✕
          </button>
        </div>

        {/* Scrollable Content */}
        <div style={{
          padding: '20px 24px',
        }} className="flex-1 overflow-y-auto flex flex-col gap-4">
          {/* Analysis Conditions */}
          <div>
            <h3 className="text-[12px] font-bold text-text-2 mb-[10px]">
              분석 조건
            </h3>
            <div style={{
              display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '8px',
            }}>
              {[
                { label: '유출 추정 시각', value: conditions.estimatedSpillTime },
                { label: '분석 범위', value: conditions.analysisRange },
                { label: '탐색 반경', value: conditions.searchRadius },
                { label: '유출 위치', value: `${conditions.spillLocation.lat.toFixed(4)}°N, ${conditions.spillLocation.lon.toFixed(4)}°E` },
              ].map((item, i) => (
                <div key={i} style={{
                  padding: '10px 12px', background: 'var(--bg3)',
                  borderRadius: '8px',
                }} className="border border-border">
                  <div className="text-[9px] text-text-3 mb-1">
                    {item.label}
                  </div>
                  <div className="text-[12px] font-semibold font-mono">
                    {item.value}
                  </div>
                </div>
              ))}
              <div style={{
                padding: '10px 12px', background: 'var(--bg3)',
                border: '1px solid rgba(168,85,247,0.3)', borderRadius: '8px',
                gridColumn: '1 / -1',
              }}>
                <div className="text-[9px] text-text-3 mb-1">
                  분석 대상 선박
                </div>
                <div className="text-sm font-bold text-primary-purple font-mono">
                  {conditions.totalVessels}척 <span className="text-[10px] font-medium text-text-3">(AIS 수신)</span>
                </div>
              </div>
            </div>
          </div>

          {/* Results */}
          {phase === 'results' && vessels.length > 0 && (
            <div>
              <div className="flex items-center justify-between mb-3">
                <h3 className="text-[12px] font-bold text-text-2 m-0">
                  분석 결과
                </h3>
                <div style={{
                  padding: '4px 10px', borderRadius: '12px',
                  background: 'rgba(239,68,68,0.1)', border: '1px solid rgba(239,68,68,0.3)',
                }} className="text-[10px] font-bold text-status-red">
                  {conditions.totalVessels}척 중 {vessels.length}척 의심
                </div>
              </div>

              <div className="flex flex-col gap-2.5">
                {vessels.map((v) => (
                  <VesselCard key={v.imo} vessel={v} />
                ))}
              </div>
            </div>
          )}
        </div>

        {/* Footer */}
        <div style={{
          padding: '16px 24px',
        }} className="border-t border-border flex gap-2">
          {phase === 'conditions' && (
            <button
              onClick={onRunAnalysis}
              style={{
                padding: '12px',
                borderRadius: '8px',
                background: 'linear-gradient(135deg, var(--purple), var(--cyan))',
                border: 'none', color: '#fff',
              }}
              className="flex-1 text-[13px] font-bold cursor-pointer"
            >
              🔍 역추적 분석 실행
            </button>
          )}
          {phase === 'analyzing' && (
            <button
              disabled
              style={{
                padding: '12px',
                borderRadius: '8px',
                background: 'var(--bg3)',
                color: 'var(--purple)', cursor: 'wait',
              }}
              className="flex-1 text-[13px] font-bold border border-border"
            >
              ⏳ AIS 항적 분석중...
            </button>
          )}
          {phase === 'results' && (
            <button
              onClick={onStartReplay}
              style={{
                padding: '12px',
                borderRadius: '8px',
                background: 'linear-gradient(135deg, var(--purple), var(--cyan))',
                border: 'none', color: '#fff',
              }}
              className="flex-1 text-[13px] font-bold cursor-pointer"
            >
              🗺 지도에서 리플레이 보기
            </button>
          )}
        </div>
      </div>
    </div>
  )
}

function VesselCard({ vessel }: { vessel: BacktrackVessel }) {
  const probColor = vessel.probability >= 80 ? 'var(--red)' :
    vessel.probability >= 20 ? 'var(--orange)' : 'var(--t3)'

  return (
    <div style={{
      padding: '14px', background: 'var(--bg0)',
      borderLeft: `4px solid ${vessel.color}`,
      borderRadius: '10px',
    }} className="border border-border">
      {/* Header row */}
      <div className="flex items-center gap-[10px] mb-[10px]">
        <div style={{
          width: '28px', height: '28px', borderRadius: '50%',
          background: `${vessel.color}20`, border: `2px solid ${vessel.color}`,
          fontSize: '12px', fontWeight: 800, color: vessel.color,
        }} className="flex items-center justify-center font-mono">
          {vessel.rank}
        </div>
        <div className="flex-1">
          <div className="text-[13px] font-bold font-mono">
            {vessel.name}
          </div>
          <div className="text-[9px] text-text-3 font-mono mt-[2px]">
            IMO: {vessel.imo} · {vessel.type} · {vessel.flag}
          </div>
        </div>
        <div style={{ textAlign: 'right' }}>
          <div style={{ fontSize: '22px', color: probColor, lineHeight: 1 }} className="font-bold font-mono">
            {vessel.probability}%
          </div>
          <div className="text-[8px] text-text-3">유출 확률</div>
        </div>
      </div>

      {/* Stats grid */}
      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr 1fr 1fr', gap: '6px', marginBottom: vessel.description ? '8px' : 0 }}>
        {[
          { label: '최근접 시각', value: vessel.closestTime },
          { label: '최근접 거리', value: `${vessel.closestDistance} NM` },
          { label: '속도 변화', value: vessel.speedChange, highlight: vessel.speedChange === '급감속' },
          { label: 'AIS 상태', value: vessel.aisStatus, highlight: vessel.aisStatus === '충돌신호' },
        ].map((s, i) => (
          <div key={i} style={{
            padding: '6px', background: 'var(--bg3)', borderRadius: '6px',
            border: s.highlight ? '1px solid rgba(239,68,68,0.3)' : '1px solid var(--bd)',
          }}>
            <div className="text-[8px] text-text-3 mb-[2px]">
              {s.label}
            </div>
            <div style={{
              color: s.highlight ? 'var(--red)' : 'var(--t1)',
            }} className="text-[10px] font-semibold font-mono">
              {s.value}
            </div>
          </div>
        ))}
      </div>

      {/* Description */}
      {vessel.description && (
        <div style={{
          padding: '8px 10px', background: 'rgba(239,68,68,0.05)',
          border: '1px solid rgba(239,68,68,0.15)', borderRadius: '6px',
          lineHeight: '1.5',
        }} className="text-[9px] text-text-2">
          {vessel.description}
        </div>
      )}
    </div>
  )
}
